feat: store leads in PostgreSQL instead of SQLite

- server.js uses pg (pinned 8.23.1), reads DATABASE_URL, creates the
  leads table on start, fails fast if Postgres is unreachable
- tests run against a real Postgres in a throwaway schema
- CI runs the test image against a throwaway Postgres on DinD
- k8s: Postgres 18 StatefulSet (Longhorn, 2Gi), init container wait,
  password secret, network policy app-to-postgres; app no longer needs
  a volume or Recreate
- README: DBeaver via port-forward, CSV export via docs/export-leads.sql

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfmHGGiYUjNcvJoyzdf
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-01 11:55:14 +08:00
1 parent 5bb2a7343d
commit f78bd635a2
15 files changed
+508 -166

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
-- Export all leads as CSV on stdout. Read-only.
-- Usage: README "Exporting leads".
-- Text cells starting with = + - @ tab or CR get a leading apostrophe, so Excel cannot run
-- form input as a formula (CSV injection).
COPY (
SELECT id,
to_char(created_at AT TIME ZONE 'Asia/Kuala_Lumpur', 'YYYY-MM-DD HH24:MI:SS') AS created_at_myt,
regexp_replace(name, E'^([=+@\\t\\r-])', E'\'\\1') AS name,
regexp_replace(email, E'^([=+@\\t\\r-])', E'\'\\1') AS email,
regexp_replace(phone, E'^([=+@\\t\\r-])', E'\'\\1') AS phone,
regexp_replace(company, E'^([=+@\\t\\r-])', E'\'\\1') AS company,
service,
regexp_replace(referrer, E'^([=+@\\t\\r-])', E'\'\\1') AS referrer,
regexp_replace(utm_source, E'^([=+@\\t\\r-])', E'\'\\1') AS utm_source,
regexp_replace(utm_medium, E'^([=+@\\t\\r-])', E'\'\\1') AS utm_medium,
regexp_replace(utm_campaign, E'^([=+@\\t\\r-])', E'\'\\1') AS utm_campaign
FROM leads
ORDER BY id
) TO STDOUT WITH (FORMAT csv, HEADER);
+7 -7
View File
@@ -1,4 +1,4 @@
-- Lead storage for ins-landing-page (SQLite).
-- Lead storage for ins-landing-page (PostgreSQL).
--
-- Reference only: server.js creates this table itself on first start (CREATE TABLE IF NOT EXISTS),
-- so nobody needs to run this file on deploy. server.test.js fails if this file and server.js
@@ -8,14 +8,14 @@
-- so a new column also needs an ALTER TABLE step against the live database.
CREATE TABLE IF NOT EXISTS leads (
id INTEGER PRIMARY KEY,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, -- UTC, 'YYYY-MM-DD HH:MM:SS'
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
name TEXT NOT NULL,
email TEXT NOT NULL, -- work email; free providers rejected
phone TEXT NOT NULL, -- phone / WhatsApp, as typed
email TEXT NOT NULL, -- work email; free providers rejected
phone TEXT NOT NULL, -- phone / WhatsApp, as typed
company TEXT NOT NULL,
service TEXT, -- one of the form's options, or NULL
referrer TEXT, -- site the visitor came from
service TEXT, -- one of the form's options, or NULL
referrer TEXT, -- site the visitor came from
utm_source TEXT,
utm_medium TEXT,
utm_campaign TEXT
@@ -53,3 +53,13 @@ The page owner supplied a new brief. It replaces the page content and form; stor
- Google Fonts allowed (owner request). Floating WhatsApp/call button on mobile.
- Decision (user, 2026-12-01): keep the existing database endpoint instead of the brief's `action="#"` placeholder.
- Schema: `industry` and `message` columns replaced by `service`. The local database held only throwaway test data and is recreated.
## Revision 2026-12-01: Postgres instead of SQLite
Decision (user, 2026-12-01): move lead storage to Postgres so the team can open the live data in DBeaver.
- Option chosen: a dedicated Postgres 18 StatefulSet in namespace `ins-landing-page`, copied from dynamic-qr (Longhorn PVC, password Secret, init container waiting for `pg_isready`, network policy allowing only the app). Rejected: a database on a shared Postgres (cross-namespace coupling, leads sharing a server with other apps).
- New dependency: `pg` (node-postgres), pinned exactly. Node has no built-in Postgres client. This ends "zero npm dependencies".
- The app reads `DATABASE_URL`, creates the `leads` table on start if missing, and closes the pool on SIGTERM.
- Tests run against a real Postgres, each run in its own throwaway schema, so they never touch existing tables. CI starts a Postgres container on the DinD daemon and runs the test image against it.
- DBeaver: `kubectl port-forward` to the Postgres service. CSV export via `psql \copy`.