mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-22 14:25:18 +02:00
Migrations should never use model structs directly, because the model structs can be different in different releases. e.g. if one migration uses "User" model, it works in the early releases, then one day, when the User model changes, the migration breaks because it will use the new (incorrect) User model, it should only use the old User model. The same to "modules/structs". --------- Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: delvh <dev.lh@web.de>
104 lines
2.6 KiB
Go
104 lines
2.6 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package base_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gitea.dev/modelmigration/base"
|
|
"gitea.dev/modelmigration/migrationtest"
|
|
"gitea.dev/modules/timeutil"
|
|
|
|
"xorm.io/xorm/names"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
migrationtest.MainTest(m)
|
|
}
|
|
|
|
func Test_DropTableColumns(t *testing.T) {
|
|
x, deferable := migrationtest.PrepareTestEnv(t, 0)
|
|
defer deferable()
|
|
// FIXME: this logic seems wrong. Need to add an assertion here in the future, but it seems causing failure.
|
|
if x == nil || t.Failed() {
|
|
t.Skip("PrepareTestEnv did not yield a usable engine")
|
|
}
|
|
|
|
type DropTest struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
FirstColumn string
|
|
ToDropColumn string `xorm:"unique"`
|
|
AnotherColumn int64
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
|
}
|
|
|
|
columns := []string{
|
|
"first_column",
|
|
"to_drop_column",
|
|
"another_column",
|
|
"created_unix",
|
|
"updated_unix",
|
|
}
|
|
|
|
x.SetMapper(names.GonicMapper{})
|
|
|
|
for i := range columns {
|
|
if err := x.Sync(new(DropTest)); err != nil {
|
|
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 := base.DropTableColumns(sess, "drop_test", columns[i:]...); err != nil {
|
|
sess.Close()
|
|
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.DropTables(new(DropTest)); err != nil {
|
|
t.Errorf("unable to drop table: %v", err)
|
|
return
|
|
}
|
|
for j := range columns[i+1:] {
|
|
if err := x.Sync(new(DropTest)); err != nil {
|
|
t.Errorf("unable to create DropTest table: %v", err)
|
|
return
|
|
}
|
|
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 := base.DropTableColumns(sess, "drop_test", dropcols...); err != nil {
|
|
sess.Close()
|
|
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.DropTables(new(DropTest)); err != nil {
|
|
t.Errorf("unable to drop table: %v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|