feat(migartions): drop foreign

This commit is contained in:
Denis Gukov 2024-10-05 12:09:17 +05:00
parent 28e81b53d5
commit a2f6a1e5f4
4 changed files with 40 additions and 10 deletions

View File

@ -142,6 +142,16 @@ func (d *SqlDb) ApplyMigration(migration db.Migration) error {
return err
}
switch migration.Version {
case "2.10.24":
err = migration_2_10_24{db: d}.PreApply(tx)
}
if err != nil {
handleRollbackError(tx.Rollback())
return err
}
queries := getVersionSQL(getVersionPath(migration))
for i, query := range queries {
fmt.Printf("\r [%d/%d]", i+1, len(query))
@ -164,20 +174,21 @@ func (d *SqlDb) ApplyMigration(migration db.Migration) error {
}
}
_, err = tx.Exec(d.PrepareQuery("insert into migrations(version, upgraded_date) values (?, ?)"), migration.Version, time.Now())
switch migration.Version {
case "2.8.26":
err = migration_2_8_26{db: d}.PostApply(tx)
case "2.8.42":
err = migration_2_8_42{db: d}.PostApply(tx)
}
if err != nil {
handleRollbackError(tx.Rollback())
return err
}
switch migration.Version {
case "2.8.26":
err = migration_2_8_26{db: d}.Apply(tx)
case "2.8.42":
err = migration_2_8_42{db: d}.Apply(tx)
}
_, err = tx.Exec(d.PrepareQuery("insert into migrations(version, upgraded_date) values (?, ?)"), migration.Version, time.Now())
if err != nil {
handleRollbackError(tx.Rollback())
return err
}

View File

@ -0,0 +1,19 @@
package sql
import "github.com/go-gorp/gorp/v3"
type migration_2_10_24 struct {
db *SqlDb
}
func (m migration_2_10_24) PreApply(tx *gorp.Transaction) error {
switch m.db.sql.Dialect.(type) {
case gorp.MySQLDialect:
_, _ = tx.Exec(m.db.PrepareQuery("alter table `project__template` drop foreign key `project__template_ibfk_6`"))
case gorp.PostgresDialect:
_, err := tx.Exec(
m.db.PrepareQuery("alter table `project__template` drop constraint if exists `project__template_vault_key_id_fkey`"))
return err
}
return nil
}

View File

@ -9,7 +9,7 @@ type migration_2_8_26 struct {
db *SqlDb
}
func (m migration_2_8_26) Apply(tx *gorp.Transaction) error {
func (m migration_2_8_26) PostApply(tx *gorp.Transaction) error {
rows, err := tx.Query(m.db.PrepareQuery("SELECT id, git_url FROM project__repository"))
if err != nil {
return err

View File

@ -6,7 +6,7 @@ type migration_2_8_42 struct {
db *SqlDb
}
func (m migration_2_8_42) Apply(tx *gorp.Transaction) error {
func (m migration_2_8_42) PostApply(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`"))