poad 2.75.2 → 2.76.0
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/global/start.js +1 -1
- package/package.json +1 -1
- package/project/cli.js +2 -0
- package/project/deploy.js +85 -26
- package/project/migrate.js +7 -3
- package/project/pull.js +4 -0
- package/project/push.js +4 -0
- package/test/cloud.test.js +55 -2
- package/test/data-loss.test.js +112 -0
- package/test/project-sync.test.js +297 -0
- package/utils/banner.js +2 -0
- package/utils/cloud/db.js +6 -7
- package/utils/data-loss.js +600 -0
- package/utils/help-details.js +61 -7
- package/utils/migrate-run.js +32 -1
- package/utils/migrations.js +29 -3
- package/utils/project-sync/code.js +122 -0
- package/utils/project-sync/pg.js +130 -0
- package/utils/project-sync/plan.js +436 -0
- package/utils/project-sync/run.js +769 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// poad push / poad pull : décisions sans base ni réseau (utils/project-sync/plan.js, code.js),
|
|
2
|
+
// puis la commande entière contre un faux poad.io et une fausse base (deps.pg).
|
|
3
|
+
const { test } = require('node:test');
|
|
4
|
+
const assert = require('node:assert');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const http = require('http');
|
|
9
|
+
const P = require('../utils/project-sync/plan');
|
|
10
|
+
const C = require('../utils/project-sync/code');
|
|
11
|
+
|
|
12
|
+
const col = (name, type = 'text', extra = {}) => ({ name, type, generated: false, notnull: false, default: false, ...extra });
|
|
13
|
+
const tbl = (schema, name, columns, extra = {}) => ({ schema, name, columns, pk: ['id'], estimate: 10, bytes: 8192, refs: [], ...extra });
|
|
14
|
+
|
|
15
|
+
test('parseParts: all by default, --only, --skip, aliases, --tables needs base, secrets only with --with-secrets', () => {
|
|
16
|
+
assert.deepStrictEqual(P.parseParts({}), { parts: ['code', 'base', 'auth', 'refs', 's3'], tables: null });
|
|
17
|
+
assert.deepStrictEqual(P.parseParts({ only: 'db,storage' }).parts, ['base', 's3']);
|
|
18
|
+
assert.deepStrictEqual(P.parseParts({ skip: 'code,refs' }).parts, ['base', 'auth', 's3']);
|
|
19
|
+
assert.deepStrictEqual(P.parseParts({ only: 'base', withSecrets: true }).parts, ['base', 'secrets']);
|
|
20
|
+
assert.deepStrictEqual(P.parseParts({ tables: 'livres,compta.ecritures' }).tables, ['public.livres', 'compta.ecritures']);
|
|
21
|
+
assert.throws(() => P.parseParts({ only: 'base', skip: 'auth' }), /choose one/);
|
|
22
|
+
assert.throws(() => P.parseParts({ only: 'secrets' }), /--with-secrets/);
|
|
23
|
+
assert.throws(() => P.parseParts({ only: 'tout' }), /unknown part/);
|
|
24
|
+
assert.throws(() => P.parseParts({ only: 'auth', tables: 'a' }), /base part/);
|
|
25
|
+
assert.throws(() => P.parseParts({ tables: 'a;drop' }), /expected table/);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('compareTables: copiable, missing, different type, new not-null column, references left out, --tables', () => {
|
|
29
|
+
const src = [
|
|
30
|
+
tbl('public', 'livres', [col('id', 'integer'), col('titre'), col('titre_maj', 'text', { generated: true })]),
|
|
31
|
+
tbl('public', 'absente', [col('id', 'integer')]),
|
|
32
|
+
tbl('public', 'typee', [col('id', 'integer'), col('prix', 'numeric')]),
|
|
33
|
+
tbl('public', 'stricte', [col('id', 'integer')]),
|
|
34
|
+
tbl('ref', 'communes', [col('id', 'integer')]),
|
|
35
|
+
];
|
|
36
|
+
const dst = [
|
|
37
|
+
tbl('public', 'livres', [col('id', 'integer'), col('titre'), col('titre_maj', 'text', { generated: true }), col('note', 'text')]),
|
|
38
|
+
tbl('public', 'typee', [col('id', 'integer'), col('prix', 'text')]),
|
|
39
|
+
tbl('public', 'stricte', [col('id', 'integer'), col('obligatoire', 'text', { notnull: true })]),
|
|
40
|
+
tbl('ref', 'communes', [col('id', 'integer')]),
|
|
41
|
+
tbl('public', 'seulement_cible', [col('id', 'integer')]),
|
|
42
|
+
];
|
|
43
|
+
const r = P.compareTables(src, dst, { exclude: ['ref.communes'] });
|
|
44
|
+
assert.deepStrictEqual(r.tables.map((t) => [t.key, t.columns]), [['public.livres', ['id', 'titre']]]);
|
|
45
|
+
assert.deepStrictEqual(r.blocked.map((b) => [b.key, b.reason]), [['public.absente', 'missing'], ['public.typee', 'differs'], ['public.stricte', 'differs']]);
|
|
46
|
+
assert.match(r.blocked[1].detail, /numeric here, text on the target/);
|
|
47
|
+
assert.match(r.blocked[2].detail, /obligatoire \(not null, no default\) only on the target/);
|
|
48
|
+
assert.deepStrictEqual(r.excluded, [{ key: 'ref.communes', reason: 'reference' }]);
|
|
49
|
+
assert.deepStrictEqual(r.extra, ['public.seulement_cible']);
|
|
50
|
+
const only = P.compareTables(src, dst, { only: ['public.livres', 'public.inconnue'] });
|
|
51
|
+
assert.deepStrictEqual(only.tables.map((t) => t.key), ['public.livres']);
|
|
52
|
+
assert.deepStrictEqual(only.unknown, ['public.inconnue']);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('diffHashes: by primary key (added, replaced, deleted, unchanged), and as multisets without one', () => {
|
|
56
|
+
const src = ['k1 a', 'k2 b', 'k3 c'].join('\n');
|
|
57
|
+
const dst = ['k1 a', 'k2 X', 'k9 z'].join('\n');
|
|
58
|
+
assert.deepStrictEqual(P.diffHashes(src, dst, true), { added: 1, replaced: 1, deleted: 1, unchanged: 1, rows: 3, target_rows: 3, exact: true });
|
|
59
|
+
assert.deepStrictEqual(P.diffHashes('a\na\nb', 'a\nc', false), { added: 2, replaced: 0, deleted: 1, unchanged: 1, rows: 3, target_rows: 2, exact: true });
|
|
60
|
+
assert.ok(!P.changed(P.diffHashes('k1 a', 'k1 a', true)));
|
|
61
|
+
assert.ok(P.changed({ exact: false, rows: 3, target_rows: 3 }));
|
|
62
|
+
assert.ok(!P.changed({ exact: false, rows: 0, target_rows: 0 }));
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('hashSql: fingerprints of key and row over the copied columns, tokens blanked; never a value out', () => {
|
|
66
|
+
const t = { schema: 'auth', name: 'users', columns: ['id', 'email', 'confirmation_token'], pk: ['id'] };
|
|
67
|
+
assert.strictEqual(P.hashSql(t, { blank: ['confirmation_token'] }), `select md5(row("id")::text) || ' ' || md5(row("id", "email", null::text)::text) from "auth"."users"`);
|
|
68
|
+
assert.strictEqual(P.hashSql({ ...t, pk: null }), `select md5(row("id", "email", "confirmation_token")::text) from "auth"."users"`);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('fits: rows to write + 10 % + journal reserve against the room left', () => {
|
|
72
|
+
const GiB = 1024 ** 3;
|
|
73
|
+
assert.deepStrictEqual(P.fits(100, null), { known: false, ok: true });
|
|
74
|
+
const ok = P.fits(100 * 1024 ** 2, { limit: GiB, used: 0.2 * GiB });
|
|
75
|
+
assert.ok(ok.known && ok.ok);
|
|
76
|
+
assert.strictEqual(ok.reserve, 256 * 1024 ** 2);
|
|
77
|
+
assert.ok(!P.fits(700 * 1024 ** 2, { limit: GiB, used: 0.2 * GiB }).ok);
|
|
78
|
+
assert.strictEqual(P.walReserve(100), 64 * 1024 ** 2);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('sequencesFor: those of the copied tables, unowned ones only for the whole base, already equal ones skipped', () => {
|
|
82
|
+
const src = [{ schema: 'public', name: 'a_id_seq', owner: 'public.a', last: 10 }, { schema: 'public', name: 'libre', owner: null, last: 3 }, { schema: 'public', name: 'b_id_seq', owner: 'public.b', last: 5 }, { schema: 'public', name: 'egal', owner: 'public.a', last: 7 }];
|
|
83
|
+
const dst = [{ schema: 'public', name: 'a_id_seq', last: 1 }, { schema: 'public', name: 'libre', last: 1 }, { schema: 'public', name: 'egal', last: 7 }];
|
|
84
|
+
assert.deepStrictEqual(P.sequencesFor(src, dst, ['public.a']).map((s) => s.name), ['a_id_seq']);
|
|
85
|
+
assert.deepStrictEqual(P.sequencesFor(src, dst, ['public.a'], { all: true }).map((s) => s.name), ['a_id_seq', 'libre']);
|
|
86
|
+
assert.match(P.sequencesSql([{ schema: 'public', name: 'a_id_seq' }]), /setval/);
|
|
87
|
+
assert.strictEqual(P.sequencesSql([]), null);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('authPlan: common columns, columns lost, sessions closed before accounts, tokens emptied', () => {
|
|
91
|
+
const users = (extra = []) => ({ rows: 2, bytes: 100, pk: ['id'], columns: [col('id', 'uuid'), col('email'), col('confirmation_token'), ...extra] });
|
|
92
|
+
const src = { users: users([col('nouvelle')]), identities: { rows: 2, bytes: 10, pk: ['id'], columns: [col('id', 'uuid'), col('email', 'text', { generated: true })] }, sessions: { rows: 0, columns: [] } };
|
|
93
|
+
const dst = { users: users(), identities: { rows: 0, bytes: 0, pk: ['id'], columns: [col('id', 'uuid'), col('email', 'text', { generated: true })] }, sessions: { rows: 4, columns: [] }, refresh_tokens: { rows: 4, columns: [] } };
|
|
94
|
+
const a = P.authPlan(src, dst);
|
|
95
|
+
assert.deepStrictEqual(a.tables.map((t) => [t.name, t.columns]), [['users', ['id', 'email', 'confirmation_token']], ['identities', ['id']]]);
|
|
96
|
+
assert.deepStrictEqual(a.dropped, ['auth.users.nouvelle']);
|
|
97
|
+
assert.deepStrictEqual(a.clear, ['delete from auth."refresh_tokens";', 'delete from auth."sessions";', 'delete from auth."identities";', 'delete from auth."users";']);
|
|
98
|
+
assert.deepStrictEqual(a.after, [`update auth."users" set "confirmation_token" = '' where "confirmation_token" <> '';`]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('transferScript: one target transaction, replica role, one consistent source snapshot, quoted names, stop on error', () => {
|
|
102
|
+
const s = P.transferScript({ srcFn: 'SRC', dstFn: 'DST', tables: [{ schema: 'public', name: "l'o", columns: ['a', 'b'] }], clear: ['delete from "public"."l\'o";'], sequences: 'select 1' });
|
|
103
|
+
assert.match(s, /set -o pipefail/);
|
|
104
|
+
assert.match(s, /printf '%s\\n' 'begin;'/);
|
|
105
|
+
assert.match(s, /session_replication_role = replica/);
|
|
106
|
+
assert.match(s, /begin isolation level repeatable read read only;/);
|
|
107
|
+
assert.match(s, /\\echo 'COPY "public"\."l''o" \("a", "b"\) FROM stdin;'/);
|
|
108
|
+
assert.match(s, /COPY \(select "a", "b" from "public"\."l'o"\) TO STDOUT;/);
|
|
109
|
+
assert.match(s, /\\echo '\\\\\.'/);
|
|
110
|
+
assert.match(s, /\| dst -v ON_ERROR_STOP=1/);
|
|
111
|
+
assert.ok(s.indexOf("'commit;'") > s.indexOf('POAD_SRC_EOF\n printf'), 'commit after the source');
|
|
112
|
+
assert.ok(!/PGPASSWORD=/.test(s), 'no password in the script');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('secretsSql: storage credentials kept on both sides, nothing read from the target; source stops on the first error', () => {
|
|
116
|
+
const q = P.secretsSql({ variables: true, credentials: true, vault: true }, { targetStorage: ['s3-prod'] }).join('\n');
|
|
117
|
+
assert.match(q, /update _poad\.variables set value = null/);
|
|
118
|
+
assert.match(q, /slot like 'storage\.%'\)::text\[\]/);
|
|
119
|
+
assert.match(q, /array\['s3-prod'\]::text\[\]/);
|
|
120
|
+
assert.match(q, /vault\.decrypted_secrets/);
|
|
121
|
+
const sh = P.secretsScript({ srcFn: 'S', dstFn: 'D', queries: ['select 1'] });
|
|
122
|
+
assert.match(sh, /src -v ON_ERROR_STOP=1 -tA -c 'select 1' 2> \/dev\/null \| dst -v ON_ERROR_STOP=1 > \/dev\/null 2>&1/);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test('parseMarkers and errorLine: timings, exit code, the ERROR line only (never CONTEXT, which quotes rows)', () => {
|
|
126
|
+
const m = P.parseMarkers('@@poad start 0 1000\n@@poad start 1 1500\npsql:<stdin>:9: ERROR: duplicate key value\nCONTEXT: COPY t, line 1: "secret row"\n@@poad done 2000 3\n');
|
|
127
|
+
assert.deepStrictEqual(m.tables, { 0: { start: 1000 }, 1: { start: 1500 } });
|
|
128
|
+
assert.strictEqual(m.rc, 3);
|
|
129
|
+
assert.strictEqual(P.errorLine(m.errors), 'ERROR: duplicate key value');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('code: mirrored paths, plan keeps uncommitted or untracked local files', () => {
|
|
133
|
+
assert.ok(C.mirrored('functions/hello/index.ts'));
|
|
134
|
+
assert.ok(C.mirrored('seed.sql'));
|
|
135
|
+
assert.ok(!C.mirrored('migrations/20260101000000_a.sql'));
|
|
136
|
+
assert.ok(!C.mirrored('functions/node_modules/x/index.js'));
|
|
137
|
+
assert.ok(!C.mirrored('config.toml'));
|
|
138
|
+
assert.ok(!C.mirrored('reference/.gitignore'));
|
|
139
|
+
const B = (t) => Buffer.from(t);
|
|
140
|
+
const remote = new Map([['functions/a/index.ts', B('new')], ['flows/f.json', B('same')], ['functions/b/index.ts', B('remote')], ['queries/new.sql', B('q')]]);
|
|
141
|
+
const local = new Map([['functions/a/index.ts', B('old')], ['flows/f.json', B('same')], ['functions/b/index.ts', B('local edit')], ['sources/gone.yaml', B('x')], ['sources/mine.yaml', B('y')]]);
|
|
142
|
+
const git = { repo: true, tracked: new Set(['functions/a/index.ts', 'flows/f.json', 'functions/b/index.ts', 'sources/gone.yaml']), dirty: new Set(['functions/b/index.ts']) };
|
|
143
|
+
const p = C.planFiles(remote, local, git);
|
|
144
|
+
assert.deepStrictEqual(p.add, ['queries/new.sql']);
|
|
145
|
+
assert.deepStrictEqual(p.update, ['functions/a/index.ts']);
|
|
146
|
+
assert.deepStrictEqual(p.remove, ['sources/gone.yaml']);
|
|
147
|
+
assert.strictEqual(p.unchanged, 1);
|
|
148
|
+
assert.deepStrictEqual(p.kept.map((k) => [k.rel, k.reason]), [['functions/b/index.ts', 'uncommitted changes'], ['sources/mine.yaml', 'not tracked by git']]);
|
|
149
|
+
// Hors dépôt git : rien n'est remplacé ni retiré
|
|
150
|
+
const nogit = C.planFiles(remote, local, { repo: false, tracked: new Set(), dirty: new Set() });
|
|
151
|
+
assert.deepStrictEqual([nogit.update, nogit.remove], [[], []]);
|
|
152
|
+
assert.strictEqual(nogit.kept[0].reason, 'not in a git repository');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// --- La commande entière : faux poad.io (HTTP) et fausse base (deps.pg) ------------------------------------
|
|
156
|
+
|
|
157
|
+
function fakeCloud(t, seen) {
|
|
158
|
+
return new Promise((resolve) => {
|
|
159
|
+
const server = http.createServer((req, res) => {
|
|
160
|
+
let body = '';
|
|
161
|
+
req.on('data', (d) => (body += d)).on('end', () => {
|
|
162
|
+
seen.push({ method: req.method, url: req.url, body });
|
|
163
|
+
const send = (s, o) => (res.writeHead(s, { 'content-type': 'application/json' }), res.end(JSON.stringify(o)));
|
|
164
|
+
if (/\/branches$/.test(req.url)) return send(200, [{ name: 'main', kind: 'production' }, { name: 'dev' }]);
|
|
165
|
+
if (/\/seeds$/.test(req.url)) return send(200, { store: true, items: [{ target: 'ref.communes', sha256: 'aa', rows: 5 }], applied: { dev: { 'ref.communes': { sha256: 'aa' } } }, quota: {}, loads: [] });
|
|
166
|
+
if (/\/sync-plan$/.test(req.url)) return send(200, { space: { limit: 1024 ** 3, used: 1024 ** 2 } });
|
|
167
|
+
if (/\/backup$/.test(req.url)) return send(200, { file: 'pre-deploy-20261205-000000.dump', bytes: 1234 });
|
|
168
|
+
if (/\/sync-events$/.test(req.url)) return send(200, { written: JSON.parse(body).lines.length });
|
|
169
|
+
send(404, { error: { code: 'not_found', message: 'route inconnue' } });
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
server.listen(0, '127.0.0.1', () => {
|
|
173
|
+
t.after(() => server.close());
|
|
174
|
+
resolve(`http://127.0.0.1:${server.address().port}`);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Fausse base : inventaires de la source et de la cible, empreintes, transfert enregistré */
|
|
180
|
+
function fakePg(scripts) {
|
|
181
|
+
const real = require('../utils/project-sync/pg');
|
|
182
|
+
const inv = (rows) => ({
|
|
183
|
+
tables: [tbl('public', 'livres', [col('id', 'integer'), col('titre')], { pk: ['id'] }), tbl('ref', 'communes', [col('id', 'integer')])],
|
|
184
|
+
sequences: [{ schema: 'public', name: 'livres_id_seq', owner: 'public.livres', last: rows }],
|
|
185
|
+
migrations: ['20261205100000'],
|
|
186
|
+
auth: { users: { rows: 1, bytes: 10, pk: ['id'], columns: [col('id', 'uuid'), col('email')] } },
|
|
187
|
+
variables: ['MAIL_FROM'],
|
|
188
|
+
credentials: ['api-meteo'],
|
|
189
|
+
storage_credentials: [],
|
|
190
|
+
});
|
|
191
|
+
return {
|
|
192
|
+
localSide: real.localSide,
|
|
193
|
+
cloudSide: real.cloudSide,
|
|
194
|
+
json: async (container, side, sql) => (sql === P.PROBE_SQL ? { history: true, variables: true, credentials: true, vault: true, auth: ['users'] } : inv(side.kind === 'local' ? 3 : 2)),
|
|
195
|
+
query: async (container, side, sql) => {
|
|
196
|
+
// Empreintes : la source a trois livres, la cible deux (un remplacé) ; un compte identique
|
|
197
|
+
const local = side.kind === 'local';
|
|
198
|
+
const parts = sql.split(/\\echo @@T (\d+)\n/).filter(Boolean);
|
|
199
|
+
let out = '';
|
|
200
|
+
for (let i = 0; i < parts.length; i += 2) {
|
|
201
|
+
out += `@@T ${parts[i]}\n`;
|
|
202
|
+
if (/"auth"\."users"/.test(parts[i + 1])) out += 'u1 h\n';
|
|
203
|
+
else out += local ? 'k1 a\nk2 b\nk3 c\n' : 'k1 a\nk2 X\n';
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
},
|
|
207
|
+
transfer: async (container, src, dst, script) => {
|
|
208
|
+
scripts.push(script);
|
|
209
|
+
return { code: 0, stdout: Buffer.alloc(0), stderr: '@@poad start 0 1000\n@@poad done 1200 0\n' };
|
|
210
|
+
},
|
|
211
|
+
dumpLocal: async () => 10,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function runCmd(t, args, { tty = false } = {}) {
|
|
216
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'poad-pp-'));
|
|
217
|
+
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
218
|
+
fs.mkdirSync(path.join(dir, 'poad'));
|
|
219
|
+
fs.writeFileSync(path.join(dir, 'poad', 'config.toml'), 'project_id = "t"\nproject_uuid = "ppuuid1"\nengine = "0.73.1"\n[db]\nport = 1\n[web]\nport = 2\n[cloud]\nproject = "pptest123456"\n');
|
|
220
|
+
const seen = [];
|
|
221
|
+
const scripts = [];
|
|
222
|
+
const base = await fakeCloud(t, seen);
|
|
223
|
+
const saved = { cwd: process.cwd(), api: process.env.POAD_CLOUD_API, tok: process.env.POAD_ACCESS_TOKEN, log: console.log, err: console.error, code: process.exitCode };
|
|
224
|
+
const printed = [];
|
|
225
|
+
process.chdir(dir);
|
|
226
|
+
process.env.POAD_CLOUD_API = base;
|
|
227
|
+
process.env.POAD_ACCESS_TOKEN = 'poad_pat_test';
|
|
228
|
+
console.log = (...a) => printed.push(a.join(' '));
|
|
229
|
+
console.error = (...a) => printed.push(a.join(' '));
|
|
230
|
+
const isTTY = process.stdin.isTTY;
|
|
231
|
+
process.stdin.isTTY = tty;
|
|
232
|
+
try {
|
|
233
|
+
process.exitCode = 0;
|
|
234
|
+
await require('../utils/project-sync/run').run(args[0], args.slice(1), {
|
|
235
|
+
pg: fakePg(scripts),
|
|
236
|
+
started: async () => true,
|
|
237
|
+
credentials: async () => ({ user: 'postgres.pptest123456--dev', password: 'pw-never-shown', host: 'db.poad.io', port: () => 5432 }),
|
|
238
|
+
});
|
|
239
|
+
return { code: process.exitCode, out: printed.join('\n'), seen, scripts };
|
|
240
|
+
} finally {
|
|
241
|
+
process.stdin.isTTY = isTTY;
|
|
242
|
+
process.chdir(saved.cwd);
|
|
243
|
+
process.env.POAD_CLOUD_API = saved.api;
|
|
244
|
+
process.env.POAD_ACCESS_TOKEN = saved.tok;
|
|
245
|
+
if (saved.api === undefined) delete process.env.POAD_CLOUD_API;
|
|
246
|
+
if (saved.tok === undefined) delete process.env.POAD_ACCESS_TOKEN;
|
|
247
|
+
console.log = saved.log;
|
|
248
|
+
console.error = saved.err;
|
|
249
|
+
process.exitCode = saved.code;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
test('poad push --dry-run: preview per table, reference tables left to refs, nothing written, nothing logged', async (t) => {
|
|
254
|
+
const r = await runCmd(t, ['push', '--env', 'dev', '--only', 'base,auth', '--dry-run', '--json']);
|
|
255
|
+
assert.strictEqual(r.code, 0);
|
|
256
|
+
const j = JSON.parse(r.out.slice(r.out.indexOf('{')));
|
|
257
|
+
assert.strictEqual(j.dry_run, true);
|
|
258
|
+
assert.deepStrictEqual(j.preview.base.tables.map((x) => [x.table, x.added, x.replaced, x.deleted, x.unchanged]), [['public.livres', 1, 1, 0, 1]]);
|
|
259
|
+
assert.deepStrictEqual(j.preview.base.excluded, [{ key: 'ref.communes', reason: 'reference' }]);
|
|
260
|
+
assert.strictEqual(j.preview.auth.changed, false);
|
|
261
|
+
assert.strictEqual(r.scripts.length, 0);
|
|
262
|
+
assert.ok(!r.seen.some((s) => /sync-events|backup/.test(s.url)));
|
|
263
|
+
assert.ok(!r.out.includes('pw-never-shown'));
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test('poad push to production outside a terminal: refused without --yes --confirm <id>, nothing written', async (t) => {
|
|
267
|
+
const r = await runCmd(t, ['push', '--env', 'main', '--only', 'base', '--yes']);
|
|
268
|
+
assert.strictEqual(r.code, 4);
|
|
269
|
+
assert.match(r.out, /--yes --confirm pptest123456/);
|
|
270
|
+
assert.strictEqual(r.scripts.length, 0);
|
|
271
|
+
assert.ok(!r.seen.some((s) => /backup/.test(s.url)));
|
|
272
|
+
const bad = await runCmd(t, ['push', '--env', 'main', '--only', 'base', '--yes', '--confirm', 'autre']);
|
|
273
|
+
assert.strictEqual(bad.code, 4);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test('poad push to production with --yes --confirm: backup first, one transfer, logs per table on poad.io (no value)', async (t) => {
|
|
277
|
+
const r = await runCmd(t, ['push', '--env', 'main', '--only', 'base,auth', '--yes', '--confirm', 'pptest123456']);
|
|
278
|
+
assert.strictEqual(r.code, 0, r.out);
|
|
279
|
+
const order = r.seen.map((s) => s.url.replace(/^.*\/branches\/[^/]+\//, '')).filter((u) => ['backup', 'sync-events'].includes(u));
|
|
280
|
+
assert.deepStrictEqual(order, ['backup', 'sync-events']);
|
|
281
|
+
assert.strictEqual(r.scripts.length, 1);
|
|
282
|
+
assert.match(r.scripts[0], /delete from "public"\."livres";/);
|
|
283
|
+
assert.ok(!/auth\."users"/.test(r.scripts[0]), 'identical accounts are not rewritten');
|
|
284
|
+
assert.match(r.scripts[0], /setval/);
|
|
285
|
+
const ev = JSON.parse(r.seen.find((s) => /sync-events/.test(s.url)).body);
|
|
286
|
+
assert.strictEqual(ev.event, 'project_push');
|
|
287
|
+
assert.deepStrictEqual(ev.lines.map((l) => [l.part, l.table || null, l.outcome]), [['backup', null, 'done'], ['base', 'public.livres', 'done'], ['data', null, 'done'], ['all', null, 'done']]);
|
|
288
|
+
assert.strictEqual(ev.lines[1].rows, 3);
|
|
289
|
+
assert.ok(!JSON.stringify(ev).includes('pw-never-shown'));
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test('poad pull --tables of an unknown table: stopped before anything is written', async (t) => {
|
|
293
|
+
const r = await runCmd(t, ['pull', '--env', 'dev', '--only', 'base', '--tables', 'inconnue', '--yes']);
|
|
294
|
+
assert.strictEqual(r.code, 5);
|
|
295
|
+
assert.match(r.out, /public\.inconnue not in the source/);
|
|
296
|
+
assert.strictEqual(r.scripts.length, 0);
|
|
297
|
+
});
|
package/utils/banner.js
CHANGED
|
@@ -31,6 +31,8 @@ const SHORT = {
|
|
|
31
31
|
db: 'Local database: reset, seed, diff',
|
|
32
32
|
upgrade: 'Upgrade the engine of the project',
|
|
33
33
|
deploy: 'Send poad/ to its poad.io project (poad link)',
|
|
34
|
+
push: 'The whole project to poad.io: code, data, accounts, files',
|
|
35
|
+
pull: 'A whole poad.io environment into the local project',
|
|
34
36
|
},
|
|
35
37
|
};
|
|
36
38
|
|
package/utils/cloud/db.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Connexion directe à la base d'un projet poad.io (passerelle db.poad.io).
|
|
2
|
-
// GET v1/projects/:id/db-password?purpose=copy|reveal|pull
|
|
2
|
+
// GET v1/projects/:id/db-password?purpose=copy|reveal|pull production
|
|
3
|
+
// GET v1/projects/:id/branches/:branche/db-password?purpose=copy|reveal environnement (postgres.<id>--<branche>)
|
|
3
4
|
// -> { user, password, host, session_port, transaction_port }
|
|
4
5
|
// Le mot de passe n'est gardé qu'en mémoire ; il ne s'affiche qu'avec
|
|
5
6
|
// --show-password (poad db url) et ne passe jamais en argument d'un processus.
|
|
@@ -10,15 +11,13 @@ const PORTS = { session: 5432, transaction: 6543 };
|
|
|
10
11
|
const MASK = '••••••••';
|
|
11
12
|
|
|
12
13
|
async function credentials(id, { env, purpose = 'copy' } = {}) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
const d = await api.call('GET', `v1/projects/${api.enc(id)}/db-password?purpose=${encodeURIComponent(purpose)}`, { what: 'database password' });
|
|
14
|
+
// Un environnement : sa propre base (route par branche de poad.io, utilisateur postgres.<id>--<branche>)
|
|
15
|
+
const route = env && !env.production ? `v1/projects/${api.enc(id)}/branches/${api.enc(env.branch)}/db-password` : `v1/projects/${api.enc(id)}/db-password`;
|
|
16
|
+
const d = await api.call('GET', `${route}?purpose=${encodeURIComponent(purpose)}`, { what: 'database password' });
|
|
18
17
|
if (typeof d.password !== 'string' || !d.password) throw new api.CloudError('poad.io sent no database password.', { code: 'bad_response' });
|
|
19
18
|
const port = (mode) => Number(mode === 'transaction' ? d.transaction_port : d.session_port) || PORTS[mode];
|
|
20
19
|
return {
|
|
21
|
-
user: typeof d.user === 'string' && d.user ? d.user : `postgres.${id}`,
|
|
20
|
+
user: typeof d.user === 'string' && /^[A-Za-z0-9._-]{1,120}$/.test(d.user) ? d.user : env && !env.production ? `postgres.${id}--${env.branch}` : `postgres.${id}`,
|
|
22
21
|
password: d.password,
|
|
23
22
|
host: typeof d.host === 'string' && /^[A-Za-z0-9.-]+$/.test(d.host) ? d.host : HOST,
|
|
24
23
|
port,
|