-
Notifications
You must be signed in to change notification settings - Fork 0
[HUB-4300] Introduce Versioned Migration Flow #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tholst-d4l
wants to merge
15
commits into
main
Choose a base branch
from
feature/migration-approaches
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0338fe4
Add target-only before migration and docs
tholst-d4l 7dd2597
Update migration diagrams and docs
tholst-d4l bdf5e94
Refine versioned migration flow
tholst-d4l 8231515
Address lint issues
tholst-d4l 7c26edc
Move migration documentation to docs
tholst-d4l 09b9225
Embed migration diagrams in docs
tholst-d4l 5fff2d5
Move migration docs into docs/migration
tholst-d4l 4cf4ddb
Move migration diagrams into docs/migration
tholst-d4l 55851b9
Fix README migration doc link
tholst-d4l 7100e47
Reorder migration docs and clarify version record
tholst-d4l 7944b8a
Add migration flow tests
tholst-d4l 4d8c4fe
Stabilize migration tests and lint
tholst-d4l 4532724
Fix gosec integer conversion warnings
tholst-d4l 7ccb9b5
Restore migrate README details
tholst-d4l 3be7264
Fix migration flow compatibility
tholst-d4l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Migration Approaches | ||
|
|
||
| This repository supports **two migration flows** for services using `pkg/db`. | ||
| Choose the versioned flow if you need per‑version interleaving, or the legacy | ||
| flow for a single AutoMigrate pass. | ||
|
|
||
| ## 1) Versioned Migration Flow | ||
|
|
||
| Use when you need **interleaving** per migration version. | ||
|
|
||
| **API** | ||
| - `WithVersionedMigrationFunc(func(*gorm.DB, uint) error)` | ||
| - Optional: `WithMigrationVersion(version)` | ||
|
|
||
| **Behavior** | ||
| - Runs `setup.sql` once (idempotent). | ||
| - Runs `fdw.up.sql` / `fdw.down.sql` once (optional). | ||
| - For each version from current+1 to target: | ||
| 1. Versioned before script (optional) | ||
| 2. `AutoMigrate(version)` (service implementation) | ||
| 3. Versioned after script (optional) | ||
| 4. Record version **after** the full sequence completes | ||
|
|
||
| **Notes** | ||
| - Missing before/after scripts are skipped. | ||
| - The version bump represents completion of before + AutoMigrate + after. | ||
| - **Recording a version** means updating the `migrations` table used by | ||
| `golang-migrate` with the new version number. | ||
| - **Supported naming (versioned flow only):** | ||
| - Before: `{version}_{name}.before.sql` **or** `{version}_{name}.before.up.sql` | ||
| - After: `{version}_{name}.after.sql` **or** `{version}_{name}.after.up.sql` | ||
|
|
||
| **Diagram** | ||
| ```mermaid | ||
| flowchart TB | ||
| %% Versioned Migration Logic | ||
|
|
||
| B0["Start migrations"] --> B1["setup.sql (optional, idempotent)"] | ||
| B1 --> B2["fdw.up.sql (optional)"] | ||
| B2 --> V1["For each version v = current+1 .. target"] | ||
| V1 --> V2["{version}_{name}.before.sql or .before.up.sql (optional)"] | ||
| V2 --> V3["AutoMigrate(version)"] | ||
| V3 --> V4["{version}_{name}.after.sql or .after.up.sql (optional)"] | ||
| V4 --> V5["Record version v (migrations table)"] | ||
| V5 -.-> V1 | ||
| V5 --> B3["fdw.down.sql (optional)"] | ||
| ``` | ||
|
|
||
| ## 2) Legacy Migration Flow | ||
|
|
||
| Use when you want a single AutoMigrate call and minimal changes to existing | ||
| services. | ||
|
|
||
| **API** | ||
| - `WithMigrationFunc(func(*gorm.DB) error)` | ||
| - Optional: `WithMigrationVersion(version)` | ||
|
|
||
| **Behavior** | ||
| - Runs **one** AutoMigrate (latest models). | ||
| - Runs `setup.sql` (idempotent), then `fdw.up.sql` (optional). | ||
| - Runs numbered SQL migrations via `golang-migrate`: | ||
| - `{version}_{name}.up.sql` / `{version}_{name}.down.sql` | ||
| - Runs `fdw.down.sql` (optional). | ||
|
|
||
| **Diagram** | ||
| ```mermaid | ||
| flowchart TB | ||
| %% Legacy Migration Logic | ||
|
|
||
| A0["Start migrations"] --> A1["AutoMigrate (once, latest models)"] | ||
| A1 --> A2["setup.sql (optional, idempotent)"] | ||
| A2 --> A3["fdw.up.sql (optional)"] | ||
| A3 --> L1["For each version v = current+1 .. target"] | ||
| L1 --> L2["{version}_{name}.up.sql"] | ||
| L2 -.-> L1 | ||
| L2 --> A4["fdw.down.sql (optional)"] | ||
| ``` | ||
|
|
||
| ## File Naming Summary | ||
|
|
||
| Versioned flow: | ||
| - Before: `{version}_{name}.before.sql` or `{version}_{name}.before.up.sql` | ||
| - After: `{version}_{name}.after.sql` or `{version}_{name}.after.up.sql` | ||
|
|
||
| Legacy flow: | ||
| - SQL migrations: `{version}_{name}.up.sql` | ||
|
|
||
| Shared: | ||
| - Setup: `setup.sql` | ||
| - FDW: `fdw.up.sql`, `fdw.down.sql` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| flowchart TB | ||
| %% Legacy Example: Current v5 -> Target v7 | ||
|
|
||
| A0["Current DB version: 5"] --> A1["Target version: 7"] | ||
| A1 --> A2["AutoMigrate (once, latest models)"] | ||
| A2 --> A3["setup.sql (optional, idempotent)"] | ||
| A3 --> A4["fdw.up.sql (optional)"] | ||
| A4 --> A5["006_fix_constraint.up.sql"] | ||
| A5 --> A6["007_add_feature.up.sql"] | ||
| A6 --> A7["fdw.down.sql (optional)"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| flowchart TB | ||
| %% Legacy Migration Logic (as on main) | ||
|
|
||
| A0["Start migrations"] --> A1["AutoMigrate (once, latest models)"] | ||
| A1 --> A2["setup.sql (optional, idempotent)"] | ||
| A2 --> A3["fdw.up.sql (optional)"] | ||
| A3 --> L1["For each version v = current+1 .. target"] | ||
| L1 --> L2["{version}_{name}.up.sql"] | ||
| L2 -.-> L1 | ||
| L2 --> A4["fdw.down.sql (optional)"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| flowchart TB | ||
| %% Versioned Example: Current v5 -> Target v7 | ||
|
|
||
| B0["Current DB version: 5"] --> B1["Target version: 7"] | ||
| B1 --> B2["setup.sql (optional, idempotent)"] | ||
| B2 --> B3["fdw.up.sql (optional)"] | ||
|
|
||
| B3 --> V1["006_fix_constraint.before.sql (optional)"] | ||
| V1 --> V2["AutoMigrate(6)"] | ||
| V2 --> V3["006_fix_constraint.after.sql (optional)"] | ||
| V3 --> V3b["Record version 6"] | ||
|
|
||
| V3b --> V4["007_add_feature.before.sql (optional)"] | ||
| V4 --> V5["AutoMigrate(7)"] | ||
| V5 --> V6["007_add_feature.after.sql (optional)"] | ||
| V6 --> V6b["Record version 7"] | ||
|
|
||
| V6b --> B4["fdw.down.sql (optional)"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| flowchart TB | ||
| %% Versioned Migration Logic (WithVersionedMigrationFunc) | ||
|
|
||
| B0["Start migrations"] --> B1["setup.sql (optional, idempotent)"] | ||
| B1 --> B2["fdw.up.sql (optional)"] | ||
| B2 --> V1["For each version v = current+1 .. target"] | ||
| V1 --> V2["{version}_{name}.before.sql or .before.up.sql (optional)"] | ||
| V2 --> V3["AutoMigrate(version)"] | ||
| V3 --> V4["{version}_{name}.after.sql or .after.up.sql (optional)"] | ||
| V4 --> V5["Record version v"] | ||
| V5 -.-> V1 | ||
| V5 --> B3["fdw.down.sql (optional)"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| func TestRunMigrationRejectsBothLegacyAndVersionedFuncs(t *testing.T) { | ||
| legacyFn := func(_ *gorm.DB) error { return nil } | ||
| versionedFn := func(_ *gorm.DB, _ uint) error { return nil } | ||
|
|
||
| err := runMigration(&gorm.DB{}, legacyFn, versionedFn, 1, true) | ||
| if err == nil { | ||
| t.Fatalf("expected error") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this doesn't warrant its own test file? Could just be a test case in the other test file.