Skip to content

Commit

Permalink
Support Gorm V2
Browse files Browse the repository at this point in the history
  • Loading branch information
dacohen committed Sep 2, 2020
1 parent 3ee2278 commit b029edf
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 108 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"log"

"gopkg.in/gormigrate.v1"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)

Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// "log"
//
// "github.com/go-gormigrate/gormigrate"
// "github.com/jinzhu/gorm"
// "gorm.io/gorm"
// _ "github.com/jinzhu/gorm/dialects/sqlite"
// )
//
Expand Down
22 changes: 8 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
module gopkg.in/gormigrate.v1

go 1.15

require (
cloud.google.com/go v0.33.1 // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/google/go-cmp v0.2.0 // indirect
github.com/jinzhu/gorm v1.9.2
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 // indirect
github.com/joho/godotenv v1.3.0
github.com/lib/pq v1.0.0 // indirect
github.com/mattn/go-sqlite3 v1.10.0 // indirect
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869 // indirect
google.golang.org/appengine v1.3.0 // indirect
github.com/stretchr/testify v1.5.1
gorm.io/driver/mysql v1.0.1
gorm.io/driver/postgres v1.0.0
gorm.io/driver/sqlite v1.1.1
gorm.io/driver/sqlserver v1.0.2
gorm.io/gorm v1.20.0
)
211 changes: 187 additions & 24 deletions go.sum

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions gormigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

"github.com/jinzhu/gorm"
"gorm.io/gorm"
)

const (
Expand Down Expand Up @@ -373,7 +373,7 @@ func (g *Gormigrate) runMigration(migration *Migration) error {
}

func (g *Gormigrate) createMigrationTableIfNotExists() error {
if g.tx.HasTable(g.options.TableName) {
if g.tx.Migrator().HasTable(g.options.TableName) {
return nil
}

Expand All @@ -382,7 +382,7 @@ func (g *Gormigrate) createMigrationTableIfNotExists() error {
}

func (g *Gormigrate) migrationRan(m *Migration) (bool, error) {
var count int
var count int64
err := g.tx.
Table(g.options.TableName).
Where(fmt.Sprintf("%s = ?", g.options.IDColumnName), m.ID).
Expand All @@ -403,7 +403,7 @@ func (g *Gormigrate) canInitializeSchema() (bool, error) {
}

// If the ID doesn't exist, we also want the list of migrations to be empty
var count int
var count int64
err = g.tx.
Table(g.options.TableName).
Count(&count).
Expand Down
101 changes: 49 additions & 52 deletions gormigrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,57 @@ package gormigrate
import (
"errors"
"fmt"
"os"
"testing"

"github.com/jinzhu/gorm"
_ "github.com/joho/godotenv/autoload"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)

var databases []database

type database struct {
name string
connEnv string
dialect string
driver gorm.Dialector
}

var migrations = []*Migration{
{
ID: "201608301400",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&Person{}).Error
return tx.AutoMigrate(&Person{})
},
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("people").Error
return tx.Migrator().DropTable("people")
},
},
{
ID: "201608301430",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&Pet{}).Error
return tx.AutoMigrate(&Pet{})
},
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("pets").Error
return tx.Migrator().DropTable("pets")
},
},
}

var extendedMigrations = append(migrations, &Migration{
ID: "201807221927",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&Book{}).Error
return tx.AutoMigrate(&Book{})
},
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("books").Error
return tx.Migrator().DropTable("books")
},
})

var failingMigration = []*Migration{
{
ID: "201904231300",
Migrate: func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Book{}).Error; err != nil {
if err := tx.AutoMigrate(&Book{}); err != nil {
return err
}
return errors.New("this transaction should be rolled back")
Expand Down Expand Up @@ -88,20 +87,20 @@ func TestMigration(t *testing.T) {

err := m.Migrate()
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, 2, tableCount(t, db, "migrations"))

err = m.RollbackLast()
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))

err = m.RollbackLast()
assert.NoError(t, err)
assert.False(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.False(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, 0, tableCount(t, db, "migrations"))
})
}
Expand All @@ -112,9 +111,9 @@ func TestMigrateTo(t *testing.T) {

err := m.MigrateTo("201608301430")
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.False(t, db.HasTable(&Book{}))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.False(t, db.Migrator().HasTable(&Book{}))
assert.Equal(t, 2, tableCount(t, db, "migrations"))
})
}
Expand All @@ -126,17 +125,17 @@ func TestRollbackTo(t *testing.T) {
// First, apply all migrations.
err := m.Migrate()
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.True(t, db.HasTable(&Book{}))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.True(t, db.Migrator().HasTable(&Book{}))
assert.Equal(t, 3, tableCount(t, db, "migrations"))

// Rollback to the first migration: only the last 2 migrations are expected to be rolled back.
err = m.RollbackTo("201608301400")
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.False(t, db.HasTable(&Book{}))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.False(t, db.Migrator().HasTable(&Book{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))
})
}
Expand All @@ -147,18 +146,18 @@ func TestInitSchemaNoMigrations(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, []*Migration{})
m.InitSchema(func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Person{}).Error; err != nil {
if err := tx.AutoMigrate(&Person{}); err != nil {
return err
}
if err := tx.AutoMigrate(&Pet{}).Error; err != nil {
if err := tx.AutoMigrate(&Pet{}); err != nil {
return err
}
return nil
})

assert.NoError(t, m.Migrate())
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))
})
}
Expand All @@ -170,15 +169,15 @@ func TestInitSchemaWithMigrations(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, migrations)
m.InitSchema(func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Person{}).Error; err != nil {
if err := tx.AutoMigrate(&Person{}); err != nil {
return err
}
return nil
})

assert.NoError(t, m.Migrate())
assert.True(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, 3, tableCount(t, db, "migrations"))
})
}
Expand All @@ -202,14 +201,14 @@ func TestInitSchemaAlreadyInitialised(t *testing.T) {
// Then migrate again, this time with a non empty initialisation
// This second initialisation should not happen!
m.InitSchema(func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Car{}).Error; err != nil {
if err := tx.AutoMigrate(&Car{}); err != nil {
return err
}
return nil
})
assert.NoError(t, m.Migrate())

assert.False(t, db.HasTable(&Car{}))
assert.False(t, db.Migrator().HasTable(&Car{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))
})
}
Expand All @@ -231,14 +230,14 @@ func TestInitSchemaExistingMigrations(t *testing.T) {
// Then migrate again, this time with a non empty initialisation
// This initialisation should not happen!
m.InitSchema(func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Car{}).Error; err != nil {
if err := tx.AutoMigrate(&Car{}); err != nil {
return err
}
return nil
})
assert.NoError(t, m.Migrate())

assert.False(t, db.HasTable(&Car{}))
assert.False(t, db.Migrator().HasTable(&Car{}))
assert.Equal(t, 2, tableCount(t, db, "migrations"))
})
}
Expand Down Expand Up @@ -333,20 +332,20 @@ func TestMigration_WithUseTransactions(t *testing.T) {

err := m.Migrate()
require.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, 2, tableCount(t, db, "migrations"))

err = m.RollbackLast()
require.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))

err = m.RollbackLast()
require.NoError(t, err)
assert.False(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.False(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, 0, tableCount(t, db, "migrations"))
}, "postgres", "sqlite3", "mssql")
}
Expand All @@ -362,7 +361,7 @@ func TestMigration_WithUseTransactionsShouldRollback(t *testing.T) {
// Migration should return an error and not leave around a Book table
err := m.Migrate()
assert.Error(t, err)
assert.False(t, db.HasTable(&Book{}))
assert.False(t, db.Migrator().HasTable(&Book{}))
}, "postgres", "sqlite3", "mssql")
}

Expand Down Expand Up @@ -398,7 +397,7 @@ func TestUnexpectedMigrationDisabled(t *testing.T) {
})
}

func tableCount(t *testing.T, db *gorm.DB, tableName string) (count int) {
func tableCount(t *testing.T, db *gorm.DB, tableName string) (count int64) {
assert.NoError(t, db.Table(tableName).Count(&count).Error)
return
}
Expand All @@ -409,19 +408,17 @@ func forEachDatabase(t *testing.T, fn func(database *gorm.DB), dialects ...strin
}

for _, database := range databases {
if len(dialects) > 0 && !contains(dialects, database.name) {
t.Skip(fmt.Sprintf("test is not supported by [%s] dialect", database.name))
if len(dialects) > 0 && !contains(dialects, database.dialect) {
t.Skip(fmt.Sprintf("test is not supported by [%s] dialect", database.dialect))
}

// Ensure defers are not stacked up for each DB
func() {
db, err := gorm.Open(database.name, os.Getenv(database.connEnv))
require.NoError(t, err, "Could not connect to database %s, %v", database.name, err)

defer db.Close()
db, err := gorm.Open(database.driver, &gorm.Config{})
require.NoError(t, err, "Could not connect to database %s, %v", database.dialect, err)

// ensure tables do not exists
assert.NoError(t, db.DropTableIfExists("migrations", "people", "pets").Error)
assert.NoError(t, db.Migrator().DropTable("migrations", "people", "pets"))

fn(db)
}()
Expand Down
7 changes: 4 additions & 3 deletions mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
package gormigrate

import (
_ "github.com/jinzhu/gorm/dialects/mysql"
"gorm.io/driver/mysql"
"os"
)

func init() {
databases = append(databases, database{
name: "mysql",
connEnv: "MYSQL_CONN_STRING",
dialect: "mysql",
driver: mysql.Open(os.Getenv("MYSQL_CONN_STRING")),
})
}
7 changes: 4 additions & 3 deletions postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
package gormigrate

import (
_ "github.com/jinzhu/gorm/dialects/postgres"
"gorm.io/driver/postgres"
"os"
)

func init() {
databases = append(databases, database{
name: "postgres",
connEnv: "PG_CONN_STRING",
dialect: "postgres",
driver: postgres.Open(os.Getenv("PG_CONN_STRING")),
})
}
Loading

0 comments on commit b029edf

Please sign in to comment.