Fixed migrations
This commit is contained in:
parent
151538e971
commit
3289482e62
@ -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;
|
@ -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;
|
@ -1,2 +0,0 @@
|
||||
-- DropForeignKey
|
||||
ALTER TABLE "customers" DROP CONSTRAINT "fk_customers_office";
|
@ -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;
|
Loading…
x
Reference in New Issue
Block a user