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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove users RoleId column
|
// Check the database schema version
|
||||||
userRoleIdCheck, _ := CheckColumnExists("users", "RoleId")
|
version, _ := GetSchemaVersion()
|
||||||
if userRoleIdCheck {
|
if version < 3 {
|
||||||
//_, err := db.Exec("ALTER TABLE users DROP COLUMN RoleId;")
|
|
||||||
_, err := db.Exec(`
|
// 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;
|
PRAGMA foreign_keys=off;
|
||||||
BEGIN TRANSACTION;
|
BEGIN TRANSACTION;
|
||||||
ALTER TABLE users RENAME TO _users_old;
|
ALTER TABLE users RENAME TO _users_old;
|
||||||
@@ -251,49 +255,49 @@ func CreateTables() {
|
|||||||
PRAGMA foreign_keys=on;
|
PRAGMA foreign_keys=on;
|
||||||
DROP TABLE _users_old;
|
DROP TABLE _users_old;
|
||||||
`)
|
`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error altering users table to drop RoleId column : '%s'\n", err)
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Set any unassigned secrets to the default safe id
|
// Remove LdapGroup column from roles table
|
||||||
if _, err = db.Exec("UPDATE users SET LdapUser = 0 WHERE LdapUser is null;"); err != nil {
|
ldapCheck, _ := CheckColumnExists("roles", "LdapGroup")
|
||||||
log.Printf("Error setting LdapUser flag to false for existing users : '%s'", err)
|
if ldapCheck {
|
||||||
os.Exit(1)
|
_, 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
|
// Add SafeId column to secrets table
|
||||||
ldapCheck, _ := CheckColumnExists("roles", "LdapGroup")
|
safeIdCheck, _ := CheckColumnExists("secrets", "SafeId")
|
||||||
if ldapCheck {
|
if !safeIdCheck {
|
||||||
_, err := db.Exec("ALTER TABLE roles DROP COLUMN LdapGroup;")
|
// Add the column for LdapGroup in the roles table
|
||||||
if err != nil {
|
_, err := db.Exec("ALTER TABLE secrets ADD COLUMN SafeId INTEGER REFERENCES safes(SafeId);")
|
||||||
log.Printf("Error altering roles table to renmove LdapGroup column : '%s'\n", err)
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Add SafeId column to secrets table
|
// Remove RoleId column from secrets table
|
||||||
safeIdCheck, _ := CheckColumnExists("secrets", "SafeId")
|
secretsRoleIdCheck, _ := CheckColumnExists("secrets", "RoleId")
|
||||||
if !safeIdCheck {
|
if secretsRoleIdCheck {
|
||||||
// Add the column for LdapGroup in the roles table
|
_, err := db.Exec(`
|
||||||
_, 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(`
|
|
||||||
PRAGMA foreign_keys=off;
|
PRAGMA foreign_keys=off;
|
||||||
BEGIN TRANSACTION;
|
BEGIN TRANSACTION;
|
||||||
ALTER TABLE secrets RENAME TO _secrets_old;
|
ALTER TABLE secrets RENAME TO _secrets_old;
|
||||||
@@ -316,26 +320,26 @@ func CreateTables() {
|
|||||||
PRAGMA foreign_keys=on;
|
PRAGMA foreign_keys=on;
|
||||||
DROP TABLE _secrets_old;
|
DROP TABLE _secrets_old;
|
||||||
`)
|
`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error altering secrets table to remove RoleId column : '%s'\n", err)
|
log.Printf("Error altering secrets table to remove RoleId column : '%s'\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the Admin column from roles table
|
// Remove the Admin column from roles table
|
||||||
rolesAdminCheck, _ := CheckColumnExists("roles", "Admin")
|
rolesAdminCheck, _ := CheckColumnExists("roles", "Admin")
|
||||||
if rolesAdminCheck {
|
if rolesAdminCheck {
|
||||||
_, err := db.Exec("ALTER TABLE roles DROP COLUMN Admin;")
|
_, err := db.Exec("ALTER TABLE roles DROP COLUMN Admin;")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error altering roles table to remove Admin column : '%s'\n", err)
|
log.Printf("Error altering roles table to remove Admin column : '%s'\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the RoleId from permissiosn table
|
// Remove the RoleId from permissiosn table
|
||||||
permissionsRoleIdCheck, _ := CheckColumnExists("permissions", "RoleId")
|
permissionsRoleIdCheck, _ := CheckColumnExists("permissions", "RoleId")
|
||||||
if permissionsRoleIdCheck {
|
if permissionsRoleIdCheck {
|
||||||
_, err := db.Exec(`
|
_, err := db.Exec(`
|
||||||
PRAGMA foreign_keys=off;
|
PRAGMA foreign_keys=off;
|
||||||
BEGIN TRANSACTION;
|
BEGIN TRANSACTION;
|
||||||
ALTER TABLE permissions RENAME TO _permissions_old;
|
ALTER TABLE permissions RENAME TO _permissions_old;
|
||||||
@@ -358,56 +362,63 @@ func CreateTables() {
|
|||||||
PRAGMA foreign_keys=on;
|
PRAGMA foreign_keys=on;
|
||||||
DROP TABLE _permissions_old;
|
DROP TABLE _permissions_old;
|
||||||
`)
|
`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error altering permissions table to remove RoleId column : '%s'\n", err)
|
log.Printf("Error altering permissions table to remove RoleId column : '%s'\n", err)
|
||||||
os.Exit(1)
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the default value
|
secretsLastUpdatedCheck, _ := CheckColumnExists("secrets", "LastUpdated")
|
||||||
if _, err = db.Exec("UPDATE secrets SET LastUpdated = (datetime('1970-01-01 00:00:00')) WHERE LastUpdated is null;"); err != nil {
|
if !secretsLastUpdatedCheck {
|
||||||
log.Printf("Error setting LastUpdated of existing secrets : '%s'", err)
|
// Add the column for LastUpdated in the secrets table
|
||||||
os.Exit(1)
|
_, 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")
|
// Set the default value
|
||||||
if !lastLoginCheck {
|
if _, err = db.Exec("UPDATE secrets SET LastUpdated = (datetime('1970-01-01 00:00:00')) WHERE LastUpdated is null;"); err != nil {
|
||||||
// Add the column for LastUpdated in the secrets table
|
log.Printf("Error setting LastUpdated of existing secrets : '%s'", err)
|
||||||
_, err := db.Exec("ALTER TABLE users ADD COLUMN LastLogin datetime;")
|
os.Exit(1)
|
||||||
if err != nil {
|
}
|
||||||
log.Printf("Error altering users table to add LastLogin column : '%s'\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the default value
|
lastLoginCheck, _ := CheckColumnExists("users", "LastLogin")
|
||||||
if _, err = db.Exec("UPDATE users SET LastLogin = (datetime('1970-01-01 00:00:00')) WHERE LastLogin is null;"); err != nil {
|
if !lastLoginCheck {
|
||||||
log.Printf("Error setting LastLogin of existing users : '%s'", err)
|
// Add the column for LastUpdated in the secrets table
|
||||||
os.Exit(1)
|
_, 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
|
// Set the default value
|
||||||
auditIPCheck, _ := CheckColumnExists("audit", "IpAddress")
|
if _, err = db.Exec("UPDATE users SET LastLogin = (datetime('1970-01-01 00:00:00')) WHERE LastLogin is null;"); err != nil {
|
||||||
if !auditIPCheck {
|
log.Printf("Error setting LastLogin of existing users : '%s'", err)
|
||||||
// Add the column for LdapGroup in the roles table
|
os.Exit(1)
|
||||||
_, 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 {
|
// Add IpAddress column to audit table
|
||||||
log.Printf("Error setting IpAddress of existing audit records : '%s'", err)
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -431,6 +442,23 @@ func CheckCount(tablename string) (int, error) {
|
|||||||
return count, nil
|
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
|
// From https://stackoverflow.com/a/60100045
|
||||||
func GenerateInsertMethod(q interface{}) (string, error) {
|
func GenerateInsertMethod(q interface{}) (string, error) {
|
||||||
if reflect.ValueOf(q).Kind() == reflect.Struct {
|
if reflect.ValueOf(q).Kind() == reflect.Struct {
|
||||||
|
Reference in New Issue
Block a user