Files
sm-user-plugin/updates/202610020001_create_user_groups.go
Jakub Zych 86970074b7 feat: add Winter user groups
- user_groups (seeded with the guest and registered groups) and the
  users_groups pivot, folded from the PHP v1.1.1 and v2.8.0 migrations
  into migration 202610020001_create_user_groups
- models.UserGroup and a User.Groups relation that is never serialized
- classes.UserGroupCodes and classes.HasGroupCode for server-side checks
  such as the site-admin group code admin
- additive only: no route, payload or existing column changes; the user
  payload still answers "groups":[]
2026-10-02 11:44:01 +02:00

55 lines
1.5 KiB
Go

package updates
import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
// Folds PHP updates/v1.1.1/create_user_groups_table.php,
// updates/v1.1.1/seed_user_groups_table.php and
// updates/v2.8.0/add_permissions_to_user_groups.php: the user_groups table
// with its Guest and Registered rows, and the users_groups pivot keyed by
// the user_group primary key.
var userGroupsMigrations = []*gormigrate.Migration{
{
ID: "202610020001_create_user_groups",
Migrate: func(tx *gorm.DB) error {
for _, stmt := range []string{
`CREATE TABLE user_groups (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
code VARCHAR(255) NULL,
description TEXT NULL,
permissions TEXT NULL,
created_at TIMESTAMPTZ NULL,
updated_at TIMESTAMPTZ NULL
)`,
`CREATE INDEX user_groups_code_index ON user_groups (code)`,
`CREATE TABLE users_groups (
user_id INTEGER NOT NULL,
user_group_id INTEGER NOT NULL,
CONSTRAINT user_group PRIMARY KEY (user_id, user_group_id)
)`,
`INSERT INTO user_groups (name, code, description, created_at, updated_at) VALUES
('Guest', 'guest', 'Default group for guest users.', NOW(), NOW()),
('Registered', 'registered', 'Default group for registered users.', NOW(), NOW())`,
} {
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 users_groups`).Error; err != nil {
return err
}
return tx.Exec(`DROP TABLE IF EXISTS user_groups`).Error
},
},
}
func init() {
Register(userGroupsMigrations...)
}