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
UPDATE "document_sequences"
SET
  "product_type" = CASE
    WHEN "document_type" = 'quotation' THEN 'legacy'
    ELSE 'all'
  END,
  "format" = '{prefix}{period}-{running}',
  "reset_policy" = 'period';
--> statement-breakpoint
DROP INDEX "document_sequences_org_doc_period_branch_idx";
--> 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"
);
