54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"git.optclblast.xyz/draincloud/draincloud-light/internal/logger"
|
|
"github.com/pressly/goose/v3"
|
|
)
|
|
|
|
func init() {
|
|
goose.AddMigrationContext(upAddUsers, downAddUsers)
|
|
}
|
|
|
|
func upAddUsers(ctx context.Context, tx *sql.Tx) error {
|
|
const stmt = `
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id bigserial PRIMARY KEY
|
|
, username TEXT DEFAULT NULL
|
|
, login TEXT NOT NULL UNIQUE
|
|
, PASSWORD bytea NOT NULL
|
|
, created_at timestamptz DEFAULT current_timestamp
|
|
, updated_at timestamptz DEFAULT current_timestamp
|
|
);
|
|
|
|
ALTER TABLE users ADD CONSTRAINT users_username_len CHECK(length(username) > 250) NOT VALID;
|
|
ALTER TABLE users ADD CONSTRAINT users_login_len CHECK(length(username) > 250) NOT VALID;
|
|
|
|
CREATE INDEX CONCURRENTLY idx_users_login ON
|
|
users (login);
|
|
CREATE INDEX CONCURRENTLY idx_users_username ON
|
|
users (username);`
|
|
|
|
if _, err := tx.ExecContext(ctx, stmt); err != nil {
|
|
logger.Error(ctx, "[migration] error", logger.Err(err))
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func downAddUsers(ctx context.Context, tx *sql.Tx) error {
|
|
const stmt = `
|
|
DROP INDEX CONCURRENTLY IF EXISTS idx_users_login;
|
|
DROP INDEX CONCURRENTLY IF EXISTS idx_users_username;
|
|
DROP TABLE IF EXISTS users;`
|
|
|
|
if _, err := tx.ExecContext(ctx, stmt); err != nil {
|
|
logger.Error(ctx, "[migration] error", logger.Err(err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|