@everystack/cli 0.4.20 → 0.4.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -1
- package/src/cli/commands/db-reconcile.ts +47 -30
- package/src/cli/commands/db.ts +167 -48
- package/src/cli/index.ts +2 -2
- package/src/reconcile.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@everystack/cli",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.22",
|
|
4
4
|
"description": "CLI and OTA updates for Expo apps on everystack",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"author": "Scalable Technology, Inc. <licensing@scalable.technology>",
|
|
@@ -37,6 +37,10 @@
|
|
|
37
37
|
"types": "./src/cli/apply-execute.ts",
|
|
38
38
|
"default": "./src/cli/apply-execute.ts"
|
|
39
39
|
},
|
|
40
|
+
"./reconcile": {
|
|
41
|
+
"types": "./src/reconcile.ts",
|
|
42
|
+
"default": "./src/reconcile.ts"
|
|
43
|
+
},
|
|
40
44
|
"./audit/source": {
|
|
41
45
|
"types": "./src/cli/audit-source-api.ts",
|
|
42
46
|
"default": "./src/cli/audit-source-api.ts"
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
* explicit trust that live == source, recorded). A source RE-RENDER over an
|
|
24
24
|
* untouched live object — the db/sql-era → descriptor migration — needs
|
|
25
25
|
* `--rebaseline` once: live is verified (defHash match), the new source hash
|
|
26
|
-
* is re-recorded, nothing rebuilds. `--apply`
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
26
|
+
* is re-recorded, nothing rebuilds. `--apply --stage` runs the whole reconcile
|
|
27
|
+
* credential-free in the ops Lambda on the operator connection (the `db:reconcile`
|
|
28
|
+
* action, the db:apply twin) — the deployer holds no URL. `--apply --database-url`
|
|
29
|
+
* runs it direct. A stage DRY-RUN (no `--apply`) reads through the `db:query` path.
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
import type { QueryRunner } from '../authz-contract.js';
|
|
@@ -380,11 +380,6 @@ export async function dbReconcileCommand(flags: Record<string, string>): Promise
|
|
|
380
380
|
process.exit(1);
|
|
381
381
|
}
|
|
382
382
|
|
|
383
|
-
if (apply && dbSource.kind !== 'url') {
|
|
384
|
-
fail('--apply needs a direct connection (--database-url or DATABASE_URL) — the ops Lambda query path is read-only by design.');
|
|
385
|
-
process.exit(1);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
383
|
// --baseline only writes under --apply (executeReconcile returns the plan and executes nothing
|
|
389
384
|
// when apply=false). Passing it alone used to print "recording provenance…" and exit 0 while
|
|
390
385
|
// persisting nothing — a silent no-op. Fail loudly instead; the plan already lists needsBaseline.
|
|
@@ -416,29 +411,51 @@ export async function dbReconcileCommand(flags: Record<string, string>): Promise
|
|
|
416
411
|
info(`${declared.objects.length} declared derived object(s) from the models barrel.`);
|
|
417
412
|
}
|
|
418
413
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
414
|
+
const reconcileOptions = {
|
|
415
|
+
apply,
|
|
416
|
+
declared: declared?.objects,
|
|
417
|
+
renamedTables: declared?.renamedTables,
|
|
418
|
+
baseline: flags.baseline === 'true',
|
|
419
|
+
rebaseline: flags.rebaseline === 'true',
|
|
420
|
+
rebuild: flags.rebuild === 'true',
|
|
421
|
+
overwriteDrift: flags['overwrite-drift'] === 'true',
|
|
422
|
+
actor: process.env.USER ?? null,
|
|
423
|
+
gitRef: currentGitRef(),
|
|
424
|
+
};
|
|
429
425
|
|
|
426
|
+
// Resolve the run from one of two venues:
|
|
427
|
+
// - --apply on a deployed stage runs the WHOLE reconcile in the ops Lambda on the operator
|
|
428
|
+
// connection (the db:reconcile action) — a least-privileged operator needs no raw admin URL,
|
|
429
|
+
// the derived-edge twin of db:apply.
|
|
430
|
+
// - otherwise executeReconcile runs locally over a direct URL, or over the read-only db:query
|
|
431
|
+
// path for a stage DRY-RUN (planning only reads).
|
|
432
|
+
let end: (() => Promise<void>) | undefined;
|
|
430
433
|
try {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
434
|
+
let run: ReconcileRun;
|
|
435
|
+
if (apply && dbSource.kind !== 'url') {
|
|
436
|
+
step('Resolving deployed config...');
|
|
437
|
+
const config = await resolveConfig(flags.stage);
|
|
438
|
+
const result: any = await invokeAction(config.region, opsFunction(config), 'db:reconcile', reconcileOptions);
|
|
439
|
+
if (result?.error) {
|
|
440
|
+
fail(`db:reconcile failed: ${result.error}`);
|
|
441
|
+
if (/Unknown action/i.test(String(result.error))) {
|
|
442
|
+
info('The deployed handler predates the db:reconcile ops action (needs @everystack/server >= 0.4.8). Upgrade the server, or apply direct: db:reconcile --apply --database-url <url>.');
|
|
443
|
+
}
|
|
444
|
+
process.exit(1);
|
|
445
|
+
}
|
|
446
|
+
run = { plan: result.plan, applied: result.applied, statements: result.statements ?? [], refusal: result.refusal ?? undefined };
|
|
447
|
+
} else {
|
|
448
|
+
let runner: QueryRunner;
|
|
449
|
+
if (dbSource.kind === 'url') {
|
|
450
|
+
step(connectingVia(dbSource));
|
|
451
|
+
({ runner, end } = await createUrlRunner(dbSource.url));
|
|
452
|
+
} else {
|
|
453
|
+
step('Resolving deployed config...');
|
|
454
|
+
const config = await resolveConfig(flags.stage);
|
|
455
|
+
runner = lambdaRunner(config.region, opsFunction(config));
|
|
456
|
+
}
|
|
457
|
+
run = await executeReconcile(runner, reconcileOptions);
|
|
458
|
+
}
|
|
442
459
|
|
|
443
460
|
if (flags.json === 'true') {
|
|
444
461
|
console.log(JSON.stringify({ ...run.plan, applied: run.applied, refusal: run.refusal ?? null }, null, 2));
|
package/src/cli/commands/db.ts
CHANGED
|
@@ -242,6 +242,55 @@ export async function loadServerProvision(importer?: () => Promise<unknown>): Pr
|
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
244
|
|
|
245
|
+
/**
|
|
246
|
+
* Resolve the DIRECT-venue privileged URL, keeping the master password OFF argv (shell
|
|
247
|
+
* history / ps / CI logs are all leak surfaces, and the whole point of provision is to STOP
|
|
248
|
+
* using master). An explicit --database-url wins; `--direct` reads the stage's already-stored
|
|
249
|
+
* ADMIN_DATABASE_URL secret; no flag returns null (the ops-Lambda venue). A consumer report:
|
|
250
|
+
* "any workflow that requires a human to re-handle the master DB password is both friction and
|
|
251
|
+
* a secret-leak point."
|
|
252
|
+
*/
|
|
253
|
+
export function resolveProvisionDirectUrl(args: {
|
|
254
|
+
flagUrl?: string;
|
|
255
|
+
direct?: boolean;
|
|
256
|
+
secrets: Record<string, string>;
|
|
257
|
+
}): { url: string; source: 'flag' | 'secret' } | null {
|
|
258
|
+
if (args.flagUrl) return { url: args.flagUrl, source: 'flag' };
|
|
259
|
+
if (args.direct) {
|
|
260
|
+
const url = args.secrets.ADMIN_DATABASE_URL ?? args.secrets.AdminDatabaseUrl;
|
|
261
|
+
if (!url) {
|
|
262
|
+
throw new Error(
|
|
263
|
+
'--direct needs the stage\'s ADMIN_DATABASE_URL secret (the privileged URL) — set it with '
|
|
264
|
+
+ '`everystack secrets set ADMIN_DATABASE_URL <url> --stage <stage>`, or pass --database-url <url> once.',
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
return { url, source: 'secret' };
|
|
268
|
+
}
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** The declared non-public schemas (sorted, public trailing) — the API connection's search_path.
|
|
273
|
+
* Empty for an all-public app, so its URL is left byte-identical. */
|
|
274
|
+
export function declaredSearchPath(schemas: string[]): string[] {
|
|
275
|
+
const nonPublic = [...new Set(schemas)].filter((s) => s && s !== 'public').sort();
|
|
276
|
+
return nonPublic.length ? [...nonPublic, 'public'] : [];
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Bake the declared schemas into a postgres URL as `?search_path=…`. A multi-schema app's role
|
|
281
|
+
* defaults to `"$user",public`, so every bare-ref query 404s until a hand ALTER ROLE pin
|
|
282
|
+
* (recorded nowhere, repeated every stage) — writing it into the minted DATABASE_URL is
|
|
283
|
+
* declarative and survives redeploys. postgres.js forwards unknown URL params as connection
|
|
284
|
+
* startup params (GridironDB proved search_path this way). No-op for an all-public app.
|
|
285
|
+
*/
|
|
286
|
+
export function withSearchPath(url: string, schemas: string[]): string {
|
|
287
|
+
const path = declaredSearchPath(schemas);
|
|
288
|
+
if (!path.length) return url;
|
|
289
|
+
const u = new URL(url);
|
|
290
|
+
u.searchParams.set('search_path', path.join(','));
|
|
291
|
+
return u.toString();
|
|
292
|
+
}
|
|
293
|
+
|
|
245
294
|
/** host/port/database from a postgres URL — the direct venue's connection info. */
|
|
246
295
|
export function parseUrlConnection(url: string): { host: string; port: string; database: string } {
|
|
247
296
|
const u = new URL(url);
|
|
@@ -267,13 +316,34 @@ export async function dbProvisionCommand(flags: Record<string, string>): Promise
|
|
|
267
316
|
process.exit(1);
|
|
268
317
|
}
|
|
269
318
|
|
|
270
|
-
//
|
|
271
|
-
//
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const
|
|
319
|
+
// Load the stage secrets once — the direct venue reads the privileged URL from here (never
|
|
320
|
+
// from argv), and the writer below reuses them.
|
|
321
|
+
const { loadSstSecrets, putSstSecrets } = await import('../utils/secrets.js');
|
|
322
|
+
const { parseAppName } = await import('../discover.js');
|
|
323
|
+
const secretsCtx = { appName: await parseAppName(), stage: flags.stage, region: config.region };
|
|
324
|
+
let stageSecrets: Record<string, string> = {};
|
|
325
|
+
try {
|
|
326
|
+
stageSecrets = await loadSstSecrets(secretsCtx);
|
|
327
|
+
} catch {
|
|
328
|
+
// Secrets unreadable (e.g. store not bootstrapped) — direct-from-secret is simply unavailable;
|
|
329
|
+
// an explicit --database-url still works, and the writer below surfaces any real failure.
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Venue: DIRECT connection (bring-your-own-RDS / no Ops Lambda) vs the ops-Lambda invoke.
|
|
333
|
+
// --database-url is explicit; --direct reads the ADMIN_DATABASE_URL secret so master never
|
|
334
|
+
// lands on argv. No flag = ops-Lambda venue, with an auto-fallback to the secret-backed
|
|
335
|
+
// direct connection when the deployed handler has no dbPlugin. --stage always names the
|
|
336
|
+
// secret store the new credentials are written to.
|
|
337
|
+
let venue: { url: string; source: 'flag' | 'secret' } | null;
|
|
338
|
+
try {
|
|
339
|
+
venue = resolveProvisionDirectUrl({ flagUrl: flags['database-url'], direct: !!flags.direct, secrets: stageSecrets });
|
|
340
|
+
} catch (err: any) {
|
|
341
|
+
fail(err.message);
|
|
342
|
+
process.exit(1);
|
|
343
|
+
}
|
|
344
|
+
let directUrl = venue?.url;
|
|
275
345
|
if (directUrl) {
|
|
276
|
-
info(`Region: ${config.region}, venue: direct connection (bring-your-own database)`);
|
|
346
|
+
info(`Region: ${config.region}, venue: direct connection (${venue!.source === 'secret' ? 'ADMIN_DATABASE_URL secret' : 'bring-your-own database'})`);
|
|
277
347
|
} else {
|
|
278
348
|
info(`Region: ${config.region}, Function: ${opsFunction(config)}`);
|
|
279
349
|
}
|
|
@@ -286,33 +356,39 @@ export async function dbProvisionCommand(flags: Record<string, string>): Promise
|
|
|
286
356
|
const authPassword = randomBytes(24).toString('hex');
|
|
287
357
|
const adminPassword = randomBytes(24).toString('hex');
|
|
288
358
|
|
|
359
|
+
// Run the role chain over a direct connection to `url` — shared by the explicit direct venue
|
|
360
|
+
// and the auto-fallback below.
|
|
361
|
+
const provisionDirect = async (url: string): Promise<any> => {
|
|
362
|
+
const { runProvision } = await loadServerProvision();
|
|
363
|
+
const { createUrlRunner } = await import('../db-source.js');
|
|
364
|
+
const { runner, end } = await createUrlRunner(url);
|
|
365
|
+
try {
|
|
366
|
+
return await runProvision({ authPassword, adminPassword }, {
|
|
367
|
+
execute: async (statement) => runner(statement),
|
|
368
|
+
connection: parseUrlConnection(url),
|
|
369
|
+
// Same probe as the ops-Lambda venue: a real login as the new role, DDL-capable.
|
|
370
|
+
connectProbe: async (probeUrl) => {
|
|
371
|
+
const { default: postgres } = await import('postgres');
|
|
372
|
+
const probe = postgres(probeUrl, { max: 1, connect_timeout: 5, ssl: 'prefer' });
|
|
373
|
+
try {
|
|
374
|
+
await probe`SELECT 1`;
|
|
375
|
+
await probe.unsafe('CREATE TEMP TABLE _everystack_provision_probe (x int)');
|
|
376
|
+
await probe.unsafe('DROP TABLE _everystack_provision_probe');
|
|
377
|
+
} finally {
|
|
378
|
+
await probe.end({ timeout: 1 });
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
});
|
|
382
|
+
} finally {
|
|
383
|
+
await end?.();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
|
|
289
387
|
let result: any;
|
|
290
388
|
let conn: any;
|
|
291
389
|
if (directUrl) {
|
|
292
390
|
try {
|
|
293
|
-
|
|
294
|
-
const { createUrlRunner } = await import('../db-source.js');
|
|
295
|
-
const { runner, end } = await createUrlRunner(directUrl);
|
|
296
|
-
try {
|
|
297
|
-
result = await runProvision({ authPassword, adminPassword }, {
|
|
298
|
-
execute: async (statement) => runner(statement),
|
|
299
|
-
connection: parseUrlConnection(directUrl),
|
|
300
|
-
// Same probe as the ops-Lambda venue: a real login as the new role, DDL-capable.
|
|
301
|
-
connectProbe: async (probeUrl) => {
|
|
302
|
-
const { default: postgres } = await import('postgres');
|
|
303
|
-
const probe = postgres(probeUrl, { max: 1, connect_timeout: 5, ssl: 'prefer' });
|
|
304
|
-
try {
|
|
305
|
-
await probe`SELECT 1`;
|
|
306
|
-
await probe.unsafe('CREATE TEMP TABLE _everystack_provision_probe (x int)');
|
|
307
|
-
await probe.unsafe('DROP TABLE _everystack_provision_probe');
|
|
308
|
-
} finally {
|
|
309
|
-
await probe.end({ timeout: 1 });
|
|
310
|
-
}
|
|
311
|
-
},
|
|
312
|
-
});
|
|
313
|
-
} finally {
|
|
314
|
-
await end?.();
|
|
315
|
-
}
|
|
391
|
+
result = await provisionDirect(directUrl);
|
|
316
392
|
} catch (err: any) {
|
|
317
393
|
fail(`db:provision failed: ${err.message}`);
|
|
318
394
|
process.exit(1);
|
|
@@ -322,23 +398,34 @@ export async function dbProvisionCommand(flags: Record<string, string>): Promise
|
|
|
322
398
|
try {
|
|
323
399
|
result = await invokeAction(config.region, opsFunction(config), 'db:provision', { authPassword, adminPassword });
|
|
324
400
|
} catch (err: any) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
401
|
+
// Auto-fallback: no Ops Lambda (Unknown action), but an ADMIN_DATABASE_URL secret exists —
|
|
402
|
+
// provision directly over it. Master never crosses the CLI; the operator sets nothing new.
|
|
403
|
+
const secretUrl = stageSecrets.ADMIN_DATABASE_URL ?? stageSecrets.AdminDatabaseUrl;
|
|
404
|
+
if (/Unknown action/i.test(String(err.message)) && secretUrl) {
|
|
405
|
+
info('No Ops Lambda (the deployed handler has no dbPlugin) — provisioning directly over the ADMIN_DATABASE_URL secret.');
|
|
406
|
+
try {
|
|
407
|
+
result = await provisionDirect(secretUrl);
|
|
408
|
+
directUrl = secretUrl;
|
|
409
|
+
conn = parseUrlConnection(secretUrl);
|
|
410
|
+
} catch (err2: any) {
|
|
411
|
+
fail(`db:provision failed: ${err2.message}`);
|
|
412
|
+
process.exit(1);
|
|
413
|
+
}
|
|
414
|
+
} else {
|
|
415
|
+
fail(`db:provision failed: ${err.message}`);
|
|
416
|
+
if (/Unknown action/i.test(String(err.message))) {
|
|
417
|
+
info('The deployed handler has no dbPlugin (no Ops Lambda), and no ADMIN_DATABASE_URL secret is set.');
|
|
418
|
+
info('Provision directly: everystack db:provision --stage <stage> --database-url <master-url> (once), or set the ADMIN_DATABASE_URL secret and use --direct.');
|
|
419
|
+
}
|
|
420
|
+
for (const line of opsAdviceLines(err, [IAM_ADVICE])) info(line);
|
|
421
|
+
process.exit(1);
|
|
329
422
|
}
|
|
330
|
-
for (const line of opsAdviceLines(err, [IAM_ADVICE])) info(line);
|
|
331
|
-
process.exit(1);
|
|
332
423
|
}
|
|
333
424
|
}
|
|
334
425
|
if (result?.error) {
|
|
335
426
|
// Includes the admin-verification refusal: the action set NO authenticator password,
|
|
336
427
|
// so nothing here rewrites DATABASE_URL — the working credentials are untouched.
|
|
337
428
|
fail(`db:provision failed: ${result.error}`);
|
|
338
|
-
if (!directUrl && /Unknown action/i.test(String(result.error))) {
|
|
339
|
-
info('The deployed handler has no dbPlugin (no Ops Lambda). For a bring-your-own database,');
|
|
340
|
-
info('run the DIRECT venue instead: everystack db:provision --stage <stage> --database-url <master-url>');
|
|
341
|
-
}
|
|
342
429
|
process.exit(1);
|
|
343
430
|
}
|
|
344
431
|
success(`Role chain ready: ${result.loginRole} (LOGIN, NOINHERIT, no BYPASSRLS) → can SET ROLE to ${result.appRoles.join(', ')}`);
|
|
@@ -360,24 +447,47 @@ export async function dbProvisionCommand(flags: Record<string, string>): Promise
|
|
|
360
447
|
info('Provide a db:psql connectionInfo callback, or set the DATABASE_URL secret yourself.');
|
|
361
448
|
process.exit(1);
|
|
362
449
|
}
|
|
363
|
-
|
|
450
|
+
// Echo the NON-SECRET target so the operator can confirm which database the secrets landed on,
|
|
451
|
+
// WITHOUT psql'ing the master in to check the prompt (a consumer burned time on a phantom
|
|
452
|
+
// wrong-database scare, and that check meant re-handling the master credential).
|
|
453
|
+
success(`Target database: ${conn.database} @ ${conn.host}:${conn.port ?? 5432} — secrets for stage "${flags.stage}" will point here.`);
|
|
454
|
+
|
|
455
|
+
// Bake the app's declared schemas into the minted URLs, so a multi-schema API connects with
|
|
456
|
+
// the right search_path instead of going dark on bare refs (best-effort: a modelless app or
|
|
457
|
+
// an all-public one changes nothing).
|
|
458
|
+
let declaredSchemas: string[] = [];
|
|
459
|
+
try {
|
|
460
|
+
const { resolveModelsPath } = await import('../models-path.js');
|
|
461
|
+
const { loadModels } = await import('./db-generate.js');
|
|
462
|
+
const models = await loadModels(resolveModelsPath(flags.models));
|
|
463
|
+
declaredSchemas = models.map((m: any) => m.schema ?? 'public');
|
|
464
|
+
} catch {
|
|
465
|
+
// No models resolvable here — leave the URLs schema-agnostic.
|
|
466
|
+
}
|
|
467
|
+
const searchPath = declaredSearchPath(declaredSchemas);
|
|
468
|
+
if (searchPath.length) info(` search_path baked into DATABASE_URL: ${searchPath.join(', ')}`);
|
|
469
|
+
|
|
470
|
+
const authUrl = withSearchPath(
|
|
471
|
+
`postgresql://${result.loginRole}:${authPassword}@${conn.host}:${conn.port ?? 5432}/${conn.database}`,
|
|
472
|
+
declaredSchemas,
|
|
473
|
+
);
|
|
364
474
|
const adminUrl = result.adminRole
|
|
365
|
-
?
|
|
475
|
+
? withSearchPath(
|
|
476
|
+
`postgresql://${result.adminRole}:${adminPassword}@${conn.host}:${conn.port ?? 5432}/${conn.database}`,
|
|
477
|
+
declaredSchemas,
|
|
478
|
+
)
|
|
366
479
|
: undefined;
|
|
367
480
|
|
|
368
481
|
// Write the secrets directly to the SST secret store — never display them. One blob
|
|
369
482
|
// write updates every key together, so there is no window where the API side is the
|
|
370
|
-
// authenticator but the operator side is still unset.
|
|
483
|
+
// authenticator but the operator side is still unset. Reuse the secrets loaded up front.
|
|
371
484
|
step('Writing the database secrets (not displayed)...');
|
|
372
485
|
let plan: ProvisionSecretPlan;
|
|
373
486
|
try {
|
|
374
|
-
const {
|
|
375
|
-
const { parseAppName } = await import('../discover.js');
|
|
376
|
-
const ctx = { appName: await parseAppName(), stage: flags.stage, region: config.region };
|
|
377
|
-
const secrets = await loadSstSecrets(ctx);
|
|
487
|
+
const secrets = { ...stageSecrets };
|
|
378
488
|
plan = buildProvisionSecretPlan({ result, authUrl, adminUrl, existing: secrets });
|
|
379
489
|
Object.assign(secrets, plan.updates);
|
|
380
|
-
await putSstSecrets(
|
|
490
|
+
await putSstSecrets(secretsCtx, secrets);
|
|
381
491
|
} catch (err: any) {
|
|
382
492
|
fail(`Failed to write the database secrets: ${err.message}`);
|
|
383
493
|
info('Ensure your IAM role can write the SST secret store (s3:PutObject, kms:Encrypt).');
|
|
@@ -418,17 +528,26 @@ export async function dbDoctorCommand(flags: Record<string, string>): Promise<vo
|
|
|
418
528
|
info(`Region: ${config.region}, Function: ${opsFunction(config)}`);
|
|
419
529
|
step('Probing database connections (api + operator)...');
|
|
420
530
|
|
|
531
|
+
const noOpsHint = () => {
|
|
532
|
+
// Name the cause, not the internal dispatch: a bring-your-own-DB app has no Ops Lambda, so
|
|
533
|
+
// db:doctor's action can't be dispatched. It has no direct venue yet — say so plainly.
|
|
534
|
+
info('This deployed handler has no dbPlugin (no Ops Lambda), so db:doctor has nothing to run against.');
|
|
535
|
+
info('db:doctor has no --database-url venue yet (unlike db:provision). To verify a bring-your-own database meanwhile, apply grants with `db:reconcile --apply --database-url <url>` and inspect roles/grants with psql. A direct db:doctor venue is on the way.');
|
|
536
|
+
};
|
|
537
|
+
|
|
421
538
|
let report: any;
|
|
422
539
|
try {
|
|
423
540
|
report = await invokeAction(config.region, opsFunction(config), 'db:doctor', {});
|
|
424
541
|
} catch (err: any) {
|
|
425
542
|
fail(`db:doctor failed: ${err.message}`);
|
|
426
|
-
|
|
543
|
+
if (/Unknown action/i.test(String(err.message))) noOpsHint();
|
|
544
|
+
else for (const line of opsAdviceLines(err, [IAM_ADVICE])) info(line);
|
|
427
545
|
process.exit(1);
|
|
428
546
|
}
|
|
429
547
|
|
|
430
548
|
if (report?.error) {
|
|
431
549
|
fail(`db:doctor failed: ${report.error}`);
|
|
550
|
+
if (/Unknown action/i.test(String(report.error))) noOpsHint();
|
|
432
551
|
process.exit(1);
|
|
433
552
|
}
|
|
434
553
|
|
package/src/cli/index.ts
CHANGED
|
@@ -353,7 +353,7 @@ Usage:
|
|
|
353
353
|
everystack db:psql --stage <name> Interactive ADMIN psql (IAM-gated; resolves the admin URL in-process)
|
|
354
354
|
everystack db:psql [--stage <name>] -c <command> Run one query via Lambda (works for private RDS)
|
|
355
355
|
everystack db:doctor [--stage <name>] Check the DB is least-privilege + RLS-subject (api vs operator connection)
|
|
356
|
-
everystack db:provision --stage <name> [--database-url <url>] Create the least-privilege role chain on an EXISTING database (idempotent; creates no DB).
|
|
356
|
+
everystack db:provision --stage <name> [--direct | --database-url <url>] Create the least-privilege role chain on an EXISTING database (idempotent; creates no DB). No flag = ops-Lambda venue (auto-falls to direct via the ADMIN_DATABASE_URL secret if the handler has no dbPlugin). --direct = direct connection reading that secret (master never on argv); --database-url = explicit URL. Declared schemas are baked into the minted DATABASE_URL as search_path
|
|
357
357
|
everystack db:snapshot [--stage <name>] [--instance <id>] Take a physical RDS snapshot (instant DR point; RDS only — use db:backup for portable logical backups)
|
|
358
358
|
everystack db:snapshots [--stage <name>] [--instance <id>] List manual RDS snapshots for the instance
|
|
359
359
|
everystack db:backup:probe [--stage <name>] Verify the pg_dump layer is attached + version-compatible with the server
|
|
@@ -367,7 +367,7 @@ Usage:
|
|
|
367
367
|
everystack db:pull [--stage <name> | --database-url <url>] [--schema public] [--out <dir | file.ts>] [--derived-out <file.ts>] [--abilities public-read] Introspect the live DB → render field() Models (the brownfield on-ramp). --out <dir> writes one file per model + index.ts (the default shape); --out <file.ts> writes a single module; stdout otherwise. --derived-out <file.ts> extracts the derived layer (descriptors + sequences) as its own self-contained module — alone it leaves the models untouched (the hand-maintained-barrel splice); with --out the models render omits the now-external derived layer. Every model scaffolds its authz decision as comments (db:check fails until authored); --abilities public-read stamps the common stanza (public read, admin write) uncommented — explicit generated code, never a runtime default. --matviews-as-tables renders every matview as defineMaterializedTable with INTROSPECTED fields (the canonical-sync flip: a pipeline-owned table everystack migrates but never refreshes) — names land in an exported materializedTables array to spread into your models; fields come back nullable/unkeyed (matviews carry no PK/NOT NULL) — tighten on review; add --suggest-keys to probe the LIVE rows for functionally-unique columns (one scan per matview) and surface each as a commented .primaryKey() suggestion. docs/derived-objects.md#flipping-a-matview-to-a-materialized-table---matviews-as-tables
|
|
368
368
|
Both introspect via the deployed ops Lambda by default; --database-url (or an inherited DATABASE_URL) connects directly — for a schema that exists only on a local Postgres.
|
|
369
369
|
everystack db:fingerprint [--stage <name> | --database-url <url>] [--models <barrel>] [--json] Content-address the live base schema (tables+constraints+authz) and compare against the models — MATCH/MISMATCH (exit 1), plus the unfingerprinted-objects report
|
|
370
|
-
everystack db:reconcile [--stage <name> | --database-url <url>] [--apply] [--check] [--baseline] [--rebuild] [--overwrite-drift] [--json] Reconcile the derived layer (functions/views/matviews/triggers) against the DECLARED descriptors (defineView/defineMaterializedView/defineFunction/defineSql/trigger() on models, from the barrel) — the single home (db/sql is retired; leftover .sql files fail with the migration path): plan with rebuild-cost estimates by default; --check is the CI gate; --apply executes (
|
|
370
|
+
everystack db:reconcile [--stage <name> | --database-url <url>] [--apply] [--check] [--baseline] [--rebuild] [--overwrite-drift] [--json] Reconcile the derived layer (functions/views/matviews/triggers) against the DECLARED descriptors (defineView/defineMaterializedView/defineFunction/defineSql/trigger() on models, from the barrel) — the single home (db/sql is retired; leftover .sql files fail with the migration path): plan with rebuild-cost estimates by default; --check is the CI gate; --apply executes (atomic — DDL + provenance in one transaction) and records provenance + schema_log; --apply --stage runs credential-free in the ops Lambda (no admin URL on the deployer, the db:apply twin), --apply --database-url runs direct. Hand-edits are drift (never overwritten silently). First contact with existing objects: --baseline TRUSTS live == source (records provenance, verifies nothing), --rebuild GUARANTEES it (drop+create from source). They are mutually exclusive.
|
|
371
371
|
everystack db:sync [--database-url <url>] [--models <barrel>] [--schema-out <file.ts>] [--allow-drops] [--overwrite-drift] [--baseline] [--json] Make the database match your checkout — one verb, both layers: apply the state diff (tables+authz, one transaction, verified by re-diff), reconcile the derived layer against the declared descriptors, report the resulting fingerprint vs the models' declared one. Dev databases only (direct connection required); DROPs held back unless --allow-drops; derived drift refuses unless --overwrite-drift; exit 1 when not converged
|
|
372
372
|
everystack db:diff --from-models <barrel> [--to-models db/models/index.ts] [--allow-drops] [--check] [--json] The state edge between two declared states, NO database: the SQL db:generate would produce, computed purely — CI plan previews (--check exits 1 on a non-empty edge) and computed rollbacks (swap the flags)
|
|
373
373
|
everystack db:plan [--stage <name> | --database-url <url>] [--models <barrel>] [--allow-drops] [--out db.plan.json | --out -] Mint a verified edge against a target: asks the TARGET its fingerprint, diffs the models, writes ONE reviewable plan (edge + both endpoint fingerprints). Held drops refuse the mint (--allow-drops carries destruction explicitly). Read-only — works via the ops Lambda; plans are ephemeral, never committed
|
package/src/reconcile.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @everystack/cli/reconcile — the derived-layer reconcile core, for the ops-Lambda lane.
|
|
3
|
+
*
|
|
4
|
+
* db:apply runs the STATE edge credential-free in the ops Lambda (@everystack/cli/apply). This is
|
|
5
|
+
* its DERIVED-edge twin: the ops `db:reconcile` action loads executeReconcile here and runs the
|
|
6
|
+
* whole reconcile (introspect → plan → apply) on the operator connection, so `db:reconcile --apply
|
|
7
|
+
* --stage` needs no raw admin URL on the deployer's machine.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export { executeReconcile, buildReconcileReport } from './cli/commands/db-reconcile.js';
|
|
11
|
+
export type { ReconcileRun, ExecuteOptions } from './cli/commands/db-reconcile.js';
|