|
| 1 | +CREATE SCHEMA shop; |
| 2 | + |
| 3 | +COMMENT ON SCHEMA shop IS 'Storefront: customers, products, orders.'; |
| 4 | + |
| 5 | +GRANT USAGE ON SCHEMA shop TO app_user; |
| 6 | + |
| 7 | +CREATE SCHEMA audit; |
| 8 | + |
| 9 | +COMMENT ON SCHEMA audit IS 'Append-only change log for shop tables.'; |
| 10 | + |
| 11 | +CREATE SEQUENCE shop.order_number_seq START 1000 INCREMENT 1 NO MINVALUE NO MAXVALUE CACHE 1; |
| 12 | + |
| 13 | +CREATE TABLE shop.customers ( |
| 14 | + id uuid DEFAULT gen_random_uuid() NOT NULL, |
| 15 | + email text NOT NULL, |
| 16 | + full_name text NOT NULL, |
| 17 | + phone text, |
| 18 | + created_at timestamptz DEFAULT now() NOT NULL, |
| 19 | + CONSTRAINT customers_pkey PRIMARY KEY (id), |
| 20 | + CONSTRAINT customers_email_key |
| 21 | + UNIQUE (email) |
| 22 | +); |
| 23 | + |
| 24 | +COMMENT ON TABLE shop.customers IS 'Registered storefront customers.'; |
| 25 | + |
| 26 | +COMMENT ON COLUMN shop.customers.email IS 'Unique login email, lowercased.'; |
| 27 | + |
| 28 | +GRANT SELECT ON shop.customers TO app_user; |
| 29 | + |
| 30 | +CREATE TABLE shop.products ( |
| 31 | + id uuid DEFAULT gen_random_uuid() NOT NULL, |
| 32 | + sku text NOT NULL, |
| 33 | + name text NOT NULL, |
| 34 | + description text, |
| 35 | + price_cents int NOT NULL, |
| 36 | + created_at timestamptz DEFAULT now() NOT NULL, |
| 37 | + CONSTRAINT products_pkey PRIMARY KEY (id), |
| 38 | + CONSTRAINT products_sku_key |
| 39 | + UNIQUE (sku), |
| 40 | + CONSTRAINT products_price_cents_check |
| 41 | + CHECK (price_cents >= 0) |
| 42 | +); |
| 43 | + |
| 44 | +COMMENT ON TABLE shop.products IS 'Sellable products with price in cents.'; |
| 45 | + |
| 46 | +GRANT SELECT ON shop.products TO app_user; |
| 47 | + |
| 48 | +CREATE TABLE shop.orders ( |
| 49 | + id uuid DEFAULT gen_random_uuid() NOT NULL, |
| 50 | + order_number bigint DEFAULT nextval(CAST('shop.order_number_seq' AS regclass)) NOT NULL, |
| 51 | + customer_id uuid NOT NULL, |
| 52 | + status text DEFAULT 'pending' NOT NULL, |
| 53 | + placed_at timestamptz DEFAULT now() NOT NULL, |
| 54 | + CONSTRAINT orders_pkey PRIMARY KEY (id), |
| 55 | + CONSTRAINT orders_order_number_key |
| 56 | + UNIQUE (order_number), |
| 57 | + CONSTRAINT orders_status_check |
| 58 | + CHECK (status IN ('pending', 'paid', 'shipped', 'cancelled')) |
| 59 | +); |
| 60 | + |
| 61 | +ALTER TABLE ONLY shop.orders |
| 62 | + ADD CONSTRAINT orders_customer_id_fkey |
| 63 | + FOREIGN KEY(customer_id) |
| 64 | + REFERENCES shop.customers (id); |
| 65 | + |
| 66 | +ALTER TABLE shop.orders |
| 67 | + ENABLE ROW LEVEL SECURITY; |
| 68 | + |
| 69 | +COMMENT ON TABLE shop.orders IS 'Customer orders; one row per checkout.'; |
| 70 | + |
| 71 | +GRANT SELECT, INSERT, UPDATE ON shop.orders TO app_user; |
| 72 | + |
| 73 | +CREATE TABLE shop.order_items ( |
| 74 | + id uuid DEFAULT gen_random_uuid() NOT NULL, |
| 75 | + order_id uuid NOT NULL, |
| 76 | + product_id uuid NOT NULL, |
| 77 | + quantity int DEFAULT 1 NOT NULL, |
| 78 | + unit_price_cents int NOT NULL, |
| 79 | + CONSTRAINT order_items_pkey PRIMARY KEY (id) |
| 80 | +); |
| 81 | + |
| 82 | +ALTER TABLE ONLY shop.order_items |
| 83 | + ADD CONSTRAINT order_items_order_id_fkey |
| 84 | + FOREIGN KEY(order_id) |
| 85 | + REFERENCES shop.orders (id) |
| 86 | + ON DELETE CASCADE; |
| 87 | + |
| 88 | +ALTER TABLE ONLY shop.order_items |
| 89 | + ADD CONSTRAINT order_items_product_id_fkey |
| 90 | + FOREIGN KEY(product_id) |
| 91 | + REFERENCES shop.products (id); |
| 92 | + |
| 93 | +GRANT SELECT, INSERT ON shop.order_items TO app_user; |
| 94 | + |
| 95 | +CREATE TABLE audit.change_log ( |
| 96 | + id bigint GENERATED ALWAYS AS IDENTITY NOT NULL, |
| 97 | + table_name text NOT NULL, |
| 98 | + row_id uuid NOT NULL, |
| 99 | + operation text NOT NULL, |
| 100 | + changed_at timestamptz DEFAULT now() NOT NULL, |
| 101 | + CONSTRAINT change_log_pkey PRIMARY KEY (id) |
| 102 | +); |
| 103 | + |
| 104 | +CREATE FUNCTION shop.order_total( |
| 105 | + p_order_id uuid |
| 106 | +) RETURNS int LANGUAGE sql STABLE AS $EOFCODE$ |
| 107 | + SELECT COALESCE(SUM(quantity * unit_price_cents), 0)::integer |
| 108 | + FROM shop.order_items |
| 109 | + WHERE order_id = p_order_id; |
| 110 | +$EOFCODE$; |
| 111 | + |
| 112 | +COMMENT ON FUNCTION shop.order_total(p_order_id uuid) IS 'Sum of line totals for an order, in cents.'; |
| 113 | + |
| 114 | +CREATE FUNCTION audit.log_order_change() RETURNS trigger LANGUAGE plpgsql AS $EOFCODE$ |
| 115 | +BEGIN |
| 116 | + INSERT INTO audit.change_log (table_name, row_id, operation) |
| 117 | + VALUES (TG_TABLE_NAME, NEW.id, TG_OP); |
| 118 | + RETURN NEW; |
| 119 | +END; |
| 120 | +$EOFCODE$; |
| 121 | + |
| 122 | +CREATE INDEX idx_orders_customer_id ON shop.orders (customer_id); |
| 123 | + |
| 124 | +CREATE INDEX idx_orders_placed_at ON shop.orders (placed_at); |
| 125 | + |
| 126 | +CREATE INDEX idx_order_items_order_id ON shop.order_items (order_id); |
| 127 | + |
| 128 | +CREATE TRIGGER orders_audit_trigger |
| 129 | + AFTER INSERT OR UPDATE |
| 130 | + ON shop.orders |
| 131 | + FOR EACH ROW |
| 132 | + EXECUTE PROCEDURE audit.log_order_change(); |
| 133 | + |
| 134 | +CREATE POLICY orders_select_own |
| 135 | + ON shop.orders |
| 136 | + AS PERMISSIVE |
| 137 | + FOR SELECT |
| 138 | + TO PUBLIC |
| 139 | + USING ( |
| 140 | + customer_id = (current_setting('app.current_customer_id', true))::uuid |
| 141 | + ); |
0 commit comments