Migration finished

This commit is contained in:
Vins 2024-05-27 16:55:01 +02:00
parent 3289482e62
commit 74b4f7f582

View File

@ -1,13 +1,8 @@
/* -- -- Add the office_uid column
Warnings: -- 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. -- -- Set the office_uid for existing rows
*/
-- 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 -- UPDATE customers
-- SET office_uid = ( -- SET office_uid = (
-- SELECT uid -- SELECT uid
@ -33,58 +28,20 @@
-- -- Drop the temporary table -- -- Drop the temporary table
-- DROP TABLE customer_office_update; -- 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 -- -- Add the foreign key constraint
-- ALTER TABLE customers -- ALTER TABLE customers
-- ADD CONSTRAINT fk_customers_office -- ADD CONSTRAINT customers_office_uid_fkey
-- FOREIGN KEY (office_uid) REFERENCES offices(uid) -- FOREIGN KEY (office_uid) REFERENCES offices(uid)
-- ON DELETE CASCADE; -- ON DELETE CASCADE;
-- -- AddForeignKey -- Add the office_uid column to the customers table
-- 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 ALTER TABLE customers
ADD COLUMN office_uid VARCHAR(255); 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 a temporary table to store the mapping
CREATE TEMPORARY TABLE customer_office_update AS CREATE TEMPORARY TABLE customer_office_update AS
SELECT c.uid AS customer_uid, of.office_uid 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 "_OfficeFolderHasCustomers" ofhc ON c.uid = ofhc."A"
JOIN office_folders of ON ofhc."B" = of.uid; 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 UPDATE customers
SET office_uid = ( SET office_uid = customer_office_update.office_uid
SELECT office_uid
FROM customer_office_update FROM customer_office_update
WHERE customer_office_update.customer_uid = customers.uid WHERE customers.uid = customer_office_update.customer_uid;
);
-- Drop the temporary table -- Drop the temporary table
DROP TABLE customer_office_update; 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 the column to be NOT NULL
ALTER TABLE customers ALTER TABLE customers
ALTER COLUMN office_uid SET NOT NULL; ALTER COLUMN office_uid SET NOT NULL;