ALTER TABLE "crm_document_template_versions" ADD COLUMN "metadata_json" jsonb;
--> statement-breakpoint
UPDATE "crm_document_template_versions"
SET
  "metadata_json" = COALESCE("metadata_json", "schema_json" -> '__templateManagement'),
  "schema_json" = CASE
    WHEN "schema_json" ? '__templateManagement'
      THEN "schema_json" - '__templateManagement'
    ELSE "schema_json"
  END
WHERE "schema_json" ? '__templateManagement';
