db: make migrations a struct instead of a plain string

This commit is contained in:
wispem-wantex
2026-09-19 21:35:27 -07:00
committed by ~wispem-wantex
parent d2b3a8173f
commit ff7f0a57c8
5 changed files with 42 additions and 26 deletions

View File

@@ -20,11 +20,11 @@ type DBConfig struct {
sql_schema *string
// Database starts at version 0. First migration brings us to version 1
migrations *[]string
migrations *[]Migration
version_number uint
}
func Init(schema *string, migrationsList *[]string) DBConfig {
func Init(schema *string, migrationsList *[]Migration) DBConfig {
return DBConfig{
sql_schema: schema,
migrations: migrationsList,
@@ -99,7 +99,7 @@ func (c DBConfig) CheckAndUpdateVersion(db *sqlx.DB) error {
func (c DBConfig) UpgradeFromXToY(db *sqlx.DB, x uint, y uint) {
for i := x; i < y; i++ {
fmt.Print(textutils.ColorCyan)
fmt.Println((*c.migrations)[i])
fmt.Println((*c.migrations)[i].SQL)
fmt.Print(textutils.ColorReset)
// Execute the migration in a transaction
@@ -107,7 +107,7 @@ func (c DBConfig) UpgradeFromXToY(db *sqlx.DB, x uint, y uint) {
if err != nil {
panic(err)
}
tx.MustExec((*c.migrations)[i])
tx.MustExec((*c.migrations)[i].SQL)
tx.MustExec("update db_version set version = ?", i+1)
if err := tx.Commit(); err != nil {
panic(err)