grada-run 0.42.0 → 0.43.1

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.
Files changed (63) hide show
  1. package/bin/cli.js +1 -1
  2. package/package.json +1 -1
  3. package/src/commands/add.js +93 -11
  4. package/src/commands/alerts.js +66 -30
  5. package/src/commands/apply.js +65 -10
  6. package/src/commands/db/backup.js +1 -2
  7. package/src/commands/db/connect.js +10 -4
  8. package/src/commands/db/enable-vector.js +19 -6
  9. package/src/commands/db/import.js +114 -22
  10. package/src/commands/db/migrate.js +82 -74
  11. package/src/commands/db/restore.js +38 -6
  12. package/src/commands/destroy.js +20 -3
  13. package/src/commands/diagnose.js +40 -21
  14. package/src/commands/doctor.js +27 -2
  15. package/src/commands/domain.js +20 -2
  16. package/src/commands/drift.js +2 -1
  17. package/src/commands/eject.js +13 -2
  18. package/src/commands/gc.js +100 -3
  19. package/src/commands/init.js +75 -9
  20. package/src/commands/logs.js +3 -1
  21. package/src/commands/mcp.js +2 -1
  22. package/src/commands/rollback.js +54 -5
  23. package/src/commands/secrets.js +260 -64
  24. package/src/commands/status.js +4 -1
  25. package/src/commands/sync-ai.js +4 -3
  26. package/src/commands/wake.js +45 -1
  27. package/src/data/action-pins.js +26 -0
  28. package/src/utils/addons.js +9 -6
  29. package/src/utils/ai-rules.js +3 -2
  30. package/src/utils/backup.js +16 -2
  31. package/src/utils/command.js +20 -0
  32. package/src/utils/db-tunnel.js +15 -5
  33. package/src/utils/detector.js +8 -0
  34. package/src/utils/ecs-runner.js +16 -3
  35. package/src/utils/frameworks.js +14 -2
  36. package/src/utils/generator.js +21 -13
  37. package/src/utils/prompts.js +20 -4
  38. package/src/utils/resolvers.js +9 -7
  39. package/src/utils/revision-discovery.js +19 -7
  40. package/templates/docker/django.Dockerfile +2 -2
  41. package/templates/docker/go.Dockerfile +2 -2
  42. package/templates/docker/nestjs.Dockerfile +2 -2
  43. package/templates/docker/nextjs.Dockerfile +3 -3
  44. package/templates/docker/node.Dockerfile +2 -2
  45. package/templates/docker/nuxt.Dockerfile +2 -2
  46. package/templates/docker/python.Dockerfile +2 -2
  47. package/templates/docker/rails.Dockerfile +2 -2
  48. package/templates/docker/static.Dockerfile +2 -2
  49. package/templates/docker/svelte.Dockerfile +2 -2
  50. package/templates/github/deploy-lambda.yml +55 -12
  51. package/templates/github/deploy-static.yml +24 -6
  52. package/templates/github/deploy.yml +96 -13
  53. package/templates/github/drift.yml +68 -5
  54. package/templates/github/preview-lambda.yml +137 -13
  55. package/templates/github/preview.yml +135 -13
  56. package/templates/github/teardown.yml +71 -12
  57. package/templates/terraform/addons/s3.tf +35 -0
  58. package/templates/terraform/backend.tf +7 -0
  59. package/templates/terraform/database-mysql.tf +9 -1
  60. package/templates/terraform/database.tf +9 -1
  61. package/templates/terraform/oidc.tf +3 -2
  62. package/templates/terraform/secrets.tf +6 -4
  63. package/templates/terraform/worker.tf +7 -0
@@ -3,7 +3,10 @@ import { ECSClient } from '@aws-sdk/client-ecs';
3
3
  import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
4
4
  import { spawn } from 'child_process';
5
5
  import fsSync from 'node:fs';
6
- import { createReadStream } from 'node:fs';
6
+ import { createReadStream, createWriteStream } from 'node:fs';
7
+ import { tmpdir } from 'node:os';
8
+ import { basename, join } from 'node:path';
9
+ import { pipeline } from 'node:stream/promises';
7
10
  import zlib from 'node:zlib';
8
11
  import color from 'picocolors';
9
12
  import { intro, outro, spinner, select, text, password, confirm, cancel, isCancel } from '@clack/prompts';
@@ -66,13 +69,26 @@ export function parseDbImportArgs(argv = []) {
66
69
  return options;
67
70
  }
68
71
 
72
+ // True for Postgres custom-archive kinds ('dump', 'dump-gzip'): both
73
+ // restore via pg_restore and share the MySQL refusal. Pure and
74
+ // unit-tested via the classifier tests.
75
+ export function isCustomArchiveFormat(kind) {
76
+ return kind === 'dump' || kind === 'dump-gzip';
77
+ }
78
+
69
79
  // Classifies an import file by extension: 'dump' (Postgres custom
70
- // archive), 'gzip' (.sql.gz / .gz), or 'sql' (plain text, the default for
71
- // unknown extensions). Pure and unit-tested.
80
+ // archive, including pgAdmin's `.backup`), 'dump-gzip' (a custom archive
81
+ // behind gzip — gunzipped to temp, never piped to psql), 'gzip'
82
+ // (.sql.gz / .gz), or 'sql' (plain text, the default for unknown
83
+ // extensions). Pure and unit-tested.
72
84
  export function classifyImportFile(filePath) {
73
85
  const lower = String(filePath || '').toLowerCase();
74
- if (lower.endsWith('.dump')) return 'dump';
75
- if (lower.endsWith('.gz')) return 'gzip';
86
+ if (lower.endsWith('.dump') || lower.endsWith('.backup')) return 'dump';
87
+ if (lower.endsWith('.gz')) {
88
+ const inner = lower.slice(0, -'.gz'.length);
89
+ if (inner.endsWith('.dump') || inner.endsWith('.backup')) return 'dump-gzip';
90
+ return 'gzip';
91
+ }
76
92
  return 'sql';
77
93
  }
78
94
 
@@ -83,7 +99,7 @@ export function requiredClientBinaries({ targetScheme, source }) {
83
99
  if (source.type === 'url') {
84
100
  return targetScheme === 'mysql' ? ['mysqldump', 'mysql'] : ['pg_dump', 'psql'];
85
101
  }
86
- if (source.kind === 'dump') return ['pg_restore'];
102
+ if (isCustomArchiveFormat(source.kind)) return ['pg_restore'];
87
103
  return targetScheme === 'mysql' ? ['mysql'] : ['psql'];
88
104
  }
89
105
 
@@ -106,7 +122,7 @@ export function buildTargetClientCommand({ targetScheme, localPort, username, db
106
122
  args: ['-h', '127.0.0.1', '-P', String(localPort), '-u', username, dbName],
107
123
  };
108
124
  }
109
- if (fileKind === 'dump') {
125
+ if (isCustomArchiveFormat(fileKind)) {
110
126
  return {
111
127
  bin: 'pg_restore',
112
128
  args: ['--no-owner', '--no-acl', '--no-password', '-h', '127.0.0.1', '-p', String(localPort), '-U', username, '-d', dbName, filePath],
@@ -142,13 +158,30 @@ function targetEnv(targetScheme, password) {
142
158
  return { ...process.env, PGPASSWORD: password, PGSSLMODE: process.env.PGSSLMODE || 'require' };
143
159
  }
144
160
 
161
+ // Source-side dump env: the SOURCE is arbitrary user infra (often no
162
+ // TLS), so prefer TLS instead of requiring it (BUG-148). MySQL sources
163
+ // were never forced; the target side keeps `require`.
164
+ function sourceEnv(scheme, password) {
165
+ if (scheme === 'mysql') {
166
+ return { ...process.env, MYSQL_PWD: password };
167
+ }
168
+ return { ...process.env, PGPASSWORD: password, PGSSLMODE: process.env.PGSSLMODE || 'prefer' };
169
+ }
170
+
145
171
  function waitForExit(child) {
146
172
  return new Promise((resolve) => {
147
173
  child.once('error', (error) => resolve({ code: 1, error }));
148
- child.once('close', (code) => resolve({ code: code ?? 0 }));
174
+ // Node reports signal death as code null — resolve it as failure
175
+ // (with the signal), never `?? 0` success (BUG-149).
176
+ child.once('close', (code, signal) => resolve(signal ? { code: 1, signal } : { code: code ?? 0 }));
149
177
  });
150
178
  }
151
179
 
180
+ function describeExit(result) {
181
+ if (result.signal) return `signal ${result.signal}`;
182
+ return `exit code ${result.code}`;
183
+ }
184
+
152
185
  function printDbNoTasksGuidance(service, cluster) {
153
186
  printNoTasksGuidance(service, cluster, 'to act as a jump host for the import tunnel');
154
187
  }
@@ -328,9 +361,21 @@ async function runDbImportMain(input = {}) {
328
361
  resultExtra: { cluster, service, region },
329
362
  });
330
363
  }
331
- if (source.type === 'file' && source.kind === 'dump' && targetIsMysql) {
364
+ if (source.type === 'file' && isCustomArchiveFormat(source.kind) && targetIsMysql) {
332
365
  return failCommand({
333
- message: '\n✖ Cannot restore a Postgres custom-format archive (.dump) into MySQL. Export plain SQL instead.\n',
366
+ message: `\n✖ Cannot restore a Postgres custom-format archive ("${source.path}") into MySQL. Export plain SQL instead.\n`,
367
+ event: 'db_import_run',
368
+ telemetry: { projectName, source: source.type },
369
+ errorCode: 'UNSUPPORTED_IMPORT_FORMAT',
370
+ reason: 'unsupported-import-format',
371
+ resultExtra: { cluster, service, region },
372
+ });
373
+ }
374
+ if (source.type === 'url' && (source.scheme === 'mysql') !== targetIsMysql) {
375
+ const sourceLabel = source.scheme === 'mysql' ? 'MySQL' : 'Postgres';
376
+ const targetLabel = targetIsMysql ? 'MySQL' : 'Postgres';
377
+ return failCommand({
378
+ message: `\n✖ Cannot import from ${sourceLabel} ("${source.redacted}") into ${targetLabel}: dump dialects are not portable across engines. Migrate the schema first, then import same-engine SQL.\n`,
334
379
  event: 'db_import_run',
335
380
  telemetry: { projectName, source: source.type },
336
381
  errorCode: 'UNSUPPORTED_IMPORT_FORMAT',
@@ -519,9 +564,7 @@ async function runDbImportMain(input = {}) {
519
564
  const dump = buildSourceDumpCommand(source);
520
565
  const dumpChild = spawnImpl(dump.bin, dump.args, {
521
566
  stdio: ['ignore', 'pipe', 'inherit'],
522
- env: source.scheme === 'mysql'
523
- ? { ...process.env, MYSQL_PWD: source.password }
524
- : { ...process.env, PGPASSWORD: source.password, PGSSLMODE: process.env.PGSSLMODE || 'require' },
567
+ env: sourceEnv(source.scheme, source.password),
525
568
  });
526
569
  const importChild = spawnImpl(targetCmd.bin, targetCmd.args, {
527
570
  stdio: ['pipe', 'inherit', 'inherit'],
@@ -534,7 +577,7 @@ async function runDbImportMain(input = {}) {
534
577
  ]);
535
578
  if (dumpResult.code !== 0) {
536
579
  return failCommand({
537
- message: `\n✖ Source dump failed with exit code ${dumpResult.code}${dumpResult.error ? `: ${dumpResult.error.message}` : ''}.\n`,
580
+ message: `\n✖ Source dump failed with ${describeExit(dumpResult)}${dumpResult.error ? `: ${dumpResult.error.message}` : ''}.\n`,
538
581
  event: 'db_import_run',
539
582
  telemetry: { projectName, source: source.type, error_code: 'IMPORT_FAILED' },
540
583
  reason: 'import-failed',
@@ -543,22 +586,50 @@ async function runDbImportMain(input = {}) {
543
586
  }
544
587
  if (importResult.code !== 0) {
545
588
  return failCommand({
546
- message: `\n✖ Import failed with exit code ${importResult.code}${importResult.error ? `: ${importResult.error.message}` : ''}.\n`,
589
+ message: `\n✖ Import failed with ${describeExit(importResult)}${importResult.error ? `: ${importResult.error.message}` : ''}.\n`,
547
590
  event: 'db_import_run',
548
591
  telemetry: { projectName, source: source.type, error_code: 'IMPORT_FAILED' },
549
592
  reason: 'import-failed',
550
593
  resultExtra: { cluster, service, region },
551
594
  });
552
595
  }
553
- } else if (source.kind === 'dump') {
554
- const child = spawnImpl(targetCmd.bin, targetCmd.args, {
596
+ } else if (isCustomArchiveFormat(source.kind)) {
597
+ // pg_restore cannot read gzip: expand a gzipped custom
598
+ // archive to a temp file first (never piped to psql as SQL).
599
+ let restoreFilePath = source.path;
600
+ let tempDir = null;
601
+ if (source.kind === 'dump-gzip') {
602
+ tempDir = fsSync.mkdtempSync(join(tmpdir(), 'grada-import-'));
603
+ const stem = basename(source.path).replace(/\.gz$/i, '').replace(/\.(dump|backup)$/i, '') || 'restore';
604
+ restoreFilePath = join(tempDir, `${stem}.dump`);
605
+ try {
606
+ await pipeline(createReadStream(source.path), zlib.createGunzip(), createWriteStream(restoreFilePath));
607
+ } catch (error) {
608
+ try { fsSync.rmSync(tempDir, { recursive: true, force: true }); } catch { /* best-effort */ }
609
+ return failCommand({
610
+ message: `\n✖ Could not gunzip import file "${source.path}": ${error.message}.\n`,
611
+ event: 'db_import_run',
612
+ telemetry: { projectName, source: source.type, error_code: 'IMPORT_READ_FAILED' },
613
+ reason: 'import-failed',
614
+ resultExtra: { cluster, service, region },
615
+ });
616
+ }
617
+ }
618
+ // pg_restore takes the archive path as its final argv element.
619
+ const restoreCmd = tempDir
620
+ ? { ...targetCmd, args: [...targetCmd.args.slice(0, -1), restoreFilePath] }
621
+ : targetCmd;
622
+ const child = spawnImpl(restoreCmd.bin, restoreCmd.args, {
555
623
  stdio: ['ignore', 'inherit', 'inherit'],
556
624
  env: targetEnv(targetScheme, creds.password),
557
625
  });
558
626
  const result = await waitForExit(child);
627
+ if (tempDir) {
628
+ try { fsSync.rmSync(tempDir, { recursive: true, force: true }); } catch { /* best-effort */ }
629
+ }
559
630
  if (result.code !== 0) {
560
631
  return failCommand({
561
- message: `\n✖ pg_restore failed with exit code ${result.code}${result.error ? `: ${result.error.message}` : ''}.\n`,
632
+ message: `\n✖ pg_restore failed with ${describeExit(result)}${result.error ? `: ${result.error.message}` : ''}.\n`,
562
633
  event: 'db_import_run',
563
634
  telemetry: { projectName, source: source.type, error_code: 'IMPORT_FAILED' },
564
635
  reason: 'import-failed',
@@ -572,14 +643,35 @@ async function runDbImportMain(input = {}) {
572
643
  });
573
644
  const fileStream = createReadStream(source.path);
574
645
  const input = source.kind === 'gzip' ? fileStream.pipe(zlib.createGunzip()) : fileStream;
575
- input.on('error', (error) => {
576
- try { child.stdin.destroy(error); } catch { /* handled below */ }
577
- });
646
+ // A broken file stream (corrupt gzip, unreadable file) must
647
+ // fail the import naming its own cause — never a bare psql
648
+ // exit code, and never silent success when psql exits 0 on
649
+ // truncated input. pipe() does not forward source errors, so
650
+ // both legs get the handler. stdin is destroyed WITHOUT the
651
+ // error: the captured cause below is the message, and
652
+ // injecting a second error into the child would surface as an
653
+ // unrelated EPIPE/exit instead.
654
+ let streamError = null;
655
+ const onStreamError = (error) => {
656
+ if (!streamError) streamError = error;
657
+ try { child.stdin.destroy(); } catch { /* handled below */ }
658
+ };
659
+ input.on('error', onStreamError);
660
+ if (input !== fileStream) fileStream.on('error', onStreamError);
578
661
  input.pipe(child.stdin);
579
662
  const result = await waitForExit(child);
663
+ if (streamError) {
664
+ return failCommand({
665
+ message: `\n✖ Could not read import file "${source.path}": ${streamError.message}.\n`,
666
+ event: 'db_import_run',
667
+ telemetry: { projectName, source: source.type, error_code: 'IMPORT_READ_FAILED' },
668
+ reason: 'import-failed',
669
+ resultExtra: { cluster, service, region },
670
+ });
671
+ }
580
672
  if (result.code !== 0) {
581
673
  return failCommand({
582
- message: `\n✖ Import failed with exit code ${result.code}${result.error ? `: ${result.error.message}` : ''}.\n`,
674
+ message: `\n✖ Import failed with ${describeExit(result)}${result.error ? `: ${result.error.message}` : ''}.\n`,
583
675
  event: 'db_import_run',
584
676
  telemetry: { projectName, source: source.type, error_code: 'IMPORT_FAILED' },
585
677
  reason: 'import-failed',
@@ -3,7 +3,7 @@ import { CloudWatchLogsClient } from '@aws-sdk/client-cloudwatch-logs';
3
3
  import color from 'picocolors';
4
4
  import { intro, outro, spinner, text, isCancel } from '@clack/prompts';
5
5
  import { trackEvent, trackSuccess, setActiveCommandName, resetActiveCommandName, getCliVersion } from '../../core/telemetry.js';
6
- import { failCommand, failAwsError, trackAwsFailure, failProjectNotInitialized, refuseUnknownFlags } from '../../utils/command.js';
6
+ import { failCommand, failAwsError, failNamedContainerMissing, trackAwsFailure, failProjectNotInitialized, refuseUnknownFlags } from '../../utils/command.js';
7
7
  import { parseFlags, normalizeOptions, normalizeArgv } from '../../utils/args.js';
8
8
  import { isAuthError, handleAuthErrorBranch, resolveClient } from '../../utils/aws.js';
9
9
  import {
@@ -22,6 +22,8 @@ import { detectMigrationCommand, isNpmShimCommand, dockerfileStripsNpmShims } fr
22
22
  import { resolveContainer } from '../../utils/ecs.js';
23
23
  import { runEphemeralEcsTask } from '../../utils/ecs-runner.js';
24
24
  import { parseTimeoutSeconds } from '../../utils/system.js';
25
+ import { atomicWriteFile } from '../../utils/atomic-write.js';
26
+ import { pinnedActionUse } from '../../data/action-pins.js';
25
27
  import fsSync from 'fs';
26
28
  import path from 'path';
27
29
 
@@ -89,7 +91,7 @@ function renderGateBlock({ includeSetupNode, stepName, runLine }) {
89
91
  if (includeSetupNode) {
90
92
  lines.push(
91
93
  ' - name: Setup Node.js for migrations',
92
- ' uses: actions/setup-node@v4',
94
+ ` uses: ${pinnedActionUse('actions/setup-node')}`,
93
95
  ' with:',
94
96
  " node-version: '20'",
95
97
  ''
@@ -97,6 +99,10 @@ function renderGateBlock({ includeSetupNode, stepName, runLine }) {
97
99
  }
98
100
  lines.push(
99
101
  ` - name: ${stepName}`,
102
+ // Gates run AWS calls: honor the credential-skip outcome like
103
+ // every other post-assume step (BUG-138). Neutral on deploy
104
+ // (a failed assume fails the run before any gate runs).
105
+ ` if: steps.aws-creds.outcome == 'success'`,
100
106
  ' run: |',
101
107
  ` ${runLine}`,
102
108
  ` ${GATE_END_MARKER}`
@@ -160,6 +166,41 @@ export function injectPreviewMigrationGate(workflowContent, { cmd, cliVersion }
160
166
  });
161
167
  }
162
168
 
169
+ // Sniffs the URL scheme from discrete credentials. Explicit values win;
170
+ // when DB_ENGINE/DB_PORT are secrets-sourced (valueFrom, values unknown
171
+ // to the CLI) the reference text is sniffed for engine/port hints
172
+ // instead (BUG-171). Indeterminate stays postgresql (back-compat).
173
+ function sniffMigrationScheme(entries, values) {
174
+ const engine = values.get('DB_ENGINE');
175
+ if (typeof engine === 'string' && engine !== '') {
176
+ return /mysql|maria/i.test(engine) ? 'mysql' : 'postgresql';
177
+ }
178
+ const port = values.get('DB_PORT');
179
+ if (typeof port === 'string' && port !== '') {
180
+ return port === '3306' ? 'mysql' : 'postgresql';
181
+ }
182
+ const refs = entries.map((entry) => entry?.valueFrom).filter((ref) => typeof ref === 'string').join('\n');
183
+ if (/mysql|maria|3306/i.test(refs)) return 'mysql';
184
+ return 'postgresql';
185
+ }
186
+
187
+ // Runtime userinfo encoder for the synthesized URL (BUG-139): node sees
188
+ // only shell references, never the values, so the encoding ladder ships
189
+ // inside the script — node (encodeURIComponent) → sed (the URL-breaking
190
+ // subset: %, ?, #, @, :, /) → warned raw. Quoting alone cannot save a
191
+ // `?` or `#` in the password: the URL parser (not the shell) is what
192
+ // breaks, so the shell refs must be encoded, not quoted.
193
+ function buildEncodedUrlExport({ scheme, defaultPort, defaultName }) {
194
+ const nodeEncode = `node -e 'var s="${scheme}",a=process.argv.slice(1);console.log(s+"://"+encodeURIComponent(a[0])+":"+encodeURIComponent(a[1])+"@"+a[2]+":"+a[3]+"/"+a[4])' "$_u" "$_p" "$_h" "$_o" "$_n"`;
195
+ const sedEncode = `printf '${scheme}://%s:%s@%s:%s/%s' "$(_grada_enc "$_u")" "$(_grada_enc "$_p")" "$_h" "$_o" "$_n"`;
196
+ const rawEncode = `printf '${scheme}://%s:%s@%s:%s/%s' "$_u" "$_p" "$_h" "$_o" "$_n"`;
197
+ return `_grada_db_url() { _u="\${DB_USER}"; _p="\${DB_PASSWORD}"; _h="\${DB_HOST}"; _o="\${DB_PORT:-${defaultPort}}"; _n="\${DB_NAME:-${defaultName}}"; `
198
+ + `if command -v node >/dev/null 2>&1; then ${nodeEncode}; `
199
+ + `elif command -v sed >/dev/null 2>&1; then _grada_enc() { printf '%s' "$1" | sed -e 's/%/%25/g' -e 's/?/%3F/g' -e 's/#/%23/g' -e 's/@/%40/g' -e 's/:/%3A/g' -e 's|/|%2F|g'; }; ${sedEncode}; `
200
+ + `else echo "grada: warning: DATABASE_URL built without userinfo encoding (no node or sed in image)" >&2; ${rawEncode}; fi; }; `
201
+ + `if [ -z "\${DATABASE_URL:-}" ]; then export DATABASE_URL="$(_grada_db_url)"; fi`;
202
+ }
203
+
163
204
  // Builds the container override command for the migration task. When the
164
205
  // task definition provides discrete `DB_*` credentials but no
165
206
  // `DATABASE_URL`, the command exports a runtime-synthesized URL (from the
@@ -176,76 +217,17 @@ export function buildMigrationCommand(resolvedCmd, containerDef) {
176
217
  return ['sh', '-c', cmd];
177
218
  }
178
219
  const values = new Map(entries.map((entry) => [entry?.name, entry?.value]));
179
- const scheme = values.get('DB_PORT') === '3306' || values.get('DB_ENGINE') === 'mysql' ? 'mysql' : 'postgresql';
220
+ const scheme = sniffMigrationScheme(entries, values);
180
221
  // RDS/Aurora enforces `rds.force_ssl = 1` by default: require TLS for
181
222
  // synthesized postgres URLs so libpq-based migrations never fail on a
182
223
  // plaintext connection (MySQL clients ignore PGSSLMODE; leave them alone).
183
224
  const tlsPrefix = scheme === 'postgresql' ? 'export PGSSLMODE="${PGSSLMODE:-require}"; ' : '';
184
- return ['sh', '-c', `${tlsPrefix}export DATABASE_URL="\${DATABASE_URL:-${scheme}://\${DB_USER}:\${DB_PASSWORD}@\${DB_HOST}:\${DB_PORT:-5432}/\${DB_NAME:-postgres}}"; ${cmd}`];
185
- }
186
-
187
- // Identity keys for one log line. FilterLogEvents results carry `eventId`
188
- // but GetLogEvents results do not, so every line registers its
189
- // timestamp:message fingerprint (present in both APIs) plus its `eventId`
190
- // when it has one — the final head-flush then never reprints live lines.
191
- function eventKeys(event) {
192
- const keys = [`${event.timestamp ?? ''}:${event.message ?? ''}`];
193
- if (event.eventId) keys.push(`id:${event.eventId}`);
194
- return keys;
195
- }
196
-
197
- function printNewEvents(events, seen) {
198
- if (seen.size > 5000) seen.clear();
199
- for (const event of events || []) {
200
- const keys = eventKeys(event);
201
- if (keys.some((key) => seen.has(key))) continue;
202
- for (const key of keys) seen.add(key);
203
- console.log(String(event.message ?? '').replace(/\n$/, ''));
204
- }
205
- }
206
-
207
- // Live tail: FilterLogEvents scoped to the single task stream. `startTime`
208
- // is deliberately omitted — the stream is unique per task run, so there is
209
- // nothing stale to exclude and local clock skew can never drop early lines.
210
- async function fetchNewLogEvents({ logsClient, logGroupName, logStreamName, seen }) {
211
- let events = [];
212
- try {
213
- const resp = await logsClient.send(new FilterLogEventsCommand({
214
- logGroupName,
215
- logStreamNames: [logStreamName],
216
- }));
217
- events = resp.events || [];
218
- } catch (error) {
219
- // The stream does not exist until the container starts emitting.
220
- if (isNotFoundError(error)) return;
221
- throw error;
222
- }
223
- printNewEvents(events, seen);
224
- }
225
-
226
- // Final flush: GetLogEvents reads the exact stream from the head, catching
227
- // anything the live tail missed (Fargate ingestion lags several seconds on
228
- // sub-second commands). Shares the `seen` set so lines print exactly once.
229
- async function flushRemainingLogs({ logsClient, logGroupName, logStreamName, seen }) {
230
- let nextToken;
231
- for (;;) {
232
- let resp;
233
- try {
234
- resp = await logsClient.send(new GetLogEventsCommand({
235
- logGroupName,
236
- logStreamName,
237
- startFromHead: true,
238
- ...(nextToken ? { nextToken } : {}),
239
- }));
240
- } catch (error) {
241
- if (isNotFoundError(error)) return;
242
- throw error;
243
- }
244
- printNewEvents(resp.events, seen);
245
- const forward = resp.nextForwardToken;
246
- if (!forward || forward === nextToken) return;
247
- nextToken = forward;
248
- }
225
+ const urlExport = buildEncodedUrlExport({
226
+ scheme,
227
+ defaultPort: scheme === 'mysql' ? '3306' : '5432',
228
+ defaultName: scheme === 'mysql' ? 'mysql' : 'postgres',
229
+ });
230
+ return ['sh', '-c', `${tlsPrefix}${urlExport}; ${cmd}`];
249
231
  }
250
232
 
251
233
  // Programmatic entry wrapper: stamps cli_command for telemetry on every
@@ -357,7 +339,7 @@ async function runDbMigrateMain(input = {}) {
357
339
  if (headless) {
358
340
  return failCommand({
359
341
  message: `\n✖ Refusing to run "${resolvedCmd}": your Dockerfile strips npm/npx from the runner image, so this command would fail with "command not found" inside the task.\n`,
360
- hint: ' Bundle a framework-compiled runner instead and re-run with an explicit node-based command, e.g. --cmd "node ./dist/migrate.cjs".\n',
342
+ hint: ' Bundle a framework-compiled runner instead and re-run with an explicit node-based command, e.g. --cmd "node ./dist/migrate.cjs". Ensure your Dockerfile builds the bundle and copies it into the runner image.\n',
361
343
  event: 'db_migrate_run',
362
344
  telemetry: { projectName, cmd_source: cmdSource, ci_setup: setupCi, error_code: 'NPM_CMD_STRIPPED_RUNNER' },
363
345
  errorCode: 'NPM_CMD_STRIPPED_RUNNER',
@@ -365,12 +347,26 @@ async function runDbMigrateMain(input = {}) {
365
347
  resultExtra: { cluster, service, region },
366
348
  });
367
349
  }
368
- console.log(color.yellow(`⚠️ "${resolvedCmd}" starts with npm/npx but your Dockerfile strips those binaries from the runner image — this will fail with "command not found" unless you switch to a node-based command (e.g. "node ./dist/migrate.cjs").`));
350
+ console.log(color.yellow(`⚠️ "${resolvedCmd}" starts with npm/npx but your Dockerfile strips those binaries from the runner image — this will fail with "command not found" unless you switch to a node-based command (e.g. "node ./dist/migrate.cjs"). Ensure your Dockerfile builds the bundle and copies it into the runner image.`));
369
351
  trackEvent('npm_cmd_stripped_runner', { projectName, cmd_source: cmdSource, ci_setup: setupCi });
370
352
  }
371
353
 
372
354
  // 2. --setup-ci: patch the workflow file, no AWS calls.
373
355
  if (setupCi) {
356
+ // BUG-191: the gate runs migrations as an ephemeral ECS task —
357
+ // refuse on lambda/static (no ECS to run on). Init auto-skips
358
+ // with a warning; this explicit command fails closed.
359
+ const setupTarget = readTerraformComputeTarget(cwd);
360
+ if (setupTarget !== 'ecs') {
361
+ return failCommand({
362
+ message: `\n✖ --setup-ci wires an ECS pre-deploy migration gate, but this tree targets ${color.cyan(setupTarget)} (no ECS service to run it on).\n`,
363
+ event: 'db_migrate_run',
364
+ telemetry: { projectName, cmd_source: cmdSource, ci_setup: true },
365
+ errorCode: 'MIGRATE_SETUP_CI_UNSUPPORTED_TARGET',
366
+ reason: 'migrate-setup-ci-unsupported-target',
367
+ resultExtra: { cluster, service, region },
368
+ });
369
+ }
374
370
  const workflowFile = path.join(cwd, '.github', 'workflows', 'deploy.yml');
375
371
  let workflowContent = null;
376
372
  try {
@@ -399,7 +395,7 @@ async function runDbMigrateMain(input = {}) {
399
395
  resultExtra: { cluster, service, region },
400
396
  });
401
397
  }
402
- fsSync.writeFileSync(workflowFile, updated, 'utf8');
398
+ await atomicWriteFile(workflowFile, updated, 'utf8');
403
399
  console.log(color.green('\n✅ Pre-deploy migration gate installed in .github/workflows/deploy.yml.'));
404
400
  console.log(color.dim(' Migrations now run against the new image revision before the ECS service updates.\n'));
405
401
  if (readTerraformComputeTarget(cwd) === 'ecs') {
@@ -413,7 +409,7 @@ async function runDbMigrateMain(input = {}) {
413
409
  if (previewContent !== null) {
414
410
  const previewUpdated = injectPreviewMigrationGate(previewContent, { cmd: resolvedCmd, cliVersion: getCliVersion() });
415
411
  if (previewUpdated !== null) {
416
- fsSync.writeFileSync(previewFile, previewUpdated, 'utf8');
412
+ await atomicWriteFile(previewFile, previewUpdated, 'utf8');
417
413
  console.log(color.green('✅ Preview migration gate installed in .github/workflows/preview.yml.'));
418
414
  } else {
419
415
  console.log(color.yellow('⚠️ Could not wire the preview migration gate: the Comment PR anchor was not found in .github/workflows/preview.yml.'));
@@ -507,6 +503,14 @@ async function runDbMigrateMain(input = {}) {
507
503
  resultExtra: { cluster, service, region },
508
504
  });
509
505
  }
506
+ if (!outcome.ok && outcome.code === 'NAMED_CONTAINER_MISSING') {
507
+ return failNamedContainerMissing({
508
+ spinner: s, outcome, cluster, service, region,
509
+ event: 'db_migrate_run',
510
+ telemetry: { projectName, cmd_source: cmdSource, ci_setup: false },
511
+ noun: 'Migration',
512
+ });
513
+ }
510
514
  if (!outcome.ok && outcome.code === 'TIMEOUT') {
511
515
  try { s.stop(color.yellow('Migration timed out.')); } catch { /* already stopped */ }
512
516
  return failCommand({
@@ -528,12 +532,16 @@ async function runDbMigrateMain(input = {}) {
528
532
  return { ok: true, success: true, exitCode: 0, taskArn: outcome.taskArn };
529
533
  }
530
534
  const propagated = (typeof exitCode === 'number' && exitCode > 0) ? exitCode : 1;
535
+ // The ECS stopped reason is the only diagnosis PostHog gets for a
536
+ // failed task (exit codes alone can't distinguish image-pull from
537
+ // command-not-found from migration errors) — stamp it.
538
+ const failureDetail = `Migration failed with exit code ${exitCode ?? 'unknown'}: ${stoppedReason}`;
531
539
  return failCommand({
532
- message: `\n✖ Migration failed with exit code ${exitCode ?? 'unknown'}: ${stoppedReason}.\n`,
540
+ message: `\n✖ ${failureDetail}.\n`,
533
541
  event: 'db_migrate_run',
534
542
  telemetry: { projectName, cmd_source: cmdSource, ci_setup: false },
535
543
  errorCode: 'MIGRATION_TASK_FAILED',
536
- extra: { exit_code: exitCode ?? -1 },
544
+ extra: { exit_code: exitCode ?? -1, error_message: failureDetail },
537
545
  reason: 'migration-task-failed',
538
546
  resultExtra: { cluster, service, region, taskArn: outcome.taskArn },
539
547
  exitCode: propagated,
@@ -7,8 +7,9 @@ import { trackSuccess, setActiveCommandName, resetActiveCommandName } from '../.
7
7
  import { failCommand, failAwsError, trackAwsFailure, failProjectNotInitialized, refuseUnknownFlags } from '../../utils/command.js';
8
8
  import { parseFlags, normalizeOptions, normalizeArgv } from '../../utils/args.js';
9
9
  import { isAuthError, handleAuthErrorBranch, resolveClient } from '../../utils/aws.js';
10
- import { resolveRegion, resolveProjectName, resolveAppName, resolveHeadless, resolveCwd } from '../../utils/resolvers.js';
10
+ import { resolveRegion, resolveProjectName, resolveHeadless, resolveCwd } from '../../utils/resolvers.js';
11
11
  import { findResourceBlock } from '../../utils/hcl.js';
12
+ import { atomicWriteFile } from '../../utils/atomic-write.js';
12
13
  import { resolveDbIdentifier, resolveDbClusterIdentifier } from '../../utils/rds.js';
13
14
 
14
15
  const SNAPSHOT_NOT_FOUND_NAMES = new Set(['DBSnapshotNotFound', 'DBSnapshotNotFoundFault']);
@@ -44,14 +45,17 @@ export function parseDbRestoreArgs(argv = []) {
44
45
  // `resource "<resourceType>" "postgres"` (an `aws_db_instance` or an
45
46
  // `aws_rds_cluster`): replaces the existing attribute or inserts it (with a
46
47
  // keep-in-place comment) after the identity line. Returns the content
47
- // unchanged when the resource block cannot be found. Pure and unit-tested.
48
+ // unchanged when the resource block cannot be found. Throws
49
+ // `SNAPSHOT_PIN_EXTERNAL` when the block already pins the attribute to a
50
+ // non-literal (var reference, local) that restore must not overwrite or
51
+ // duplicate. Pure and unit-tested.
48
52
  export function upsertSnapshotIdentifier(hclContent, snapshotId, resourceType = 'aws_db_instance') {
49
53
  const content = String(hclContent ?? '');
50
54
  const bounds = findResourceBlock(content, resourceType, 'postgres');
51
55
  if (!bounds) return content;
52
56
 
53
57
  const block = content.slice(bounds.openIdx, bounds.closeIdx + 1);
54
- const attrPattern = /^[ \t]*snapshot_identifier\s*=\s*"[^"]*"\s*$/m;
58
+ const attrPattern = /^[ \t]*snapshot_identifier\s*=\s*"[^"]*"\s*(?:#.*|\/\/.*)?$/m;
55
59
  const attrMatch = attrPattern.exec(block);
56
60
 
57
61
  const indent = ' ';
@@ -68,6 +72,19 @@ export function upsertSnapshotIdentifier(hclContent, snapshotId, resourceType =
68
72
  return content.slice(0, bounds.openIdx) + next + content.slice(bounds.closeIdx + 1);
69
73
  }
70
74
 
75
+ // Any other snapshot_identifier line (a var reference, local, or
76
+ // function call) is user-managed config: replacing it would destroy
77
+ // the reference and inserting beside it would duplicate the
78
+ // attribute, so refuse loud instead of either.
79
+ const existingPattern = /^[ \t]*snapshot_identifier\s*=[^\n]*/m;
80
+ const existingMatch = existingPattern.exec(block);
81
+ if (existingMatch) {
82
+ throw Object.assign(
83
+ new Error(`terraform/database.tf already sets ${existingMatch[0].trim()} — remove it or pin a literal snapshot id before restoring.`),
84
+ { code: 'SNAPSHOT_PIN_EXTERNAL' }
85
+ );
86
+ }
87
+
71
88
  // Insert after the identity line (`identifier` or `cluster_identifier`)
72
89
  // so the restore pin sits next to the resource identity; fall back to
73
90
  // the top of the block.
@@ -184,7 +201,6 @@ async function runDbRestoreMain(input = {}) {
184
201
  cwd = resolveCwd(options);
185
202
  region = resolveRegion(options, cwd);
186
203
  projectName = resolveProjectName(options, cwd);
187
- const appName = resolveAppName(projectName, options.workspace, cwd);
188
204
  dbIdentifier = resolveDbIdentifier({ ...options, projectName }, cwd);
189
205
  dbClusterIdentifier = resolveDbClusterIdentifier({ ...options, projectName }, cwd);
190
206
  } catch {
@@ -345,8 +361,24 @@ async function runDbRestoreMain(input = {}) {
345
361
  s.stop('Snapshot selected.');
346
362
  }
347
363
 
348
- const updated = upsertSnapshotIdentifier(hclContent, snapshotId, isCluster ? 'aws_rds_cluster' : 'aws_db_instance');
349
- fsSync.writeFileSync(databaseTf, updated, 'utf8');
364
+ let updated;
365
+ try {
366
+ updated = upsertSnapshotIdentifier(hclContent, snapshotId, isCluster ? 'aws_rds_cluster' : 'aws_db_instance');
367
+ } catch (error) {
368
+ if (error?.code === 'SNAPSHOT_PIN_EXTERNAL') {
369
+ try { s.stop(color.red('Snapshot pin is externally managed.')); } catch { /* already stopped */ }
370
+ return failCommand({
371
+ message: `\n✖ ${error.message}\n`,
372
+ event: 'db_restore_run',
373
+ telemetry: { projectName },
374
+ errorCode: 'SNAPSHOT_PIN_EXTERNAL',
375
+ reason: 'snapshot-pin-external',
376
+ resultExtra: { dbIdentifier, region },
377
+ });
378
+ }
379
+ throw error;
380
+ }
381
+ await atomicWriteFile(databaseTf, updated, 'utf8');
350
382
  console.log(color.green(`\n✅ terraform/database.tf now pins snapshot_identifier = "${snapshotId}".`));
351
383
  console.log(` Run ${color.green('npx grada-run apply')} to restore the database.`);
352
384
  console.log(color.dim(' Keep snapshot_identifier in place afterwards so future applies stay no-op.\n'));
@@ -406,6 +406,7 @@ async function destroyStackMain(input = {}) {
406
406
  // 1c. Pre-destroy snapshot checkpoint (M1). Skipped only with
407
407
  // --allow-data-loss (explicit acceptance) or a `deleting` database
408
408
  // (nothing left to protect).
409
+ let snapshotReadyId = null;
409
410
  if (dbNeedsProtection) {
410
411
  const snapshotId = generateSnapshotId(dbTarget.id);
411
412
  s.start(truncateSpinnerText(`Snapshotting database ${dbTarget.id} before destruction...`));
@@ -430,20 +431,26 @@ async function destroyStackMain(input = {}) {
430
431
  // database: deleting while it is still `creating` races snapshot
431
432
  // completion on large DBs (BUG-98). A failed snapshot or a timeout
432
433
  // refuses the destroy — re-run with --allow-data-loss to skip.
433
- s.start(truncateSpinnerText(`Waiting for snapshot ${snapshotId} to complete...`));
434
+ // Re-message (never re-start): the phase spinner is already
435
+ // running, and a second start leaks clack's first interval so the
436
+ // session never exits (BUG-198).
437
+ s.message(truncateSpinnerText(`Waiting for snapshot ${snapshotId} to complete...`));
434
438
  const snapshotReady = await pollUntil({
435
439
  intervalMs: pollIntervalMs,
436
440
  timeoutMs: snapshotTimeoutMs,
437
441
  ...pollOverrides,
438
442
  onTick: async () => {
439
443
  let status = null;
444
+ let percent = null;
440
445
  try {
441
446
  if (dbTarget.kind === 'cluster') {
442
447
  const resp = await rdsClient.send(new DescribeDBClusterSnapshotsCommand({ DBClusterSnapshotIdentifier: snapshotId }));
443
448
  status = resp.DBClusterSnapshots?.[0]?.Status ?? null;
449
+ percent = resp.DBClusterSnapshots?.[0]?.PercentProgress ?? null;
444
450
  } else {
445
451
  const resp = await rdsClient.send(new DescribeDBSnapshotsCommand({ DBSnapshotIdentifier: snapshotId }));
446
452
  status = resp.DBSnapshots?.[0]?.Status ?? null;
453
+ percent = resp.DBSnapshots?.[0]?.PercentProgress ?? null;
447
454
  }
448
455
  } catch (error) {
449
456
  return { done: true, value: { failed: true, reason: `snapshot describe failed (${error.message})` } };
@@ -452,7 +459,10 @@ async function destroyStackMain(input = {}) {
452
459
  if (status === 'failed' || status === 'deleted') {
453
460
  return { done: true, value: { failed: true, reason: `snapshot ${status}` } };
454
461
  }
455
- s.message(truncateSpinnerText(`Waiting for snapshot ${snapshotId} to complete... (${status || 'pending'})`));
462
+ // BUG-196: surface PercentProgress while creating so long
463
+ // snapshots don't look wedged.
464
+ const detail = typeof percent === 'number' ? `${status || 'pending'} (${percent}% complete)` : (status || 'pending');
465
+ s.message(truncateSpinnerText(`Waiting for snapshot ${snapshotId} to complete... (${detail})`));
456
466
  return { done: false };
457
467
  },
458
468
  });
@@ -469,6 +479,7 @@ async function destroyStackMain(input = {}) {
469
479
  });
470
480
  }
471
481
  s.stop(`Database snapshot ${snapshotId} ready.`);
482
+ snapshotReadyId = snapshotId;
472
483
  console.log(color.green(`📸 Pre-destroy snapshot: ${snapshotId}`));
473
484
  trackEvent('db_destroy_checkpoint', { projectName: projectNameForTelemetry, db_kind: dbTarget.kind });
474
485
  await flushTelemetry();
@@ -625,8 +636,14 @@ async function destroyStackMain(input = {}) {
625
636
  });
626
637
  if (refused) return refused;
627
638
  }
639
+ // BUG-198: "spin this back up" overstated recovery — apply
640
+ // recreates compute, never the destroyed database. Name the
641
+ // snapshot (or its absence) so the choice is informed.
642
+ const dbRecoveryNote = snapshotReadyId
643
+ ? `\n (The database itself was destroyed — only pre-destroy snapshot ${snapshotReadyId} remains, for manual restore; apply will not bring the data back.)`
644
+ : (dbTarget ? `\n (The database itself was destroyed WITHOUT a snapshot (--allow-data-loss); apply will recreate it empty.)` : '');
628
645
  deleteS3Bucket = shouldAutoApprove(options) ? true : await confirm({
629
- message: color.yellow(`AWS compute resources destroyed. Do you also want to permanently delete the S3 state bucket?\n (Select 'No' if you plan to run 'grada apply' later to spin this back up.)`),
646
+ message: color.yellow(`AWS compute resources destroyed. Do you also want to permanently delete the S3 state bucket?\n (Select 'No' if you plan to run 'grada apply' later to spin this back up.)${dbRecoveryNote}`),
630
647
  initialValue: false,
631
648
  });
632
649