Files
stream.api/script/create_database.sql
2026-04-02 11:01:30 +00:00

376 lines
11 KiB
PL/PgSQL

-- DROP SCHEMA public;
CREATE SCHEMA public AUTHORIZATION pg_database_owner;
COMMENT ON SCHEMA public IS 'standard public schema';
-- public.ad_templates definition
-- Drop table
-- DROP TABLE public.ad_templates;
CREATE TABLE public.ad_templates (
id uuid NOT NULL,
user_id uuid NOT NULL,
"name" text NOT NULL,
description text NULL,
vast_tag_url text NOT NULL,
ad_format varchar(50) DEFAULT 'pre-roll'::character varying NOT NULL,
duration int8 NULL,
is_active bool DEFAULT true NOT NULL,
created_at timestamptz NULL,
updated_at timestamptz NULL,
is_default bool DEFAULT false NOT NULL,
"version" int8 DEFAULT 1 NOT NULL,
CONSTRAINT ad_templates_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_ad_templates_user_id ON public.ad_templates USING btree (user_id);
-- public.domains definition
-- Drop table
-- DROP TABLE public.domains;
CREATE TABLE public.domains (
id uuid NOT NULL,
user_id uuid NOT NULL,
"name" text NOT NULL,
created_at timestamptz NULL,
updated_at timestamptz NULL,
"version" int8 DEFAULT 1 NOT NULL,
CONSTRAINT domains_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_domains_user_id ON public.domains USING btree (user_id);
-- public.jobs definition
-- Drop table
-- DROP TABLE public.jobs;
CREATE TABLE public.jobs (
id uuid DEFAULT gen_random_uuid() NOT NULL,
status text NULL,
priority int8 DEFAULT 0 NULL,
input_url text NULL,
output_url text NULL,
total_duration int8 NULL,
current_time int8 NULL,
progress numeric NULL,
agent_id int8 NULL,
logs text NULL,
config text NULL,
cancelled bool DEFAULT false NULL,
retry_count int8 DEFAULT 0 NULL,
max_retries int8 DEFAULT 3 NULL,
created_at timestamptz NULL,
updated_at timestamptz NULL,
"version" int8 NULL,
video_id uuid NULL,
user_id uuid NULL,
time_limit int8 DEFAULT 3600000 NULL,
CONSTRAINT jobs_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_jobs_priority ON public.jobs USING btree (priority);
-- public.notifications definition
-- Drop table
-- DROP TABLE public.notifications;
CREATE TABLE public.notifications (
id uuid NOT NULL,
user_id uuid NOT NULL,
"type" varchar(50) NOT NULL,
title text NOT NULL,
message text NOT NULL,
metadata text NULL,
action_url text NULL,
action_label text NULL,
is_read bool DEFAULT false NOT NULL,
created_at timestamptz NULL,
updated_at timestamptz NULL,
"version" int8 DEFAULT 1 NOT NULL,
CONSTRAINT notifications_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_notifications_user_id ON public.notifications USING btree (user_id);
-- public."plan" definition
-- Drop table
-- DROP TABLE public."plan";
CREATE TABLE public."plan" (
id uuid DEFAULT gen_random_uuid() NOT NULL,
"name" text NOT NULL,
description text NULL,
price numeric(65, 30) NOT NULL,
"cycle" varchar(20) NOT NULL,
storage_limit int8 NOT NULL,
upload_limit int4 NOT NULL,
duration_limit int4 NOT NULL,
quality_limit text NOT NULL,
features _text NULL,
is_active bool DEFAULT true NOT NULL,
"version" int8 DEFAULT 1 NOT NULL,
CONSTRAINT plan_pkey PRIMARY KEY (id)
);
-- public.plan_subscriptions definition
-- Drop table
-- DROP TABLE public.plan_subscriptions;
CREATE TABLE public.plan_subscriptions (
id uuid NOT NULL,
user_id uuid NOT NULL,
payment_id uuid NOT NULL,
plan_id uuid NOT NULL,
term_months int4 NOT NULL,
payment_method varchar(20) NOT NULL,
wallet_amount numeric(65, 30) NOT NULL,
topup_amount numeric(65, 30) NOT NULL,
started_at timestamptz NOT NULL,
expires_at timestamptz NOT NULL,
reminder_7d_sent_at timestamptz NULL,
reminder_3d_sent_at timestamptz NULL,
reminder_1d_sent_at timestamptz NULL,
created_at timestamptz NULL,
updated_at timestamptz NULL,
"version" int8 DEFAULT 1 NOT NULL,
CONSTRAINT plan_subscriptions_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_plan_subscriptions_expires_at ON public.plan_subscriptions USING btree (expires_at);
CREATE INDEX idx_plan_subscriptions_payment_id ON public.plan_subscriptions USING btree (payment_id);
CREATE INDEX idx_plan_subscriptions_plan_id ON public.plan_subscriptions USING btree (plan_id);
CREATE INDEX idx_plan_subscriptions_user_id ON public.plan_subscriptions USING btree (user_id);
-- public.user_preferences definition
-- Drop table
-- DROP TABLE public.user_preferences;
CREATE TABLE public.user_preferences (
user_id uuid NOT NULL,
"language" text DEFAULT 'en'::text NOT NULL,
locale text DEFAULT 'en'::text NOT NULL,
email_notifications bool DEFAULT true NOT NULL,
push_notifications bool DEFAULT true NOT NULL,
marketing_notifications bool DEFAULT false NOT NULL,
telegram_notifications bool DEFAULT false NOT NULL,
created_at timestamptz NULL,
updated_at timestamptz NULL,
"version" int8 DEFAULT 1 NOT NULL,
CONSTRAINT user_preferences_pkey PRIMARY KEY (user_id)
);
-- public.wallet_transactions definition
-- Drop table
-- DROP TABLE public.wallet_transactions;
CREATE TABLE public.wallet_transactions (
id uuid NOT NULL,
user_id uuid NOT NULL,
"type" varchar(50) NOT NULL,
amount numeric(65, 30) NOT NULL,
currency text DEFAULT 'USD'::text NOT NULL,
note text NULL,
created_at timestamptz NULL,
updated_at timestamptz NULL,
payment_id uuid NULL,
plan_id uuid NULL,
term_months int4 NULL,
"version" int8 DEFAULT 1 NOT NULL,
CONSTRAINT wallet_transactions_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_wallet_transactions_payment_id ON public.wallet_transactions USING btree (payment_id);
CREATE INDEX idx_wallet_transactions_plan_id ON public.wallet_transactions USING btree (plan_id);
CREATE INDEX idx_wallet_transactions_user_id ON public.wallet_transactions USING btree (user_id);
-- public."user" definition
-- Drop table
-- DROP TABLE public."user";
CREATE TABLE public."user" (
id uuid DEFAULT gen_random_uuid() NOT NULL,
email text NOT NULL,
"password" text NULL,
username text NULL,
avatar text NULL,
"role" varchar(20) DEFAULT 'USER'::character varying NOT NULL,
google_id text NULL,
storage_used int8 DEFAULT 0 NOT NULL,
plan_id uuid NULL,
created_at timestamp(3) DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp(3) NOT NULL,
"version" int8 DEFAULT 1 NOT NULL,
telegram_id varchar NULL,
referred_by_user_id uuid NULL,
referral_eligible bool DEFAULT true NOT NULL,
referral_reward_bps int4 NULL,
referral_reward_granted_at timestamptz NULL,
referral_reward_payment_id uuid NULL,
referral_reward_amount numeric(65, 30) NULL,
CONSTRAINT user_pkey PRIMARY KEY (id),
CONSTRAINT user_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public."plan"(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT user_referred_by_user_id_fkey FOREIGN KEY (referred_by_user_id) REFERENCES public."user"(id) ON DELETE SET NULL
);
CREATE INDEX idx_user_referred_by_user_id ON public."user" USING btree (referred_by_user_id);
CREATE UNIQUE INDEX user_email_key ON public."user" USING btree (email);
CREATE UNIQUE INDEX user_google_id_key ON public."user" USING btree (google_id);
-- public.video definition
-- Drop table
-- DROP TABLE public.video;
CREATE TABLE public.video (
id uuid DEFAULT gen_random_uuid() NOT NULL,
"name" text NOT NULL,
title text NOT NULL,
description text NULL,
url text NOT NULL,
thumbnail text NULL,
hls_token text NULL,
hls_path text NULL,
duration int4 NOT NULL,
"size" int8 NOT NULL,
storage_type varchar(20) DEFAULT 'tiktok_avatar'::character varying NOT NULL,
format text NOT NULL,
status varchar(20) DEFAULT 'PUBLIC'::character varying NOT NULL,
processing_status varchar(20) DEFAULT 'PENDING'::character varying NOT NULL,
"views" int4 DEFAULT 0 NOT NULL,
user_id uuid NOT NULL,
created_at timestamp(3) DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp(3) NOT NULL,
"version" int8 DEFAULT 1 NOT NULL,
ad_id uuid NULL,
metadata jsonb NULL,
CONSTRAINT video_pkey PRIMARY KEY (id),
CONSTRAINT video_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE RESTRICT ON UPDATE CASCADE
);
-- public.payment definition
-- Drop table
-- DROP TABLE public.payment;
CREATE TABLE public.payment (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
plan_id uuid NULL,
amount numeric(65, 30) NOT NULL,
currency text DEFAULT 'USD'::text NOT NULL,
status varchar(20) DEFAULT 'PENDING'::character varying NOT NULL,
provider varchar(20) DEFAULT 'STRIPE'::character varying NOT NULL,
transaction_id text NULL,
created_at timestamp(3) DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp(3) NOT NULL,
"version" int8 DEFAULT 1 NOT NULL,
CONSTRAINT payment_pkey PRIMARY KEY (id),
CONSTRAINT payment_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public."plan"(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT payment_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE RESTRICT ON UPDATE CASCADE
);
-- public.player_configs definition
-- Drop table
-- DROP TABLE public.player_configs;
CREATE TABLE public.player_configs (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
"name" text NOT NULL,
description text NULL,
autoplay bool DEFAULT false NOT NULL,
"loop" bool DEFAULT false NOT NULL,
muted bool DEFAULT false NOT NULL,
show_controls bool DEFAULT true NOT NULL,
pip bool DEFAULT true NOT NULL,
airplay bool DEFAULT true NOT NULL,
chromecast bool DEFAULT true NOT NULL,
is_active bool DEFAULT true NOT NULL,
is_default bool DEFAULT false NOT NULL,
created_at timestamp(3) DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp(3) NOT NULL,
"version" int8 DEFAULT 1 NOT NULL,
encrytion_m3u8 bool DEFAULT true NOT NULL,
logo_url varchar(500) NULL,
CONSTRAINT player_configs_pkey PRIMARY KEY (id),
CONSTRAINT player_configs_url_check CHECK (((logo_url)::text ~* '^https?://'::text)),
CONSTRAINT player_configs_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE
);
CREATE INDEX idx_player_configs_is_default ON public.player_configs USING btree (is_default);
CREATE UNIQUE INDEX idx_player_configs_one_default_per_user ON public.player_configs USING btree (user_id) WHERE (is_default = true);
CREATE INDEX idx_player_configs_user_default ON public.player_configs USING btree (user_id, is_default);
CREATE INDEX idx_player_configs_user_id ON public.player_configs USING btree (user_id);
-- Table Triggers
create trigger trg_update_player_configs before
update
on
public.player_configs for each row execute function update_player_configs_updated_at();
-- public.popup_ads definition
-- Drop table
-- DROP TABLE public.popup_ads;
CREATE TABLE public.popup_ads (
id uuid NOT NULL,
user_id uuid NOT NULL,
"type" varchar(20) NOT NULL,
"label" text NOT NULL,
value text NOT NULL,
is_active bool DEFAULT true NOT NULL,
max_triggers_per_session int4 DEFAULT 3 NOT NULL,
created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
updated_at timestamptz NULL,
"version" int8 DEFAULT 1 NOT NULL,
CONSTRAINT popup_ads_pkey PRIMARY KEY (id),
CONSTRAINT popup_ads_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE
);
CREATE INDEX idx_popup_ads_user_active ON public.popup_ads USING btree (user_id, is_active);
CREATE INDEX idx_popup_ads_user_id ON public.popup_ads USING btree (user_id);
-- DROP FUNCTION public.update_player_configs_updated_at();
CREATE OR REPLACE FUNCTION public.update_player_configs_updated_at()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
NEW.version = OLD.version + 1;
RETURN NEW;
END;
$function$
;