poad 2.77.0 → 2.78.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.
- package/README.md +49 -0
- package/global/cli.js +1 -0
- package/global/doctor.js +41 -0
- package/global/init.js +4 -0
- package/global/start.js +2 -0
- package/package.json +4 -1
- package/project/deploy.js +13 -1
- package/project/link.js +2 -0
- package/project/ref/push.js +42 -29
- package/test/doctor.test.js +159 -0
- package/test/project-sync.test.js +141 -13
- package/test/push-progress.test.js +181 -0
- package/test/storage-tus.test.js +7 -1
- package/utils/cloud/api.js +39 -3
- package/utils/cloud/deploy-memo.js +46 -0
- package/utils/doctor.js +368 -0
- package/utils/help-details.js +21 -3
- package/utils/progress.js +198 -0
- package/utils/project-sync/pg.js +106 -3
- package/utils/project-sync/plan.js +129 -12
- package/utils/project-sync/run.js +158 -49
- package/utils/reference-store.js +20 -26
- package/utils/seed/run.js +57 -11
- package/utils/storage-sync.js +28 -6
- package/utils/storage.js +18 -2
|
@@ -112,7 +112,7 @@ function run(direction, raw, deps = {}) {
|
|
|
112
112
|
out.log(chalk.grey(` parts: ${sel.parts.join(', ')}${sel.tables ? ` tables: ${sel.tables.join(', ')}` : ''}${clean ? ' --clean: what is replaced is emptied first' : ''}`));
|
|
113
113
|
|
|
114
114
|
// --- Aperçu (rien ne change) -------------------------------------------------------------------
|
|
115
|
-
const preview = { code: null, base: null, auth: null, refs: null, s3: null, secrets: null, space: null };
|
|
115
|
+
const preview = { code: null, base: null, auth: null, refs: null, s3: null, secrets: null, oauth: null, space: null };
|
|
116
116
|
const needDb = has('base') || has('auth') || has('secrets');
|
|
117
117
|
let local = null;
|
|
118
118
|
let cloud = null;
|
|
@@ -139,17 +139,22 @@ function run(direction, raw, deps = {}) {
|
|
|
139
139
|
if (has('refs')) out.log(chalk.yellowBright(` ⚠ reference store not readable (${e.message}): refs left out.`));
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
|
-
|
|
142
|
+
// Une table de référence n'a qu'un chemin : refs (ou poad deploy pour les gros fichiers). Elle sort de la
|
|
143
|
+
// partie base, que refs fasse partie de la copie ou non : registre de poad.io ET fichiers de poad/reference
|
|
144
|
+
// (un gros fichier jamais envoyé n'est pas encore au registre : il le sera par cette poussée même)
|
|
145
|
+
const localRefs = require('../reference').files(root).map((f) => f.replace(/\.jsonl$/, ''));
|
|
146
|
+
const refTargets = [...new Set([...(registry && Array.isArray(registry.items) ? registry.items.map((s) => s.target) : []), ...localRefs])];
|
|
143
147
|
|
|
144
148
|
// Code
|
|
145
|
-
if (has('code')) preview.code = push ? await codePushPreview(env, opts, out) : await codePullPreview(id, env, poadDir, out, deps);
|
|
149
|
+
if (has('code')) preview.code = push ? await codePushPreview(env, opts, out, { id, poadDir, deps }) : await codePullPreview(id, env, poadDir, out, deps);
|
|
146
150
|
|
|
147
151
|
// Base et comptes
|
|
148
152
|
if (has('base')) preview.base = await basePlan(pg, P, container, src, dst, inv, { exclude: refTargets, only: sel.tables, code: preview.code, push });
|
|
149
153
|
if (has('auth')) preview.auth = await authPreview(pg, P, container, src, dst, inv);
|
|
150
154
|
|
|
151
155
|
// Références
|
|
152
|
-
|
|
156
|
+
const codeRuns = Boolean(has('code') && preview.code && preview.code.todo);
|
|
157
|
+
if (has('refs') && registry) preview.refs = await refsPreview(registry, env, poadDir, push, codeRuns, push ? null : container);
|
|
153
158
|
|
|
154
159
|
// Fichiers du Stockage
|
|
155
160
|
let storage = null;
|
|
@@ -159,7 +164,21 @@ function run(direction, raw, deps = {}) {
|
|
|
159
164
|
}
|
|
160
165
|
|
|
161
166
|
// Secrets : des noms seulement
|
|
162
|
-
if (has('secrets'))
|
|
167
|
+
if (has('secrets')) {
|
|
168
|
+
preview.secrets = { variables: inv.src.variables, credentials: inv.src.credentials, target_variables: inv.dst.variables, target_credentials: inv.dst.credentials, target_storage: inv.dst.storage_credentials };
|
|
169
|
+
// Un condensé de chaque côté (jamais une valeur) : déjà pareils, rien à copier
|
|
170
|
+
try {
|
|
171
|
+
const exclude = [...new Set([...(inv.src.storage_credentials || []), ...(inv.dst.storage_credentials || [])])];
|
|
172
|
+
const [a, b] = await Promise.all([pg.query(container, src, P.secretsDigestSql(inv.srcProbe, { exclude })), pg.query(container, dst, P.secretsDigestSql(inv.dstProbe, { exclude }))]);
|
|
173
|
+
preview.secrets.same = Boolean(String(a).trim()) && String(a).trim() === String(b).trim();
|
|
174
|
+
} catch {
|
|
175
|
+
preview.secrets.same = false;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Autorisations OAuth : jamais copiées (une autorisation est donnée à un environnement) ; le plan le dit
|
|
180
|
+
preview.oauth = inv ? P.oauthPlan(inv.src.oauth, inv.dst.oauth) : null;
|
|
181
|
+
if (preview.auth && inv) preview.auth.oauth_target_users = (inv.dst.oauth || []).reduce((x, o) => x + (Number(o.users) || 0), 0);
|
|
163
182
|
|
|
164
183
|
// Place sur la cible : ce qui sera écrit (base et comptes, fichiers) et ce qui sera libéré
|
|
165
184
|
const amounts = spaceAmounts(preview, { clean });
|
|
@@ -197,7 +216,7 @@ function run(direction, raw, deps = {}) {
|
|
|
197
216
|
const target = push ? cloudLabel : 'the local project';
|
|
198
217
|
if (push && env.production) await strongConfirm(id, env, opts, out);
|
|
199
218
|
else await cli.confirm(`Make ${target} the mirror of ${push ? 'the local project' : cloudLabel} for ${sel.parts.filter((p) => p !== 'secrets').join(', ')}? What the target has of its own in these parts is replaced.${clean ? ' --clean: the tables and files to replace are emptied FIRST, outside the transaction (a failure leaves them empty until the next run).' : ''}`, { yes: opts.yes, json: out.json });
|
|
200
|
-
if (has('secrets')) {
|
|
219
|
+
if (has('secrets') && !preview.secrets.same) {
|
|
201
220
|
const s = preview.secrets;
|
|
202
221
|
await cli.confirm(`Also copy the values of ${s.variables.length} variable(s) and ${s.credentials.length} credential(s)${s.credentials.length ? ` (${s.credentials.slice(0, 8).join(', ')})` : ''} to ${target}? Values are never shown nor logged.`, { yes: opts.yes, json: out.json });
|
|
203
222
|
}
|
|
@@ -247,11 +266,11 @@ function run(direction, raw, deps = {}) {
|
|
|
247
266
|
|
|
248
267
|
if (has('code') && preview.code && preview.code.todo) {
|
|
249
268
|
const t0 = Date.now();
|
|
250
|
-
const r = push ? await codePush(env, opts, out) : await codePull(env, opts, out, poadDir, preview.code, localStarted);
|
|
269
|
+
const r = push ? await codePush(env, opts, out, deps) : await codePull(env, opts, out, poadDir, preview.code, localStarted);
|
|
251
270
|
results.code = r;
|
|
252
271
|
note({ part: 'code', rows: r.count || 0, outcome: r.ok ? 'done' : 'failed', duration_ms: Date.now() - t0 });
|
|
253
272
|
if (!r.ok) stop('code', r.error || 'code part failed');
|
|
254
|
-
}
|
|
273
|
+
} else if (has('code') && preview.code) note({ part: 'code', outcome: 'skipped', duration_ms: 0 });
|
|
255
274
|
|
|
256
275
|
if (!failed && (has('base') || has('auth'))) {
|
|
257
276
|
// La structure a pu changer (partie code) : inventaire et différence refaits
|
|
@@ -282,18 +301,24 @@ function run(direction, raw, deps = {}) {
|
|
|
282
301
|
}
|
|
283
302
|
if (!r) r = await transferData(pg, P, container, src, dst, { base, auth, inv, sel, out });
|
|
284
303
|
for (const l of r.lines) note(l);
|
|
304
|
+
if (has('base') && r.ok && !r.tables.some((t) => t.part === 'base')) note({ part: 'base', outcome: 'skipped', duration_ms: 0 });
|
|
305
|
+
if (has('auth') && r.ok && !r.tables.some((t) => t.part === 'auth')) note({ part: 'auth', outcome: 'skipped', duration_ms: 0 });
|
|
306
|
+
const dl = r.lines.find((l) => l.part === 'data');
|
|
307
|
+
if (dl && !out.json && dl.bytes && dl.duration_ms > 2000) out.log(chalk.grey(` base: ${P.size(dl.bytes)} in ${require('../progress').duration(dl.duration_ms / 1000)} (${P.size(dl.bytes_per_s)}/s)`));
|
|
285
308
|
if (has('base')) results.base = { outcome: r.ok ? 'done' : 'failed', tables: r.tables.filter((t) => t.part === 'base'), error: r.ok ? undefined : r.error };
|
|
286
309
|
if (has('auth')) results.auth = { outcome: r.ok ? 'done' : 'failed', tables: r.tables.filter((t) => t.part === 'auth'), error: r.ok ? undefined : r.error };
|
|
287
310
|
if (!r.ok) failed = true;
|
|
288
311
|
}
|
|
289
312
|
}
|
|
290
313
|
|
|
291
|
-
if (!failed && has('refs') && preview.refs) {
|
|
314
|
+
if (!failed && has('refs') && preview.refs && !preview.refs.todo) note({ part: 'refs', outcome: 'skipped', duration_ms: 0 });
|
|
315
|
+
if (!failed && has('refs') && preview.refs && preview.refs.todo) {
|
|
292
316
|
const t0 = Date.now();
|
|
293
317
|
try {
|
|
294
|
-
const r = await refsApply(id, env, poadDir, preview.refs, push, opts, out, config,
|
|
318
|
+
const r = await refsApply(id, env, poadDir, preview.refs, push, opts, out, config, codeRuns);
|
|
295
319
|
results.refs = r;
|
|
296
|
-
for (const t of r.tables || []) note({ part: 'refs', table: t.target, rows: t.rows, outcome: t.outcome, duration_ms: t.duration_ms });
|
|
320
|
+
for (const t of r.tables || []) note({ part: 'refs', table: t.target, rows: t.rows, bytes: t.bytes, bytes_per_s: t.bytes && t.send_ms ? require('../progress').rateOf(t.bytes, t.send_ms) : undefined, outcome: t.outcome, duration_ms: t.duration_ms });
|
|
321
|
+
note({ part: 'refs', outcome: r.failed ? 'failed' : 'done', bytes: (r.tables || []).reduce((x, t) => x + (t.bytes || 0), 0), duration_ms: Date.now() - t0 });
|
|
297
322
|
if (r.failed) stop('refs', 'a reference table could not be loaded');
|
|
298
323
|
} catch (e) {
|
|
299
324
|
note({ part: 'refs', outcome: 'failed', duration_ms: Date.now() - t0 });
|
|
@@ -301,16 +326,19 @@ function run(direction, raw, deps = {}) {
|
|
|
301
326
|
}
|
|
302
327
|
}
|
|
303
328
|
|
|
329
|
+
if (!failed && has('s3') && preview.s3 && !preview.s3.todo) note({ part: 's3', outcome: 'skipped', duration_ms: 0 });
|
|
304
330
|
if (!failed && has('s3') && preview.s3 && preview.s3.todo) {
|
|
305
331
|
const t0 = Date.now();
|
|
306
332
|
const r = await storagePlan(storage, false, out, { clean, concurrency: deps.concurrency, sleep: deps.sleep });
|
|
307
333
|
results.s3 = r;
|
|
308
334
|
for (const l of fileLines(r)) note(l);
|
|
309
|
-
note({ part: 's3', rows: r.totals.added + r.totals.updated + r.totals.removed, bytes: r.totals.bytes, outcome: r.totals.failed ? 'failed' : 'done', duration_ms: Date.now() - t0 });
|
|
335
|
+
note({ part: 's3', rows: r.totals.added + r.totals.updated + r.totals.removed, bytes: r.totals.bytes, bytes_per_s: require('../progress').rateOf(r.totals.bytes, Date.now() - t0), outcome: r.totals.failed ? 'failed' : 'done', duration_ms: Date.now() - t0 });
|
|
336
|
+
if (!out.json) out.log(`${r.totals.failed ? out.chalk.yellowBright('!') : out.chalk.greenBright('✔')} s3: ${n(r.totals.added)} added, ${n(r.totals.updated)} replaced, ${n(r.totals.removed)} removed, ${n(r.totals.unchanged)} unchanged${r.totals.bytes ? `, ${P.size(r.totals.bytes)} sent in ${require('../progress').duration((Date.now() - t0) / 1000)} (${P.size(require('../progress').rateOf(r.totals.bytes, Date.now() - t0))}/s)` : ''}`);
|
|
310
337
|
if (r.totals.failed) stop('s3', `${r.totals.failed} file(s) failed`);
|
|
311
338
|
}
|
|
312
339
|
|
|
313
|
-
if (!failed && has('secrets')) {
|
|
340
|
+
if (!failed && has('secrets') && preview.secrets.same) note({ part: 'secrets', outcome: 'skipped', duration_ms: 0 });
|
|
341
|
+
if (!failed && has('secrets') && !preview.secrets.same) {
|
|
314
342
|
const t0 = Date.now();
|
|
315
343
|
const script = P.secretsScript({ srcFn: src.psql, dstFn: dst.psql, queries: P.secretsSql(inv.srcProbe, { targetStorage: inv.dst.storage_credentials }) });
|
|
316
344
|
const r = await pg.transfer(container, src, dst, script);
|
|
@@ -347,10 +375,12 @@ async function inventories(pg, container, src, dst, P) {
|
|
|
347
375
|
async function basePlan(pg, P, container, src, dst, inv, { exclude, only, code, push }) {
|
|
348
376
|
const cmp = P.compareTables(inv.src.tables, inv.dst.tables, { exclude, only });
|
|
349
377
|
const migrations = P.compareMigrations(inv.src.migrations, inv.dst.migrations);
|
|
350
|
-
|
|
378
|
+
// Petites tables : différence ligne à ligne (copie incrémentale possible) ; grandes : nombre de lignes et somme
|
|
379
|
+
// des empreintes, en un passage (identiques : sautées ; sinon réécrites en entier)
|
|
380
|
+
const items = cmp.tables.map((t) => ({ t, sql: P.diffable(t) ? P.hashSql(t) : P.aggSql(t), exact: P.diffable(t) }));
|
|
351
381
|
const [a, b] = await Promise.all([batch(pg, container, src, items), batch(pg, container, dst, items)]);
|
|
352
382
|
const tables = items.map((it, i) => {
|
|
353
|
-
const d = it.exact ? P.diffHashes(a[i], b[i], Boolean(it.t.pk)
|
|
383
|
+
const d = it.exact ? P.diffHashes(a[i], b[i], Boolean(it.t.pk), { keys: true }) : P.diffAgg(a[i], b[i]);
|
|
354
384
|
return { ...it.t, diff: d, changed: P.changed(d) };
|
|
355
385
|
});
|
|
356
386
|
const seqs = P.sequencesFor(inv.src.sequences, inv.dst.sequences, tables.map((t) => t.key), { all: !only });
|
|
@@ -377,14 +407,28 @@ const localHasData = (inv) => {
|
|
|
377
407
|
|
|
378
408
|
const envArgs = (env, opts) => ['--env', env.branch, ...(opts.project ? ['--project', opts.project] : [])];
|
|
379
409
|
|
|
380
|
-
async function codePushPreview(env, opts, out) {
|
|
381
|
-
const r = await self(['deploy', ...envArgs(env, opts), '--dry-run', '--json'], { json: true });
|
|
410
|
+
async function codePushPreview(env, opts, out, { id, poadDir, deps = {} } = {}) {
|
|
411
|
+
const r = await (deps.self || self)(['deploy', ...envArgs(env, opts), '--dry-run', '--json'], { json: true });
|
|
382
412
|
const d = r.data || {};
|
|
383
413
|
if (d.error) return { ok: false, error: d.error.message, todo: true, structure: false };
|
|
384
414
|
const m = d.migrations || {};
|
|
385
415
|
const pending = (m.pending || []).length;
|
|
386
416
|
const errors = (m.errors || []).map((e) => e.message || String(e));
|
|
387
|
-
|
|
417
|
+
// Rien à redéployer : aucune migration en attente, même archive que le dernier déploiement réussi fait
|
|
418
|
+
// d'ici, et ce déploiement est toujours le dernier de l'environnement (utils/cloud/deploy-memo)
|
|
419
|
+
let same = null;
|
|
420
|
+
if (!pending && !errors.length && r.code === 0 && d.archive_sha256 && id && poadDir) {
|
|
421
|
+
const memo = require('../cloud/deploy-memo');
|
|
422
|
+
const last = memo.get(poadDir, id, env.branch);
|
|
423
|
+
if (last && last.archive_sha256 === d.archive_sha256) {
|
|
424
|
+
try {
|
|
425
|
+
const api = require('../cloud/api');
|
|
426
|
+
const list = await api.call('GET', `v1/projects/${api.enc(id)}/branches/${api.enc(env.branch)}/deploys`, { what: 'deploys' });
|
|
427
|
+
if (memo.upToDate(last, d.archive_sha256, list.deploys)) same = { deploy_id: last.deploy_id, at: last.at };
|
|
428
|
+
} catch {}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return { ok: r.code === 0, files: d.files || 0, bytes: d.bytes || 0, archive_sha256: d.archive_sha256 || null, pending, pending_list: m.pending || [], errors, fix: d.fix || [], todo: !same, same, structure: pending > 0, large_references: d.large_references || [] };
|
|
388
432
|
}
|
|
389
433
|
|
|
390
434
|
async function codePullPreview(id, env, poadDir, out, deps) {
|
|
@@ -407,15 +451,16 @@ async function codePullPreview(id, env, poadDir, out, deps) {
|
|
|
407
451
|
return { migrations: pull, conflicts: m.conflicts || [], unavailable: m.unavailable || [], files, error: error || (m.error && m.error.message) || null, todo, structure: pull.length > 0 };
|
|
408
452
|
}
|
|
409
453
|
|
|
410
|
-
async function codePush(env, opts, out) {
|
|
454
|
+
async function codePush(env, opts, out, deps = {}) {
|
|
455
|
+
const self_ = deps.self || self;
|
|
411
456
|
const args = ['deploy', ...envArgs(env, opts), '--yes', ...(opts['allow-destructive'] || opts['accept-data-loss'] ? ['--allow-destructive'] : [])];
|
|
412
457
|
if (out.json) {
|
|
413
|
-
const r = await
|
|
458
|
+
const r = await self_([...args, '--json'], { json: true });
|
|
414
459
|
const d = r.data || {};
|
|
415
460
|
return { ok: r.code === 0, changed: ((d.migrations || {}).applied || []).length > 0, count: ((d.migrations || {}).applied || []).length, deploy: d, error: r.code === 0 ? null : (d.error && d.error.message) || `poad deploy exit ${r.code}` };
|
|
416
461
|
}
|
|
417
462
|
out.log(out.chalk.bold('\n code: poad deploy'));
|
|
418
|
-
const r = await
|
|
463
|
+
const r = await self_(args, { inherit: true });
|
|
419
464
|
return { ok: r.code === 0, changed: true, error: r.code === 0 ? null : `poad deploy exit ${r.code}` };
|
|
420
465
|
}
|
|
421
466
|
|
|
@@ -456,31 +501,52 @@ async function transferData(pg, P, container, src, dst, { base, auth, inv, sel,
|
|
|
456
501
|
const after = [];
|
|
457
502
|
if (base) {
|
|
458
503
|
for (const t of base.rewrite) {
|
|
459
|
-
|
|
460
|
-
|
|
504
|
+
// Copie incrémentale (seulement les lignes ajoutées ou changées) quand la différence le permet
|
|
505
|
+
const w = P.rewriteOf(t);
|
|
506
|
+
const share = t.diff.rows ? Math.min(1, w.rows / t.diff.rows) : 1;
|
|
507
|
+
tables.push({ part: 'base', schema: t.schema, name: t.name, key: t.key, columns: t.columns, rows: w.rows, bytes: Math.round((t.bytes || 0) * share), diff: t.diff, where: w.where, mode: w.mode });
|
|
508
|
+
if (w.clear) clear.push(w.clear);
|
|
461
509
|
}
|
|
462
510
|
}
|
|
463
511
|
if (auth && auth.changed) {
|
|
464
512
|
clear.push(...auth.clear);
|
|
465
513
|
after.push(...auth.after);
|
|
466
|
-
for (const t of auth.tables) tables.push({ part: 'auth', schema: 'auth', name: t.name, key: t.key, columns: t.columns, rows: t.diff.rows, bytes: t.bytes, diff: t.diff });
|
|
514
|
+
for (const t of auth.tables) tables.push({ part: 'auth', schema: 'auth', name: t.name, key: t.key, columns: t.columns, rows: t.diff.rows, bytes: t.bytes, diff: t.diff, mode: 'full' });
|
|
467
515
|
}
|
|
468
516
|
const seqSql = base ? P.sequencesSql(base.sequences) : null;
|
|
469
517
|
if (!tables.length && !seqSql) return { ok: true, lines: [], tables: [] };
|
|
470
518
|
const script = P.transferScript({ srcFn: src.psql, dstFn: dst.psql, tables, clear, after, sequences: seqSql });
|
|
471
519
|
const t0 = Date.now();
|
|
472
|
-
|
|
520
|
+
// Progression : une ligne par table (octets passés dans le tube, lignes, débit, temps restant)
|
|
521
|
+
const progress = require('../progress');
|
|
522
|
+
const sent = tables.map(() => ({ bytes: 0, lines: 0 }));
|
|
523
|
+
let cur = -1;
|
|
524
|
+
let bar = null;
|
|
525
|
+
const open = (i) => {
|
|
526
|
+
if (bar) bar.end();
|
|
527
|
+
const t = tables[i];
|
|
528
|
+
bar = progress.start({ label: t.key, phase: t.mode === 'rows' ? 'changed rows' : '', total: t.bytes || 0, rowsTotal: t.rows || 0, rows: 0, json: out.json, part: t.part, table: t.key });
|
|
529
|
+
};
|
|
473
530
|
const r = await pg.transfer(container, src, dst, script, {
|
|
474
531
|
onMarker: (l) => {
|
|
475
532
|
const m = /^@@poad start (\d+) (\d+)/.exec(l);
|
|
476
|
-
if (m)
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
533
|
+
if (m && tables[Number(m[1])]) open((cur = Number(m[1])));
|
|
534
|
+
},
|
|
535
|
+
onData: (bytes, lines) => {
|
|
536
|
+
if (cur < 0) return;
|
|
537
|
+
const x = sent[cur];
|
|
538
|
+
x.bytes += bytes;
|
|
539
|
+
x.lines += lines;
|
|
540
|
+
// La première ligne d'une table est l'en-tête COPY
|
|
541
|
+
const t = tables[cur];
|
|
542
|
+
bar.update(x.bytes, { rows: Math.max(0, Math.min(t.rows || Infinity, x.lines - 1)) });
|
|
543
|
+
},
|
|
544
|
+
onSourceDone: () => {
|
|
545
|
+
if (bar) bar.end();
|
|
546
|
+
bar = progress.start({ label: 'base', phase: 'committing on the target (one transaction), then analyze', json: out.json, part: 'base', total: 0 });
|
|
481
547
|
},
|
|
482
548
|
});
|
|
483
|
-
if (
|
|
549
|
+
if (bar) bar.end();
|
|
484
550
|
const mk = P.parseMarkers(r.stderr);
|
|
485
551
|
const ok = r.code === 0 && mk.rc === 0;
|
|
486
552
|
const end = mk.done || Date.now();
|
|
@@ -488,20 +554,24 @@ async function transferData(pg, P, container, src, dst, { base, auth, inv, sel,
|
|
|
488
554
|
const done = tables.map((t, i) => {
|
|
489
555
|
const s = (mk.tables[i] || {}).start;
|
|
490
556
|
const next = (mk.tables[i + 1] || {}).start || end;
|
|
491
|
-
|
|
492
|
-
|
|
557
|
+
// Octets réellement passés dans le tube (flux COPY), sinon l'estimation
|
|
558
|
+
const bytes = sent[i].bytes || t.bytes || 0;
|
|
559
|
+
const res = { part: t.part, table: t.key, rows: t.rows, bytes, duration_ms: s ? Math.max(0, next - s) : 0, outcome: ok ? 'done' : 'rolled_back', diff: t.diff, mode: t.mode };
|
|
560
|
+
lines.push({ part: t.part, table: t.key, rows: t.rows, bytes, duration_ms: res.duration_ms, bytes_per_s: progress.rateOf(bytes, res.duration_ms), outcome: res.outcome });
|
|
493
561
|
if (!out.json) {
|
|
494
562
|
const d = t.diff;
|
|
495
563
|
const what = d.exact ? `+${n(d.added)} ~${n(d.replaced)} -${n(d.deleted)}` : `${n(d.target_rows)} → ${n(d.rows)}`;
|
|
496
|
-
|
|
564
|
+
const rate = res.duration_ms > 1000 ? `, ${P.size(progress.rateOf(bytes, res.duration_ms))}/s` : '';
|
|
565
|
+
out.log(` ${ok ? chalk.greenBright('✔') : chalk.redBright('✕')} ${t.key} ${chalk.grey(`${what} ${n(t.rows)} row(s) ${t.mode === 'rows' ? 'sent (changed only)' : 'copied'}, ${P.size(bytes)}${res.duration_ms ? `, ${(res.duration_ms / 1000).toFixed(1)} s` : ''}${rate}`)}`);
|
|
497
566
|
}
|
|
498
567
|
return res;
|
|
499
568
|
});
|
|
500
569
|
if (base && base.sequences.length && ok && !out.json) out.log(chalk.grey(` ${base.sequences.length} sequence(s) set as on the source`));
|
|
501
570
|
if (auth && auth.changed && ok && !out.json && auth.closed.length) out.log(chalk.grey(' sessions and refresh tokens of the target closed: accounts sign in again there'));
|
|
502
|
-
const error = ok ? null : P.errorLine(mk.errors) || `exit ${r.code}`;
|
|
571
|
+
const error = ok ? null : r.stalled ? `no byte went through for ${Math.round(pg.stallMs ? pg.stallMs() / 1000 : 300)} s: connection lost or too slow (nothing changed on the target; run it again)` : P.errorLine(mk.errors) || `exit ${r.code}`;
|
|
503
572
|
if (!ok) out.log(chalk.redBright(` ✕ data not copied, nothing changed on the target (one transaction): ${error}`));
|
|
504
|
-
|
|
573
|
+
const total = done.reduce((s, t) => s + t.bytes, 0);
|
|
574
|
+
lines.push({ part: 'data', rows: tables.reduce((s, t) => s + (t.rows || 0), 0), bytes: total, duration_ms: Date.now() - t0, bytes_per_s: progress.rateOf(total, Date.now() - t0), outcome: ok ? 'done' : 'rolled_back' });
|
|
505
575
|
return { ok, lines, tables: done, error };
|
|
506
576
|
}
|
|
507
577
|
|
|
@@ -547,7 +617,11 @@ async function refsApply(id, env, poadDir, plan, push, opts, out, config, withCo
|
|
|
547
617
|
if (plan.large.length && !withCode) {
|
|
548
618
|
const refStore = require('../reference-store');
|
|
549
619
|
const r = await refStore.sendAndLoad(id, env.branch, poadDir, refStore.largeFiles(poadDir), opts, out);
|
|
550
|
-
|
|
620
|
+
const steps = (r.load && r.load.steps) || [];
|
|
621
|
+
for (const t of r.tables) {
|
|
622
|
+
const st = steps.find((x) => x.target === t.target);
|
|
623
|
+
tables.push({ target: t.target, rows: t.rows || null, bytes: t.bytes || 0, send_ms: t.send_ms || 0, outcome: t.state === 'loaded' ? 'done' : t.state, duration_ms: (t.send_ms || 0) + ((st && st.duration_ms) || 0) });
|
|
624
|
+
}
|
|
551
625
|
if (r.failed) return { tables, failed: true };
|
|
552
626
|
}
|
|
553
627
|
const rest = plan.load.filter((t) => !tables.some((x) => x.target === t));
|
|
@@ -588,9 +662,15 @@ async function storagePlan(st, dryRun, out, { clean = false, concurrency = 4, sl
|
|
|
588
662
|
const ss = require('../storage-sync');
|
|
589
663
|
const s = require('../storage');
|
|
590
664
|
const P = require('./plan');
|
|
591
|
-
const live = { failed: 0, retried: 0, shownRetries: 0 };
|
|
665
|
+
const live = { failed: 0, retried: 0, shownRetries: 0, listed: 0 };
|
|
592
666
|
const resumes = [];
|
|
593
|
-
|
|
667
|
+
// Progression : fichiers et octets sur le total, débit, temps restant, fichier en cours ; les lignes de
|
|
668
|
+
// fichiers s'écrivent au-dessus (les 20 premières : au-delà, la barre et le bilan suffisent)
|
|
669
|
+
let bar = null;
|
|
670
|
+
let files = 0;
|
|
671
|
+
let bytesDone = 0;
|
|
672
|
+
const LISTED = 20;
|
|
673
|
+
const say = (line) => out && !out.json && (bar ? bar.log(line) : out.log(line));
|
|
594
674
|
const r = await ss.sync(st.from, st.to, {
|
|
595
675
|
prune: true,
|
|
596
676
|
dryRun,
|
|
@@ -601,8 +681,17 @@ async function storagePlan(st, dryRun, out, { clean = false, concurrency = 4, sl
|
|
|
601
681
|
fromLabel: st.fromT.label,
|
|
602
682
|
toLabel: st.toT.label,
|
|
603
683
|
confirm: async () => true,
|
|
684
|
+
onPlan: (pl) => {
|
|
685
|
+
if (dryRun || !out) return;
|
|
686
|
+
bar = require('../progress').start({ label: 's3', total: pl.mode === 'bytes' ? pl.bytes : 0, filesTotal: pl.files, json: out.json, part: 's3' });
|
|
687
|
+
},
|
|
688
|
+
onBytes: (d) => {
|
|
689
|
+
bytesDone += d;
|
|
690
|
+
if (bar) bar.update(bytesDone);
|
|
691
|
+
},
|
|
604
692
|
onItem: (e) => {
|
|
605
693
|
if (dryRun || !out) return;
|
|
694
|
+
if (e.action === 'start') return bar && bar.set({ file: `${e.bucket}/${e.name}` });
|
|
606
695
|
const c = out.chalk;
|
|
607
696
|
const where = `${e.bucket}${e.name ? `/${e.name}` : ''}`;
|
|
608
697
|
if (e.action === 'resume') {
|
|
@@ -624,11 +713,19 @@ async function storagePlan(st, dryRun, out, { clean = false, concurrency = 4, sl
|
|
|
624
713
|
else if (live.failed === 6) say(c.redBright(' ✖ more failures: see the summary at the end'));
|
|
625
714
|
return;
|
|
626
715
|
}
|
|
716
|
+
if (e.action === 'add' || e.action === 'update') {
|
|
717
|
+
files++;
|
|
718
|
+
if (bar) bar.update(undefined, { files });
|
|
719
|
+
}
|
|
627
720
|
if (out.json) return;
|
|
628
721
|
if (e.action === 'bucket') return say(` ${c.greenBright('+')} bucket ${e.bucket}`);
|
|
722
|
+
if (++live.listed > LISTED) {
|
|
723
|
+
if (live.listed === LISTED + 1) say(c.grey(' … the next files are counted, not listed (--json lists the totals; poad storage ls shows them)'));
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
629
726
|
say(` ${e.action === 'add' ? c.greenBright('+') : e.action === 'update' ? c.yellowBright('~') : c.redBright('-')} ${where}`);
|
|
630
727
|
},
|
|
631
|
-
});
|
|
728
|
+
}).finally(() => bar && bar.end());
|
|
632
729
|
const t = r.totals;
|
|
633
730
|
const sizes = (list, k = 'size') => list.reduce((m, o) => m + (Number.isFinite(o[k]) ? o[k] : 0), 0);
|
|
634
731
|
const planned = r.buckets.reduce((acc, p) => acc + sizes(p.add) + sizes(p.update), 0);
|
|
@@ -703,7 +800,7 @@ function spaceAmounts(pv, { clean = false } = {}) {
|
|
|
703
800
|
/** La sauvegarde n'a de sens que si la base va être écrite (lignes, comptes, secrets, migrations) */
|
|
704
801
|
function dbWrites(pv, sel, push) {
|
|
705
802
|
const has = (p) => sel.parts.includes(p);
|
|
706
|
-
if (has('secrets')) return true;
|
|
803
|
+
if (has('secrets') && !(pv.secrets && pv.secrets.same)) return true;
|
|
707
804
|
if (has('base') && pv.base && (pv.base.rewrite.length || pv.base.sequences.length)) return true;
|
|
708
805
|
if (has('auth') && pv.auth && pv.auth.changed) return true;
|
|
709
806
|
if (has('code') && pv.code) {
|
|
@@ -755,7 +852,7 @@ async function flushLogs({ api, id, env, rid, event, events, config, localStarte
|
|
|
755
852
|
if (e.file) l.file = e.file;
|
|
756
853
|
if (e.reason) l.reason = e.reason;
|
|
757
854
|
if (e.step) l.step = e.step;
|
|
758
|
-
for (const k of ['rows', 'bytes', 'attempts', 'offset']) if (Number.isFinite(e[k])) l[k] = e[k];
|
|
855
|
+
for (const k of ['rows', 'bytes', 'attempts', 'offset', 'bytes_per_s']) if (Number.isFinite(e[k])) l[k] = e[k];
|
|
759
856
|
return l;
|
|
760
857
|
});
|
|
761
858
|
if (localStarted) {
|
|
@@ -799,7 +896,7 @@ function isNothing(pv, sel) {
|
|
|
799
896
|
if (has('auth') && pv.auth && pv.auth.changed) return false;
|
|
800
897
|
if (has('refs') && pv.refs && pv.refs.todo) return false;
|
|
801
898
|
if (has('s3') && pv.s3 && pv.s3.todo) return false;
|
|
802
|
-
if (has('secrets')) return false;
|
|
899
|
+
if (has('secrets') && !(pv.secrets && pv.secrets.same)) return false;
|
|
803
900
|
return true;
|
|
804
901
|
}
|
|
805
902
|
|
|
@@ -812,6 +909,7 @@ function printPreview(out, P, pv, { push, env, sel }) {
|
|
|
812
909
|
const c = pv.code;
|
|
813
910
|
if (push) {
|
|
814
911
|
if (c.error) out.log(` ${pad('code')}${chalk.redBright(c.error)}`);
|
|
912
|
+
else if (c.same) out.log(` ${pad('code')}nothing to deploy: no migration pending, same ${c.files} file(s) as the last deploy from here ${chalk.grey(`(${c.same.deploy_id}, still the last one on ${env.branch})`)}`);
|
|
815
913
|
else out.log(` ${pad('code')}poad deploy: ${c.files} file(s), ${c.pending} migration(s) to apply${c.pending_list && c.pending_list.length ? chalk.grey(` (${c.pending_list.slice(0, 5).join(', ')}${c.pending_list.length > 5 ? '…' : ''})`) : ''}`);
|
|
816
914
|
for (const e of c.errors || []) out.log(` ${chalk.redBright('✕')} ${e}`);
|
|
817
915
|
for (const f of c.fix || []) out.log(` ${chalk.grey('fix:')} ${chalk.cyanBright(f)}`);
|
|
@@ -830,12 +928,16 @@ function printPreview(out, P, pv, { push, env, sel }) {
|
|
|
830
928
|
if (pv.base) {
|
|
831
929
|
const b = pv.base;
|
|
832
930
|
const same = b.tables.length - b.rewrite.length;
|
|
833
|
-
out.log(` ${pad('base')}${b.tables.length} table(s): ${b.rewrite.length} to rewrite, ${same} identical${b.excluded.length ? chalk.grey(
|
|
931
|
+
out.log(` ${pad('base')}${b.tables.length} table(s): ${b.rewrite.length || b.blocked.length ? `${b.rewrite.length} to rewrite, ${same} identical` : 'all identical, nothing to copy'}${b.excluded.length ? chalk.grey(`; ${b.excluded.length} reference table(s) left to refs`) : ''}`);
|
|
834
932
|
for (const t of b.rewrite) {
|
|
835
933
|
const d = t.diff;
|
|
836
|
-
const
|
|
837
|
-
|
|
934
|
+
const w = P.rewriteOf(t);
|
|
935
|
+
const what = d.exact ? `${chalk.greenBright(`+${n(d.added)}`)} ${chalk.yellowBright(`~${n(d.replaced)}`)} ${chalk.redBright(`-${n(d.deleted)}`)}` : `${n(d.target_rows)} → ${n(d.rows)} rows (large table: rewritten whole)`;
|
|
936
|
+
const how = w.mode === 'rows' ? `${n(w.rows)} row(s) to send of ${n(d.rows)}` : `${n(d.rows)} rows, ${P.size(t.bytes)}`;
|
|
937
|
+
out.log(` ${t.key.padEnd(30)} ${what} ${chalk.grey(how)}`);
|
|
838
938
|
}
|
|
939
|
+
// Une table, un chemin : les tables de référence ne passent jamais par la partie base
|
|
940
|
+
for (const x of b.excluded) out.log(` ${chalk.grey(`${x.key}: reference table, goes through refs${sel.parts.includes('refs') ? '' : ' (left out here: --only refs to push it)'}`)}`);
|
|
839
941
|
for (const x of b.blocked) out.log(` ${chalk.redBright('✕')} ${x.key}: ${x.detail}`);
|
|
840
942
|
if (b.blocked.length) out.log(` ${b.structure_after_code ? chalk.grey('the code part runs first and brings the structure: checked again after it') : chalk.yellowBright(`structure first: ${push ? 'poad deploy (or --only code)' : 'poad migration pull, then poad migrate (or --only code)'}`)}`);
|
|
841
943
|
if (b.migrations.missing.length) out.log(chalk.grey(` ${b.migrations.missing.length} migration(s) of the source not applied on the target${pv.code ? ' (code part)' : ''}`));
|
|
@@ -850,8 +952,13 @@ function printPreview(out, P, pv, { push, env, sel }) {
|
|
|
850
952
|
const d = u.diff;
|
|
851
953
|
out.log(` ${pad('auth')}${n(d.rows)} account(s): ${a.changed ? `${chalk.greenBright(`+${n(d.added)}`)} ${chalk.yellowBright(`~${n(d.replaced)}`)} ${chalk.redBright(`-${n(d.deleted)}`)}${a.closed.length ? chalk.grey(', sessions of the target closed') : ''}` : 'identical'}`);
|
|
852
954
|
if (a.dropped.length) out.log(chalk.yellowBright(` ⚠ not on the target, left out: ${a.dropped.join(', ')}`));
|
|
955
|
+
if (a.changed && a.oauth_target_users) out.log(chalk.grey(` accounts that leave the target take their connected OAuth accounts with them (engine 0.83+)`));
|
|
853
956
|
}
|
|
854
957
|
}
|
|
958
|
+
if (pv.oauth && pv.oauth.length) {
|
|
959
|
+
out.log(` ${pad('oauth')}${pv.oauth[0].text}`);
|
|
960
|
+
for (const l of pv.oauth.slice(1)) out.log(` ${l.text}`);
|
|
961
|
+
}
|
|
855
962
|
if (pv.refs) {
|
|
856
963
|
const r = pv.refs;
|
|
857
964
|
// Une ligne par table, lue dans la base de l'autre côté : déjà chargée (lignes, empreinte) / à charger (pourquoi)
|
|
@@ -869,8 +976,9 @@ function printPreview(out, P, pv, { push, env, sel }) {
|
|
|
869
976
|
}
|
|
870
977
|
if (sel.parts.includes('secrets')) {
|
|
871
978
|
const s = pv.secrets;
|
|
872
|
-
out.log(` ${pad('secrets')}${s.variables.length} variable value(s), ${s.credentials.length} credential(s)${
|
|
873
|
-
|
|
979
|
+
if (s.same) out.log(` ${pad('secrets')}identical: ${s.variables.length} variable value(s), ${s.credentials.length} credential(s) ${chalk.grey('(compared by a digest, never shown)')}`);
|
|
980
|
+
else out.log(` ${pad('secrets')}${s.variables.length} variable value(s), ${s.credentials.length} credential(s)${s.credentials.length ? chalk.grey(` (${s.credentials.slice(0, 8).join(', ')})`) : ''}${s.target_storage.length ? chalk.grey(`; storage credentials of the target kept (${s.target_storage.join(', ')})`) : ''}`);
|
|
981
|
+
} else out.log(chalk.grey(` ${'secrets'.padEnd(8)}left out (--with-secrets to copy variable values and credentials; OAuth authorizations never)`));
|
|
874
982
|
if (pv.space) for (const [i, l] of roomLines(P, pv.space).entries()) out.log(i === 0 ? ` ${pad('room')}${l}` : ` ${l}`);
|
|
875
983
|
}
|
|
876
984
|
|
|
@@ -908,7 +1016,8 @@ function resultOf({ direction, id, env, sel, dry, preview, blocking, events, sta
|
|
|
908
1016
|
auth: pv.auth ? { changed: pv.auth.changed, tables: pv.auth.tables.map((t) => strip({ ...t, bytes: t.bytes, changed: pv.auth.changed })), dropped: pv.auth.dropped } : null,
|
|
909
1017
|
refs: pv.refs,
|
|
910
1018
|
s3: pv.s3,
|
|
911
|
-
secrets: pv.secrets ? { variables: pv.secrets.variables.length, credentials: pv.secrets.credentials } : null,
|
|
1019
|
+
secrets: pv.secrets ? { variables: pv.secrets.variables.length, credentials: pv.secrets.credentials, same: Boolean(pv.secrets.same) } : null,
|
|
1020
|
+
oauth: pv.oauth ? pv.oauth.map(({ text, ...o }) => ({ ...o, message: text, copied: false })) : null,
|
|
912
1021
|
space: pv.space,
|
|
913
1022
|
},
|
|
914
1023
|
blocking,
|
package/utils/reference-store.js
CHANGED
|
@@ -182,39 +182,33 @@ async function sendAndLoad(id, branch, poadDir, files, opts, out, { dryRun = fal
|
|
|
182
182
|
// Conversion puis envoi (contenu déjà au magasin : rien n'est renvoyé)
|
|
183
183
|
const dir = S.tmpDir();
|
|
184
184
|
try {
|
|
185
|
-
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
onProgress: (read
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
},
|
|
195
|
-
});
|
|
196
|
-
if (!out.json && process.stderr.isTTY) process.stderr.write('\r\x1b[K');
|
|
185
|
+
const P = require('./progress');
|
|
186
|
+
const reading = P.start({ label: f.target, phase: 'reading', total: f.size, json: out.json, part: 'refs', table: f.target });
|
|
187
|
+
let conv;
|
|
188
|
+
try {
|
|
189
|
+
conv = await S.convertFile(f.abs, { format: 'jsonl', dir, onProgress: (read) => reading.update(read) });
|
|
190
|
+
} finally {
|
|
191
|
+
reading.end();
|
|
192
|
+
}
|
|
197
193
|
const t = conv.tables[0];
|
|
198
194
|
if (!t) throw new api.CloudError(`${f.rel}: no row`, { code: 'seed_empty' });
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
});
|
|
210
|
-
if (!out.json && process.stderr.isTTY) process.stderr.write('\r\x1b[K');
|
|
195
|
+
// Envoi au magasin : octets chiffrés envoyés sur le total, débit, temps restant
|
|
196
|
+
const sending = P.start({ label: t.target, phase: 'sending to the reference store', unit: 'bytes', json: out.json, part: 'refs', table: t.target });
|
|
197
|
+
const t0 = Date.now();
|
|
198
|
+
let r;
|
|
199
|
+
try {
|
|
200
|
+
r = await S.pushTable(id, t, { mode: 'reference', source: `poad/${f.rel}`, onProgress: (done, total) => sending.update(done, { total }) });
|
|
201
|
+
} finally {
|
|
202
|
+
sending.end();
|
|
203
|
+
}
|
|
204
|
+
if (r.uploaded) t.sent = { bytes: r.stored, duration_ms: Date.now() - t0 };
|
|
211
205
|
cache[f.file] = { size: f.size, mtimeMs: f.mtimeMs, sha256: t.sha256, target: t.target };
|
|
212
206
|
writeCache(poadDir, cache);
|
|
213
207
|
// Contenu inchangé au magasin : il peut être déjà chargé (l'état lu dans la base le dit)
|
|
214
208
|
const st = cloudState(registry, branch, t.target, t.sha256);
|
|
215
209
|
const loaded = st.state === 'loaded';
|
|
216
|
-
out.log(`${loaded ? chalk.greenBright('✔') : chalk.yellow('○')} ${chalk.bold(t.target)} ${r.uploaded ? `sent to the reference store (${S.fmt(r.stored)} encrypted)` : 'already in the reference store'}${loaded ? `, already loaded in ${branch} ${chalk.grey(`(${n(st.rows)} rows, sha256 ${short(t.sha256)})`)}` : `, to load in ${branch}`}`);
|
|
217
|
-
tables.push({ target: t.target, action: 'sent', sha256: t.sha256, rows: t.rows, uploaded: r.uploaded, reason: st.state });
|
|
210
|
+
out.log(`${loaded ? chalk.greenBright('✔') : chalk.yellow('○')} ${chalk.bold(t.target)} ${r.uploaded ? `sent to the reference store (${S.fmt(r.stored)} encrypted${t.sent ? ` in ${require('./progress').duration(t.sent.duration_ms / 1000)}, ${S.fmt(require('./progress').rateOf(r.stored, t.sent.duration_ms))}/s` : ''})` : 'already in the reference store'}${loaded ? `, already loaded in ${branch} ${chalk.grey(`(${n(st.rows)} rows, sha256 ${short(t.sha256)})`)}` : `, to load in ${branch}`}`);
|
|
211
|
+
tables.push({ target: t.target, action: 'sent', sha256: t.sha256, rows: t.rows, uploaded: r.uploaded, reason: st.state, bytes: r.uploaded ? r.stored : 0, send_ms: t.sent ? t.sent.duration_ms : 0 });
|
|
218
212
|
if (!loaded) toLoad.push(t.target);
|
|
219
213
|
} finally {
|
|
220
214
|
fs.rmSync(dir, { recursive: true, force: true });
|