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 <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:41:56 +08:00
1 parent b20ef0a592
commit 5bb2a7343d
3 files changed
+52 -3

No files matched your search

+20 -3
View File
@@ -62,8 +62,25 @@ for(const r of rows)console.log(Object.values(r).map(q).join(','));
" > leads.csv " > 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.
+22
View File
@@ -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
);
+10
View File
@@ -144,3 +144,13 @@ test('exits promptly on SIGTERM', async () => {
assert.ok(Date.now() - start < 2000); assert.ok(Date.now() - start < 2000);
fs.rmSync(tmp, { recursive: true, force: true }); 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();
});