Phase 3 Completed

This commit is contained in:
Rayyan
2026-08-02 17:44:50 +07:00
parent 7ce0de4e91
commit ffc5ecd259
68 changed files with 3132 additions and 163 deletions
+112
View File
@@ -0,0 +1,112 @@
-- Initial migration for RayLab Core (PostgreSQL)
-- Requires: CREATE EXTENSION IF NOT EXISTS pgcrypto; (for gen_random_uuid())
BEGIN;
-- Extension for UUID generation
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
-- Enum types
DO $$ BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'userrolesource') THEN
CREATE TYPE "UserRoleSource" AS ENUM ('AUTHENTIK', 'SYSTEM');
END IF;
END $$;
-- Users
CREATE TABLE IF NOT EXISTS "User" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
authentik_id text UNIQUE,
authentik_user_id text UNIQUE,
authentik_subject text UNIQUE,
username varchar(255) UNIQUE,
email varchar(320) UNIQUE,
name varchar(255),
picture text,
is_active boolean NOT NULL DEFAULT true,
deleted_at timestamptz,
last_seen_at timestamptz,
last_synced_at timestamptz,
sync_status text,
storage_quota bigint DEFAULT 10737418240,
storage_used bigint DEFAULT 0,
metadata jsonb,
last_group_hash text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_user_authentik_id ON "User" (authentik_id);
CREATE INDEX IF NOT EXISTS idx_user_username ON "User" (username);
CREATE INDEX IF NOT EXISTS idx_user_email ON "User" (email);
-- Roles
CREATE TABLE IF NOT EXISTS "Role" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
code varchar(100) NOT NULL UNIQUE,
name varchar(255) NOT NULL,
display_name varchar(255),
description text,
is_default boolean NOT NULL DEFAULT false,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
-- Permissions
CREATE TABLE IF NOT EXISTS "Permission" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
code varchar(150) NOT NULL UNIQUE,
name varchar(255) NOT NULL,
display_name varchar(255),
description text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
-- RolePermission
CREATE TABLE IF NOT EXISTS "RolePermission" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
role_id uuid NOT NULL REFERENCES "Role"(id) ON DELETE CASCADE,
permission_id uuid NOT NULL REFERENCES "Permission"(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT uq_role_permission UNIQUE (role_id, permission_id)
);
CREATE INDEX IF NOT EXISTS idx_rolepermission_role_id ON "RolePermission" (role_id);
CREATE INDEX IF NOT EXISTS idx_rolepermission_permission_id ON "RolePermission" (permission_id);
-- UserRole
CREATE TABLE IF NOT EXISTS "UserRole" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL REFERENCES "User"(id) ON DELETE CASCADE,
role_id uuid NOT NULL REFERENCES "Role"(id) ON DELETE CASCADE,
source "UserRoleSource" NOT NULL DEFAULT 'AUTHENTIK',
synced_at timestamptz NOT NULL DEFAULT now(),
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT uq_user_role UNIQUE (user_id, role_id)
);
CREATE INDEX IF NOT EXISTS idx_userrole_user_id ON "UserRole" (user_id);
CREATE INDEX IF NOT EXISTS idx_userrole_role_id ON "UserRole" (role_id);
-- AuthGroupRoleMapping
CREATE TABLE IF NOT EXISTS "AuthGroupRoleMapping" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
auth_group varchar(255) NOT NULL,
role_id uuid NOT NULL REFERENCES "Role"(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_authgroup_name ON "AuthGroupRoleMapping" (auth_group);
-- AuditLog
CREATE TABLE IF NOT EXISTS "AuditLog" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid REFERENCES "User"(id) ON DELETE SET NULL,
action varchar(200) NOT NULL,
resource varchar(200),
resource_id text,
details jsonb,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_auditlog_user_id ON "AuditLog" (user_id);
CREATE INDEX IF NOT EXISTS idx_auditlog_action ON "AuditLog" (action);
COMMIT;
@@ -0,0 +1,37 @@
-- Phase 4 migration: add ScheduledJob and MediaObject
BEGIN;
-- ScheduledJob
CREATE TABLE IF NOT EXISTS "ScheduledJob" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name varchar(255) NOT NULL,
payload jsonb,
cron varchar(255),
run_at timestamptz,
status varchar(50) NOT NULL DEFAULT 'pending',
attempts integer NOT NULL DEFAULT 0,
last_run_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_scheduledjob_name ON "ScheduledJob" (name);
CREATE INDEX IF NOT EXISTS idx_scheduledjob_status ON "ScheduledJob" (status);
-- MediaObject
CREATE TABLE IF NOT EXISTS "MediaObject" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
owner_user_id uuid REFERENCES "User"(id) ON DELETE SET NULL,
storage_key varchar(1024) NOT NULL,
filename varchar(1024),
mime_type varchar(255),
size bigint,
is_public boolean NOT NULL DEFAULT false,
metadata jsonb,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_media_owner_user_id ON "MediaObject" (owner_user_id);
CREATE INDEX IF NOT EXISTS idx_media_storage_key ON "MediaObject" (storage_key);
COMMIT;
@@ -0,0 +1,18 @@
-- Migration: add Application table
CREATE TABLE IF NOT EXISTS "Application" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
"code" varchar(100) NOT NULL UNIQUE,
"name" varchar(255) NOT NULL,
"description" text,
"icon" text,
"url" text,
"applicationsClaim" varchar(255) NOT NULL UNIQUE,
"isActive" boolean NOT NULL DEFAULT true,
"displayOrder" integer NOT NULL DEFAULT 0,
"createdAt" timestamptz NOT NULL DEFAULT now(),
"updatedAt" timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS "Application_code_idx" ON "Application" ("code");
CREATE INDEX IF NOT EXISTS "Application_applicationsClaim_idx" ON "Application" ("applicationsClaim");