在前后端开发联调前 的提交20260223
This commit is contained in:
121
docs/database/ddl/etl_feiqiu__app.sql
Normal file
121
docs/database/ddl/etl_feiqiu__app.sql
Normal file
@@ -0,0 +1,121 @@
|
||||
-- =============================================================================
|
||||
-- etl_feiqiu / app(RLS 视图层)
|
||||
-- 生成日期:2026-02-23
|
||||
-- 来源:测试库(通过脚本自动导出)
|
||||
-- =============================================================================
|
||||
|
||||
CREATE SCHEMA IF NOT EXISTS app;
|
||||
|
||||
-- 视图
|
||||
CREATE OR REPLACE VIEW app.v_assistant AS
|
||||
SELECT assistant_id,
|
||||
tenant_id,
|
||||
site_id,
|
||||
real_name,
|
||||
nickname,
|
||||
mobile,
|
||||
level,
|
||||
assistant_status,
|
||||
leave_status
|
||||
FROM core.dim_assistant a;
|
||||
;
|
||||
|
||||
CREATE OR REPLACE VIEW app.v_assistant_daily AS
|
||||
SELECT id,
|
||||
site_id,
|
||||
tenant_id,
|
||||
assistant_id,
|
||||
assistant_nickname,
|
||||
stat_date,
|
||||
total_service_count,
|
||||
total_hours,
|
||||
base_hours,
|
||||
bonus_hours,
|
||||
room_hours,
|
||||
total_ledger_amount,
|
||||
unique_customers,
|
||||
unique_tables,
|
||||
created_at
|
||||
FROM dws.dws_assistant_daily_detail d;
|
||||
;
|
||||
|
||||
CREATE OR REPLACE VIEW app.v_finance_daily AS
|
||||
SELECT id,
|
||||
site_id,
|
||||
tenant_id,
|
||||
stat_date,
|
||||
gross_amount,
|
||||
table_fee_amount,
|
||||
goods_amount,
|
||||
assistant_pd_amount,
|
||||
assistant_cx_amount,
|
||||
discount_total,
|
||||
confirmed_income,
|
||||
cash_inflow_total,
|
||||
recharge_count,
|
||||
recharge_total,
|
||||
order_count,
|
||||
member_order_count,
|
||||
guest_order_count,
|
||||
avg_order_amount,
|
||||
created_at
|
||||
FROM dws.dws_finance_daily_summary f;
|
||||
;
|
||||
|
||||
CREATE OR REPLACE VIEW app.v_member AS
|
||||
SELECT member_id,
|
||||
system_member_id,
|
||||
tenant_id,
|
||||
register_site_id AS site_id,
|
||||
mobile,
|
||||
nickname,
|
||||
member_card_grade_name,
|
||||
status
|
||||
FROM core.dim_member m;
|
||||
;
|
||||
|
||||
CREATE OR REPLACE VIEW app.v_member_consumption AS
|
||||
SELECT id,
|
||||
site_id,
|
||||
tenant_id,
|
||||
member_id,
|
||||
stat_date,
|
||||
member_nickname,
|
||||
card_grade_name,
|
||||
total_visit_count,
|
||||
total_consume_amount,
|
||||
total_recharge_amount,
|
||||
last_consume_date,
|
||||
first_consume_date,
|
||||
days_since_last,
|
||||
customer_tier,
|
||||
created_at
|
||||
FROM dws.dws_member_consumption_summary mc;
|
||||
;
|
||||
|
||||
CREATE OR REPLACE VIEW app.v_order_summary AS
|
||||
SELECT site_id,
|
||||
order_settle_id,
|
||||
order_trade_no,
|
||||
order_date,
|
||||
tenant_id,
|
||||
member_id,
|
||||
member_flag,
|
||||
order_original_amount,
|
||||
order_final_amount,
|
||||
total_paid_amount,
|
||||
refund_amount,
|
||||
net_income,
|
||||
created_at
|
||||
FROM dws.dws_order_summary os;
|
||||
;
|
||||
|
||||
CREATE OR REPLACE VIEW app.v_site AS
|
||||
SELECT site_id,
|
||||
tenant_id,
|
||||
shop_name,
|
||||
site_label,
|
||||
shop_status
|
||||
FROM core.dim_site s;
|
||||
;
|
||||
|
||||
99
docs/database/ddl/etl_feiqiu__core.sql
Normal file
99
docs/database/ddl/etl_feiqiu__core.sql
Normal file
@@ -0,0 +1,99 @@
|
||||
-- =============================================================================
|
||||
-- etl_feiqiu / core(跨门店标准化维度/事实)
|
||||
-- 生成日期:2026-02-23
|
||||
-- 来源:测试库(通过脚本自动导出)
|
||||
-- =============================================================================
|
||||
|
||||
CREATE SCHEMA IF NOT EXISTS core;
|
||||
|
||||
-- 表
|
||||
CREATE TABLE core.dim_assistant (
|
||||
assistant_id bigint NOT NULL,
|
||||
tenant_id bigint NOT NULL,
|
||||
site_id bigint NOT NULL,
|
||||
real_name text NOT NULL,
|
||||
nickname text,
|
||||
mobile text,
|
||||
level integer,
|
||||
assistant_status integer,
|
||||
leave_status integer
|
||||
);
|
||||
|
||||
CREATE TABLE core.dim_goods_category (
|
||||
category_id bigint NOT NULL,
|
||||
tenant_id bigint NOT NULL,
|
||||
category_name text NOT NULL,
|
||||
parent_id bigint,
|
||||
level integer
|
||||
);
|
||||
|
||||
CREATE TABLE core.dim_member (
|
||||
member_id bigint NOT NULL,
|
||||
system_member_id bigint,
|
||||
tenant_id bigint NOT NULL,
|
||||
register_site_id bigint NOT NULL,
|
||||
mobile text,
|
||||
nickname text,
|
||||
member_card_grade_name text,
|
||||
status integer
|
||||
);
|
||||
|
||||
CREATE TABLE core.dim_site (
|
||||
site_id bigint NOT NULL,
|
||||
tenant_id bigint NOT NULL,
|
||||
shop_name text NOT NULL,
|
||||
site_label text,
|
||||
shop_status integer,
|
||||
site_id_alias bigint
|
||||
);
|
||||
|
||||
CREATE TABLE core.dim_table (
|
||||
table_id bigint NOT NULL,
|
||||
site_id bigint NOT NULL,
|
||||
table_name text NOT NULL,
|
||||
site_table_area_name text,
|
||||
table_price numeric(18,2)
|
||||
);
|
||||
|
||||
CREATE TABLE core.fact_payment (
|
||||
payment_id bigint NOT NULL,
|
||||
site_id bigint NOT NULL,
|
||||
order_settle_id bigint,
|
||||
pay_type integer,
|
||||
pay_amount numeric(18,2),
|
||||
pay_time timestamp with time zone,
|
||||
status integer
|
||||
);
|
||||
|
||||
CREATE TABLE core.fact_settlement (
|
||||
order_settle_id bigint NOT NULL,
|
||||
site_id bigint NOT NULL,
|
||||
tenant_id bigint NOT NULL,
|
||||
order_trade_no bigint,
|
||||
member_id bigint,
|
||||
total_amount numeric(18,2),
|
||||
actual_amount numeric(18,2),
|
||||
discount_amount numeric(18,2),
|
||||
pay_status integer,
|
||||
settle_time timestamp with time zone,
|
||||
created_at timestamp with time zone,
|
||||
updated_at timestamp with time zone
|
||||
);
|
||||
|
||||
-- 约束(主键 / 唯一 / 外键)
|
||||
ALTER TABLE core.dim_assistant ADD CONSTRAINT dim_assistant_pkey PRIMARY KEY (assistant_id);
|
||||
ALTER TABLE core.dim_goods_category ADD CONSTRAINT dim_goods_category_pkey PRIMARY KEY (category_id);
|
||||
ALTER TABLE core.dim_member ADD CONSTRAINT dim_member_pkey PRIMARY KEY (member_id);
|
||||
ALTER TABLE core.dim_site ADD CONSTRAINT dim_site_pkey PRIMARY KEY (site_id);
|
||||
ALTER TABLE core.dim_table ADD CONSTRAINT dim_table_pkey PRIMARY KEY (table_id);
|
||||
ALTER TABLE core.fact_payment ADD CONSTRAINT fact_payment_pkey PRIMARY KEY (payment_id);
|
||||
ALTER TABLE core.fact_settlement ADD CONSTRAINT fact_settlement_pkey PRIMARY KEY (order_settle_id);
|
||||
|
||||
-- 索引
|
||||
CREATE INDEX idx_core_assistant_site ON core.dim_assistant USING btree (site_id);
|
||||
CREATE INDEX idx_core_member_site ON core.dim_member USING btree (register_site_id);
|
||||
CREATE INDEX idx_core_table_site ON core.dim_table USING btree (site_id);
|
||||
CREATE INDEX idx_core_payment_site ON core.fact_payment USING btree (site_id);
|
||||
CREATE INDEX idx_core_settlement_site ON core.fact_settlement USING btree (site_id);
|
||||
CREATE INDEX idx_core_settlement_time ON core.fact_settlement USING btree (settle_time);
|
||||
|
||||
1290
docs/database/ddl/etl_feiqiu__dwd.sql
Normal file
1290
docs/database/ddl/etl_feiqiu__dwd.sql
Normal file
File diff suppressed because it is too large
Load Diff
1453
docs/database/ddl/etl_feiqiu__dws.sql
Normal file
1453
docs/database/ddl/etl_feiqiu__dws.sql
Normal file
File diff suppressed because it is too large
Load Diff
77
docs/database/ddl/etl_feiqiu__meta.sql
Normal file
77
docs/database/ddl/etl_feiqiu__meta.sql
Normal file
@@ -0,0 +1,77 @@
|
||||
-- =============================================================================
|
||||
-- etl_feiqiu / meta(ETL 调度元数据)
|
||||
-- 生成日期:2026-02-23
|
||||
-- 来源:测试库(通过脚本自动导出)
|
||||
-- =============================================================================
|
||||
|
||||
CREATE SCHEMA IF NOT EXISTS meta;
|
||||
|
||||
-- 序列
|
||||
CREATE SEQUENCE IF NOT EXISTS meta.etl_cursor_cursor_id_seq AS bigint;
|
||||
CREATE SEQUENCE IF NOT EXISTS meta.etl_run_run_id_seq AS bigint;
|
||||
CREATE SEQUENCE IF NOT EXISTS meta.etl_task_task_id_seq AS bigint;
|
||||
|
||||
-- 表
|
||||
CREATE TABLE meta.etl_cursor (
|
||||
cursor_id bigint DEFAULT nextval('meta.etl_cursor_cursor_id_seq'::regclass) NOT NULL,
|
||||
task_id bigint NOT NULL,
|
||||
store_id bigint NOT NULL,
|
||||
last_start timestamp with time zone,
|
||||
last_end timestamp with time zone,
|
||||
last_id bigint,
|
||||
last_run_id bigint,
|
||||
extra jsonb DEFAULT '{}'::jsonb,
|
||||
created_at timestamp with time zone DEFAULT now(),
|
||||
updated_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE meta.etl_run (
|
||||
run_id bigint DEFAULT nextval('meta.etl_run_run_id_seq'::regclass) NOT NULL,
|
||||
run_uuid text NOT NULL,
|
||||
task_id bigint NOT NULL,
|
||||
store_id bigint NOT NULL,
|
||||
status text NOT NULL,
|
||||
started_at timestamp with time zone DEFAULT now(),
|
||||
ended_at timestamp with time zone,
|
||||
window_start timestamp with time zone,
|
||||
window_end timestamp with time zone,
|
||||
window_minutes integer,
|
||||
overlap_seconds integer,
|
||||
fetched_count integer DEFAULT 0,
|
||||
loaded_count integer DEFAULT 0,
|
||||
updated_count integer DEFAULT 0,
|
||||
skipped_count integer DEFAULT 0,
|
||||
error_count integer DEFAULT 0,
|
||||
unknown_fields integer DEFAULT 0,
|
||||
export_dir text,
|
||||
log_path text,
|
||||
request_params jsonb DEFAULT '{}'::jsonb,
|
||||
manifest jsonb DEFAULT '{}'::jsonb,
|
||||
error_message text,
|
||||
extra jsonb DEFAULT '{}'::jsonb
|
||||
);
|
||||
|
||||
CREATE TABLE meta.etl_task (
|
||||
task_id bigint DEFAULT nextval('meta.etl_task_task_id_seq'::regclass) NOT NULL,
|
||||
task_code text NOT NULL,
|
||||
store_id bigint NOT NULL,
|
||||
enabled boolean DEFAULT true,
|
||||
cursor_field text,
|
||||
window_minutes_default integer DEFAULT 30,
|
||||
overlap_seconds integer DEFAULT 600,
|
||||
page_size integer DEFAULT 200,
|
||||
retry_max integer DEFAULT 3,
|
||||
params jsonb DEFAULT '{}'::jsonb,
|
||||
created_at timestamp with time zone DEFAULT now(),
|
||||
updated_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
-- 约束(主键 / 唯一 / 外键)
|
||||
ALTER TABLE meta.etl_cursor ADD CONSTRAINT etl_cursor_task_id_fkey FOREIGN KEY (task_id) REFERENCES meta.etl_task(task_id) ON DELETE CASCADE;
|
||||
ALTER TABLE meta.etl_cursor ADD CONSTRAINT etl_cursor_pkey PRIMARY KEY (cursor_id);
|
||||
ALTER TABLE meta.etl_cursor ADD CONSTRAINT etl_cursor_task_id_store_id_key UNIQUE (task_id, store_id);
|
||||
ALTER TABLE meta.etl_run ADD CONSTRAINT etl_run_task_id_fkey FOREIGN KEY (task_id) REFERENCES meta.etl_task(task_id) ON DELETE CASCADE;
|
||||
ALTER TABLE meta.etl_run ADD CONSTRAINT etl_run_pkey PRIMARY KEY (run_id);
|
||||
ALTER TABLE meta.etl_task ADD CONSTRAINT etl_task_pkey PRIMARY KEY (task_id);
|
||||
ALTER TABLE meta.etl_task ADD CONSTRAINT etl_task_task_code_store_id_key UNIQUE (task_code, store_id);
|
||||
|
||||
1133
docs/database/ddl/etl_feiqiu__ods.sql
Normal file
1133
docs/database/ddl/etl_feiqiu__ods.sql
Normal file
File diff suppressed because it is too large
Load Diff
71
docs/database/ddl/fdw.sql
Normal file
71
docs/database/ddl/fdw.sql
Normal file
@@ -0,0 +1,71 @@
|
||||
-- =============================================================================
|
||||
-- FDW 跨库映射(在 zqyy_app 中执行)
|
||||
-- 生成日期:2026-02-23
|
||||
-- 来源:db/fdw/setup_fdw.sql
|
||||
-- =============================================================================
|
||||
|
||||
-- =============================================================================
|
||||
-- FDW 映射配置(生产环境)— 在 zqyy_app 数据库中执行
|
||||
-- 用途:通过 postgres_fdw 将 etl_feiqiu.app schema 只读映射到 zqyy_app,
|
||||
-- 使业务后端无需直接连接 ETL 数据库即可读取汇总/维度数据。
|
||||
-- 前提:etl_feiqiu 数据库已部署 app schema 及 app_reader 角色(见 app.sql)
|
||||
-- 测试环境版本:setup_fdw_test.sql(指向 test_etl_feiqiu / test_zqyy_app)
|
||||
-- Requirements: 8.3, 8.4, 8.5
|
||||
-- =============================================================================
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- 1. 安装 postgres_fdw 扩展
|
||||
-- -----------------------------------------------------------------------------
|
||||
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- 2. 创建外部服务器(指向 etl_feiqiu 数据库)
|
||||
-- 部署时按实际环境替换 host / port
|
||||
-- -----------------------------------------------------------------------------
|
||||
CREATE SERVER IF NOT EXISTS etl_feiqiu_server
|
||||
FOREIGN DATA WRAPPER postgres_fdw
|
||||
OPTIONS (host 'localhost', dbname 'etl_feiqiu', port '5432');
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- 3. 创建用户映射(只读角色)
|
||||
-- app_user = zqyy_app 侧的应用连接角色
|
||||
-- app_reader = etl_feiqiu 侧的只读角色(在 app.sql 中已创建)
|
||||
-- 密码占位符 '***',部署时替换为真实凭据
|
||||
-- -----------------------------------------------------------------------------
|
||||
CREATE USER MAPPING IF NOT EXISTS FOR app_user
|
||||
SERVER etl_feiqiu_server
|
||||
OPTIONS (user 'app_reader', password '***');
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- 4. 创建目标 schema(存放外部表)
|
||||
-- -----------------------------------------------------------------------------
|
||||
CREATE SCHEMA IF NOT EXISTS fdw_etl;
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- 5. 导入 etl_feiqiu.app schema 的所有外部表到 fdw_etl
|
||||
-- 映射为只读,zqyy_app 不存储 ETL 数据副本(Requirements 8.4)
|
||||
-- -----------------------------------------------------------------------------
|
||||
IMPORT FOREIGN SCHEMA app
|
||||
FROM SERVER etl_feiqiu_server
|
||||
INTO fdw_etl;
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- 6. 授权:允许 app_user 访问 fdw_etl schema 及其外部表
|
||||
-- -----------------------------------------------------------------------------
|
||||
GRANT USAGE ON SCHEMA fdw_etl TO app_user;
|
||||
GRANT SELECT ON ALL TABLES IN SCHEMA fdw_etl TO app_user;
|
||||
|
||||
-- 未来新导入的外部表自动授权
|
||||
ALTER DEFAULT PRIVILEGES IN SCHEMA fdw_etl GRANT SELECT ON TABLES TO app_user;
|
||||
|
||||
|
||||
-- =============================================================================
|
||||
-- 回滚脚本(按逆序执行)
|
||||
-- =============================================================================
|
||||
-- ALTER DEFAULT PRIVILEGES IN SCHEMA fdw_etl REVOKE SELECT ON TABLES FROM app_user;
|
||||
-- REVOKE SELECT ON ALL TABLES IN SCHEMA fdw_etl FROM app_user;
|
||||
-- REVOKE USAGE ON SCHEMA fdw_etl FROM app_user;
|
||||
-- DROP SCHEMA IF EXISTS fdw_etl CASCADE;
|
||||
-- DROP USER MAPPING IF EXISTS FOR app_user SERVER etl_feiqiu_server;
|
||||
-- DROP SERVER IF EXISTS etl_feiqiu_server CASCADE;
|
||||
-- DROP EXTENSION IF EXISTS postgres_fdw;
|
||||
192
docs/database/ddl/zqyy_app__public.sql
Normal file
192
docs/database/ddl/zqyy_app__public.sql
Normal file
@@ -0,0 +1,192 @@
|
||||
-- =============================================================================
|
||||
-- zqyy_app / public(小程序业务表)
|
||||
-- 生成日期:2026-02-23
|
||||
-- 来源:测试库(通过脚本自动导出)
|
||||
-- =============================================================================
|
||||
|
||||
CREATE SCHEMA IF NOT EXISTS public;
|
||||
|
||||
-- 序列
|
||||
CREATE SEQUENCE IF NOT EXISTS public.admin_users_id_seq AS integer;
|
||||
CREATE SEQUENCE IF NOT EXISTS public.approvals_id_seq AS bigint;
|
||||
CREATE SEQUENCE IF NOT EXISTS public.member_birthday_manual_id_seq AS bigint;
|
||||
CREATE SEQUENCE IF NOT EXISTS public.permissions_id_seq AS integer;
|
||||
CREATE SEQUENCE IF NOT EXISTS public.roles_id_seq AS integer;
|
||||
CREATE SEQUENCE IF NOT EXISTS public.tasks_id_seq AS bigint;
|
||||
CREATE SEQUENCE IF NOT EXISTS public.users_id_seq AS bigint;
|
||||
|
||||
-- 表
|
||||
CREATE TABLE public.admin_users (
|
||||
id integer DEFAULT nextval('admin_users_id_seq'::regclass) NOT NULL,
|
||||
username character varying(64) NOT NULL,
|
||||
password_hash character varying(256) NOT NULL,
|
||||
display_name character varying(128),
|
||||
site_id bigint NOT NULL,
|
||||
is_active boolean DEFAULT true,
|
||||
created_at timestamp with time zone DEFAULT now(),
|
||||
updated_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE public.approvals (
|
||||
id bigint DEFAULT nextval('approvals_id_seq'::regclass) NOT NULL,
|
||||
task_id bigint,
|
||||
approver_id bigint,
|
||||
status text DEFAULT 'pending'::text,
|
||||
comment text,
|
||||
site_id bigint NOT NULL,
|
||||
created_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE public.member_birthday_manual (
|
||||
id bigint DEFAULT nextval('member_birthday_manual_id_seq'::regclass) NOT NULL,
|
||||
member_id bigint NOT NULL,
|
||||
birthday_value date NOT NULL,
|
||||
recorded_by_assistant_id bigint,
|
||||
recorded_by_name character varying(50),
|
||||
recorded_at timestamp with time zone DEFAULT now() NOT NULL,
|
||||
source character varying(20) DEFAULT 'assistant'::character varying,
|
||||
site_id bigint NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE public.permissions (
|
||||
id integer DEFAULT nextval('permissions_id_seq'::regclass) NOT NULL,
|
||||
resource text NOT NULL,
|
||||
action text NOT NULL,
|
||||
description text
|
||||
);
|
||||
|
||||
CREATE TABLE public.role_permissions (
|
||||
role_id integer NOT NULL,
|
||||
permission_id integer NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE public.roles (
|
||||
id integer DEFAULT nextval('roles_id_seq'::regclass) NOT NULL,
|
||||
name text NOT NULL,
|
||||
description text,
|
||||
site_id bigint NOT NULL,
|
||||
created_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE public.scheduled_tasks (
|
||||
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
||||
site_id bigint NOT NULL,
|
||||
name character varying(256) NOT NULL,
|
||||
task_codes _text NOT NULL,
|
||||
task_config jsonb NOT NULL,
|
||||
schedule_config jsonb NOT NULL,
|
||||
enabled boolean DEFAULT true,
|
||||
last_run_at timestamp with time zone,
|
||||
next_run_at timestamp with time zone,
|
||||
run_count integer DEFAULT 0,
|
||||
last_status character varying(20),
|
||||
created_at timestamp with time zone DEFAULT now(),
|
||||
updated_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE public.task_execution_log (
|
||||
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
||||
queue_id uuid,
|
||||
site_id bigint NOT NULL,
|
||||
task_codes _text NOT NULL,
|
||||
status character varying(20) NOT NULL,
|
||||
started_at timestamp with time zone NOT NULL,
|
||||
finished_at timestamp with time zone,
|
||||
exit_code integer,
|
||||
duration_ms integer,
|
||||
command text,
|
||||
output_log text,
|
||||
error_log text,
|
||||
summary jsonb,
|
||||
created_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE public.task_queue (
|
||||
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
||||
site_id bigint NOT NULL,
|
||||
config jsonb NOT NULL,
|
||||
status character varying(20) DEFAULT 'pending'::character varying NOT NULL,
|
||||
"position" integer DEFAULT 0 NOT NULL,
|
||||
created_at timestamp with time zone DEFAULT now(),
|
||||
started_at timestamp with time zone,
|
||||
finished_at timestamp with time zone,
|
||||
exit_code integer,
|
||||
error_message text
|
||||
);
|
||||
|
||||
CREATE TABLE public.tasks (
|
||||
id bigint DEFAULT nextval('tasks_id_seq'::regclass) NOT NULL,
|
||||
title text NOT NULL,
|
||||
description text,
|
||||
status text DEFAULT 'pending'::text,
|
||||
assignee_id bigint,
|
||||
creator_id bigint,
|
||||
site_id bigint NOT NULL,
|
||||
created_at timestamp with time zone DEFAULT now(),
|
||||
updated_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE public.user_roles (
|
||||
user_id bigint NOT NULL,
|
||||
role_id integer NOT NULL,
|
||||
site_id bigint NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE public.users (
|
||||
id bigint DEFAULT nextval('users_id_seq'::regclass) NOT NULL,
|
||||
wx_openid text,
|
||||
mobile text,
|
||||
nickname text,
|
||||
status integer DEFAULT 1,
|
||||
site_id bigint NOT NULL,
|
||||
created_at timestamp with time zone DEFAULT now(),
|
||||
updated_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
-- 约束(主键 / 唯一 / 外键)
|
||||
ALTER TABLE admin_users ADD CONSTRAINT admin_users_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE admin_users ADD CONSTRAINT admin_users_username_key UNIQUE (username);
|
||||
ALTER TABLE approvals ADD CONSTRAINT approvals_approver_id_fkey FOREIGN KEY (approver_id) REFERENCES users(id);
|
||||
ALTER TABLE approvals ADD CONSTRAINT approvals_task_id_fkey FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE;
|
||||
ALTER TABLE approvals ADD CONSTRAINT approvals_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE member_birthday_manual ADD CONSTRAINT member_birthday_manual_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE member_birthday_manual ADD CONSTRAINT uk_member_birthday_manual UNIQUE (member_id, recorded_by_assistant_id);
|
||||
ALTER TABLE permissions ADD CONSTRAINT permissions_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE permissions ADD CONSTRAINT permissions_resource_action_key UNIQUE (resource, action);
|
||||
ALTER TABLE role_permissions ADD CONSTRAINT role_permissions_permission_id_fkey FOREIGN KEY (permission_id) REFERENCES permissions(id) ON DELETE CASCADE;
|
||||
ALTER TABLE role_permissions ADD CONSTRAINT role_permissions_role_id_fkey FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE;
|
||||
ALTER TABLE role_permissions ADD CONSTRAINT role_permissions_pkey PRIMARY KEY (role_id, permission_id);
|
||||
ALTER TABLE roles ADD CONSTRAINT roles_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE roles ADD CONSTRAINT roles_name_key UNIQUE (name);
|
||||
ALTER TABLE scheduled_tasks ADD CONSTRAINT scheduled_tasks_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE task_execution_log ADD CONSTRAINT task_execution_log_queue_id_fkey FOREIGN KEY (queue_id) REFERENCES task_queue(id);
|
||||
ALTER TABLE task_execution_log ADD CONSTRAINT task_execution_log_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE task_queue ADD CONSTRAINT task_queue_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE tasks ADD CONSTRAINT tasks_assignee_id_fkey FOREIGN KEY (assignee_id) REFERENCES users(id);
|
||||
ALTER TABLE tasks ADD CONSTRAINT tasks_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES users(id);
|
||||
ALTER TABLE tasks ADD CONSTRAINT tasks_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE user_roles ADD CONSTRAINT user_roles_role_id_fkey FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE;
|
||||
ALTER TABLE user_roles ADD CONSTRAINT user_roles_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
||||
ALTER TABLE user_roles ADD CONSTRAINT user_roles_pkey PRIMARY KEY (user_id, role_id);
|
||||
ALTER TABLE users ADD CONSTRAINT users_pkey PRIMARY KEY (id);
|
||||
ALTER TABLE users ADD CONSTRAINT users_wx_openid_key UNIQUE (wx_openid);
|
||||
|
||||
-- 索引
|
||||
CREATE INDEX idx_admin_users_site ON public.admin_users USING btree (site_id);
|
||||
CREATE INDEX idx_approvals_site_id ON public.approvals USING btree (site_id);
|
||||
CREATE INDEX idx_approvals_task_id ON public.approvals USING btree (task_id);
|
||||
CREATE INDEX idx_mbd_member ON public.member_birthday_manual USING btree (member_id);
|
||||
CREATE INDEX idx_mbd_site_id ON public.member_birthday_manual USING btree (site_id);
|
||||
CREATE INDEX idx_roles_site_id ON public.roles USING btree (site_id);
|
||||
CREATE INDEX idx_scheduled_tasks_next_run ON public.scheduled_tasks USING btree (next_run_at) WHERE (enabled = true);
|
||||
CREATE INDEX idx_scheduled_tasks_site ON public.scheduled_tasks USING btree (site_id);
|
||||
CREATE INDEX idx_execution_log_site_started ON public.task_execution_log USING btree (site_id, started_at DESC);
|
||||
CREATE INDEX idx_task_queue_site_position ON public.task_queue USING btree (site_id, "position") WHERE ((status)::text = 'pending'::text);
|
||||
CREATE INDEX idx_task_queue_status ON public.task_queue USING btree (status);
|
||||
CREATE INDEX idx_tasks_assignee_id ON public.tasks USING btree (assignee_id);
|
||||
CREATE INDEX idx_tasks_site_id ON public.tasks USING btree (site_id);
|
||||
CREATE INDEX idx_tasks_status ON public.tasks USING btree (status);
|
||||
CREATE INDEX idx_user_roles_site_id ON public.user_roles USING btree (site_id);
|
||||
CREATE INDEX idx_users_mobile ON public.users USING btree (mobile);
|
||||
CREATE INDEX idx_users_site_id ON public.users USING btree (site_id);
|
||||
|
||||
Reference in New Issue
Block a user