try using schema version better
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
226
models/db.go
226
models/db.go
@@ -229,11 +229,15 @@ func CreateTables() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Remove users RoleId column
|
||||
userRoleIdCheck, _ := CheckColumnExists("users", "RoleId")
|
||||
if userRoleIdCheck {
|
||||
//_, err := db.Exec("ALTER TABLE users DROP COLUMN RoleId;")
|
||||
_, err := db.Exec(`
|
||||
// Check the database schema version
|
||||
version, _ := GetSchemaVersion()
|
||||
if version < 3 {
|
||||
|
||||
// Remove users RoleId column
|
||||
userRoleIdCheck, _ := CheckColumnExists("users", "RoleId")
|
||||
if userRoleIdCheck {
|
||||
//_, err := db.Exec("ALTER TABLE users DROP COLUMN RoleId;")
|
||||
_, err := db.Exec(`
|
||||
PRAGMA foreign_keys=off;
|
||||
BEGIN TRANSACTION;
|
||||
ALTER TABLE users RENAME TO _users_old;
|
||||
@@ -251,49 +255,49 @@ func CreateTables() {
|
||||
PRAGMA foreign_keys=on;
|
||||
DROP TABLE _users_old;
|
||||
`)
|
||||
if err != nil {
|
||||
log.Printf("Error altering users table to drop RoleId column : '%s'\n", err)
|
||||
if err != nil {
|
||||
log.Printf("Error altering users table to drop RoleId column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Set any unassigned secrets to the default safe id
|
||||
if _, err = db.Exec("UPDATE users SET LdapUser = 0 WHERE LdapUser is null;"); err != nil {
|
||||
log.Printf("Error setting LdapUser flag to false for existing users : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Set any unassigned secrets to the default safe id
|
||||
if _, err = db.Exec("UPDATE users SET LdapUser = 0 WHERE LdapUser is null;"); err != nil {
|
||||
log.Printf("Error setting LdapUser flag to false for existing users : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// Remove LdapGroup column from roles table
|
||||
ldapCheck, _ := CheckColumnExists("roles", "LdapGroup")
|
||||
if ldapCheck {
|
||||
_, err := db.Exec("ALTER TABLE roles DROP COLUMN LdapGroup;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering roles table to renmove LdapGroup column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove LdapGroup column from roles table
|
||||
ldapCheck, _ := CheckColumnExists("roles", "LdapGroup")
|
||||
if ldapCheck {
|
||||
_, err := db.Exec("ALTER TABLE roles DROP COLUMN LdapGroup;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering roles table to renmove LdapGroup column : '%s'\n", err)
|
||||
// Add SafeId column to secrets table
|
||||
safeIdCheck, _ := CheckColumnExists("secrets", "SafeId")
|
||||
if !safeIdCheck {
|
||||
// Add the column for LdapGroup in the roles table
|
||||
_, err := db.Exec("ALTER TABLE secrets ADD COLUMN SafeId INTEGER REFERENCES safes(SafeId);")
|
||||
if err != nil {
|
||||
log.Printf("Error altering secrets table to add SafeId column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Set any unassigned secrets to the default safe id
|
||||
if _, err = db.Exec("UPDATE secrets SET SafeId = 1 WHERE SafeId is null;"); err != nil {
|
||||
log.Printf("Error setting safe ID of existing secrets : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Add SafeId column to secrets table
|
||||
safeIdCheck, _ := CheckColumnExists("secrets", "SafeId")
|
||||
if !safeIdCheck {
|
||||
// Add the column for LdapGroup in the roles table
|
||||
_, err := db.Exec("ALTER TABLE secrets ADD COLUMN SafeId INTEGER REFERENCES safes(SafeId);")
|
||||
if err != nil {
|
||||
log.Printf("Error altering secrets table to add SafeId column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Set any unassigned secrets to the default safe id
|
||||
if _, err = db.Exec("UPDATE secrets SET SafeId = 1 WHERE SafeId is null;"); err != nil {
|
||||
log.Printf("Error setting safe ID of existing secrets : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Remove RoleId column from secrets table
|
||||
secretsRoleIdCheck, _ := CheckColumnExists("secrets", "RoleId")
|
||||
if secretsRoleIdCheck {
|
||||
_, err := db.Exec(`
|
||||
// Remove RoleId column from secrets table
|
||||
secretsRoleIdCheck, _ := CheckColumnExists("secrets", "RoleId")
|
||||
if secretsRoleIdCheck {
|
||||
_, err := db.Exec(`
|
||||
PRAGMA foreign_keys=off;
|
||||
BEGIN TRANSACTION;
|
||||
ALTER TABLE secrets RENAME TO _secrets_old;
|
||||
@@ -316,26 +320,26 @@ func CreateTables() {
|
||||
PRAGMA foreign_keys=on;
|
||||
DROP TABLE _secrets_old;
|
||||
`)
|
||||
if err != nil {
|
||||
log.Printf("Error altering secrets table to remove RoleId column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
if err != nil {
|
||||
log.Printf("Error altering secrets table to remove RoleId column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the Admin column from roles table
|
||||
rolesAdminCheck, _ := CheckColumnExists("roles", "Admin")
|
||||
if rolesAdminCheck {
|
||||
_, err := db.Exec("ALTER TABLE roles DROP COLUMN Admin;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering roles table to remove Admin column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
// Remove the Admin column from roles table
|
||||
rolesAdminCheck, _ := CheckColumnExists("roles", "Admin")
|
||||
if rolesAdminCheck {
|
||||
_, err := db.Exec("ALTER TABLE roles DROP COLUMN Admin;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering roles table to remove Admin column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the RoleId from permissiosn table
|
||||
permissionsRoleIdCheck, _ := CheckColumnExists("permissions", "RoleId")
|
||||
if permissionsRoleIdCheck {
|
||||
_, err := db.Exec(`
|
||||
// Remove the RoleId from permissiosn table
|
||||
permissionsRoleIdCheck, _ := CheckColumnExists("permissions", "RoleId")
|
||||
if permissionsRoleIdCheck {
|
||||
_, err := db.Exec(`
|
||||
PRAGMA foreign_keys=off;
|
||||
BEGIN TRANSACTION;
|
||||
ALTER TABLE permissions RENAME TO _permissions_old;
|
||||
@@ -358,56 +362,63 @@ func CreateTables() {
|
||||
PRAGMA foreign_keys=on;
|
||||
DROP TABLE _permissions_old;
|
||||
`)
|
||||
if err != nil {
|
||||
log.Printf("Error altering permissions table to remove RoleId column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
secretsLastUpdatedCheck, _ := CheckColumnExists("secrets", "LastUpdated")
|
||||
if !secretsLastUpdatedCheck {
|
||||
// Add the column for LastUpdated in the secrets table
|
||||
_, err := db.Exec("ALTER TABLE secrets ADD COLUMN LastUpdated datetime;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering secrets table to add LastUpdated column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
if err != nil {
|
||||
log.Printf("Error altering permissions table to remove RoleId column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Set the default value
|
||||
if _, err = db.Exec("UPDATE secrets SET LastUpdated = (datetime('1970-01-01 00:00:00')) WHERE LastUpdated is null;"); err != nil {
|
||||
log.Printf("Error setting LastUpdated of existing secrets : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
secretsLastUpdatedCheck, _ := CheckColumnExists("secrets", "LastUpdated")
|
||||
if !secretsLastUpdatedCheck {
|
||||
// Add the column for LastUpdated in the secrets table
|
||||
_, err := db.Exec("ALTER TABLE secrets ADD COLUMN LastUpdated datetime;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering secrets table to add LastUpdated column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
lastLoginCheck, _ := CheckColumnExists("users", "LastLogin")
|
||||
if !lastLoginCheck {
|
||||
// Add the column for LastUpdated in the secrets table
|
||||
_, err := db.Exec("ALTER TABLE users ADD COLUMN LastLogin datetime;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering users table to add LastLogin column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
// Set the default value
|
||||
if _, err = db.Exec("UPDATE secrets SET LastUpdated = (datetime('1970-01-01 00:00:00')) WHERE LastUpdated is null;"); err != nil {
|
||||
log.Printf("Error setting LastUpdated of existing secrets : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Set the default value
|
||||
if _, err = db.Exec("UPDATE users SET LastLogin = (datetime('1970-01-01 00:00:00')) WHERE LastLogin is null;"); err != nil {
|
||||
log.Printf("Error setting LastLogin of existing users : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
lastLoginCheck, _ := CheckColumnExists("users", "LastLogin")
|
||||
if !lastLoginCheck {
|
||||
// Add the column for LastUpdated in the secrets table
|
||||
_, err := db.Exec("ALTER TABLE users ADD COLUMN LastLogin datetime;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering users table to add LastLogin column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Add IpAddress column to audit table
|
||||
auditIPCheck, _ := CheckColumnExists("audit", "IpAddress")
|
||||
if !auditIPCheck {
|
||||
// Add the column for LdapGroup in the roles table
|
||||
_, err := db.Exec("ALTER TABLE audit ADD COLUMN IpAddress VARCHAR;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering audit table to add IpAddress column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
// Set the default value
|
||||
if _, err = db.Exec("UPDATE users SET LastLogin = (datetime('1970-01-01 00:00:00')) WHERE LastLogin is null;"); err != nil {
|
||||
log.Printf("Error setting LastLogin of existing users : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = db.Exec("UPDATE audit SET IpAddress = '' WHERE IpAddress is null;"); err != nil {
|
||||
log.Printf("Error setting IpAddress of existing audit records : '%s'", err)
|
||||
// Add IpAddress column to audit table
|
||||
auditIPCheck, _ := CheckColumnExists("audit", "IpAddress")
|
||||
if !auditIPCheck {
|
||||
// Add the column for LdapGroup in the roles table
|
||||
_, err := db.Exec("ALTER TABLE audit ADD COLUMN IpAddress VARCHAR;")
|
||||
if err != nil {
|
||||
log.Printf("Error altering audit table to add IpAddress column : '%s'\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if _, err = db.Exec("UPDATE audit SET IpAddress = '' WHERE IpAddress is null;"); err != nil {
|
||||
log.Printf("Error setting IpAddress of existing audit records : '%s'", err)
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
Reference in New Issue
Block a user