LifeMate — Personal, Family & Learning Companion

feat/weekly-planner-android
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
js 82 lines 2.59 KB
Raw
1 import fs from 'node:fs/promises';
2 import path from 'node:path';
3 import crypto from 'node:crypto';
4 import pg from 'pg';
5
6 const { Client } = pg;
7 const databaseUrl = process.env.DATABASE_URL;
8 const client = new Client(
9 databaseUrl
10 ? {
11 connectionString: databaseUrl,
12 ssl:
13 process.env.PGSSLMODE === 'require'
14 ? { rejectUnauthorized: false }
15 : false,
16 }
17 : {
18 host: process.env.PGHOST,
19 port: Number(process.env.PGPORT ?? 5432),
20 database: process.env.PGDATABASE,
21 user: process.env.PGUSER,
22 password: process.env.PGPASSWORD,
23 ssl:
24 process.env.PGSSLMODE === 'require'
25 ? { rejectUnauthorized: false }
26 : false,
27 },
28 );
29
30 await client.connect();
31 try {
32 await client.query("select pg_advisory_lock(hashtextextended('lifemate_schema_migration',0))");
33 await client.query(`
34 create table if not exists schema_migration (
35 filename text primary key,
36 applied_at timestamptz not null default now()
37 )
38 `);
39 await client.query('alter table schema_migration add column if not exists sha256 text');
40
41 const dir = path.resolve('migrations');
42 const files = (await fs.readdir(dir))
43 .filter((name) => /^\d+_.*\.sql$/.test(name))
44 .sort();
45
46 for (const filename of files) {
47 const source = await fs.readFile(path.join(dir, filename), 'utf8');
48 const checksum = crypto.createHash('sha256').update(source).digest('hex');
49 const exists = await client.query(
50 'select sha256 from schema_migration where filename=$1',
51 [filename],
52 );
53 if (exists.rowCount) {
54 const stored = exists.rows[0].sha256;
55 if (stored && stored !== checksum) throw new Error(`Applied migration changed: ${filename}`);
56 if (!stored) console.warn(`UNVERIFIED historical migration checksum: ${filename}`);
57 continue;
58 }
59
60 // Existing files contain their own outer BEGIN/COMMIT. Keep the schema
61 // and ledger insert in ONE transaction instead of committing the SQL early.
62 const trimmed = source.trim();
63 const sql = /^begin\s*;/i.test(trimmed) && /commit\s*;$/i.test(trimmed)
64 ? trimmed.replace(/^begin\s*;/i, '').replace(/commit\s*;$/i, '')
65 : source;
66 await client.query('begin');
67 try {
68 await client.query(sql);
69 await client.query(
70 'insert into schema_migration(filename,sha256) values($1,$2)',
71 [filename,checksum],
72 );
73 await client.query('commit');
74 console.log('applied', filename);
75 } catch (error) {
76 await client.query('rollback');
77 throw error;
78 }
79 }
80 } finally {
81 await client.end();
82 }