From 0d3a74f826c07e9f15cddbfb73aad35092bd1b09 Mon Sep 17 00:00:00 2001 From: huayitang Date: Fri, 19 Sep 2025 20:28:35 +0200 Subject: [PATCH 1/3] feat(api): refactor code base --- .../000003_add_users_orders.down.sql | 2 ++ .../migrations/000003_add_users_orders.up.sql | 18 ++++++++++++++++++ .../000003_add_users_orders.down.sql | 2 ++ .../migrations/000003_add_users_orders.up.sql | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.down.sql create mode 100644 apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql create mode 100644 charts/product-query-svc/migrations/000003_add_users_orders.down.sql create mode 100644 charts/product-query-svc/migrations/000003_add_users_orders.up.sql diff --git a/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.down.sql b/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.down.sql new file mode 100644 index 0000000..e45e329 --- /dev/null +++ b/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS orders; +DROP TABLE IF EXISTS users; diff --git a/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql b/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql new file mode 100644 index 0000000..712337c --- /dev/null +++ b/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql @@ -0,0 +1,18 @@ +-- Create users table +CREATE TABLE IF NOT EXISTS users ( + id BIGSERIAL PRIMARY KEY, + name TEXT NOT NULL, + email TEXT NOT NULL UNIQUE, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +-- Create orders table referencing users +CREATE TABLE IF NOT EXISTS orders ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + product_name TEXT NOT NULL, + total BIGINT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +CREATE INDEX IF NOT EXISTS orders_user_id_idx ON orders(user_id); diff --git a/charts/product-query-svc/migrations/000003_add_users_orders.down.sql b/charts/product-query-svc/migrations/000003_add_users_orders.down.sql new file mode 100644 index 0000000..e45e329 --- /dev/null +++ b/charts/product-query-svc/migrations/000003_add_users_orders.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS orders; +DROP TABLE IF EXISTS users; diff --git a/charts/product-query-svc/migrations/000003_add_users_orders.up.sql b/charts/product-query-svc/migrations/000003_add_users_orders.up.sql new file mode 100644 index 0000000..712337c --- /dev/null +++ b/charts/product-query-svc/migrations/000003_add_users_orders.up.sql @@ -0,0 +1,18 @@ +-- Create users table +CREATE TABLE IF NOT EXISTS users ( + id BIGSERIAL PRIMARY KEY, + name TEXT NOT NULL, + email TEXT NOT NULL UNIQUE, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +-- Create orders table referencing users +CREATE TABLE IF NOT EXISTS orders ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + product_name TEXT NOT NULL, + total BIGINT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +CREATE INDEX IF NOT EXISTS orders_user_id_idx ON orders(user_id); From b1ead978c511040c1bc97b17a2cff024778f8c35 Mon Sep 17 00:00:00 2001 From: huayitang Date: Sat, 20 Sep 2025 00:18:59 +0200 Subject: [PATCH 2/3] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93:=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E7=94=A8=E6=88=B7=E4=B8=8E=E8=AE=A2=E5=8D=95=E8=A1=A8?= =?UTF-8?q?=EF=BC=88CITEXT=20=E9=82=AE=E7=AE=B1=E3=80=81=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/000003_add_users_orders.up.sql | 21 +++++++++++++++++-- .../migrations/000003_add_users_orders.up.sql | 21 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql b/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql index 712337c..28ad1e7 100644 --- a/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql +++ b/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql @@ -1,8 +1,9 @@ --- Create users table +CREATE EXTENSION IF NOT EXISTS citext; + CREATE TABLE IF NOT EXISTS users ( id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL, - email TEXT NOT NULL UNIQUE, + email CITEXT NOT NULL UNIQUE, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); @@ -16,3 +17,19 @@ CREATE TABLE IF NOT EXISTS orders ( ); CREATE INDEX IF NOT EXISTS orders_user_id_idx ON orders(user_id); + +-- Seed sample data +INSERT INTO users (name, email) +VALUES ('Alice', 'alice@example.com'), + ('Bob', 'bob@example.com') +ON CONFLICT (email) DO NOTHING; + +INSERT INTO orders (user_id, product_name, total) +SELECT u.id, p.product_name, p.total +FROM users u +JOIN (VALUES + ('alice@example.com', 'Starter Pack', 4999), + ('alice@example.com', 'Premium Pack', 19999), + ('bob@example.com', 'Gift Card', 2500) +) AS p(email, product_name, total) +WHERE u.email = p.email; diff --git a/charts/product-query-svc/migrations/000003_add_users_orders.up.sql b/charts/product-query-svc/migrations/000003_add_users_orders.up.sql index 712337c..28ad1e7 100644 --- a/charts/product-query-svc/migrations/000003_add_users_orders.up.sql +++ b/charts/product-query-svc/migrations/000003_add_users_orders.up.sql @@ -1,8 +1,9 @@ --- Create users table +CREATE EXTENSION IF NOT EXISTS citext; + CREATE TABLE IF NOT EXISTS users ( id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL, - email TEXT NOT NULL UNIQUE, + email CITEXT NOT NULL UNIQUE, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); @@ -16,3 +17,19 @@ CREATE TABLE IF NOT EXISTS orders ( ); CREATE INDEX IF NOT EXISTS orders_user_id_idx ON orders(user_id); + +-- Seed sample data +INSERT INTO users (name, email) +VALUES ('Alice', 'alice@example.com'), + ('Bob', 'bob@example.com') +ON CONFLICT (email) DO NOTHING; + +INSERT INTO orders (user_id, product_name, total) +SELECT u.id, p.product_name, p.total +FROM users u +JOIN (VALUES + ('alice@example.com', 'Starter Pack', 4999), + ('alice@example.com', 'Premium Pack', 19999), + ('bob@example.com', 'Gift Card', 2500) +) AS p(email, product_name, total) +WHERE u.email = p.email; From 13bf725a81fe9ded4a90d9bec287e38107979e6c Mon Sep 17 00:00:00 2001 From: huayitang Date: Sat, 20 Sep 2025 00:34:26 +0200 Subject: [PATCH 3/3] fix(sql): update user and order join logic to prevent duplicate entries --- .../postgres/migrations/000003_add_users_orders.up.sql | 4 +++- .../migrations/000003_add_users_orders.up.sql | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql b/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql index 28ad1e7..a2fb184 100644 --- a/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql +++ b/apps/product-query-svc/adapters/outbound/postgres/migrations/000003_add_users_orders.up.sql @@ -32,4 +32,6 @@ JOIN (VALUES ('alice@example.com', 'Premium Pack', 19999), ('bob@example.com', 'Gift Card', 2500) ) AS p(email, product_name, total) -WHERE u.email = p.email; + ON u.email = p.email +LEFT JOIN orders o ON o.user_id = u.id AND o.product_name = p.product_name +WHERE o.id IS NULL; diff --git a/charts/product-query-svc/migrations/000003_add_users_orders.up.sql b/charts/product-query-svc/migrations/000003_add_users_orders.up.sql index 28ad1e7..a2fb184 100644 --- a/charts/product-query-svc/migrations/000003_add_users_orders.up.sql +++ b/charts/product-query-svc/migrations/000003_add_users_orders.up.sql @@ -32,4 +32,6 @@ JOIN (VALUES ('alice@example.com', 'Premium Pack', 19999), ('bob@example.com', 'Gift Card', 2500) ) AS p(email, product_name, total) -WHERE u.email = p.email; + ON u.email = p.email +LEFT JOIN orders o ON o.user_id = u.id AND o.product_name = p.product_name +WHERE o.id IS NULL;