|
| 1 | +CREATE SCHEMA IF NOT EXISTS copy_data; |
| 2 | + |
| 3 | +CREATE TABLE IF NOT EXISTS copy_data.users ( |
| 4 | + id BIGINT NOT NULL, |
| 5 | + tenant_id BIGINT NOT NULL, |
| 6 | + email VARCHAR NOT NULL, |
| 7 | + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 8 | + settings JSONB NOT NULL DEFAULT '{}'::jsonb |
| 9 | +) PARTITION BY HASH(tenant_id); |
| 10 | + |
| 11 | +CREATE TABLE IF NOT EXISTS copy_data.users_0 PARTITION OF copy_data.users |
| 12 | + FOR VALUES WITH (MODULUS 2, REMAINDER 0); |
| 13 | + |
| 14 | +CREATE TABLE IF NOT EXISTS copy_data.users_1 PARTITION OF copy_data.users |
| 15 | + FOR VALUES WITH (MODULUS 2, REMAINDER 1); |
| 16 | + |
| 17 | +TRUNCATE TABLE copy_data.users; |
| 18 | + |
| 19 | +INSERT INTO copy_data.users (id, tenant_id, email, created_at, settings) |
| 20 | +SELECT |
| 21 | + gs.id, |
| 22 | + ((gs.id - 1) % 20) + 1 AS tenant_id, -- distribute across 20 tenants |
| 23 | + format( 'user_%s_tenant_%[email protected]', gs. id, (( gs. id - 1) % 20) + 1) AS email, |
| 24 | + NOW() - (random() * interval '365 days') AS created_at, -- random past date |
| 25 | + jsonb_build_object( |
| 26 | + 'theme', CASE (random() * 3)::int |
| 27 | + WHEN 0 THEN 'light' |
| 28 | + WHEN 1 THEN 'dark' |
| 29 | + ELSE 'auto' |
| 30 | + END, |
| 31 | + 'notifications', (random() > 0.5) |
| 32 | + ) AS settings |
| 33 | +FROM generate_series(1, 10000) AS gs(id); |
| 34 | + |
| 35 | +DROP TABLE copy_data.orders; |
| 36 | +CREATE TABLE IF NOT EXISTS copy_data.orders ( |
| 37 | + id BIGSERIAL PRIMARY KEY, |
| 38 | + user_id BIGINT NOT NULL, |
| 39 | + tenant_id BIGINT NOT NULL, |
| 40 | + amount DOUBLE PRECISION NOT NULL DEFAULT 0.0, |
| 41 | + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 42 | + refunded_at TIMESTAMPTZ |
| 43 | +); |
| 44 | + |
| 45 | +CREATE TABLE IF NOT EXISTS copy_data.order_items ( |
| 46 | + user_id BIGINT NOT NULL, |
| 47 | + tenant_id BIGINT NOT NULL, |
| 48 | + order_id BIGINT NOT NULL REFERENCES copy_data.orders(id), |
| 49 | + amount DOUBLE PRECISION NOT NULL DEFAULT 0.0, |
| 50 | + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 51 | + refunded_at TIMESTAMPTZ |
| 52 | +); |
| 53 | + |
| 54 | +-- --- Fix/define schema (safe to run if you're starting fresh) --- |
| 55 | +-- Adjust/drop statements as needed if the tables already exist. |
| 56 | +TRUNCATE TABLE copy_data.order_items CASCADE; |
| 57 | +TRUNCATE TABLE copy_data.orders CASCADE; |
| 58 | + |
| 59 | +WITH u AS ( |
| 60 | + -- Pull the 10k users we inserted earlier |
| 61 | + SELECT id AS user_id, tenant_id |
| 62 | + FROM copy_data.users |
| 63 | + WHERE id BETWEEN 1 AND 10000 |
| 64 | + ORDER BY id |
| 65 | +), |
| 66 | +orders_base AS ( |
| 67 | + -- One order per user (10k orders), deterministic order_id = user_id |
| 68 | + SELECT |
| 69 | + u.user_id AS order_id, |
| 70 | + u.user_id, |
| 71 | + u.tenant_id, |
| 72 | + -- random created_at in last 365 days |
| 73 | + NOW() - (random() * INTERVAL '365 days') AS created_at, |
| 74 | + -- ~10% refunded |
| 75 | + CASE WHEN random() < 0.10 |
| 76 | + THEN NOW() - (random() * INTERVAL '180 days') |
| 77 | + ELSE NULL |
| 78 | + END AS refunded_at |
| 79 | + FROM u |
| 80 | +), |
| 81 | +items_raw AS ( |
| 82 | + -- 1–5 items per order, random amounts $5–$200 |
| 83 | + SELECT |
| 84 | + ob.order_id, |
| 85 | + ob.user_id, |
| 86 | + ob.tenant_id, |
| 87 | + -- skew item counts 1..5 (uniform) |
| 88 | + gs.i AS item_index, |
| 89 | + -- random item amount with cents |
| 90 | + ROUND((5 + random() * 195)::numeric, 2)::float8 AS item_amount, |
| 91 | + -- item created_at: on/after order created_at by up to 3 hours |
| 92 | + ob.created_at + (random() * INTERVAL '3 hours') AS item_created_at, |
| 93 | + -- if order refunded, item refunded too (optionally jitter within 2 hours) |
| 94 | + CASE WHEN ob.refunded_at IS NOT NULL |
| 95 | + THEN ob.refunded_at + (random() * INTERVAL '2 hours') |
| 96 | + ELSE NULL |
| 97 | + END AS item_refunded_at |
| 98 | + FROM orders_base ob |
| 99 | + CROSS JOIN LATERAL generate_series(1, 1 + (floor(random()*5))::int) AS gs(i) |
| 100 | +), |
| 101 | +order_totals AS ( |
| 102 | + SELECT |
| 103 | + order_id, |
| 104 | + user_id, |
| 105 | + tenant_id, |
| 106 | + MIN(item_created_at) AS created_at, |
| 107 | + -- sum of item amounts per order |
| 108 | + ROUND(SUM(item_amount)::numeric, 2)::float8 AS order_amount, |
| 109 | + -- carry refund state from items_raw (same per order) |
| 110 | + MAX(item_refunded_at) AS refunded_at |
| 111 | + FROM items_raw |
| 112 | + GROUP BY order_id, user_id, tenant_id |
| 113 | +), |
| 114 | +ins_orders AS ( |
| 115 | + INSERT INTO copy_data.orders (id, user_id, tenant_id, amount, created_at, refunded_at) |
| 116 | + SELECT |
| 117 | + ot.order_id, -- id = user_id = 1..10000 |
| 118 | + ot.user_id, |
| 119 | + ot.tenant_id, |
| 120 | + ot.order_amount, |
| 121 | + ot.created_at, |
| 122 | + ot.refunded_at |
| 123 | + FROM order_totals ot |
| 124 | + RETURNING id |
| 125 | +) |
| 126 | +INSERT INTO copy_data.order_items (user_id, tenant_id, order_id, amount, created_at, refunded_at) |
| 127 | +SELECT |
| 128 | + ir.user_id, |
| 129 | + ir.tenant_id, |
| 130 | + ir.order_id, |
| 131 | + ir.item_amount, |
| 132 | + ir.item_created_at, |
| 133 | + ir.item_refunded_at |
| 134 | +FROM items_raw ir; |
| 135 | + |
| 136 | +DROP PUBLICATION IF EXISTS pgdog; |
| 137 | +CREATE PUBLICATION pgdog FOR TABLES IN SCHEMA copy_data; |
0 commit comments