package lagoon import ( "github.com/go-gormigrate/gormigrate/v2" "gorm.io/gorm" ) // BackendAdminMigrations creates Winter-shaped backend identity tables and // seeds the developer and publisher system roles. History is isolated under // the summercms.cabana plugin id. var BackendAdminMigrations = []*gormigrate.Migration{ { ID: "202609240001_backend_admin_identity", Migrate: func(tx *gorm.DB) error { stmts := []string{ `CREATE TABLE backend_user_roles ( id SERIAL PRIMARY KEY, name TEXT NOT NULL UNIQUE, code TEXT UNIQUE, description TEXT, permissions TEXT, is_system BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() )`, `CREATE TABLE backend_users ( id SERIAL PRIMARY KEY, first_name TEXT, last_name TEXT, login TEXT NOT NULL UNIQUE, email TEXT NOT NULL UNIQUE, password TEXT NOT NULL, activation_code TEXT, persist_code TEXT, reset_password_code TEXT, permissions TEXT, is_activated BOOLEAN NOT NULL DEFAULT FALSE, is_superuser BOOLEAN NOT NULL DEFAULT FALSE, role_id INTEGER REFERENCES backend_user_roles(id), activated_at TIMESTAMPTZ, last_login TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), deleted_at TIMESTAMPTZ )`, `CREATE INDEX backend_users_role_id_index ON backend_users (role_id)`, `CREATE INDEX backend_users_deleted_at_index ON backend_users (deleted_at)`, `INSERT INTO backend_user_roles (name, code, description, permissions, is_system) VALUES ('Developer', 'developer', 'Site administrator with access to developer tools.', '{}', TRUE), ('Publisher', 'publisher', 'Site editor with access to publishing tools.', '{}', TRUE)`, } for _, stmt := range stmts { if err := tx.Exec(stmt).Error; err != nil { return err } } return nil }, Rollback: func(tx *gorm.DB) error { if err := tx.Exec(`DROP TABLE IF EXISTS backend_users`).Error; err != nil { return err } return tx.Exec(`DROP TABLE IF EXISTS backend_user_roles`).Error }, }, }