From 5bb2a7343ddd7af4ae29c8a07d21298ab8e58497 Mon Sep 17 00:00:00 2001 From: ngqile Date: Tue, 1 Dec 2026 11:41:56 +0800 Subject: [PATCH] docs: schema.sql reference and DBeaver snapshot instructions A test fails if docs/schema.sql drifts from the table server.js creates. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfmHGGiYUjNcvJoyzdf --- README.md | 23 ++++++++++++++++++++--- docs/schema.sql | 22 ++++++++++++++++++++++ server.test.js | 10 ++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 docs/schema.sql diff --git a/README.md b/README.md index 73c4549..4485aa2 100644 --- a/README.md +++ b/README.md @@ -62,8 +62,25 @@ for(const r of rows)console.log(Object.values(r).map(q).join(',')); " > leads.csv ``` -Prefer the CSV export above. The database runs in WAL mode, so copying only `leads.db` can miss the newest leads; if you copy the file, copy `leads.db-wal` with it. +### Opening the database in DBeaver -Columns: `id, created_at (UTC), name, email, phone, company, service, referrer, utm_source, utm_medium, utm_campaign`. +SQLite is a file, not a network server, so DBeaver cannot connect to the pod. Take a consistent snapshot and open that copy: + +```bash +NS=ins-landing-page +POD=$(microk8s kubectl -n $NS get pod -l app=ins-landing-page -o name | head -1 | cut -d/ -f2) +# VACUUM INTO writes one complete file, including leads still in the WAL +microk8s kubectl -n $NS exec $POD -- node --disable-warning=ExperimentalWarning -e \ + "new (require('node:sqlite').DatabaseSync)(process.env.DB_PATH).exec(\"VACUUM INTO '/data/snapshot.db'\")" +microk8s kubectl -n $NS cp $POD:/data/snapshot.db ./leads-snapshot.db +microk8s kubectl -n $NS exec $POD -- rm /data/snapshot.db +``` + +In DBeaver: **New Database Connection → SQLite →** point **Path** at `leads-snapshot.db`. Query and export freely; it's your own copy. + +Edits made in DBeaver stay in the copy. **Never copy a file back into the pod while the app is running**: it would overwrite leads submitted in the meantime. To fix or delete live rows, run the SQL in the pod (`exec ... node -e "..."`) and keep a snapshot first. + +Leads are personal data (PDPA): delete snapshots and CSVs when you're done. + +Schema: `docs/schema.sql`. Columns: `id, created_at (UTC), name, email, phone, company, service, referrer, utm_source, utm_medium, utm_campaign`. -Leads contain personal data (PDPA). Keep exported files out of shared drives and delete them when done. diff --git a/docs/schema.sql b/docs/schema.sql new file mode 100644 index 0000000..b499a2b --- /dev/null +++ b/docs/schema.sql @@ -0,0 +1,22 @@ +-- Lead storage for ins-landing-page (SQLite). +-- +-- 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 +-- drift apart. +-- +-- Changing the schema after go-live: CREATE TABLE IF NOT EXISTS never alters an existing table, +-- 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' + name TEXT NOT NULL, + 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 + utm_source TEXT, + utm_medium TEXT, + utm_campaign TEXT +); diff --git a/server.test.js b/server.test.js index 9800276..0ea2bf0 100644 --- a/server.test.js +++ b/server.test.js @@ -144,3 +144,13 @@ test('exits promptly on SIGTERM', async () => { assert.ok(Date.now() - start < 2000); fs.rmSync(tmp, { recursive: true, force: true }); }); + +test('docs/schema.sql matches the table the server creates', () => { + const cols = (db) => db.prepare('PRAGMA table_info(leads)').all().map((c) => [c.name, c.type, c.notnull, c.dflt_value, c.pk]); + const fromDoc = new DatabaseSync(':memory:'); + fromDoc.exec(fs.readFileSync(path.join(__dirname, 'docs', 'schema.sql'), 'utf8')); + const fromServer = new DatabaseSync(dbPath, { readOnly: true }); + assert.deepStrictEqual(cols(fromDoc), cols(fromServer)); + fromDoc.close(); + fromServer.close(); +});