From 154adb2cd135228edf50f0876cb0e47eab4ca3df Mon Sep 17 00:00:00 2001 From: Ruslan Bakiev <572431+veikab@users.noreply.github.com> Date: Fri, 5 Jun 2026 12:37:09 +0700 Subject: [PATCH] Create missing orders tables --- .../migration.sql | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 prisma/migrations/1_create_missing_order_tables/migration.sql diff --git a/prisma/migrations/1_create_missing_order_tables/migration.sql b/prisma/migrations/1_create_missing_order_tables/migration.sql new file mode 100644 index 0000000..8a05241 --- /dev/null +++ b/prisma/migrations/1_create_missing_order_tables/migration.sql @@ -0,0 +1,163 @@ +-- CreateSchema +CREATE SCHEMA IF NOT EXISTS "public"; + +-- CreateTable +CREATE TABLE IF NOT EXISTS "orders_tariff_reference" ( + "id" SERIAL NOT NULL, + "uuid" TEXT NOT NULL, + "team_uuid" VARCHAR(100) NOT NULL, + "name" VARCHAR(255) NOT NULL, + "status" VARCHAR(20) NOT NULL DEFAULT 'active', + "operation_code" VARCHAR(50), + "incoterms_code" VARCHAR(20), + "transport_type_code" VARCHAR(50), + "tare_type_code" VARCHAR(50), + "source_country_code" VARCHAR(10), + "destination_country_code" VARCHAR(10), + "source_hub_uuid" VARCHAR(100), + "destination_hub_uuid" VARCHAR(100), + "min_weight_kg" DECIMAL(12,2), + "max_weight_kg" DECIMAL(12,2), + "min_volume_cbm" DECIMAL(12,3), + "max_volume_cbm" DECIMAL(12,3), + "min_distance_km" INTEGER, + "max_distance_km" INTEGER, + "amount_usd" DECIMAL(12,2) NOT NULL, + "min_price_usd" DECIMAL(12,2), + "eta_days" INTEGER, + "priority" INTEGER NOT NULL DEFAULT 100, + "currency" VARCHAR(10) NOT NULL DEFAULT 'USD', + "dmn_expression" TEXT, + "notes" TEXT, + "created_by_user_id" VARCHAR(255), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + CONSTRAINT "orders_tariff_reference_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE IF NOT EXISTS "orders_quotation" ( + "id" SERIAL NOT NULL, + "uuid" TEXT NOT NULL, + "team_uuid" VARCHAR(100) NOT NULL, + "created_by_user_id" VARCHAR(255), + "title" VARCHAR(255) NOT NULL DEFAULT 'Quotation', + "status" VARCHAR(30) NOT NULL DEFAULT 'draft', + "operation_code" VARCHAR(50), + "incoterms_code" VARCHAR(20), + "transport_type_code" VARCHAR(50), + "tare_type_code" VARCHAR(50), + "source_country_code" VARCHAR(10), + "source_hub_uuid" VARCHAR(100), + "source_location_uuid" VARCHAR(100), + "source_location_name" VARCHAR(255), + "source_latitude" DOUBLE PRECISION, + "source_longitude" DOUBLE PRECISION, + "destination_country_code" VARCHAR(10), + "destination_hub_uuid" VARCHAR(100), + "destination_location_uuid" VARCHAR(100), + "destination_location_name" VARCHAR(255), + "destination_latitude" DOUBLE PRECISION, + "destination_longitude" DOUBLE PRECISION, + "chargeable_weight_kg" DECIMAL(12,2), + "gross_weight_kg" DECIMAL(12,2), + "volume_cbm" DECIMAL(12,3), + "units_count" INTEGER, + "route_distance_km" INTEGER, + "selected_tariff_id" INTEGER, + "tariff_match_summary" TEXT, + "tariff_snapshot" TEXT, + "total_amount" DECIMAL(12,2) NOT NULL DEFAULT 0, + "currency" VARCHAR(10) NOT NULL DEFAULT 'USD', + "eta_days" INTEGER, + "notes" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + CONSTRAINT "orders_quotation_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE IF NOT EXISTS "orders_quotation_change" ( + "id" SERIAL NOT NULL, + "uuid" TEXT NOT NULL, + "quotation_id" INTEGER NOT NULL, + "actor_user_id" VARCHAR(255), + "actor_label" VARCHAR(255), + "source" VARCHAR(50) NOT NULL, + "summary" TEXT, + "payload_json" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "orders_quotation_change_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE IF NOT EXISTS "orders_order" ( + "id" SERIAL NOT NULL, + "uuid" TEXT NOT NULL, + "team_uuid" VARCHAR(100) NOT NULL, + "quotation_id" INTEGER, + "created_by_user_id" VARCHAR(255), + "name" VARCHAR(255) NOT NULL, + "status" VARCHAR(30) NOT NULL DEFAULT 'draft', + "total_amount" DECIMAL(12,2) NOT NULL DEFAULT 0, + "currency" VARCHAR(10) NOT NULL DEFAULT 'USD', + "source_location_uuid" VARCHAR(100), + "source_location_name" VARCHAR(255), + "source_country_code" VARCHAR(10), + "source_latitude" DOUBLE PRECISION, + "source_longitude" DOUBLE PRECISION, + "destination_location_uuid" VARCHAR(100), + "destination_location_name" VARCHAR(255), + "destination_country_code" VARCHAR(10), + "destination_latitude" DOUBLE PRECISION, + "destination_longitude" DOUBLE PRECISION, + "eta_days" INTEGER, + "notes" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + CONSTRAINT "orders_order_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX IF NOT EXISTS "orders_tariff_reference_uuid_key" ON "orders_tariff_reference"("uuid"); +CREATE INDEX IF NOT EXISTS "orders_tariff_reference_team_uuid_status_priority_idx" ON "orders_tariff_reference"("team_uuid", "status", "priority"); +CREATE INDEX IF NOT EXISTS "orders_tariff_reference_team_uuid_source_country_code_desti_idx" ON "orders_tariff_reference"("team_uuid", "source_country_code", "destination_country_code"); +CREATE UNIQUE INDEX IF NOT EXISTS "orders_quotation_uuid_key" ON "orders_quotation"("uuid"); +CREATE INDEX IF NOT EXISTS "orders_quotation_team_uuid_status_created_at_idx" ON "orders_quotation"("team_uuid", "status", "created_at"); +CREATE INDEX IF NOT EXISTS "orders_quotation_team_uuid_source_country_code_destination__idx" ON "orders_quotation"("team_uuid", "source_country_code", "destination_country_code"); +CREATE UNIQUE INDEX IF NOT EXISTS "orders_quotation_change_uuid_key" ON "orders_quotation_change"("uuid"); +CREATE INDEX IF NOT EXISTS "orders_quotation_change_quotation_id_created_at_idx" ON "orders_quotation_change"("quotation_id", "created_at"); +CREATE UNIQUE INDEX IF NOT EXISTS "orders_order_uuid_key" ON "orders_order"("uuid"); +CREATE UNIQUE INDEX IF NOT EXISTS "orders_order_quotation_id_key" ON "orders_order"("quotation_id"); +CREATE INDEX IF NOT EXISTS "orders_order_team_uuid_created_at_idx" ON "orders_order"("team_uuid", "created_at"); + +-- AddForeignKey +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint WHERE conname = 'orders_quotation_selected_tariff_id_fkey' + ) THEN + ALTER TABLE "orders_quotation" + ADD CONSTRAINT "orders_quotation_selected_tariff_id_fkey" + FOREIGN KEY ("selected_tariff_id") REFERENCES "orders_tariff_reference"("id") + ON DELETE SET NULL ON UPDATE CASCADE; + END IF; + + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint WHERE conname = 'orders_quotation_change_quotation_id_fkey' + ) THEN + ALTER TABLE "orders_quotation_change" + ADD CONSTRAINT "orders_quotation_change_quotation_id_fkey" + FOREIGN KEY ("quotation_id") REFERENCES "orders_quotation"("id") + ON DELETE CASCADE ON UPDATE CASCADE; + END IF; + + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint WHERE conname = 'orders_order_quotation_id_fkey' + ) THEN + ALTER TABLE "orders_order" + ADD CONSTRAINT "orders_order_quotation_id_fkey" + FOREIGN KEY ("quotation_id") REFERENCES "orders_quotation"("id") + ON DELETE SET NULL ON UPDATE CASCADE; + END IF; +END $$;