Skip to content

Commit

Permalink
Upgraded gorm version, fixed issue with migration table name, init mi…
Browse files Browse the repository at this point in the history
…gration is also recorded, FK disable issue
  • Loading branch information
Solution committed Jan 4, 2021
1 parent 9ea292b commit 0de2874
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
gorm.io/gorm v1.20.2
gorm.io/gorm v1.20.9
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
gorm.io/gorm v1.20.2 h1:bZzSEnq7NDGsrd+n3evOOedDrY5oLM5QPlCjZJUK2ro=
gorm.io/gorm v1.20.2/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.9 h1:M3aIZKXAC1PtPVu9t3WGwkBTE1le5c2telz3I/qjRNg=
gorm.io/gorm v1.20.9/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
23 changes: 17 additions & 6 deletions gormigro.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"gorm.io/gorm/logger"
)

const (
InitialSchemaMigrationName = "__initialMigration"
)

// Gormigro is migration tool which allow you to simply handle migrations with gorm.io
type Gormigro struct {
// db gorm database
Expand Down Expand Up @@ -48,7 +52,7 @@ func NewGormigro(db *gorm.DB, options Options) *Gormigro {
options: options,
migrationsCollection: DefaultCollector.Export(),
migrationManager: NewMigrationManager(db.Session(&gorm.Session{
WithConditions: false,
NewDB: true,
}), options.MigrationTable),
}
}
Expand All @@ -65,7 +69,7 @@ func NewGormigroWithMigrations(db *gorm.DB, options Options, migrations []Migrat
options: options,
migrationsCollection: NewCollectionWithMigrations(migrations),
migrationManager: NewMigrationManager(db.Session(&gorm.Session{
WithConditions: false,
NewDB: true,
}), options.MigrationTable),
}
}
Expand Down Expand Up @@ -132,7 +136,7 @@ func (g *Gormigro) MigrateFrom(id string) error {
log.Printf("Running migration with ID %s\n", m.ID)
// start the transaction per migration
tx := g.db.Session(&gorm.Session{
WithConditions: false,
NewDB: true,
})
if tx.Error != nil {
return tx.Error
Expand Down Expand Up @@ -192,7 +196,7 @@ func (g *Gormigro) DropSchema() error {
}

rows, err := g.db.Session(&gorm.Session{
WithConditions: false,
NewDB: true,
}).Raw("SHOW TABLES").Rows()
if err != nil {
return err
Expand All @@ -201,7 +205,7 @@ func (g *Gormigro) DropSchema() error {
defer rows.Close()

// disable FK check for safe remove
g.db.Exec("SET FOREIGN_KEY_CHECKS=0;")
g.db.Exec("SET GLOBAL FOREIGN_KEY_CHECKS = 0;")
for rows.Next() {
var table Table

Expand All @@ -216,7 +220,7 @@ func (g *Gormigro) DropSchema() error {
}

// and enable again
g.db.Exec("SET FOREIGN_KEY_CHECKS=1;")
g.db.Exec("SET GLOBAL FOREIGN_KEY_CHECKS = 1;")

log.Println("Dropping completed")

Expand All @@ -227,6 +231,10 @@ func (g *Gormigro) DropSchema() error {
func (g *Gormigro) runInitialSchemaFunc() error {
log.Println("Running initial schema migration")

if g.migrationManager.IsMigrationExecuted(InitialSchemaMigrationName) {
return nil
}

// start transaction
tx := g.db.Begin()

Expand All @@ -238,5 +246,8 @@ func (g *Gormigro) runInitialSchemaFunc() error {
return err
}

// and mark the init func as executed
g.migrationManager.AddMigration(InitialSchemaMigrationName)

return tx.Commit().Error
}
14 changes: 14 additions & 0 deletions migrationManager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gormigro

import (
"errors"

"gorm.io/gorm"
)

Expand Down Expand Up @@ -60,6 +62,18 @@ func (mm MigrationManager) RemoveMigration(id string) {
}
}

func (mm MigrationManager) IsMigrationExecuted(id string) bool {
res := mm.db.Find(NewMigrationTable(id))
if res.Error != nil && errors.Is(res.Error, gorm.ErrRecordNotFound) {
return false
}
if res.Error != nil {
panic(res.Error)
}

return true
}

func (mm MigrationManager) ClearExecutedMigrations() error {
res := mm.db.Exec("TRUNCATE `", mm.tableName)

Expand Down
4 changes: 3 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ type MigrationTable struct {
}

func NewMigrationTable(tableName string) *MigrationTable {
// temporary fix, gorm cloning the struct thru reflection and there's
// so it'll override the tableName settings
return &MigrationTable{
tableName: tableName,
tableName: DefaultMigrationTable,
}
}

Expand Down

0 comments on commit 0de2874

Please sign in to comment.