LifeMate — Personal, Family & Learning Companion

test/personal-notification-settings
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 34 lines 1.93 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import crypto from 'node:crypto';
4 import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
5 import { tmpdir } from 'node:os';
6 import path from 'node:path';
7 import { fileURLToPath } from 'node:url';
8 import { spawnSync } from 'node:child_process';
9 import pg from 'pg';
10
11 test('migration schema and ledger remain atomic when ledger insert fails', async () => {
12 const base = new URL(process.env.DATABASE_URL);
13 const name = `lifeguide_migration_${crypto.randomUUID().replaceAll('-', '')}`;
14 const admin = new pg.Client({connectionString: base.toString()});
15 await admin.connect();
16 const folder = await mkdtemp(path.join(tmpdir(), 'lifeguide-migration-'));
17 let database;
18 try {
19 await admin.query(`create database ${name}`);
20 const target = new URL(base); target.pathname = `/${name}`;
21 database = new pg.Client({connectionString: target.toString()});
22 await database.connect();
23 await database.query("create table schema_migration(filename text primary key check(filename <> '9999_atomic.sql'), applied_at timestamptz not null default now())");
24 await mkdir(path.join(folder, 'migrations'));
25 await writeFile(path.join(folder, 'migrations', '9999_atomic.sql'), 'begin;\ncreate table atomic_marker(id integer);\ncommit;\n');
26 const result = spawnSync(process.execPath, [fileURLToPath(new URL('../scripts/migrate.js', import.meta.url))], {cwd: folder, env: {...process.env, DATABASE_URL: target.toString()}, encoding: 'utf8'});
27 assert.notEqual(result.status, 0, 'deliberate ledger constraint failure must fail');
28 assert.equal((await database.query("select to_regclass('atomic_marker') as marker")).rows[0].marker, null, 'schema must roll back together with failed ledger');
29 } finally {
30 if (database) { await database.end(); await admin.query(`drop database ${name}`); }
31 await admin.end();
32 await rm(folder, {recursive: true, force: true});
33 }
34 });