-- Document Center: template folders, folder-level variable mappings, and generation targets -- Safe to run multiple times (CREATE TABLE IF NOT EXISTS, CREATE INDEX IF NOT EXISTS). CREATE TABLE IF NOT EXISTS "document_template_folders" ( "id" serial PRIMARY KEY NOT NULL, "organization_id" integer NOT NULL, "parent_id" integer, "name" varchar(100) NOT NULL, "position" integer DEFAULT 0 NOT NULL, "created_at" timestamp DEFAULT now(), "updated_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "document_folder_mappings" ( "id" serial PRIMARY KEY NOT NULL, "folder_id" integer NOT NULL, "code" varchar(100) NOT NULL, "label" varchar(200), "description" text, "source" varchar(30) NOT NULL, "mapping" jsonb DEFAULT '{}'::jsonb, "default_value" text, "sort_order" integer DEFAULT 0, "created_at" timestamp DEFAULT now() ); --> statement-breakpoint -- Add folder and target columns to document_templates ALTER TABLE "document_templates" ADD COLUMN IF NOT EXISTS "folder_id" integer; ALTER TABLE "document_templates" ADD COLUMN IF NOT EXISTS "target_form_id" integer; ALTER TABLE "document_templates" ADD COLUMN IF NOT EXISTS "target_field_code" varchar(100); ALTER TABLE "document_templates" ADD COLUMN IF NOT EXISTS "target_task_source" varchar(20) DEFAULT 'current'; ALTER TABLE "document_templates" ADD COLUMN IF NOT EXISTS "target_source_field_code" varchar(100); -- Foreign keys ALTER TABLE "document_template_folders" DROP CONSTRAINT IF EXISTS "document_template_folders_organization_id_organizations_id_fk"; ALTER TABLE "document_template_folders" ADD CONSTRAINT "document_template_folders_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE cascade; -- parent_id не объявляем как FK, чтобы избежать циклической ссылки в Drizzle ORM (как у других деревьев: form_folders, directory_folders, roles) ALTER TABLE "document_folder_mappings" DROP CONSTRAINT IF EXISTS "document_folder_mappings_folder_id_document_template_folders_id_fk"; ALTER TABLE "document_folder_mappings" ADD CONSTRAINT "document_folder_mappings_folder_id_document_template_folders_id_fk" FOREIGN KEY ("folder_id") REFERENCES "document_template_folders"("id") ON DELETE cascade; ALTER TABLE "document_templates" DROP CONSTRAINT IF EXISTS "document_templates_folder_id_document_template_folders_id_fk"; ALTER TABLE "document_templates" ADD CONSTRAINT "document_templates_folder_id_document_template_folders_id_fk" FOREIGN KEY ("folder_id") REFERENCES "document_template_folders"("id") ON DELETE set null; ALTER TABLE "document_templates" DROP CONSTRAINT IF EXISTS "document_templates_target_form_id_forms_id_fk"; ALTER TABLE "document_templates" ADD CONSTRAINT "document_templates_target_form_id_forms_id_fk" FOREIGN KEY ("target_form_id") REFERENCES "forms"("id") ON DELETE set null; -- Indexes CREATE INDEX IF NOT EXISTS "document_template_folders_org_parent_idx" ON "document_template_folders" ("organization_id", "parent_id"); CREATE INDEX IF NOT EXISTS "document_template_folders_position_idx" ON "document_template_folders" ("position"); CREATE UNIQUE INDEX IF NOT EXISTS "document_folder_mappings_code_unique" ON "document_folder_mappings" ("folder_id", "code"); CREATE INDEX IF NOT EXISTS "document_templates_org_folder_idx" ON "document_templates" ("organization_id", "folder_id");