-- CreateEnum CREATE TYPE "ScenarioDestinationMode" AS ENUM ('fixed', 'payload'); -- DropForeignKey ALTER TABLE "reference_field_mappings" DROP CONSTRAINT "reference_field_mappings_subsidiary_code_fkey"; -- AlterTable ALTER TABLE "replication_attempts" DROP COLUMN "subsidiary", ADD COLUMN "destination_database" TEXT NOT NULL, ADD COLUMN "origin_database" TEXT NOT NULL, ADD COLUMN "payload_sent" JSONB, ADD COLUMN "scenario_id" INTEGER NOT NULL; -- DropTable DROP TABLE "mapped_fields"; -- DropTable DROP TABLE "reference_field_mappings"; -- DropTable DROP TABLE "replication_rules"; -- DropTable DROP TABLE "subsidiaries"; -- DropEnum DROP TYPE "ReplicationRuleType"; -- CreateTable CREATE TABLE "databases" ( "id" SERIAL NOT NULL, "code" TEXT NOT NULL, "name" TEXT NOT NULL, "base_url" TEXT NOT NULL, "company_db" TEXT NOT NULL, "username" TEXT NOT NULL, "password" TEXT NOT NULL, "active" BOOLEAN NOT NULL DEFAULT true, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "databases_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "scenarios" ( "id" SERIAL NOT NULL, "code" TEXT NOT NULL, "name" TEXT NOT NULL, "business_object" TEXT NOT NULL, "origin_database_code" TEXT NOT NULL, "destination_mode" "ScenarioDestinationMode" NOT NULL, "key_field" TEXT NOT NULL, "routing_field" TEXT, "filter_field" TEXT, "filter_operator" TEXT, "filter_value" TEXT, "active" BOOLEAN NOT NULL DEFAULT true, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "scenarios_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "scenario_destinations" ( "id" SERIAL NOT NULL, "scenario_id" INTEGER NOT NULL, "database_code" TEXT NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "scenario_destinations_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "scenario_mapped_fields" ( "id" SERIAL NOT NULL, "scenario_id" INTEGER NOT NULL, "field_name" TEXT NOT NULL, "active" BOOLEAN NOT NULL DEFAULT true, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "scenario_mapped_fields_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "scenario_value_mappings" ( "id" SERIAL NOT NULL, "scenario_id" INTEGER NOT NULL, "field_name" TEXT NOT NULL, "database_code" TEXT NOT NULL, "source_value" TEXT NOT NULL, "target_value" TEXT NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "scenario_value_mappings_pkey" PRIMARY KEY ("id") ); -- CreateIndex CREATE UNIQUE INDEX "databases_code_key" ON "databases"("code"); -- CreateIndex CREATE UNIQUE INDEX "scenarios_code_key" ON "scenarios"("code"); -- CreateIndex CREATE UNIQUE INDEX "scenario_destinations_scenario_id_database_code_key" ON "scenario_destinations"("scenario_id", "database_code"); -- CreateIndex CREATE UNIQUE INDEX "scenario_mapped_fields_scenario_id_field_name_key" ON "scenario_mapped_fields"("scenario_id", "field_name"); -- CreateIndex CREATE UNIQUE INDEX "scenario_value_mappings_scenario_id_field_name_database_cod_key" ON "scenario_value_mappings"("scenario_id", "field_name", "database_code", "source_value"); -- CreateIndex CREATE INDEX "replication_attempts_event_id_idx" ON "replication_attempts"("event_id"); -- CreateIndex CREATE INDEX "replication_attempts_scenario_id_idx" ON "replication_attempts"("scenario_id"); -- CreateIndex CREATE INDEX "replication_attempts_attempted_at_idx" ON "replication_attempts"("attempted_at"); -- AddForeignKey ALTER TABLE "scenarios" ADD CONSTRAINT "scenarios_origin_database_code_fkey" FOREIGN KEY ("origin_database_code") REFERENCES "databases"("code") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "scenario_destinations" ADD CONSTRAINT "scenario_destinations_scenario_id_fkey" FOREIGN KEY ("scenario_id") REFERENCES "scenarios"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "scenario_destinations" ADD CONSTRAINT "scenario_destinations_database_code_fkey" FOREIGN KEY ("database_code") REFERENCES "databases"("code") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "scenario_mapped_fields" ADD CONSTRAINT "scenario_mapped_fields_scenario_id_fkey" FOREIGN KEY ("scenario_id") REFERENCES "scenarios"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "scenario_value_mappings" ADD CONSTRAINT "scenario_value_mappings_scenario_id_fkey" FOREIGN KEY ("scenario_id") REFERENCES "scenarios"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "scenario_value_mappings" ADD CONSTRAINT "scenario_value_mappings_database_code_fkey" FOREIGN KEY ("database_code") REFERENCES "databases"("code") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "replication_attempts" ADD CONSTRAINT "replication_attempts_scenario_id_fkey" FOREIGN KEY ("scenario_id") REFERENCES "scenarios"("id") ON DELETE RESTRICT ON UPDATE CASCADE;