PRAGMA foreign_keys=OFF;

DROP INDEX IF EXISTS user_groups_one_all_users;
DROP INDEX IF EXISTS idx_user_groups_name;

CREATE TABLE user_groups_new (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  public_id TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL UNIQUE,
  description TEXT NOT NULL DEFAULT '',
  disabled INTEGER NOT NULL DEFAULT 0,
  totp_required INTEGER NOT NULL DEFAULT 0,
  created_at INTEGER NOT NULL,
  updated_at INTEGER NOT NULL
);

INSERT INTO user_groups_new (id, public_id, name, description, disabled, totp_required, created_at, updated_at)
SELECT id, public_id, name, description, disabled, totp_required, created_at, updated_at
FROM user_groups;

DROP TABLE user_groups;
ALTER TABLE user_groups_new RENAME TO user_groups;

CREATE INDEX idx_user_groups_name ON user_groups(name);

PRAGMA foreign_keys=ON;
