basic code setup
This commit is contained in:
137
models/setup.go
Normal file
137
models/setup.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"ccsecrets/utils"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
var db *sqlx.DB
|
||||
|
||||
const (
|
||||
sqlFile = "ccsecrets.db"
|
||||
)
|
||||
|
||||
const createRoles string = `
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
RoleId INTEGER PRIMARY KEY ASC,
|
||||
RoleName VARCHAR,
|
||||
ReadOnly BOOLEAN,
|
||||
Admin BOOLEAN
|
||||
);
|
||||
`
|
||||
|
||||
const createUsers string = `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
UserId INTEGER PRIMARY KEY ASC,
|
||||
RoleId INTEGER,
|
||||
UserName VARCHAR,
|
||||
UserPass VARCHAR,
|
||||
AccessToken varchar,
|
||||
FOREIGN KEY (RoleId) REFERENCES roles(RoleId)
|
||||
);
|
||||
`
|
||||
|
||||
const createSecrets string = `
|
||||
CREATE TABLE IF NOT EXISTS secrets (
|
||||
SecretId INTEGER PRIMARY KEY ASC,
|
||||
RoleId INTEGER,
|
||||
DeviceName VARCHAR,
|
||||
UserName VARCHAR,
|
||||
Secret VARCHAR,
|
||||
FOREIGN KEY (RoleId) REFERENCES roles(RoleId)
|
||||
);
|
||||
`
|
||||
|
||||
const createSchema string = `
|
||||
CREATE TABLE IF NOT EXISTS schema (
|
||||
Version INTEGER
|
||||
);
|
||||
`
|
||||
|
||||
// Establish connection to sqlite database
|
||||
func ConnectDatabase() {
|
||||
var err error
|
||||
|
||||
// Try using sqlite as our database
|
||||
sqlPath := utils.GetFilePath(sqlFile)
|
||||
db, err = sqlx.Open("sqlite", sqlPath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error opening sqlite database connection to file '%s' : '%s'\n", sqlPath, err)
|
||||
os.Exit(1)
|
||||
} else {
|
||||
fmt.Printf("Connected to sqlite database file '%s'\n", sqlPath)
|
||||
}
|
||||
|
||||
// Make sure our tables exist
|
||||
CreateTables()
|
||||
|
||||
defer db.Close()
|
||||
}
|
||||
|
||||
func CreateTables() {
|
||||
var err error
|
||||
// Create database tables if it doesn't exist
|
||||
// Roles table should go first since other tables refer to it
|
||||
if _, err = db.Exec(createRoles); err != nil {
|
||||
fmt.Printf("Error checking roles table : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// Users table
|
||||
if _, err = db.Exec(createUsers); err != nil {
|
||||
fmt.Printf("Error checking users table : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// Secrets table
|
||||
if _, err = db.Exec(createSecrets); err != nil {
|
||||
fmt.Printf("Error checking secrets table : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// Schema table should go last so we know if the database has a value in the schema table then everything was created properly
|
||||
if _, err = db.Exec(createSchema); err != nil {
|
||||
fmt.Printf("Error checking schema table : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
schemaCheck, _ := CheckColumnExists("schema", "version")
|
||||
if !schemaCheck {
|
||||
if _, err = db.Exec("INSERT INTO schema VALUES(1);"); err != nil {
|
||||
fmt.Printf("Error adding initial scehama version : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CheckColumnExists(table string, column string) (bool, error) {
|
||||
var count int64
|
||||
rows, err := db.Queryx("SELECT COUNT(*) AS CNTREC FROM pragma_table_info('" + table + "') WHERE name='" + column + "';")
|
||||
if err != nil {
|
||||
fmt.Printf("CheckColumnExists error querying database for existence of column '%s' : '%s'\n", column, err)
|
||||
return false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
// cols is an []interface{} of all of the column results
|
||||
cols, _ := rows.SliceScan()
|
||||
fmt.Printf("CheckColumnExists Value is '%v'\n", cols[0].(int64))
|
||||
count = cols[0].(int64)
|
||||
|
||||
if count == 1 {
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
fmt.Printf("CheckColumnExists error getting results : '%s'\n", err)
|
||||
return false, err
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
Reference in New Issue
Block a user