Skip to content

Commit

Permalink
Merge pull request #100 from EduOJ/dev/migrate-test
Browse files Browse the repository at this point in the history
Migration Test
  • Loading branch information
leoleoasd authored Aug 30, 2021
2 parents 0a0e857 + 30ccdea commit 0b82c49
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
18 changes: 17 additions & 1 deletion database/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"github.com/EduOJ/backend/base"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gorm.io/datatypes"
"gorm.io/gorm"
"time"
Expand Down Expand Up @@ -576,6 +578,9 @@ func GetMigration() *gormigrate.Gormigrate {
if err != nil {
return err
}
if viper.GetString("database.dialect") == "sqlite" {
return nil
}
return tx.Migrator().DropConstraint(&Submission{}, "fk_submissions_language")
},
},
Expand Down Expand Up @@ -729,6 +734,9 @@ func GetMigration() *gormigrate.Gormigrate {
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
}
if viper.GetString("database.dialect") == "sqlite" {
return nil
}
return tx.Migrator().DropConstraint(&Problem{}, "fk_submissions_language")
},
},
Expand Down Expand Up @@ -1097,7 +1105,15 @@ func GetMigration() *gormigrate.Gormigrate {
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
}
return tx.Migrator().DropIndex(&Grade{}, "grade_user_problem_set")
if tx.Migrator().HasIndex(&Grade{}, "grade_user_problem_set") {
return tx.Migrator().DropIndex(&Grade{}, "grade_user_problem_set")
} else {
if viper.GetString("database.dialect") != "sqlite" {
return errors.New("Missing grade_user_problem_set index")
} else {
return nil
}
}
},
},
{
Expand Down
12 changes: 12 additions & 0 deletions database/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package database

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestMigrate(t *testing.T) {
defer SetupDatabaseForTest()()
m := GetMigration()
assert.NoError(t, m.RollbackTo("start"))
}
2 changes: 2 additions & 0 deletions database/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"fmt"
"github.com/EduOJ/backend/base"
"github.com/spf13/viper"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
Expand All @@ -16,6 +17,7 @@ func SetupDatabaseForTest() func() {
Logger: logger.Default.LogMode(logger.Silent),
DisableForeignKeyConstraintWhenMigrating: true,
})
viper.Set("database.dialect", "sqlite")
if err != nil {
fmt.Print(err)
panic(err)
Expand Down

0 comments on commit 0b82c49

Please sign in to comment.