-
Notifications
You must be signed in to change notification settings - Fork 26
Feat/db auto migrate #539
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
Merged
Merged
Feat/db auto migrate #539
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8525ef4
feat(datastore): added AutoMigrate
cnlangzi 5559369
feat(datastore): added log if migrate fails
cnlangzi b98e4df
feat(datastore): added AutoMigrate
cnlangzi 896f3e3
feat(datastore): added log if migrate fails
cnlangzi 0d1f022
Merge branch 'feat/db_auto_migrate' of github.com:0chain/blobber into…
cnlangzi 805c4cb
feat(datastore): improved gorm config for performance
cnlangzi 4a745b9
feat(datastore): improved migration comparasion
cnlangzi f988aef
feat(datastore): get latest version by created_at instead of version
cnlangzi 97a69d9
feat(datastore): improved migrate logic
cnlangzi df58b53
feat(datastore): improved README.md
cnlangzi fd4b5a9
feat(datastore): improved README.md
cnlangzi 112c468
feat(datastore): improved naming
cnlangzi 1dee25d
feat(datastore): improved loop
cnlangzi 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
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
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
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
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
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,43 @@ | ||
| # How to auto migrate database schema | ||
|
|
||
| ## what is `version`. how it works | ||
| Given a version number TABLE.INDEX.COLUMN, increment the: | ||
|
|
||
| - TABLE version when you add/drop any table, | ||
| - INDEX version when you add/drop/update and index | ||
| - COLUMN version when you add/drop/update any column | ||
|
|
||
| NB: current schema that is created by sql scripts is versioned as `0.0.0`. | ||
|
|
||
| ## How to add a new version | ||
|
|
||
| ### Migrate table/column in gorm.AutoMigrate | ||
| if migration works with gorm.AutoMigrate, please use it to migrate. | ||
| - update your model | ||
| - added your model in [AutoMigrate](postgres.go#L76)if it doesn't exists | ||
| ``` | ||
| db.AutoMigrate(&Migration{},&YourModel{}) | ||
| ``` | ||
|
|
||
| ### Migrate index/constraints manually if it is not easy to do in `AutoMigrate` | ||
| - create a new `Migration` with scripts | ||
| - append it in [releases](postgres_releases.go#L4) | ||
| ``` | ||
| var releases = []Migration{ | ||
| { | ||
| Version: "0.1.0", | ||
| CreatedAt: time.Date(2021, 10, 15, 0, 0, 0, 0, time.UTC), | ||
| Scripts: []string{ | ||
| "CREATE INDEX idx_allocation_path ON reference_objects (allocation_id,path);", | ||
| }, | ||
| }, | ||
| { | ||
| Version: "0.1.1", | ||
| CreatedAt: time.Date(2021, 10, 16, 0, 0, 0, 0, time.UTC), | ||
| Scripts: []string{ | ||
| "sql1", | ||
| "sql2", | ||
| }, | ||
| }, | ||
| } | ||
| ``` |
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
45 changes: 45 additions & 0 deletions
45
code/go/0chain.net/blobbercore/datastore/postgres_migration.go
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,45 @@ | ||
| package datastore | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| const ( | ||
| TableNameMigration = "migrations" | ||
| ) | ||
|
|
||
| // Migration migration history | ||
| type Migration struct { | ||
| // Version format is "[table].[index].[column]" | ||
| // + increase table version if any table is added or deleted | ||
| // + increase index version if any index is changed | ||
| // + increase column version if any column/constraint is changed | ||
| Version string `gorm:"column:version;primary_key"` | ||
| CreatedAt time.Time `gorm:"column:created_at"` | ||
| Scripts []string `gorm:"-"` | ||
| } | ||
|
|
||
| // Migrate migrate database | ||
| func (m *Migration) Migrate(db *gorm.DB) error { | ||
| return db.Transaction(func(tx *gorm.DB) error { | ||
|
|
||
| for _, s := range m.Scripts { | ||
| if err := tx.Exec(s).Error; err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if err := tx.Create(m).Error; err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| // TableName get table name of migrate | ||
| func (Migration) TableName() string { | ||
| return TableNameMigration | ||
| } |
12 changes: 12 additions & 0 deletions
12
code/go/0chain.net/blobbercore/datastore/postgres_releases.go
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 @@ | ||
| package datastore | ||
|
|
||
| // releases migration histories | ||
| var releases = []Migration{ | ||
| // { | ||
| // Version: "0.1.0", | ||
| // CreatedAt: time.Date(2021, 10, 15, 0, 0, 0, 0, time.UTC), | ||
| // Scripts: []string{ | ||
| // "CREATE INDEX idx_allocation_path ON reference_objects (allocation_id,path,deleted_at);", | ||
| // }, | ||
| // }, | ||
| } |
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 |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ type Store interface { | |
|
|
||
| Open() error | ||
| Close() | ||
| AutoMigrate() error | ||
| } | ||
|
|
||
| var instance Store | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.