File size: 944 Bytes
d4f6fe7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-- Migration: driver records now reference customers as primary identity
-- 1) Add customer_id foreign key to drivers
-- 2) Populate customer_id from existing customer records when possible
-- 3) Enforce uniqueness and non-nullability
-- 4) Drop redundant driver identity columns

ALTER TABLE public.drivers
ADD COLUMN customer_id uuid;

ALTER TABLE public.customers
RENAME COLUMN phone_number TO "remoteJid";

UPDATE public.drivers
SET customer_id = public.customers.id
FROM public.customers
WHERE public.drivers.phone_number = public.customers."remoteJid";

ALTER TABLE public.drivers
ADD CONSTRAINT drivers_customer_id_key UNIQUE (customer_id);

ALTER TABLE public.drivers
ALTER COLUMN customer_id SET NOT NULL;

ALTER TABLE public.drivers
ADD CONSTRAINT drivers_customer_id_fkey
    FOREIGN KEY (customer_id)
    REFERENCES public.customers(id)
    ON DELETE CASCADE;

ALTER TABLE public.drivers
DROP COLUMN name,
DROP COLUMN phone_number;