Remove MSSQL specific code and cleanups (#1796)

We don't have MSSQL support

---------

Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
qwerty287 2023-05-31 21:27:57 +02:00 committed by GitHub
parent 524611cf00
commit f91ee5d23a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 11 additions and 58 deletions

View file

@ -46,7 +46,7 @@ func newTestStore(t *testing.T, tables ...interface{}) (*storage, func()) {
}
for _, table := range tables {
if err := engine.Sync2(table); err != nil {
if err := engine.Sync(table); err != nil {
t.Error(err)
t.FailNow()
}

View file

@ -86,13 +86,13 @@ var legacy2Xorm = task{
{ // recreate build_config
type BuildConfig struct {
ConfigID int64 `xorm:"NOT NULL 'config_id'"` // xorm.Sync2() do not use index info of sess -> so it try to create it twice
ConfigID int64 `xorm:"NOT NULL 'config_id'"` // xorm.Sync() do not use index info of sess -> so it tries to create it twice
BuildID int64 `xorm:"NOT NULL 'build_id'"`
}
if err := renameTable(sess, "build_config", "old_build_config"); err != nil {
return err
}
if err := sess.Sync2(new(BuildConfig)); err != nil {
if err := sess.Sync(new(BuildConfig)); err != nil {
return err
}
if _, err := sess.Exec("INSERT INTO build_config (config_id, build_id) SELECT config_id,build_id FROM old_build_config;"); err != nil {

View file

@ -15,8 +15,6 @@ var alterTableLogUpdateColumnLogDataType = task{
_, err = sess.Exec("ALTER TABLE logs ALTER COLUMN log_data TYPE BYTEA")
case schemas.MYSQL:
_, err = sess.Exec("ALTER TABLE logs MODIFY COLUMN log_data LONGBLOB")
case schemas.MSSQL:
_, err = sess.Exec("ALTER TABLE logs MODIFY COLUMN log_data VARBINARY")
default:
// sqlite does only know BLOB in all cases
return nil

View file

@ -32,7 +32,7 @@ func (SecretV007) TableName() string {
var alterTableSecretsAddUserCol = task{
name: "alter-table-add-secrets-user-id",
fn: func(sess *xorm.Session) error {
if err := sess.Sync2(new(SecretV007)); err != nil {
if err := sess.Sync(new(SecretV007)); err != nil {
return err
}
if err := alterColumnDefault(sess, "secrets", "secret_repo_id", "0"); err != nil {

View file

@ -23,9 +23,9 @@ import (
var recreateAgentsTable = task{
name: "recreate-agents-table",
fn: func(sess *xorm.Session) error {
if err := dropTable(sess, "agents"); err != nil {
if err := sess.DropTable("agents"); err != nil {
return err
}
return sess.Sync2(new(model.Agent))
return sess.Sync(new(model.Agent))
},
}

View file

@ -36,7 +36,7 @@ var renameRemoteToForge = task{
}
// make sure the column exist before rename it
if err := sess.Sync2(new(oldRepo012)); err != nil {
if err := sess.Sync(new(oldRepo012)); err != nil {
return err
}

View file

@ -23,17 +23,6 @@ import (
"xorm.io/xorm/schemas"
)
func dropTable(sess *xorm.Session, table string) error {
dialect := sess.Engine().Dialect().URI().DBType
switch dialect {
case schemas.MYSQL, schemas.POSTGRES, schemas.SQLITE:
_, err := sess.Exec(fmt.Sprintf("DROP TABLE `%s`;", table))
return err
default:
return fmt.Errorf("dialect '%s' not supported", dialect)
}
}
func renameTable(sess *xorm.Session, old, new string) error {
dialect := sess.Engine().Dialect().URI().DBType
switch dialect {
@ -182,40 +171,6 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` %s", tableName, cols)); err != nil {
return fmt.Errorf("drop table `%s` columns %v: %w", tableName, columnNames, err)
}
case schemas.MSSQL:
cols := ""
for _, col := range columnNames {
if cols != "" {
cols += ", "
}
cols += "`" + strings.ToLower(col) + "`"
}
sql := fmt.Sprintf("SELECT Name FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID('%[1]s') AND parent_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: %w", err)
}
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` default constraint `%s`: %w", 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: %w", err)
}
for _, constraint := range constraints {
if _, err := sess.Exec(fmt.Sprintf("DROP INDEX `%s` ON `%s`", constraint, tableName)); err != nil {
return fmt.Errorf("drop index `%s` on `%s`: %w", constraint, tableName, 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: %w", tableName, columnNames, err)
}
default:
return fmt.Errorf("dialect '%s' not supported", dialect)
}

View file

@ -92,7 +92,7 @@ func initNew(sess *xorm.Session) error {
}
func Migrate(e *xorm.Engine) error {
if err := e.Sync2(new(migrations)); err != nil {
if err := e.Sync(new(migrations)); err != nil {
return err
}
@ -171,13 +171,13 @@ func runTasks(sess *xorm.Session, tasks []*task) error {
}
type syncEngine interface {
Sync2(beans ...interface{}) error
Sync(beans ...interface{}) error
}
func syncAll(sess syncEngine) error {
for _, bean := range allBeans {
if err := sess.Sync2(bean); err != nil {
return fmt.Errorf("sync2 error '%s': %w", reflect.TypeOf(bean), err)
if err := sess.Sync(bean); err != nil {
return fmt.Errorf("Sync error '%s': %w", reflect.TypeOf(bean), err)
}
}
return nil