Capabilities handling, database.
This commit is contained in:
44
database/migrations/1_create_users_table.go
Normal file
44
database/migrations/1_create_users_table.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// CreateUsersTableUp creates local users table used for authentication.
|
||||
func CreateUsersTableUp(tx *sql.Tx) error {
|
||||
if _, err := tx.Exec(`
|
||||
CREATE TABLE users (
|
||||
uuid UUID NOT NULL,
|
||||
login TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
password_salt TEXT NOT NULL,
|
||||
active BOOLEAN NOT NULL DEFAULT false,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN users.uuid IS 'User UUID';
|
||||
COMMENT ON COLUMN users.login IS 'User login';
|
||||
COMMENT ON COLUMN users.password_hash IS 'Hashed user password';
|
||||
COMMENT ON COLUMN users.password_salt IS 'Salt for user password';
|
||||
COMMENT ON COLUMN users.active IS 'Active user flag. 0 - banned';
|
||||
COMMENT ON COLUMN users.created_at IS 'User registration timestamp';
|
||||
|
||||
CREATE INDEX users_uuid_idx ON users(uuid);
|
||||
CREATE INDEX users_login_idx ON users(login);
|
||||
CREATE INDEX users_created_at_idx ON users(created_at);
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateUsersTableDown deletes local users table used for authentication.
|
||||
func CreateUsersTableDown(tx *sql.Tx) error {
|
||||
if _, err := tx.Exec(`DROP TABLE users;`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
81
database/migrations/exported.go
Normal file
81
database/migrations/exported.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"database/sql"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
// other
|
||||
"github.com/pressly/goose"
|
||||
)
|
||||
|
||||
func Initialize() {
|
||||
log.Println("Initializing database migrations...")
|
||||
|
||||
_ = goose.SetDialect("postgres")
|
||||
|
||||
goose.AddNamedMigration("1_create_users_table.go", CreateUsersTableUp, CreateUsersTableDown)
|
||||
|
||||
// Migrations should be registered here.
|
||||
}
|
||||
|
||||
func Migrate(db *sql.DB) {
|
||||
log.Println("Starting database migration procedure...")
|
||||
|
||||
// Prepare migrations configuration.
|
||||
var action = "UP"
|
||||
actionFromEnv, actionFound := os.LookupEnv("DATABASE_ACTION")
|
||||
if actionFound {
|
||||
log.Println("Migration action override: " + actionFromEnv)
|
||||
action = actionFromEnv
|
||||
} else {
|
||||
log.Println("Executing default migration action (UP)")
|
||||
}
|
||||
|
||||
var count int64
|
||||
countFromEnv, countFound := os.LookupEnv("DATABASE_COUNT")
|
||||
if countFound {
|
||||
log.Println("Migration count override: " + countFromEnv)
|
||||
countAsInt, err := strconv.ParseInt(countFromEnv, 10, 64)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to convert count gathered from DATABASE_COUNT to integer")
|
||||
}
|
||||
count = countAsInt
|
||||
} else {
|
||||
log.Println("Applying or rollback this count of migrations: " + countFromEnv + ". 0 - all.")
|
||||
}
|
||||
|
||||
// Execute migrations.
|
||||
var err error
|
||||
currentDBVersion, gooseerr := goose.GetDBVersion(db)
|
||||
if gooseerr != nil {
|
||||
log.Fatalln("Failed to get database version: " + gooseerr.Error())
|
||||
}
|
||||
log.Println("Current database version obtained: " + strconv.Itoa(int(currentDBVersion)))
|
||||
if action == "UP" && count == 0 {
|
||||
log.Println("Applying all unapplied migrations...")
|
||||
err = goose.Up(db, ".")
|
||||
} else if action == "UP" && count != 0 {
|
||||
newVersion := currentDBVersion + count
|
||||
log.Println("Migrating database to specific version: " + strconv.Itoa(int(newVersion)))
|
||||
err = goose.UpTo(db, ".", newVersion)
|
||||
} else if action == "DOWN" && count == 0 {
|
||||
log.Println("Downgrading database to zero state, you'll need to re-apply migrations!")
|
||||
err = goose.Down(db, ".")
|
||||
log.Fatalln("Database downgraded to zero state. You have to re-apply migrations")
|
||||
} else if action == "DOWN" && count != 0 {
|
||||
newVersion := currentDBVersion - count
|
||||
log.Println("Downgrading database to specific version: " + strconv.Itoa(int(newVersion)))
|
||||
err = goose.DownTo(db, ".", newVersion)
|
||||
} else {
|
||||
log.Fatalln("Unsupported set of migration parameters, cannot continue: " + action + "/" + countFromEnv)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to execute migration sequence: " + err.Error())
|
||||
}
|
||||
|
||||
log.Println("Database migrated successfully")
|
||||
}
|
Reference in New Issue
Block a user