migrations: complete migrations flow
- create and drop tables or exec any sql in order
- important to add the last line in each sql file
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
2b9029464d
commit
5ee2a03280
@ -82,8 +82,12 @@ func main() {
|
|||||||
|
|
||||||
_, err = db.Exec(string(initSql))
|
_, err = db.Exec(string(initSql))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error("failed to exec db init for rev0", "err", err.Error())
|
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
|
||||||
rootLog.Warn("assume rev0 applied")
|
rootLog.Info("db is already initilized to rev0")
|
||||||
|
} else {
|
||||||
|
rootLog.Error("can not exec on db", "err", err.Error())
|
||||||
|
os.Exit(EXIT_FAIL)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rootLog.Info("db initilized")
|
rootLog.Info("db initilized")
|
||||||
@ -105,7 +109,7 @@ func main() {
|
|||||||
// At this point we don't have fixed file names for migrations. Use prefix to increment
|
// At this point we don't have fixed file names for migrations. Use prefix to increment
|
||||||
// Read all file names in embedFS and Query all migrations in _sft_db
|
// Read all file names in embedFS and Query all migrations in _sft_db
|
||||||
|
|
||||||
rows, err = db.Query("SELECT db_rev FROM _sft_db;")
|
rows, err = db.Query("SELECT db_rev FROM _sft_db ORDER BY db_rev;")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error("error", "err", err.Error())
|
rootLog.Error("error", "err", err.Error())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
@ -176,35 +180,53 @@ func main() {
|
|||||||
migs = append(migs, rev)
|
migs = append(migs, rev)
|
||||||
rootLog.Debug("loaded db migration", "name", rev.Filename)
|
rootLog.Debug("loaded db migration", "name", rev.Filename)
|
||||||
|
|
||||||
|
|
||||||
// FIXME: this logic is bad when we have 2 or more migrations pending
|
|
||||||
dbTx, err := db.Begin()
|
|
||||||
if err != nil {
|
|
||||||
rootLog.Error("failed to start migration Tx")
|
|
||||||
os.Exit(EXIT_FAIL)
|
|
||||||
}
|
|
||||||
if rev.Rev >= len(appliedMigs) {
|
|
||||||
rootLog.Info("applying migration", "name", rev.Filename)
|
|
||||||
_, err = dbTx.Exec(string(rev.Data))
|
|
||||||
if err != nil {
|
|
||||||
rootLog.Error("could not apply database migration", "err", err.Error())
|
|
||||||
dbTx.Rollback()
|
|
||||||
os.Exit(EXIT_FAIL)
|
|
||||||
}
|
|
||||||
rootLog.Debug("committing migration", "name", rev.Filename)
|
|
||||||
rootLog.Info("applied migration", "name", rev.Filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = dbTx.Commit()
|
|
||||||
if err != nil {
|
|
||||||
rootLog.Error("failed to commit migration", "err", err.Error(), "name", rev.Filename)
|
|
||||||
os.Exit(EXIT_FAIL)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// NOTE: Expect migs to be ordered 0..1..2...
|
// NOTE: Expect migs to be ordered 0..1..2...
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We have migs from migrations dir and appliedMigs from the db
|
||||||
|
|
||||||
|
if len(appliedMigs) > len(migs) {
|
||||||
|
rootLog.Error("wtf", "migs", len(migs), "appliedmigs", len(appliedMigs))
|
||||||
|
os.Exit(EXIT_FAIL)
|
||||||
|
}
|
||||||
|
|
||||||
|
rootLog.Info("database currently",
|
||||||
|
"applied_count", appliedMigs[len(appliedMigs)-1] + 1,
|
||||||
|
"available_count", migs[len(migs)-1].Rev + 1)
|
||||||
|
|
||||||
|
needMigration := false
|
||||||
|
if appliedMigs[len(appliedMigs)-1] < migs[len(migs)-1].Rev {
|
||||||
|
needMigration = true
|
||||||
|
}
|
||||||
|
|
||||||
|
pRev := appliedMigs[len(appliedMigs)-1] + 1
|
||||||
|
for {
|
||||||
|
if !needMigration { break }
|
||||||
|
rev := migs[pRev]
|
||||||
|
rlog := rootLog.With("rev", pRev, "name", rev.Filename)
|
||||||
|
rlog.Info("applying database migration")
|
||||||
|
rlog.Debug("apply migrations", "sql", string(rev.Data))
|
||||||
|
tx, err := db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
rootLog.Error("error: create db transaction", "err", err.Error())
|
||||||
|
os.Exit(EXIT_FAIL)
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(string(rev.Data))
|
||||||
|
if err != nil {
|
||||||
|
rootLog.Error("error: tx exec migration", "err", err.Error())
|
||||||
|
os.Exit(EXIT_FAIL)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
rootLog.Error("error: commit migration tx", "rev", rev.Rev, "err", err.Error())
|
||||||
|
os.Exit(EXIT_FAIL)
|
||||||
|
}
|
||||||
|
|
||||||
|
pRev++
|
||||||
|
if pRev > migs[len(migs)-1].Rev { break }
|
||||||
|
}
|
||||||
rootLog.Info("database up-to-date")
|
rootLog.Info("database up-to-date")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,6 @@ CREATE TABLE IF NOT EXISTS _sft_migtest_table2 (
|
|||||||
id integer PRIMARY KEY
|
id integer PRIMARY KEY
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO _sft_migtest_table (id) VALUES (42);
|
INSERT INTO _sft_migtest_table2 (id) VALUES (42);
|
||||||
|
|
||||||
INSERT INTO _sft_db (db_rev, sft_ver, applied_at) VALUES (2, "0.0.1-pre-alpha1", CURRENT_TIMESTAMP);
|
INSERT INTO _sft_db (db_rev, sft_ver, applied_at) VALUES (2, "0.0.1-pre-alpha1", CURRENT_TIMESTAMP);
|
||||||
|
|||||||
@ -4,6 +4,6 @@ CREATE TABLE IF NOT EXISTS _sft_migtest_table3 (
|
|||||||
id integer PRIMARY KEY
|
id integer PRIMARY KEY
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO _sft_migtest_table (id) VALUES (42);
|
INSERT INTO _sft_migtest_table3 (id) VALUES (42);
|
||||||
|
|
||||||
INSERT INTO _sft_db (db_rev, sft_ver, applied_at) VALUES (3, "0.0.1-pre-alpha1", CURRENT_TIMESTAMP);
|
INSERT INTO _sft_db (db_rev, sft_ver, applied_at) VALUES (3, "0.0.1-pre-alpha1", CURRENT_TIMESTAMP);
|
||||||
|
|||||||
@ -2,7 +2,7 @@ CREATE TABLE IF NOT EXISTS _sft_migtest_table4 (
|
|||||||
id integer PRIMARY KEY
|
id integer PRIMARY KEY
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO _sft_migtest_table (id) VALUES (42);
|
INSERT INTO _sft_migtest_table4 (id) VALUES (42);
|
||||||
|
|
||||||
INSERT INTO _sft_db (db_rev, sft_ver, applied_at) VALUES (4, "0.0.1-pre-alpha1", CURRENT_TIMESTAMP);
|
INSERT INTO _sft_db (db_rev, sft_ver, applied_at) VALUES (4, "0.0.1-pre-alpha1", CURRENT_TIMESTAMP);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user