From 3289482e62f966913b3e831eb079a53d7a7845f7 Mon Sep 17 00:00:00 2001 From: Vins Date: Mon, 27 May 2024 16:48:06 +0200 Subject: [PATCH] Fixed migrations --- .../migration.sql | 114 ++++++++++++++++++ .../20240527091523_test/migration.sql | 47 -------- .../migration.sql | 2 - .../20240527144510_test/migration.sql | 5 + 4 files changed, 119 insertions(+), 49 deletions(-) create mode 100644 src/common/databases/migrations/20240527091523_office_customer_relation_migration/migration.sql delete mode 100644 src/common/databases/migrations/20240527091523_test/migration.sql delete mode 100644 src/common/databases/migrations/20240527091621_office_customer_relation/migration.sql create mode 100644 src/common/databases/migrations/20240527144510_test/migration.sql 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 new file mode 100644 index 00000000..1e3aac27 --- /dev/null +++ b/src/common/databases/migrations/20240527091523_office_customer_relation_migration/migration.sql @@ -0,0 +1,114 @@ +/* + 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; + +-- Set the office_uid for existing rows based on the office with "idNot" = '0000' +-- 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 +-- 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 foreign key constraint +-- ALTER TABLE customers +-- ADD CONSTRAINT fk_customers_office +-- 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 +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 +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; + +-- 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 customers_office_uid_fkey +FOREIGN KEY (office_uid) REFERENCES offices(uid) +ON DELETE CASCADE; \ No newline at end of file diff --git a/src/common/databases/migrations/20240527091523_test/migration.sql b/src/common/databases/migrations/20240527091523_test/migration.sql deleted file mode 100644 index 0eb15a1e..00000000 --- a/src/common/databases/migrations/20240527091523_test/migration.sql +++ /dev/null @@ -1,47 +0,0 @@ -/* - 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; - --- Set the office_uid for existing rows based on the office with "idNot" = '0000' -UPDATE customers -SET office_uid = ( - SELECT uid - FROM offices - WHERE "idNot" = '0000' -); - --- Alter the column to be NOT NULL -ALTER TABLE customers -ALTER COLUMN office_uid SET NOT NULL; - --- 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 foreign key constraint -ALTER TABLE customers -ADD CONSTRAINT fk_customers_office -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; diff --git a/src/common/databases/migrations/20240527091621_office_customer_relation/migration.sql b/src/common/databases/migrations/20240527091621_office_customer_relation/migration.sql deleted file mode 100644 index 16a52de4..00000000 --- a/src/common/databases/migrations/20240527091621_office_customer_relation/migration.sql +++ /dev/null @@ -1,2 +0,0 @@ --- DropForeignKey -ALTER TABLE "customers" DROP CONSTRAINT "fk_customers_office"; diff --git a/src/common/databases/migrations/20240527144510_test/migration.sql b/src/common/databases/migrations/20240527144510_test/migration.sql new file mode 100644 index 00000000..643339d8 --- /dev/null +++ b/src/common/databases/migrations/20240527144510_test/migration.sql @@ -0,0 +1,5 @@ +-- DropForeignKey +ALTER TABLE "customers" DROP CONSTRAINT "customers_office_uid_fkey"; + +-- AddForeignKey +ALTER TABLE "customers" ADD CONSTRAINT "customers_office_uid_fkey" FOREIGN KEY ("office_uid") REFERENCES "offices"("uid") ON DELETE CASCADE ON UPDATE CASCADE;