So, i`m writing migration.
My goal is to:
- Create another table, which contains all the necessary fields
- Move existing data from the
source
table to thedest
one. (insert data into thedest
, then update reference in thesource
, setting thedest_id
asid
of the just created row in thedest
table) - Delete the rows which were moved from the
source
table.
I`m having difficulties with the step 2. Any ideas?
I currently have this:
do $$
declare os_id bigint;
begin
insert into order_shipments (order_id, method_id, deliver_at, barcode, sent_at, city_id, subway_id)
select o.id, o.shipment_method_id, o.courier_deliver_at, o.barcode, o.sent_at, o.courier_city_id, o.courier_subway_id
from orders o
returning id into os_id;
end
$$;
update orders as o set current_shipment_id = os_id;
I'm creating anonymous block, which takes data from orders
(source
) and inserts it into the order_shipments
(dest
).