main: move db code to func
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
f3215035bd
commit
e3f12f9c57
@ -34,9 +34,6 @@ func main() {
|
|||||||
|
|
||||||
rootLog.Info("init sft server")
|
rootLog.Info("init sft server")
|
||||||
|
|
||||||
|
|
||||||
// TODO: Init DB
|
|
||||||
|
|
||||||
// This creates the db file if it does not exist
|
// This creates the db file if it does not exist
|
||||||
db, err := sql.Open(DB_DRIVER, DB_PATH)
|
db, err := sql.Open(DB_DRIVER, DB_PATH)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -46,17 +43,25 @@ func main() {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
db.SetMaxOpenConns(0)
|
db.SetMaxOpenConns(0)
|
||||||
|
|
||||||
|
err = initDb(db, *rootLog.With("sub", "initDb"))
|
||||||
|
if err != nil {
|
||||||
|
rootLog.Error("error in initDb", "err", err.Error())
|
||||||
|
os.Exit(EXIT_FAIL)
|
||||||
|
}
|
||||||
|
|
||||||
// sqlStmt := `
|
|
||||||
// create table if not exists _sft_db (db_rev integer not null, sft_ver text);
|
|
||||||
// delete from _sft_db;
|
// TODO: Create local admin users
|
||||||
// `
|
// TODO: File transfer, quarintine, scan with local accounts
|
||||||
//
|
|
||||||
// _, err = db.Exec(sqlStmt)
|
|
||||||
// if err != nil {
|
|
||||||
// rootLog.Error("error in init db", "err", err.Error())
|
}
|
||||||
// os.Exit(EXIT_FAIL)
|
|
||||||
// }
|
/*
|
||||||
|
initilize database with sft parameters and table from starting point
|
||||||
|
*/
|
||||||
|
func initDb(db *sql.DB, logger slog.Logger) error {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
- Case 1: Database file does not exist. Created with sql.Open()
|
- Case 1: Database file does not exist. Created with sql.Open()
|
||||||
@ -68,41 +73,40 @@ func main() {
|
|||||||
It is critical to run in order. In this case we don't need the next column
|
It is critical to run in order. In this case we don't need the next column
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// TODO: apply rev 0
|
|
||||||
f, _ := migrations.DbMigrations.Open("0_init.sql")
|
f, _ := migrations.DbMigrations.Open("0_init.sql")
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
initSql, err := io.ReadAll(f)
|
initSql, err := io.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error("can not read embedFS db rev0", "err", err.Error())
|
logger.Error("can not read embedFS db rev0", "err", err.Error())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
rootLog.Debug("migrations read", "bytes", len(initSql))
|
logger.Debug("migrations read rev0", "bytes", len(initSql))
|
||||||
|
|
||||||
_, err = db.Exec(string(initSql))
|
_, err = db.Exec(string(initSql))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
|
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
|
||||||
rootLog.Info("db is already initilized to rev0")
|
logger.Info("db is already initilized to rev0")
|
||||||
} else {
|
} else {
|
||||||
rootLog.Error("can not exec on db", "err", err.Error())
|
logger.Error("can not exec on db", "err", err.Error())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rootLog.Info("db initilized")
|
logger.Info("db initilized")
|
||||||
|
|
||||||
|
|
||||||
rootLog.Debug("read on _sft_db")
|
logger.Debug("read on _sft_db")
|
||||||
|
|
||||||
rows, err := db.Query("select db_rev, sft_ver from _sft_db;")
|
rows, err := db.Query("select db_rev, sft_ver from _sft_db;")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error("error in init db", "err", err.Error())
|
logger.Error("error in init db", "err", err.Error())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !rows.Next() {
|
if !rows.Next() {
|
||||||
rootLog.Warn("db not migrated to 1")
|
logger.Warn("db not migrated to 1")
|
||||||
}
|
}
|
||||||
rows.Close()
|
rows.Close()
|
||||||
|
|
||||||
@ -111,7 +115,7 @@ func main() {
|
|||||||
|
|
||||||
rows, err = db.Query("SELECT db_rev FROM _sft_db ORDER BY db_rev;")
|
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())
|
logger.Error("error", "err", err.Error())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,11 +125,11 @@ func main() {
|
|||||||
var val int
|
var val int
|
||||||
|
|
||||||
if err := rows.Scan(&val); err != nil {
|
if err := rows.Scan(&val); err != nil {
|
||||||
rootLog.Error("revision stored in db is not an integer")
|
logger.Error("revision stored in db is not an integer")
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
rootLog.Debug("found applied database revision", "rev", val)
|
logger.Debug("found applied database revision", "rev", val)
|
||||||
|
|
||||||
appliedMigs = append(appliedMigs, val)
|
appliedMigs = append(appliedMigs, val)
|
||||||
}
|
}
|
||||||
@ -134,7 +138,7 @@ func main() {
|
|||||||
|
|
||||||
migrationFilesDir, err := migrations.DbMigrations.ReadDir(".")
|
migrationFilesDir, err := migrations.DbMigrations.ReadDir(".")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error("could not list files in migrations embedFS", "err", err.Error())
|
logger.Error("could not list files in migrations embedFS", "err", err.Error())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,19 +152,19 @@ func main() {
|
|||||||
|
|
||||||
for i, file := range migrationFilesDir {
|
for i, file := range migrationFilesDir {
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
rootLog.Debug("migration dir. skipping", "name", file.Name())
|
logger.Debug("migration dir. skipping", "name", file.Name())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rootLog.Debug("migration file", "name", file.Name())
|
logger.Debug("migration file", "name", file.Name())
|
||||||
migStmts, err := migrations.DbMigrations.ReadFile(file.Name())
|
migStmts, err := migrations.DbMigrations.ReadFile(file.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error("could not read migration file", "err", err.Error(), "name", file.Name())
|
logger.Error("could not read migration file", "err", err.Error(), "name", file.Name())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
mRev, err := strconv.Atoi(strings.Split(file.Name(), "_")[0])
|
mRev, err := strconv.Atoi(strings.Split(file.Name(), "_")[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error(
|
logger.Error(
|
||||||
"unexpected db revision. invalid file naming in migrations",
|
"unexpected db revision. invalid file naming in migrations",
|
||||||
"err", err.Error(),
|
"err", err.Error(),
|
||||||
"name", file.Name())
|
"name", file.Name())
|
||||||
@ -168,7 +172,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if mRev != i {
|
if mRev != i {
|
||||||
rootLog.Error("unexpected migration sequence", "found", mRev, "expected", i)
|
logger.Error("unexpected migration sequence", "found", mRev, "expected", i)
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +182,7 @@ func main() {
|
|||||||
Rev: mRev,
|
Rev: mRev,
|
||||||
}
|
}
|
||||||
migs = append(migs, rev)
|
migs = append(migs, rev)
|
||||||
rootLog.Debug("loaded db migration", "name", rev.Filename)
|
logger.Debug("loaded db migration", "name", rev.Filename)
|
||||||
|
|
||||||
// NOTE: Expect migs to be ordered 0..1..2...
|
// NOTE: Expect migs to be ordered 0..1..2...
|
||||||
|
|
||||||
@ -187,11 +191,11 @@ func main() {
|
|||||||
// We have migs from migrations dir and appliedMigs from the db
|
// We have migs from migrations dir and appliedMigs from the db
|
||||||
|
|
||||||
if len(appliedMigs) > len(migs) {
|
if len(appliedMigs) > len(migs) {
|
||||||
rootLog.Error("wtf", "migs", len(migs), "appliedmigs", len(appliedMigs))
|
logger.Error("wtf", "migs", len(migs), "appliedmigs", len(appliedMigs))
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
rootLog.Info("database currently",
|
logger.Info("database currently",
|
||||||
"applied_count", appliedMigs[len(appliedMigs)-1] + 1,
|
"applied_count", appliedMigs[len(appliedMigs)-1] + 1,
|
||||||
"available_count", migs[len(migs)-1].Rev + 1)
|
"available_count", migs[len(migs)-1].Rev + 1)
|
||||||
|
|
||||||
@ -204,44 +208,31 @@ func main() {
|
|||||||
for {
|
for {
|
||||||
if !needMigration { break }
|
if !needMigration { break }
|
||||||
rev := migs[pRev]
|
rev := migs[pRev]
|
||||||
rlog := rootLog.With("rev", pRev, "name", rev.Filename)
|
rlog := logger.With("rev", pRev, "name", rev.Filename)
|
||||||
rlog.Info("applying database migration")
|
rlog.Info("applying database migration")
|
||||||
rlog.Debug("apply migrations", "sql", string(rev.Data))
|
rlog.Debug("apply migrations", "sql", string(rev.Data))
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error("error: create db transaction", "err", err.Error())
|
logger.Error("error: create db transaction", "err", err.Error())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
_, err = tx.Exec(string(rev.Data))
|
_, err = tx.Exec(string(rev.Data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error("error: tx exec migration", "err", err.Error())
|
logger.Error("error: tx exec migration", "err", err.Error())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tx.Commit()
|
err = tx.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rootLog.Error("error: commit migration tx", "rev", rev.Rev, "err", err.Error())
|
logger.Error("error: commit migration tx", "rev", rev.Rev, "err", err.Error())
|
||||||
os.Exit(EXIT_FAIL)
|
os.Exit(EXIT_FAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
pRev++
|
pRev++
|
||||||
if pRev > migs[len(migs)-1].Rev { break }
|
if pRev > migs[len(migs)-1].Rev { break }
|
||||||
}
|
}
|
||||||
rootLog.Info("database up-to-date")
|
logger.Info("database up-to-date")
|
||||||
|
|
||||||
|
|
||||||
// TODO: Create local admin users
|
|
||||||
// TODO: File transfer, quarintine, scan with local accounts
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
initilize database with sft parameters and table from starting point
|
|
||||||
*/
|
|
||||||
func initDb(db sql.DB) {
|
|
||||||
|
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user