try using schema version better
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-01-23 12:17:38 +11:00
parent b801563074
commit c82bffe421

View File

@@ -229,6 +229,10 @@ func CreateTables() {
os.Exit(1)
}
// Check the database schema version
version, _ := GetSchemaVersion()
if version < 3 {
// Remove users RoleId column
userRoleIdCheck, _ := CheckColumnExists("users", "RoleId")
if userRoleIdCheck {
@@ -411,6 +415,13 @@ func CreateTables() {
os.Exit(1)
}
}
// Set the schema version
if _, err = db.Exec("UPDATE schema SET Version = 3"); err != nil {
log.Printf("Error setting schema to version 3 : '%s'", err)
os.Exit(1)
}
}
}
// Count the number of records in the sqlite database
@@ -431,6 +442,23 @@ func CheckCount(tablename string) (int, error) {
return count, nil
}
func GetSchemaVersion() (int, error) {
var version int
stmt, err := db.Prepare("SELECT Version FROM schema")
if err != nil {
log.Printf("GetSchemaVersion error preparing sqlite statement : '%s'\n", err)
return 0, err
}
err = stmt.QueryRow().Scan(&version)
if err != nil {
log.Printf("GetSchemaVersion error querying database record count : '%s'\n", err)
return 0, err
}
stmt.Close() // or use defer rows.Close(), idc
return version, nil
}
// From https://stackoverflow.com/a/60100045
func GenerateInsertMethod(q interface{}) (string, error) {
if reflect.ValueOf(q).Kind() == reflect.Struct {