listmonk/internal/migrations/v3.1.0.go

54 lines
1.5 KiB
Go
Raw Normal View History

2024-04-02 17:01:19 +08:00
package migrations
import (
"log"
"github.com/jmoiron/sqlx"
"github.com/knadh/koanf/v2"
"github.com/knadh/stuffbin"
)
// V3_1_0 performs the DB migrations.
func V3_1_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf, lo *log.Logger) error {
if _, err := db.Exec(`
DO $$
2024-04-10 02:50:48 +08:00
CREATE EXTENSION IF NOT EXISTS pgcrypto;
BEGIN
2024-05-07 13:38:31 +08:00
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'user_type') THEN
CREATE TYPE user_type AS ENUM ('user', 'super', 'api');
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'user_status') THEN
2024-05-07 13:38:31 +08:00
CREATE TYPE user_status AS ENUM ('enabled', 'disabled');
END IF;
END$$;
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password_login BOOLEAN NOT NULL DEFAULT false,
password TEXT NULL,
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
status user_status NOT NULL DEFAULT 'disabled',
loggedin_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
`); err != nil {
return err
}
2024-04-02 17:01:19 +08:00
// Insert new preference settings.
if _, err := db.Exec(`
INSERT INTO settings (key, value) VALUES
('security.oidc', '{"enabled": false, "provider_url": "", "client_id": "", "client_secret": ""}'),
ON CONFLICT DO NOTHING;
`); err != nil {
return err
}
return nil
}