From 74b4f7f582cd7e5208f27a0fcfaf6d3d47c63688 Mon Sep 17 00:00:00 2001 From: Vins Date: Mon, 27 May 2024 16:55:01 +0200 Subject: [PATCH] Migration finished --- .../migration.sql | 82 ++++++------------- 1 file changed, 23 insertions(+), 59 deletions(-) diff --git a/src/common/databases/migrations/20240527091523_office_customer_relation_migration/migration.sql b/src/common/databases/migrations/20240527091523_office_customer_relation_migration/migration.sql index 1e3aac27..7fb62691 100644 --- a/src/common/databases/migrations/20240527091523_office_customer_relation_migration/migration.sql +++ b/src/common/databases/migrations/20240527091523_office_customer_relation_migration/migration.sql @@ -1,13 +1,8 @@ -/* - Warnings: +-- -- Add the office_uid column +-- ALTER TABLE customers +-- ADD COLUMN office_uid VARCHAR(255); - - Added the required column `office_uid` to the `customers` table without a default value. This is not possible if the table is not empty. - -*/ --- AlterTable --- ALTER TABLE "customers" ADD COLUMN "office_uid" VARCHAR(255) NOT NULL; - --- Set the office_uid for existing rows based on the office with "idNot" = '0000' +-- -- Set the office_uid for existing rows -- UPDATE customers -- SET office_uid = ( -- SELECT uid @@ -33,58 +28,20 @@ -- -- Drop the temporary table -- DROP TABLE customer_office_update; +-- -- Alter the column to be NOT NULL +-- ALTER TABLE customers +-- ALTER COLUMN office_uid SET NOT NULL; + -- -- Add the foreign key constraint -- ALTER TABLE customers --- ADD CONSTRAINT fk_customers_office +-- ADD CONSTRAINT customers_office_uid_fkey -- FOREIGN KEY (office_uid) REFERENCES offices(uid) -- ON DELETE CASCADE; --- -- AddForeignKey --- ALTER TABLE "customers" ADD CONSTRAINT "customers_office_uid_fkey" FOREIGN KEY ("office_uid") REFERENCES "offices"("uid") ON DELETE CASCADE ON UPDATE CASCADE; - - -/* - Warnings: - - - Added the required column `office_uid` to the `customers` table without a default value. This is not possible if the table is not empty. - -*/ --- AlterTable --- ALTER TABLE "customers" ADD COLUMN "office_uid" VARCHAR(255) NOT NULL; - --- -- AddForeignKey --- ALTER TABLE "customers" ADD CONSTRAINT "customers_office_uid_fkey" FOREIGN KEY ("office_uid") REFERENCES "offices"("uid") ON DELETE CASCADE ON UPDATE CASCADE; - --- -- Create a temporary table to store the mapping --- CREATE TEMPORARY TABLE customer_office_update AS --- SELECT c.uid AS customer_uid, of.office_uid --- FROM customers c --- JOIN "_OfficeFolderHasCustomers" ofhc ON c.uid = ofhc."A" --- JOIN office_folders of ON ofhc."B" = of.uid; - --- -- Update customers with the corresponding office_uid --- UPDATE customers --- SET office_uid = ( --- SELECT office_uid --- FROM customer_office_update --- WHERE customer_office_update.customer_uid = customers.uid --- ); - --- -- Drop the temporary table --- DROP TABLE customer_office_update; - --- Add the office_uid column +-- Add the office_uid column to the customers table ALTER TABLE customers ADD COLUMN office_uid VARCHAR(255); --- Set the office_uid for existing rows -UPDATE customers -SET office_uid = ( - SELECT uid - FROM offices - WHERE "idNot" = '0000' -); - -- Create a temporary table to store the mapping CREATE TEMPORARY TABLE customer_office_update AS SELECT c.uid AS customer_uid, of.office_uid @@ -92,17 +49,24 @@ FROM customers c JOIN "_OfficeFolderHasCustomers" ofhc ON c.uid = ofhc."A" JOIN office_folders of ON ofhc."B" = of.uid; --- Update customers with the corresponding office_uid +-- Update customers with the corresponding office_uid where a mapping exists UPDATE customers -SET office_uid = ( - SELECT office_uid - FROM customer_office_update - WHERE customer_office_update.customer_uid = customers.uid -); +SET office_uid = customer_office_update.office_uid +FROM customer_office_update +WHERE customers.uid = customer_office_update.customer_uid; -- Drop the temporary table DROP TABLE customer_office_update; +-- Set the office_uid for customers not linked to any office_folder based on the office with "idNot" = '0000' +UPDATE customers +SET office_uid = ( + SELECT uid + FROM offices + WHERE "idNot" = '0000' +) +WHERE office_uid IS NULL; + -- Alter the column to be NOT NULL ALTER TABLE customers ALTER COLUMN office_uid SET NOT NULL;