From 2b16b173085579b6537df9250ae22b64a7177890 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sat, 8 Nov 2025 18:19:21 +0100 Subject: [PATCH] Fixes in migrations --- .../basket-svc/0001_create_base_tables.up.sql | 39 ++++++++++++------- .../0001_create_base_tables.up.sql | 2 +- deploy/db_migrations/init/init.sql | 1 + .../order-svc/0001_create_base_tables.up.sql | 8 +--- .../0001_create_base_tables.up.sql | 5 +-- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/deploy/db_migrations/basket-svc/0001_create_base_tables.up.sql b/deploy/db_migrations/basket-svc/0001_create_base_tables.up.sql index 152d9b2..9786ace 100644 --- a/deploy/db_migrations/basket-svc/0001_create_base_tables.up.sql +++ b/deploy/db_migrations/basket-svc/0001_create_base_tables.up.sql @@ -7,28 +7,41 @@ CREATE TABLE IF NOT EXISTS basket.basket PRIMARY KEY (id) ); +ALTER TABLE IF EXISTS basket.basket OWNER to egommerce; + CREATE TABLE IF NOT EXISTS basket.basket_item ( id uuid NOT NULL DEFAULT gen_random_uuid(), basket_id uuid NOT NULL, - product_id integer NOT NULL, + product_id uuid NOT NULL, quantity integer NOT NULL DEFAULT 1, price double precision NOT NULL DEFAULT 0.00, created_at timestamp without time zone NOT NULL DEFAULT now(), updated_at timestamp without time zone, - PRIMARY KEY (id) + PRIMARY KEY (id), + CONSTRAINT basket_item_basket_fkey FOREIGN KEY (basket_id) + REFERENCES basket.basket (id) MATCH SIMPLE + ON UPDATE NO ACTION + ON DELETE NO ACTION + NOT VALID ); -ALTER TABLE IF EXISTS basket.basket_item - ADD CONSTRAINT basket_item_basket_fkey FOREIGN KEY (basket_id) - REFERENCES basket.basket (id) MATCH SIMPLE - ON UPDATE NO ACTION - ON DELETE NO ACTION - NOT VALID; +ALTER TABLE IF EXISTS basket.basket_item OWNER to egommerce; -ALTER TABLE IF EXISTS basket.basket - OWNER to postgres; +CREATE FUNCTION basket.update_basket_timestamp() + RETURNS trigger + LANGUAGE 'plpgsql' + VOLATILE +AS $BODY$ +BEGIN + UPDATE basket.basket SET updated_at = NOW() WHERE id = NEW.basket_id; + RETURN NEW; +END; +$BODY$; -ALTER TABLE IF EXISTS basket.basket_item - OWNER to postgres; --- TODO ^^ PRIVILEGES... \ No newline at end of file +ALTER FUNCTION basket.update_basket_timestamp() OWNER TO egommerce; + +CREATE TRIGGER trigger_update_basket_timestamp + AFTER INSERT OR UPDATE OR DELETE ON basket.basket_item + FOR EACH ROW + EXECUTE FUNCTION basket.update_basket_timestamp(); diff --git a/deploy/db_migrations/identity-svc/0001_create_base_tables.up.sql b/deploy/db_migrations/identity-svc/0001_create_base_tables.up.sql index dfe1c2b..2ab4e7f 100644 --- a/deploy/db_migrations/identity-svc/0001_create_base_tables.up.sql +++ b/deploy/db_migrations/identity-svc/0001_create_base_tables.up.sql @@ -36,7 +36,7 @@ CREATE TABLE IF NOT EXISTS identity.users_roles REFERENCES identity.roles (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION - NOT VALID + NOT VALID, UNIQUE (user_id, role_id) ); diff --git a/deploy/db_migrations/init/init.sql b/deploy/db_migrations/init/init.sql index 61c58af..868dd64 100644 --- a/deploy/db_migrations/init/init.sql +++ b/deploy/db_migrations/init/init.sql @@ -8,3 +8,4 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE SCHEMA "identity" AUTHORIZATION egommerce; CREATE SCHEMA catalog AUTHORIZATION egommerce; +CREATE SCHEMA basket AUTHORIZATION egommerce; \ No newline at end of file diff --git a/deploy/db_migrations/order-svc/0001_create_base_tables.up.sql b/deploy/db_migrations/order-svc/0001_create_base_tables.up.sql index 75f0de1..6598556 100644 --- a/deploy/db_migrations/order-svc/0001_create_base_tables.up.sql +++ b/deploy/db_migrations/order-svc/0001_create_base_tables.up.sql @@ -26,9 +26,5 @@ ALTER TABLE IF EXISTS "ordering".order_item ON DELETE NO ACTION NOT VALID; -ALTER TABLE IF EXISTS "ordering"."order" - OWNER to postgres; - -ALTER TABLE IF EXISTS "ordering".order_item - OWNER to postgres; --- TODO ^^ PRIVILEGES... \ No newline at end of file +ALTER TABLE IF EXISTS "ordering"."order" OWNER to egommerce; +ALTER TABLE IF EXISTS "ordering".order_item OWNER to egommerce; diff --git a/deploy/db_migrations/pricing-svc/0001_create_base_tables.up.sql b/deploy/db_migrations/pricing-svc/0001_create_base_tables.up.sql index 380305b..c1e7ce4 100644 --- a/deploy/db_migrations/pricing-svc/0001_create_base_tables.up.sql +++ b/deploy/db_migrations/pricing-svc/0001_create_base_tables.up.sql @@ -3,7 +3,4 @@ CREATE TABLE IF NOT EXISTS pricing."price" id uuid NOT NULL DEFAULT gen_random_uuid() ); -ALTER TABLE IF EXISTS "pricing"."price" - OWNER to postgres; - --- TODO ^^ PRIVILEGES... \ No newline at end of file +ALTER TABLE IF EXISTS "pricing"."price" OWNER to egommerce;