Skip to content

Commit

Permalink
RHINENG-5394: execute all add/drop queries and log error
Browse files Browse the repository at this point in the history
  • Loading branch information
psegedy committed Dec 11, 2023
1 parent 5e63d58 commit e24d99c
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions turnpike/controllers/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type Session struct {
Expand Down Expand Up @@ -301,50 +302,47 @@ func MigrateSystemPackage(c *gin.Context) {
var cnt int64
db := database.Db

execQuery(db, "VACUUM ANALYZE system_package;")
db.Table("system_package2").Count(&cnt)
if cnt > 0 {
utils.LogInfo("System_package2 table is not empty")
c.JSON(http.StatusNoContent, "System_package2 table is not empty, nothing to do.")
return
}

// nolint:lll
go func() {
if err := db.Exec(`
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_applicable_id_fkey;
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_installable_id_fkey;
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_name_id_fkey;
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_package_id_fkey;
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_rh_account_id_system_id_fkey;
DROP INDEX system_package2_account_pkg_name_idx;
DROP INDEX system_package2_package_id_idx;
`).Error; err != nil {
// don't fail when we couldn't remove constraints and indexes
// continue with migration
utils.LogWarn("err", err.Error(), "Couldn't remove constraints and indexes")
}
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_applicable_id_fkey;")
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_installable_id_fkey;")
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_name_id_fkey;")
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_package_id_fkey;")
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_rh_account_id_system_id_fkey;")
execQuery(db, "DROP INDEX system_package2_account_pkg_name_idx;")
execQuery(db, "DROP INDEX system_package2_package_id_idx;")

if err := db.Exec("CALL copy_system_packages();").Error; err != nil {
// truncate system_package2 table on failed migration
db.Exec("TRUNCATE TABLE system_package2;")
execQuery(db, "TRUNCATE TABLE system_package2;")
utils.LogError("err", err.Error(), "Migration failed")
return
}

// nolint:lll
if err := db.Exec(`
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_applicable_id_fkey FOREIGN KEY (applicable_id) REFERENCES package(id);
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_installable_id_fkey FOREIGN KEY (installable_id) REFERENCES package(id);
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_name_id_fkey FOREIGN KEY (name_id) REFERENCES package_name(id);
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_package_id_fkey FOREIGN KEY (package_id) REFERENCES package(id);
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_rh_account_id_system_id_fkey FOREIGN KEY (rh_account_id, system_id) REFERENCES system_platform (rh_account_id, id);
CREATE INDEX IF NOT EXISTS system_package2_account_pkg_name_idx
ON system_package2 (rh_account_id, name_id) INCLUDE (system_id, package_id, installable_id, applicable_id);
CREATE INDEX IF NOT EXISTS system_package2_package_id_idx on system_package2 (package_id);
`).Error; err != nil {
utils.LogError("err", err.Error(), "Couldn't add constraints and indexes")
return
}
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_applicable_id_fkey FOREIGN KEY (applicable_id) REFERENCES package(id);")
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_installable_id_fkey FOREIGN KEY (installable_id) REFERENCES package(id);")
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_name_id_fkey FOREIGN KEY (name_id) REFERENCES package_name(id);")
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_package_id_fkey FOREIGN KEY (package_id) REFERENCES package(id);")
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_rh_account_id_system_id_fkey FOREIGN KEY (rh_account_id, system_id) REFERENCES system_platform (rh_account_id, id);")
execQuery(db, `CREATE INDEX IF NOT EXISTS system_package2_account_pkg_name_idx
ON system_package2 (rh_account_id, name_id) INCLUDE (system_id, package_id, installable_id, applicable_id);`)
execQuery(db, "CREATE INDEX IF NOT EXISTS system_package2_package_id_idx on system_package2 (package_id);")
utils.LogInfo("System_package migration completed")
}()
c.JSON(http.StatusOK, "Migration started")
}

func execQuery(db *gorm.DB, query string) {
err := db.Exec(query).Error
if err != nil {
utils.LogWarn("err", err.Error(), "query", query, "Exec of query failed.")
}
}

0 comments on commit e24d99c

Please sign in to comment.