Skip to content
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

Support Gorm V2 #46

Merged
merged 1 commit into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
127 changes: 62 additions & 65 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,21 +87,21 @@ 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.Equal(t, 2, tableCount(t, db, "migrations"))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, int64(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.Equal(t, 1, tableCount(t, db, "migrations"))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, int64(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.Equal(t, 0, tableCount(t, db, "migrations"))
assert.False(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, int64(0), tableCount(t, db, "migrations"))
})
}

Expand All @@ -112,10 +111,10 @@ 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.Equal(t, 2, tableCount(t, db, "migrations"))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.False(t, db.Migrator().HasTable(&Book{}))
assert.Equal(t, int64(2), tableCount(t, db, "migrations"))
})
}

Expand All @@ -126,18 +125,18 @@ 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.Equal(t, 3, tableCount(t, db, "migrations"))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.True(t, db.Migrator().HasTable(&Book{}))
assert.Equal(t, int64(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.Equal(t, 1, tableCount(t, db, "migrations"))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.False(t, db.Migrator().HasTable(&Book{}))
assert.Equal(t, int64(1), tableCount(t, db, "migrations"))
})
}

Expand All @@ -147,19 +146,19 @@ 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.Equal(t, 1, tableCount(t, db, "migrations"))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, int64(1), tableCount(t, db, "migrations"))
})
}

Expand All @@ -170,16 +169,16 @@ 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.Equal(t, 3, tableCount(t, db, "migrations"))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, int64(3), tableCount(t, db, "migrations"))
})
}

Expand All @@ -202,15 +201,15 @@ 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.Equal(t, 1, tableCount(t, db, "migrations"))
assert.False(t, db.Migrator().HasTable(&Car{}))
assert.Equal(t, int64(1), tableCount(t, db, "migrations"))
})
}

Expand All @@ -231,15 +230,15 @@ 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.Equal(t, 2, tableCount(t, db, "migrations"))
assert.False(t, db.Migrator().HasTable(&Car{}))
assert.Equal(t, int64(2), tableCount(t, db, "migrations"))
})
}

Expand Down Expand Up @@ -333,21 +332,21 @@ 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.Equal(t, 2, tableCount(t, db, "migrations"))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.True(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, int64(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.Equal(t, 1, tableCount(t, db, "migrations"))
assert.True(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, int64(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.Equal(t, 0, tableCount(t, db, "migrations"))
assert.False(t, db.Migrator().HasTable(&Person{}))
assert.False(t, db.Migrator().HasTable(&Pet{}))
assert.Equal(t, int64(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")),
})
}
Loading