CREATE TABLE dms_documents (tenant_id text NOT NULL,id uuid PRIMARY KEY,main_document_id uuid,folder_id uuid,name text,document_no text,document_type text,responsible_dept_id text,created_by text,created_at timestamptz DEFAULT now()); CREATE TABLE dms_version_states (tenant_id text,document_id uuid,file_version_id uuid PRIMARY KEY,lifecycle_status text, effective_from timestamptz,effective_to timestamptz,approved_at timestamptz DEFAULT '2020-01-01Z',approved_by text DEFAULT 'actor',archived_at timestamptz DEFAULT '2020-01-01Z', approval_source_type text DEFAULT 'WORKFLOW',approval_source_id text,workflow_flow_id text,workflow_task_id text,workflow_file_type text, status_reason text,status_changed_by text,status_changed_at timestamptz,updated_at timestamptz,created_at timestamptz DEFAULT now(),revision bigint DEFAULT 0, review_cycle_months int,last_reviewed_on date,next_review_on date,CHECK(effective_to IS NULL OR effective_to>effective_from)); CREATE UNIQUE INDEX one_effective ON dms_version_states(tenant_id,document_id) WHERE lifecycle_status='EFFECTIVE'; CREATE TABLE dms_file_versions (tenant_id text,id uuid PRIMARY KEY,document_id uuid,version_no bigint,version_label text,file_name text,mime_type text,size_bytes bigint,checksum_sha256 text,prepared_by text,prepared_on date,created_at timestamptz DEFAULT now()); CREATE TABLE dms_wenjian_ziyuan (f_tenant_id text,f_id text PRIMARY KEY,dangqian_banben_id text,wenjian_zhongxin_banben_id text,wenjian_zhongxin_wenjian_id text,biz_status text,jinyong_laiyuan text, youxiao_qi_zhi timestamp,f_last_modify_user_id text,f_last_modify_time timestamp,f_delete_mark int); CREATE TABLE dms_wenjian_banben (f_tenant_id text,f_id text PRIMARY KEY,wenjian_id text,wenjian_zhongxin_banben_id text,wenjian_zhongxin_wenjian_id text,biz_status text,f_delete_mark int,f_last_modify_user_id text,f_last_modify_time timestamp); CREATE TABLE dms_wenjian_guanxi (f_tenant_id text,zhu_wenjian_id text,guanlian_wenjian_id text,biz_status text,shengxiao_shijian timestamp,shixiao_shijian timestamp, f_last_modify_user_id text,f_last_modify_time timestamp,f_delete_mark int,CONSTRAINT ck_dms_guanxi_interval CHECK(shixiao_shijian IS NULL OR (shengxiao_shijian IS NOT NULL AND shixiao_shijian>shengxiao_shijian)), CONSTRAINT ck_dms_guanxi_interval_state CHECK((biz_status='effective' AND shixiao_shijian IS NULL) OR (biz_status='disabled' AND shixiao_shijian IS NOT NULL))); CREATE TABLE dms_approved_file_batches (tenant_id text,id uuid PRIMARY KEY,task_id text,flow_id text,audit_id text,audit_type text,actor_id text,approved_at timestamptz, lease_token text,lease_until timestamptz,status text,attempts int DEFAULT 0,last_error text,scheduled_at timestamptz); CREATE TABLE dms_approved_file_items (tenant_id text,id uuid PRIMARY KEY,batch_id uuid,change_type text,source_row_id text,file_id text,business_version_id text, source_file_id text,source_version_id text,document_id uuid,file_version_id uuid,folder_id uuid,requested_at timestamp,scheduled_at timestamptz,status text,executed_at timestamptz, CHECK((status='WAITING' AND executed_at IS NULL) OR (status IN ('CENTER_APPLIED','APPLIED') AND executed_at>=scheduled_at))); CREATE TABLE dms_file_operation_occupancies (tenant_id text,task_id text,application_id text,file_id text,folder_id text,operation text,active boolean,released_at timestamptz); CREATE TABLE dms_wenjian_shixiao_shenqing (f_tenant_id text,f_id text PRIMARY KEY,f_flow_task_id text,f_flow_id text,f_flow_state int,f_delete_mark int); CREATE TABLE dms_wenjian_shixiao_shenqing_zixiang (f_tenant_id text,f_id text PRIMARY KEY,f_foreign_id text,wenjian_id text,wenjian_banben_id text, laiyuan_wenjian_id text,laiyuan_wenjian_banben_id text,shixiao_shengxiao_shijian timestamp,f_delete_mark int); CREATE TABLE workflow_task (f_tenant_id text,f_id text PRIMARY KEY,f_status int,f_flow_id text,f_template_id text,f_end_time timestamptz,f_delete_mark int); CREATE TABLE dms_file_expiry_cleanup (tenant_id text,file_version_id uuid,search_task_id bigint,attempts int DEFAULT 0,next_attempt_at timestamptz DEFAULT now(), completed_at timestamptz,last_error text,updated_at timestamptz DEFAULT now(),PRIMARY KEY(tenant_id,file_version_id)); CREATE TABLE dms_folders (tenant_id text,id uuid,parent_id uuid,f_delete_mark int); CREATE TABLE dms_file_permissions (tenant_id text,action_code text,revoked_at timestamptz,valid_from timestamptz,valid_to timestamptz,max_use_count bigint,used_count bigint, principal_type text,principal_id text,include_child_orgs boolean,resource_type text,resource_id uuid,resource_scope text); CREATE TABLE dms_access_exclusions (tenant_id text,user_id text,action_code text,revoked_at timestamptz,valid_from timestamptz,valid_to timestamptz,resource_type text,resource_id uuid);