Skip to content

Commit

Permalink
attempt to fix drop tables test
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Mar 23, 2021
1 parent c8ac860 commit a64dbd7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 38 deletions.
17 changes: 16 additions & 1 deletion models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,24 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
}
for _, constraint := range constraints {
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT `%s`", tableName, constraint)); err != nil {
return fmt.Errorf("Drop table `%s` constraint `%s`: %v", tableName, constraint, err)
return fmt.Errorf("Drop table `%s` default constraint `%s`: %v", tableName, constraint, err)
}
}
sql = fmt.Sprintf("SELECT DISTINCT Name FROM SYS.INDEXES INNER JOIN SYS.INDEX_COLUMNS ON INDEXES.INDEX_ID = INDEX_COLUMNS.INDEX_ID AND INDEXES.OBJECT_ID = INDEX_COLUMNS.OBJECT_ID WHERE INDEXES.OBJECT_ID = OBJECT_ID('%[1]s') AND INDEX_COLUMNS.COLUMN_ID IN (SELECT column_id FROM sys.columns WHERE lower(NAME) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
tableName, strings.ReplaceAll(cols, "`", "'"))
constraints = make([]string, 0)
if err := sess.SQL(sql).Find(&constraints); err != nil {
return fmt.Errorf("Find constraints: %v", err)
}
for _, constraint := range constraints {
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT IF EXISTS `%s`", tableName, constraint)); err != nil {
return fmt.Errorf("Drop table `%s` index constraint `%s`: %v", tableName, constraint, err)
}
if _, err := sess.Exec(fmt.Sprintf("DROP INDEX IF EXISTS `%[2]s` ON `%[1]s`", tableName, constraint)); err != nil {
return fmt.Errorf("Drop index `%[2]s` on `%[1]s`: %v", tableName, constraint, err)
}
}

if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP COLUMN %s", tableName, cols)); err != nil {
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
}
Expand Down
72 changes: 38 additions & 34 deletions models/migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,9 @@ func removeAllWithRetry(dir string) error {
return err
}

func getEngine() (*xorm.Engine, error) {
connStr, err := setting.DBConnStr()
if err != nil {
return nil, err
}

engine, err := xorm.NewEngine(setting.Database.Type, connStr)
if err != nil {
return nil, err
}
if setting.Database.Type == "mysql" {
engine.Dialect().SetParams(map[string]string{"rowFormat": "DYNAMIC"})
}
engine.SetSchema(setting.Database.Schema)
return engine, nil
}

// SetEngine sets the xorm.Engine
func SetEngine() (*xorm.Engine, error) {
x, err := getEngine()
x, err := models.GetNewEngine()
if err != nil {
return x, fmt.Errorf("Failed to connect to database: %v", err)
}
Expand Down Expand Up @@ -190,45 +173,66 @@ func Test_dropTableColumns(t *testing.T) {
}

columns := []string{
"FirstColumn",
"ToDropColumn",
"AnotherColumn",
"CreatedUnix",
"UpdatedUnix",
"first_column",
"to_drop_column",
"another_column",
"created_unix",
"updated_unix",
}

for i := range columns {
x.SetMapper(names.GonicMapper{})
if err := x.Sync2(new(DropTest)); err != nil {
assert.Fail(t, "%v", err)
t.Errorf("unable to create DropTest table: %v", err)
return
}
sess := x.NewSession()

if err := sess.Begin(); err != nil {
sess.Close()
t.Errorf("unable to begin transaction: %v", err)
return
}
if err := dropTableColumns(sess, "drop_test", columns[i:]...); err != nil {
sess.Close()
assert.Fail(t, "%v", err)
t.Errorf("Unable to drop columns[%d:]: %s from drop_test: %v", i, columns[i:], err)
return
}
if err := sess.Commit(); err != nil {
sess.Close()
t.Errorf("unable to commit transaction: %v", err)
return
}
sess.Close()
if _, err := x.DB().DB.Exec("DROP TABLE drop_test"); err != nil {
assert.Fail(t, "%v", err)
if err := x.DropTables(new(DropTest)); err != nil {
t.Errorf("unable to drop table: %v", err)
return
}
for j := range columns[i:] {
for j := range columns[i+1:] {
x.SetMapper(names.GonicMapper{})
if err := x.Sync2(new(DropTest)); err != nil {
assert.Fail(t, "%v", err)
t.Errorf("unable to create DropTest table: %v", err)
return
}
dropcols := append([]string{columns[i]}, columns[j:]...)
dropcols := append([]string{columns[i]}, columns[j+i+1:]...)
sess := x.NewSession()
if err := sess.Begin(); err != nil {
sess.Close()
t.Errorf("unable to begin transaction: %v", err)
return
}
if err := dropTableColumns(sess, "drop_test", dropcols...); err != nil {
sess.Close()
assert.Fail(t, "%v", err)
t.Errorf("Unable to drop columns: %s from drop_test: %v", dropcols, err)
return
}
if err := sess.Commit(); err != nil {
sess.Close()
t.Errorf("unable to commit transaction: %v", err)
return
}
sess.Close()
if _, err := x.DB().DB.Exec("DROP TABLE drop_test"); err != nil {
assert.Fail(t, "%v", err)
if err := x.DropTables(new(DropTest)); err != nil {
t.Errorf("unable to drop table: %v", err)
return
}
}
Expand Down
7 changes: 4 additions & 3 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ func init() {
}
}

func getEngine() (*xorm.Engine, error) {
// GetNewEngine returns a new xorm engine from the configuration
func GetNewEngine() (*xorm.Engine, error) {
connStr, err := setting.DBConnStr()
if err != nil {
return nil, err
Expand Down Expand Up @@ -172,7 +173,7 @@ func getEngine() (*xorm.Engine, error) {

// NewTestEngine sets a new test xorm.Engine
func NewTestEngine() (err error) {
x, err = getEngine()
x, err = GetNewEngine()
if err != nil {
return fmt.Errorf("Connect to database: %v", err)
}
Expand All @@ -185,7 +186,7 @@ func NewTestEngine() (err error) {

// SetEngine sets the xorm.Engine
func SetEngine() (err error) {
x, err = getEngine()
x, err = GetNewEngine()
if err != nil {
return fmt.Errorf("Failed to connect to database: %v", err)
}
Expand Down

0 comments on commit a64dbd7

Please sign in to comment.