Skip to content

Commit

Permalink
Make patch script idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
zdevaty committed May 27, 2024
1 parent 59f04f6 commit 5e9c3cb
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions conf/000200.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ CREATE TABLE IF NOT EXISTS ews.room_booking
exchange_id text UNIQUE
);

INSERT INTO ews.unified_booking (exchange_uid, exchange_organizer_mailbox, booking_id)
SELECT DISTINCT exchange_uid, exchange_mailbox, booking_id
FROM ews.booking;
DO $$
BEGIN
IF EXISTS (SELECT FROM pg_tables WHERE schemaname = 'ews' AND tablename = 'booking') THEN

INSERT INTO ews.room_booking (unified_booking_id, exchange_id)
SELECT ub.id, b.exchange_id
FROM ews.booking b
JOIN ews.unified_booking ub ON b.exchange_uid = ub.exchange_uid;
-- Insert data into ews.unified_booking
INSERT INTO ews.unified_booking (exchange_uid, exchange_organizer_mailbox, booking_id)
SELECT DISTINCT exchange_uid, exchange_mailbox, booking_id
FROM ews.booking;

DROP TABLE IF EXISTS ews.booking;
-- Insert data into ews.room_booking
INSERT INTO ews.room_booking (unified_booking_id, exchange_id)
SELECT ub.id, b.exchange_id
FROM ews.booking b
JOIN ews.unified_booking ub ON b.exchange_uid = ub.exchange_uid;

-- Drop the old ews.booking table
DROP TABLE IF EXISTS ews.booking;

END IF;
END $$;

0 comments on commit 5e9c3cb

Please sign in to comment.