mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 20:35:24 +01:00
fix(be): migration for dropping foreign key
This commit is contained in:
parent
c578653f25
commit
1993a14fb6
@ -11,14 +11,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
autoIncrementRE = regexp.MustCompile(`(?i)\bautoincrement\b`)
|
autoIncrementRE = regexp.MustCompile(`(?i)\bautoincrement\b`)
|
||||||
serialRE = regexp.MustCompile(`(?i)\binteger primary key autoincrement\b`)
|
serialRE = regexp.MustCompile(`(?i)\binteger primary key autoincrement\b`)
|
||||||
dateTimeTypeRE = regexp.MustCompile(`(?i)\bdatetime\b`)
|
dateTimeTypeRE = regexp.MustCompile(`(?i)\bdatetime\b`)
|
||||||
tinyintRE = regexp.MustCompile(`(?i)\btinyint\b`)
|
tinyintRE = regexp.MustCompile(`(?i)\btinyint\b`)
|
||||||
longtextRE = regexp.MustCompile(`(?i)\blongtext\b`)
|
longtextRE = regexp.MustCompile(`(?i)\blongtext\b`)
|
||||||
ifExistsRE = regexp.MustCompile(`(?i)\bif exists\b`)
|
ifExistsRE = regexp.MustCompile(`(?i)\bif exists\b`)
|
||||||
changeRE = regexp.MustCompile(`^alter table \x60(\w+)\x60 change \x60(\w+)\x60 \x60(\w+)\x60 ([\w\(\)]+)( not null)?$`)
|
changeRE = regexp.MustCompile(`^alter table \x60(\w+)\x60 change \x60(\w+)\x60 \x60(\w+)\x60 ([\w\(\)]+)( not null)?$`)
|
||||||
dropForeignKeyRE = regexp.MustCompile(`^alter table \x60(\w+)\x60 drop foreign key \x60(\w+)\x60 /\* postgres:\x60(\w*)\x60 mysql:\x60(\w*)\x60 \*/$`)
|
//dropForeignKeyRE = regexp.MustCompile(`^alter table \x60(\w+)\x60 drop foreign key \x60(\w+)\x60 /\* postgres:\x60(\w*)\x60 mysql:\x60(\w*)\x60 \*/$`)
|
||||||
dropForeignKey2RE = regexp.MustCompile(`(?i)\bdrop foreign key\b`)
|
dropForeignKey2RE = regexp.MustCompile(`(?i)\bdrop foreign key\b`)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -51,33 +51,10 @@ func getVersionSQL(path string) (queries []string) {
|
|||||||
func (d *SqlDb) prepareMigration(query string) string {
|
func (d *SqlDb) prepareMigration(query string) string {
|
||||||
switch d.sql.Dialect.(type) {
|
switch d.sql.Dialect.(type) {
|
||||||
case gorp.MySQLDialect:
|
case gorp.MySQLDialect:
|
||||||
mysqlFullVersion, err := d.sql.SelectStr("select version()")
|
|
||||||
if err == nil && strings.Contains(mysqlFullVersion, "MariaDB") {
|
|
||||||
// Actions for MariaDB only
|
|
||||||
} else {
|
|
||||||
// Actions for MySQL only
|
|
||||||
m := dropForeignKeyRE.FindStringSubmatch(query)
|
|
||||||
if m != nil {
|
|
||||||
tableName := m[1]
|
|
||||||
foreignKeyNameMySQL := m[4]
|
|
||||||
if foreignKeyNameMySQL == "" {
|
|
||||||
query = ""
|
|
||||||
} else {
|
|
||||||
query = "alter table `" + tableName + "` drop constraint `" + foreignKeyNameMySQL + "`"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
query = autoIncrementRE.ReplaceAllString(query, "auto_increment")
|
query = autoIncrementRE.ReplaceAllString(query, "auto_increment")
|
||||||
query = ifExistsRE.ReplaceAllString(query, "")
|
query = ifExistsRE.ReplaceAllString(query, "")
|
||||||
case gorp.PostgresDialect:
|
case gorp.PostgresDialect:
|
||||||
m := dropForeignKeyRE.FindStringSubmatch(query)
|
m := changeRE.FindStringSubmatch(query)
|
||||||
if m != nil {
|
|
||||||
tableName := m[1]
|
|
||||||
foreignKeyNamePostgres := m[3]
|
|
||||||
query = "alter table `" + tableName + "` drop constraint `" + foreignKeyNamePostgres + "`"
|
|
||||||
}
|
|
||||||
|
|
||||||
m = changeRE.FindStringSubmatch(query)
|
|
||||||
if m != nil {
|
if m != nil {
|
||||||
tableName := m[1]
|
tableName := m[1]
|
||||||
oldColumnName := m[2]
|
oldColumnName := m[2]
|
||||||
@ -194,6 +171,8 @@ func (d *SqlDb) ApplyMigration(migration db.Migration) error {
|
|||||||
switch migration.Version {
|
switch migration.Version {
|
||||||
case "2.8.26":
|
case "2.8.26":
|
||||||
err = migration_2_8_26{db: d}.Apply(tx)
|
err = migration_2_8_26{db: d}.Apply(tx)
|
||||||
|
case "2.8.42":
|
||||||
|
err = migration_2_8_42{db: d}.Apply(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
19
db/sql/migration_2_8_42.go
Normal file
19
db/sql/migration_2_8_42.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package sql
|
||||||
|
|
||||||
|
import "github.com/go-gorp/gorp/v3"
|
||||||
|
|
||||||
|
type migration_2_8_42 struct {
|
||||||
|
db *SqlDb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migration_2_8_42) Apply(tx *gorp.Transaction) error {
|
||||||
|
switch m.db.sql.Dialect.(type) {
|
||||||
|
case gorp.MySQLDialect:
|
||||||
|
_, _ = tx.Exec(m.db.PrepareQuery("alter table `task` drop foreign key `task_ibfk_3`"))
|
||||||
|
case gorp.PostgresDialect:
|
||||||
|
_, err := tx.Exec(
|
||||||
|
m.db.PrepareQuery("alter table `task` drop constraint if exists `task_build_task_id_fkey`"))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
alter table `task` drop foreign key `task_ibfk_3` /* postgres:`task_build_task_id_fkey` mysql:`` */;
|
-- see migration_2_8_42.go
|
||||||
|
Loading…
Reference in New Issue
Block a user