CREATE TABLE "app_notification_dispatches" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "event_id" text NOT NULL, "template_id" text, "entity_type" text NOT NULL, "entity_id" text NOT NULL, "channel" text NOT NULL, "provider_key" text NOT NULL, "status" text DEFAULT 'pending' NOT NULL, "recipient_to" jsonb NOT NULL, "recipient_cc" jsonb, "recipient_bcc" jsonb, "reply_to" text, "subject" text NOT NULL, "body_text" text NOT NULL, "body_html" text, "attachments" jsonb, "recipient_user_id" text, "recipient_address" text, "requested_by" text NOT NULL, "metadata" jsonb, "last_error" text, "retry_count" integer DEFAULT 0 NOT NULL, "max_retry_count" integer DEFAULT 3 NOT NULL, "last_attempt_at" timestamp with time zone, "sent_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "crm_document_libraries" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "code" text NOT NULL, "name" text NOT NULL, "document_type" text NOT NULL, "description" text, "brand" text NOT NULL, "language" text NOT NULL, "product_type" text NOT NULL, "status" text DEFAULT 'active' NOT NULL, "is_active" boolean DEFAULT true NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone, "created_by" text NOT NULL, "updated_by" text NOT NULL ); --> statement-breakpoint CREATE TABLE "crm_document_library_versions" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "library_id" text NOT NULL, "version" text NOT NULL, "file_name" text NOT NULL, "storage_provider" text NOT NULL, "storage_key" text NOT NULL, "mime_type" text NOT NULL, "file_size" integer NOT NULL, "checksum" text NOT NULL, "page_count" integer NOT NULL, "status" text DEFAULT 'draft' NOT NULL, "is_active" boolean DEFAULT false NOT NULL, "published_at" timestamp with time zone, "published_by" text, "archived_at" timestamp with time zone, "archived_by" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone, "created_by" text NOT NULL ); --> statement-breakpoint DROP INDEX "document_sequences_org_doc_period_branch_idx";--> statement-breakpoint ALTER TABLE "app_notification_templates" ADD COLUMN "code" text DEFAULT '' NOT NULL;--> statement-breakpoint ALTER TABLE "app_notification_templates" ADD COLUMN "name" text DEFAULT '' NOT NULL;--> statement-breakpoint ALTER TABLE "app_notification_templates" ADD COLUMN "subject_template" text;--> statement-breakpoint ALTER TABLE "app_notification_templates" ADD COLUMN "body_format" text DEFAULT 'plain' NOT NULL;--> statement-breakpoint ALTER TABLE "app_notification_templates" ADD COLUMN "language" text DEFAULT 'th' NOT NULL;--> statement-breakpoint ALTER TABLE "app_notification_templates" ADD COLUMN "variables" jsonb;--> statement-breakpoint ALTER TABLE "app_notification_templates" ADD COLUMN "version" integer DEFAULT 1 NOT NULL;--> statement-breakpoint ALTER TABLE "document_sequences" ADD COLUMN "product_type" text DEFAULT 'all' NOT NULL;--> statement-breakpoint ALTER TABLE "document_sequences" ADD COLUMN "format" text DEFAULT '{prefix}{period}-{running}' NOT NULL;--> statement-breakpoint ALTER TABLE "document_sequences" ADD COLUMN "reset_policy" text DEFAULT 'period' NOT NULL;--> statement-breakpoint CREATE UNIQUE INDEX "app_notification_dispatches_org_event_channel_recipient_idx" ON "app_notification_dispatches" USING btree ("organization_id","event_id","channel","recipient_address");--> statement-breakpoint CREATE UNIQUE INDEX "crm_document_libraries_org_code_idx" ON "crm_document_libraries" USING btree ("organization_id","code");--> statement-breakpoint CREATE UNIQUE INDEX "crm_document_library_versions_library_version_idx" ON "crm_document_library_versions" USING btree ("library_id","version");--> statement-breakpoint CREATE UNIQUE INDEX "document_sequences_org_branch_product_doc_period_idx" ON "document_sequences" USING btree ("organization_id","branch_id","product_type","document_type","period");