fix
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.users (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
@@ -7,7 +9,8 @@ CREATE TABLE IF NOT EXISTS public.users (
|
||||
first_name text NOT NULL,
|
||||
second_name text NOT NULL,
|
||||
birth_date date,
|
||||
sex text NOT NULL
|
||||
sex text NOT NULL,
|
||||
is_admin boolean DEFAULT false
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.oauth_clients (
|
||||
@@ -38,9 +41,20 @@ CREATE TABLE IF NOT EXISTS public.authorization_codes (
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_code_hash ON public.authorization_codes (code_hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_expires_at ON public.authorization_codes (expires_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_consumed_at ON public.authorization_codes (consumed_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_code_hash
|
||||
ON public.authorization_codes (code_hash);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_expires_at
|
||||
ON public.authorization_codes (expires_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_consumed_at
|
||||
ON public.authorization_codes (consumed_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_user_id
|
||||
ON public.authorization_codes(user_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_client_id
|
||||
ON public.authorization_codes(client_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.refresh_tokens (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
@@ -52,14 +66,11 @@ CREATE TABLE IF NOT EXISTS public.refresh_tokens (
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_token_hash ON public.refresh_tokens (token_hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_expires_at ON public.refresh_tokens (expires_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_token_hash
|
||||
ON public.refresh_tokens (token_hash);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_user_id
|
||||
ON public.authorization_codes(user_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_authorization_codes_client_id
|
||||
ON public.authorization_codes(client_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_expires_at
|
||||
ON public.refresh_tokens (expires_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_user_id
|
||||
ON public.refresh_tokens(user_id);
|
||||
@@ -83,9 +94,15 @@ CREATE UNIQUE INDEX IF NOT EXISTS unique_active_consent
|
||||
ON public.user_consents (user_id, client_id)
|
||||
WHERE revoked_at IS NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_consents_user_id ON public.user_consents(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_consents_client_id ON public.user_consents(client_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_consents_revoked ON public.user_consents(revoked_at) WHERE revoked_at IS NOT NULL;
|
||||
CREATE INDEX IF NOT EXISTS idx_user_consents_user_id
|
||||
ON public.user_consents(user_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_consents_client_id
|
||||
ON public.user_consents(client_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_consents_revoked
|
||||
ON public.user_consents(revoked_at)
|
||||
WHERE revoked_at IS NOT NULL;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.positions (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
@@ -94,23 +111,78 @@ CREATE TABLE IF NOT EXISTS public.positions (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.position_translations (
|
||||
position_id uuid NOT NULL REFERENCES public.positions(id) ON DELETE CASCADE,
|
||||
locale text NOT NULL,
|
||||
name text NOT NULL,
|
||||
CREATE TABLE IF NOT EXISTS public.permissions (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
code text NOT NULL UNIQUE,
|
||||
description text,
|
||||
PRIMARY KEY (position_id, locale)
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
INSERT INTO public.permissions (code, description) VALUES
|
||||
('positions.manage', 'Create and edit positions'),
|
||||
('positions.assign', 'Assign positions to users')
|
||||
ON CONFLICT (code) DO NOTHING;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.position_permissions (
|
||||
position_id uuid NOT NULL REFERENCES public.positions(id) ON DELETE CASCADE,
|
||||
permission_id uuid NOT NULL REFERENCES public.permissions(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (position_id, permission_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_position_permissions_position_id
|
||||
ON public.position_permissions (position_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_position_permissions_permission_id
|
||||
ON public.position_permissions (permission_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.user_positions (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id uuid NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
|
||||
position_id uuid NOT NULL REFERENCES public.positions(id) ON DELETE CASCADE,
|
||||
valid_from timestamptz NOT NULL DEFAULT now(),
|
||||
valid_to timestamptz,
|
||||
CONSTRAINT user_positions_unique_active UNIQUE (user_id, position_id)
|
||||
DEFERRABLE INITIALLY IMMEDIATE
|
||||
assigned_by uuid REFERENCES public.users(id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_positions_user_id ON public.user_positions(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_positions_position_id ON public.user_positions(position_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_positions_user_id
|
||||
ON public.user_positions(user_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_positions_position_id
|
||||
ON public.user_positions(position_id);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_positions_unique_active
|
||||
ON public.user_positions (user_id, position_id)
|
||||
WHERE valid_to IS NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_permissions_code
|
||||
ON public.permissions (code);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_users_is_admin
|
||||
ON public.users (is_admin)
|
||||
WHERE is_admin = TRUE;
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.set_updated_at()
|
||||
RETURNS trigger AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = now();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
DROP TRIGGER IF EXISTS users_updated_at ON public.users;
|
||||
CREATE TRIGGER users_updated_at
|
||||
BEFORE UPDATE ON public.users
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION public.set_updated_at();
|
||||
|
||||
DROP TRIGGER IF EXISTS oauth_clients_updated_at ON public.oauth_clients;
|
||||
CREATE TRIGGER oauth_clients_updated_at
|
||||
BEFORE UPDATE ON public.oauth_clients
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION public.set_updated_at();
|
||||
|
||||
DROP TRIGGER IF EXISTS positions_updated_at ON public.positions;
|
||||
CREATE TRIGGER positions_updated_at
|
||||
BEFORE UPDATE ON public.positions
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION public.set_updated_at();
|
||||
Reference in New Issue
Block a user