@skrr-ai/cli 0.1.165 → 0.1.166
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/dist/commands/apps/installations/compatibility.js +1 -1
- package/dist/commands/apps/installations/migrate/abort.d.ts +13 -0
- package/dist/commands/apps/installations/migrate/abort.js +45 -0
- package/dist/commands/apps/installations/migrate/cutover.d.ts +15 -0
- package/dist/commands/apps/installations/migrate/cutover.js +102 -0
- package/dist/commands/apps/installations/migrate/plan.d.ts +13 -0
- package/dist/commands/apps/installations/migrate/plan.js +51 -0
- package/dist/commands/apps/installations/migrate/rollback.d.ts +16 -0
- package/dist/commands/apps/installations/migrate/rollback.js +103 -0
- package/dist/commands/apps/installations/migrate/stage.d.ts +15 -0
- package/dist/commands/apps/installations/migrate/stage.js +96 -0
- package/dist/commands/apps/installations/migrate/status.d.ts +12 -0
- package/dist/commands/apps/installations/migrate/status.js +52 -0
- package/dist/commands/apps/installations/migrate/validate.d.ts +14 -0
- package/dist/commands/apps/installations/migrate/validate.js +58 -0
- package/dist/commands/apps/installations/show.js +5 -6
- package/dist/commands/apps/operations/check.js +1 -1
- package/dist/commands/apps/resume.js +1 -1
- package/dist/commands/apps/settings/set.js +22 -10
- package/dist/lib/api-fetch.js +32 -5
- package/dist/lib/appCompatibility.js +1 -1
- package/dist/lib/appData.d.ts +1 -1
- package/dist/lib/appData.js +6 -2
- package/dist/lib/appDataMigration.d.ts +204 -0
- package/dist/lib/appDataMigration.js +753 -0
- package/dist/lib/appDataMigrationCommand.d.ts +39 -0
- package/dist/lib/appDataMigrationCommand.js +90 -0
- package/dist/lib/appInstallations.d.ts +8 -0
- package/dist/lib/appInstallations.js +54 -1
- package/dist/lib/apps-command.js +17 -10
- package/dist/lib/node-adapter.d.ts +0 -2
- package/dist/lib/node-adapter.js +46 -42
- package/dist/lib/rate-limit-retry.d.ts +4 -5
- package/dist/lib/rate-limit-retry.js +6 -10
- package/dist/lib/refresh.d.ts +2 -0
- package/dist/lib/refresh.js +6 -0
- package/dist/lib/write-outcome.d.ts +2 -0
- package/dist/lib/write-outcome.js +14 -0
- package/dist/node_modules/@skrr-ai/data-provider/index.js +10041 -9838
- package/oclif.manifest.json +599 -3
- package/package.json +5 -2
|
@@ -0,0 +1,753 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MigrationLoopError = exports.migrationCalls = exports.APPS_MIGRATION_CALLS_MAX = exports.APPS_MIGRATION_CODES = exports.APPS_MIGRATION_EXIT = void 0;
|
|
4
|
+
exports.installationRef = installationRef;
|
|
5
|
+
exports.readMigration = readMigration;
|
|
6
|
+
exports.readMigrationState = readMigrationState;
|
|
7
|
+
exports.migrationUnreadableMessage = migrationUnreadableMessage;
|
|
8
|
+
exports.formatMigration = formatMigration;
|
|
9
|
+
exports.formatNoMigration = formatNoMigration;
|
|
10
|
+
exports.progressLine = progressLine;
|
|
11
|
+
exports.runUntilSettled = runUntilSettled;
|
|
12
|
+
exports.loopExit = loopExit;
|
|
13
|
+
exports.loopStopNote = loopStopNote;
|
|
14
|
+
exports.describeFreezeGate = describeFreezeGate;
|
|
15
|
+
exports.describeCutoverGate = describeCutoverGate;
|
|
16
|
+
exports.describeRollbackGate = describeRollbackGate;
|
|
17
|
+
exports.describeLocalRefusal = describeLocalRefusal;
|
|
18
|
+
exports.describeNamedMigrationNotOpen = describeNamedMigrationNotOpen;
|
|
19
|
+
exports.describeMigrationFailure = describeMigrationFailure;
|
|
20
|
+
const data_provider_1 = require("@skrr-ai/data-provider");
|
|
21
|
+
const appData_1 = require("./appData");
|
|
22
|
+
const http_error_1 = require("./http-error");
|
|
23
|
+
const write_outcome_1 = require("./write-outcome");
|
|
24
|
+
/**
|
|
25
|
+
* The installer's explicit DATA MIGRATION of an installation, from a terminal: `apps installations migrate plan | status | stage |
|
|
26
|
+
* validate | abort | cutover | rollback` (docs/architecture/apps-installation-lifecycle-2026-11-10.md, section 11, steps 6b-1 and 6b-2,
|
|
27
|
+
* OSK-23687). The contract and every sentence it says (states, findings, refusals, the disclaimers) are `@skrr-ai/data-provider`'s
|
|
28
|
+
* `appDataMigration.ts`; this file only reads the server's answer through that contract and decides what a terminal prints and which exit
|
|
29
|
+
* code a script gets.
|
|
30
|
+
*
|
|
31
|
+
* WHAT CHANGES LIVE DATA. Only `cutover` and `rollback`, and each needs `--yes`: without it they change nothing and say what they would do.
|
|
32
|
+
* `cutover` copies the installation's documents to an archive (kept for 14 days after it completes, outside the storage quota), replaces
|
|
33
|
+
* them with the converted ones and moves the app to the new version; `rollback` puts the archived ones back and moves the app back.
|
|
34
|
+
*
|
|
35
|
+
* EXIT CODES (distinct on purpose, so a script can tell "it failed" from "it was refused" from "nobody can tell"):
|
|
36
|
+
*
|
|
37
|
+
* 0 the step did what it says: planned, staged, validated, aborted, cut over (`completed`), rolled back, or a status (with or without a migration
|
|
38
|
+
* open)
|
|
39
|
+
* 1 the migration is `failed` (a status of one too), or a finding stopped it: the bounded findings are printed. A cutover that could not
|
|
40
|
+
* finish and undid itself from the archive (`rolled_back`) is also 1: the app is as it was, and the cutover did not happen
|
|
41
|
+
* 4 the server REFUSED the request: `migration_refused` (the reason's words), `forbidden`, a stranger or a missing installation, a
|
|
42
|
+
* `changed` migration, a bad request. A refusal changes nothing the person has not already been told about
|
|
43
|
+
* 5 UNKNOWN: an answer this client cannot read, a loop that made no progress or hit its cap, a validation that could not judge, or a
|
|
44
|
+
* request that may have reached the server and got no readable answer. It is never a success: run `status`
|
|
45
|
+
* 6 NOT ENABLED: HTTP 503 `APP_DATA_MIGRATION_UNAVAILABLE`. A deployment setting, not an outage: nothing was changed and retrying
|
|
46
|
+
* will not help
|
|
47
|
+
*
|
|
48
|
+
* A request that fails in the ordinary way (a sign-in that expired, a rate limit, the server being down on a READ) is the CLI's ordinary
|
|
49
|
+
* failure and keeps its own code. A transport failure on a WRITE is 5, because the write may have landed.
|
|
50
|
+
*
|
|
51
|
+
* WHAT A TERMINAL SHOWS. The state in words, the plan's counts (a collection, a number, the kinds of step: never a step's values), the freeze
|
|
52
|
+
* and staging counts, the validation, the failure's words, the findings (`collection/documentId code at path`: the document id is shown
|
|
53
|
+
* because the person who asks can already list it with `apps data query`; it is printed in the findings and nowhere else, never in an
|
|
54
|
+
* error and never in a hint) and, always, the contract's three sentences. Every value that came from the server is written out safely.
|
|
55
|
+
*/
|
|
56
|
+
exports.APPS_MIGRATION_EXIT = {
|
|
57
|
+
ok: 0,
|
|
58
|
+
failed: 1,
|
|
59
|
+
refused: 4,
|
|
60
|
+
unknown: 5,
|
|
61
|
+
notEnabled: 6,
|
|
62
|
+
};
|
|
63
|
+
/** The machine codes this command family prints under (`--json` `error.code`), next to the exit each has. */
|
|
64
|
+
exports.APPS_MIGRATION_CODES = {
|
|
65
|
+
unreadable: 'APP_MIGRATION_UNREADABLE',
|
|
66
|
+
outcomeUnknown: 'APP_MIGRATION_OUTCOME_UNKNOWN',
|
|
67
|
+
noProgress: 'APP_MIGRATION_NO_PROGRESS',
|
|
68
|
+
unfinished: 'APP_MIGRATION_UNFINISHED',
|
|
69
|
+
validationGap: 'APP_MIGRATION_VALIDATION_GAP',
|
|
70
|
+
freezeNotConfirmed: 'APP_MIGRATION_FREEZE_NOT_CONFIRMED',
|
|
71
|
+
cutoverNotConfirmed: 'APP_MIGRATION_CUTOVER_NOT_CONFIRMED',
|
|
72
|
+
rollbackNotConfirmed: 'APP_MIGRATION_ROLLBACK_NOT_CONFIRMED',
|
|
73
|
+
refused: 'APP_MIGRATION_REFUSED',
|
|
74
|
+
forbidden: 'APP_MIGRATION_FORBIDDEN',
|
|
75
|
+
notFound: 'APP_MIGRATION_NOT_FOUND',
|
|
76
|
+
changed: 'APP_MIGRATION_CHANGED',
|
|
77
|
+
rejected: 'APP_MIGRATION_REQUEST_REJECTED',
|
|
78
|
+
failed: 'APP_MIGRATION_FAILED',
|
|
79
|
+
notEnabled: data_provider_1.APP_DATA_MIGRATION_UNAVAILABLE,
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* The most calls one `stage` or `validate` makes. The server carries a bounded batch per call (100 documents, within an 8 second budget) and
|
|
83
|
+
* a migration holds at most 60,000 documents, so about 600 calls is the most a real migration needs; this leaves room above it and still
|
|
84
|
+
* stops a loop that is somehow advancing one document at a time forever. Reaching it is "not finished", never a success.
|
|
85
|
+
*/
|
|
86
|
+
exports.APPS_MIGRATION_CALLS_MAX = 700;
|
|
87
|
+
const INSTALLATION_ID = /^appi_[A-Za-z0-9_-]{1,60}(?![\s\S])/;
|
|
88
|
+
const VERSION_ID = /^appv_[A-Za-z0-9_-]{1,60}(?![\s\S])/;
|
|
89
|
+
const MIGRATION_ID = /^[A-Za-z0-9_-]{1,128}(?![\s\S])/;
|
|
90
|
+
/** A value of the shape an id of this kind has, or null. Whatever a person typed or a server sent goes into a command only through this. */
|
|
91
|
+
function plain(pattern, value) {
|
|
92
|
+
return typeof value === 'string' && pattern.test(value) ? value : null;
|
|
93
|
+
}
|
|
94
|
+
/** The installation as a command argument: the id when it is one, else a placeholder (never a string that was not an id). */
|
|
95
|
+
function installationRef(value) {
|
|
96
|
+
return plain(INSTALLATION_ID, value) ?? '<installation>';
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Read the server's answer as a migration of THIS installation (and of THIS migration and version, when the call pinned them), through
|
|
100
|
+
* the contract's reader. `null` is an answer this client does not understand: it then knows nothing and says so, and never calls it a
|
|
101
|
+
* success. A state, a failure code or a finding code the contract does not list is unreadable by the contract's own rule.
|
|
102
|
+
*/
|
|
103
|
+
function readMigration(raw, expected) {
|
|
104
|
+
const view = (0, data_provider_1.parseAppDataMigration)(raw);
|
|
105
|
+
if (view === null || view.installationId !== expected.installationId)
|
|
106
|
+
return null;
|
|
107
|
+
if (expected.migrationId !== undefined && view.id !== expected.migrationId)
|
|
108
|
+
return null;
|
|
109
|
+
if (expected.targetVersionId !== undefined && view.targetVersionId !== expected.targetVersionId) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
return view;
|
|
113
|
+
}
|
|
114
|
+
/** The answer of the status read: `{ migration: view | null }`, or `null` when it is not an answer this client understands. */
|
|
115
|
+
function readMigrationState(raw, installationId) {
|
|
116
|
+
const state = (0, data_provider_1.parseAppDataMigrationState)(raw);
|
|
117
|
+
if (state === undefined)
|
|
118
|
+
return null;
|
|
119
|
+
if (state.migration !== null && state.migration.installationId !== installationId)
|
|
120
|
+
return null;
|
|
121
|
+
return state;
|
|
122
|
+
}
|
|
123
|
+
/** What is said when the server's answer is not a migration this client understands. It is not a success. */
|
|
124
|
+
function migrationUnreadableMessage(step, installation) {
|
|
125
|
+
const inst = installationRef(installation);
|
|
126
|
+
const where = `Run \`skrr apps installations migrate status ${inst}\` to see where it stands`;
|
|
127
|
+
return step === 'status'
|
|
128
|
+
? `The server answered, but not with a migration status this client understands, so nothing is known about whether a migration is open. This is not "there is none"; do not treat it as that. Update the CLI, or ask again later.`
|
|
129
|
+
: `The server answered, but not with a migration this client understands, so it is not known what the ${step} did. This is not a success; do not treat it as one. ${where} (a write may have been applied), and update the CLI if it keeps happening.`;
|
|
130
|
+
}
|
|
131
|
+
// ── the server calls (the five routes, each through `dataService`) ───────────
|
|
132
|
+
exports.migrationCalls = {
|
|
133
|
+
status: (installation) => data_provider_1.dataService.getAppInstallationDataMigration(installation),
|
|
134
|
+
plan: (installation, targetVersionId) => data_provider_1.dataService.planAppInstallationDataMigration(installation, { targetVersionId }),
|
|
135
|
+
stage: (installation, body) => data_provider_1.dataService.stageAppInstallationDataMigration(installation, body),
|
|
136
|
+
validate: (installation, body) => data_provider_1.dataService.validateAppInstallationDataMigration(installation, body),
|
|
137
|
+
abort: (installation, body) => data_provider_1.dataService.abortAppInstallationDataMigration(installation, body),
|
|
138
|
+
cutover: (installation, body) => data_provider_1.dataService.cutoverAppInstallationDataMigration(installation, body),
|
|
139
|
+
rollback: (installation, body) => data_provider_1.dataService.rollbackAppInstallationDataMigration(installation, body),
|
|
140
|
+
};
|
|
141
|
+
// ── the words ────────────────────────────────────────────────────────────────
|
|
142
|
+
const FINDING_WORDS = {
|
|
143
|
+
...data_provider_1.APP_COMPATIBILITY_FINDING_WORDS,
|
|
144
|
+
...data_provider_1.APP_DATA_MIGRATION_STEP_FAILURE_WORDS,
|
|
145
|
+
};
|
|
146
|
+
const FINDINGS_SHOWN = 20;
|
|
147
|
+
const say = (value) => (0, appData_1.safeForTerminal)(value);
|
|
148
|
+
const plural = (count, one, many = `${one}s`) => `${count} ${count === 1 ? one : many}`;
|
|
149
|
+
/**
|
|
150
|
+
* The migration as the lines a person reads. Document ids appear in the findings and nowhere else. Every value from the server is
|
|
151
|
+
* written out safely, and the three sentences of the contract close it, always.
|
|
152
|
+
*/
|
|
153
|
+
function formatMigration(view, bin) {
|
|
154
|
+
const inst = installationRef(view.installationId);
|
|
155
|
+
const lines = [];
|
|
156
|
+
lines.push(`migration ${say(view.id)} of ${say(view.installationId)}: ${say(view.fromVersionId)} -> ${say(view.targetVersionId)}`);
|
|
157
|
+
lines.push(`state: ${data_provider_1.APP_DATA_MIGRATION_STATE_WORDS[view.state]}`);
|
|
158
|
+
if (view.failure) {
|
|
159
|
+
lines.push(`why it failed: ${data_provider_1.APP_DATA_MIGRATION_FAILURE_WORDS[view.failure.code]}`);
|
|
160
|
+
}
|
|
161
|
+
const collections = view.plan.collections;
|
|
162
|
+
lines.push(`plan (made at generation ${view.plan.lifecycleGen}, data epoch ${view.plan.dataEpoch}): ${plural(collections.length, 'collection')}`);
|
|
163
|
+
for (const collection of collections) {
|
|
164
|
+
const ops = collection.steps.map((step) => step.op);
|
|
165
|
+
lines.push(` ${say(collection.name)}: ${plural(collection.documents, 'document')}, ${ops.length === 0
|
|
166
|
+
? 'no steps (carried over unchanged and then checked)'
|
|
167
|
+
: `${plural(ops.length, 'step')} (${ops.join(', ')})`}`);
|
|
168
|
+
}
|
|
169
|
+
if (view.freeze) {
|
|
170
|
+
lines.push(`frozen at ${say(view.freeze.at)}: ${plural(view.freeze.documents, 'document')} counted (generation ${view.freeze.lifecycleGen}, data epoch ${view.freeze.dataEpoch})`);
|
|
171
|
+
}
|
|
172
|
+
// a planned migration has staged nothing, and an aborted one, or one whose cutover completed (or is being undone), has had its staging erased:
|
|
173
|
+
// there is nothing to count
|
|
174
|
+
const stagingGone = ['planned', 'aborted', 'completed', 'rolling_back', 'rolled_back'];
|
|
175
|
+
if (!stagingGone.includes(view.state)) {
|
|
176
|
+
const total = view.staging.total === null ? 'an uncounted number of' : String(view.staging.total);
|
|
177
|
+
lines.push(`staging: ${view.staging.staged} of ${total} documents converted, ${view.staging.failed} could not be${view.staging.complete ? ' (every document was gone through)' : ''}`);
|
|
178
|
+
}
|
|
179
|
+
if (view.validation) {
|
|
180
|
+
const v = view.validation;
|
|
181
|
+
lines.push(`validation: ${plural(v.checked, 'document')} checked; ${v.complete
|
|
182
|
+
? v.ok === true
|
|
183
|
+
? 'finished, and every one fits the new schemas'
|
|
184
|
+
: v.ok === false
|
|
185
|
+
? 'finished, and some do not fit'
|
|
186
|
+
: `finished without an answer${v.gap ? `: ${data_provider_1.APP_DATA_MIGRATION_VALIDATION_GAP_WORDS[v.gap]}` : ''}`
|
|
187
|
+
: 'not finished'}`);
|
|
188
|
+
}
|
|
189
|
+
if (view.cutover) {
|
|
190
|
+
lines.push(`cutover: ${data_provider_1.APP_DATA_MIGRATION_CUTOVER_PHASE_WORDS[view.cutover.phase]} (confirmed ${say(view.cutover.startedAt)}); ${plural(view.cutover.archived, 'document')} archived, ${plural(view.cutover.replaced, 'document')} written`);
|
|
191
|
+
}
|
|
192
|
+
if (view.completedAt)
|
|
193
|
+
lines.push(`completed at ${say(view.completedAt)}`);
|
|
194
|
+
if (view.archive) {
|
|
195
|
+
lines.push(view.archive.expiresAt === null
|
|
196
|
+
? `archive: ${plural(view.archive.documents, 'old document')} copied; it is kept for ${data_provider_1.APP_DATA_MIGRATION_LIMITS.archiveRetentionDays} days after the cutover completes`
|
|
197
|
+
: `archive: ${plural(view.archive.documents, 'old document')}, kept until ${say(view.archive.expiresAt)} and then erased for good (outside your storage quota)`);
|
|
198
|
+
}
|
|
199
|
+
if (view.rollback) {
|
|
200
|
+
lines.push(`rollback (${data_provider_1.APP_DATA_MIGRATION_ROLLBACK_REASON_WORDS[view.rollback.reason]}): ${data_provider_1.APP_DATA_MIGRATION_ROLLBACK_PHASE_WORDS[view.rollback.phase]}; ${plural(view.rollback.restored, 'document')} restored`);
|
|
201
|
+
for (const entry of view.rollback.discarded ?? []) {
|
|
202
|
+
lines.push(` discarded: ${plural(entry.documents, 'document')} of ${say(entry.collection)} written after the cutover`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (view.rolledBackAt)
|
|
206
|
+
lines.push(`rolled back at ${say(view.rolledBackAt)}`);
|
|
207
|
+
// the window of an OPEN migration that has not begun its cutover; once the cutover has begun it no longer expires
|
|
208
|
+
if (['planned', 'frozen', 'staging', 'staged', 'validated', 'failed'].includes(view.state)) {
|
|
209
|
+
lines.push(`open until ${say(view.expiresAt)}`);
|
|
210
|
+
}
|
|
211
|
+
if (view.findingsTotal > 0 || view.findings.length > 0) {
|
|
212
|
+
const shown = view.findings.slice(0, FINDINGS_SHOWN);
|
|
213
|
+
lines.push(`findings: ${view.findingsTotal}${view.findingsTotal > shown.length ? ` (only the first ${shown.length} are kept and listed)` : ''}`);
|
|
214
|
+
for (const finding of shown) {
|
|
215
|
+
lines.push(` ${finding.phase} ${say(finding.collection)}/${say(finding.documentId)} ${finding.code}${finding.path === undefined ? '' : ` at ${say(finding.path)}`}${finding.step === undefined ? '' : ` (step ${finding.step})`}: ${FINDING_WORDS[finding.code]}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
for (const line of nextStepLines(view, bin, inst))
|
|
219
|
+
lines.push(line);
|
|
220
|
+
for (const sentence of view.disclaimers)
|
|
221
|
+
lines.push(sentence);
|
|
222
|
+
return lines;
|
|
223
|
+
}
|
|
224
|
+
/** What to do next, by state. */
|
|
225
|
+
function nextStepLines(view, bin, inst) {
|
|
226
|
+
const cmd = (verb, flags = '') => `${bin} apps installations migrate ${verb} ${inst}${flags}`;
|
|
227
|
+
switch (view.state) {
|
|
228
|
+
case 'planned':
|
|
229
|
+
return [
|
|
230
|
+
`next: \`${cmd('stage', ' --yes')}\` freezes the app and converts its documents into a staging area; \`${cmd('abort')}\` drops this plan. Nothing is frozen yet.`,
|
|
231
|
+
];
|
|
232
|
+
case 'frozen':
|
|
233
|
+
case 'staging':
|
|
234
|
+
return [
|
|
235
|
+
`next: \`${cmd('stage')}\` carries on from where it stopped; \`${cmd('abort')}\` erases the staging and puts the app back.`,
|
|
236
|
+
];
|
|
237
|
+
case 'staged':
|
|
238
|
+
return [
|
|
239
|
+
`next: \`${cmd('validate')}\` checks every converted document against the new version's schemas; \`${cmd('abort')}\` erases the staging and puts the app back.`,
|
|
240
|
+
];
|
|
241
|
+
case 'validated':
|
|
242
|
+
return [
|
|
243
|
+
`next: \`${cmd('cutover', ' --yes')}\` copies the live documents to an archive (kept ${data_provider_1.APP_DATA_MIGRATION_LIMITS.archiveRetentionDays} days after it completes), replaces them with the converted ones and moves the app to the new version; \`${cmd('abort')}\` erases the staging and puts the app back as it was. Until you run one of them the app stays frozen and nothing live has changed.`,
|
|
244
|
+
];
|
|
245
|
+
case 'cutting_over':
|
|
246
|
+
return [
|
|
247
|
+
`next: \`${cmd('cutover', ' --yes')}\` carries on from where it stopped. From here it can only finish or be rolled back; it cannot be aborted. If this terminal is closed the platform resumes it by itself.`,
|
|
248
|
+
];
|
|
249
|
+
case 'completed':
|
|
250
|
+
return [
|
|
251
|
+
`next: nothing is needed. To undo it within the archive window: \`${cmd('rollback', ' --yes')}\` puts the archived documents back and moves the app back to ${say(view.fromVersionId)}; it refuses if documents were written since the cutover, and tells you how many, unless you add --discard-writes-since-cutover.`,
|
|
252
|
+
];
|
|
253
|
+
case 'rolling_back':
|
|
254
|
+
return [
|
|
255
|
+
`next: \`${cmd('rollback', ' --yes')}\` carries on from where it stopped. If this terminal is closed the platform resumes it by itself.`,
|
|
256
|
+
];
|
|
257
|
+
case 'rolled_back':
|
|
258
|
+
return [];
|
|
259
|
+
case 'failed':
|
|
260
|
+
return [
|
|
261
|
+
`next: \`${cmd('abort')}\` erases the staging and lifts this migration's freeze, putting the app back as it was.`,
|
|
262
|
+
];
|
|
263
|
+
case 'aborted':
|
|
264
|
+
return [];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/** The status of an installation with no migration open, in lines. */
|
|
268
|
+
function formatNoMigration(installation, bin) {
|
|
269
|
+
const inst = installationRef(installation);
|
|
270
|
+
return [
|
|
271
|
+
`${say(installation)} has no data migration open. Nothing is frozen on its account.`,
|
|
272
|
+
`To plan one for data that does not fit another version, check it with \`${bin} apps installations compatibility ${inst} --version <version>\`, then \`${bin} apps installations migrate plan ${inst} --version <version>\`.`,
|
|
273
|
+
];
|
|
274
|
+
}
|
|
275
|
+
/** One line per call of a loop, in the words of what that call moved. */
|
|
276
|
+
function progressLine(step, call, view) {
|
|
277
|
+
if (step === 'cutover') {
|
|
278
|
+
return `cutover call ${call}: ${view.state}${view.cutover ? `, ${view.cutover.phase}; ${view.cutover.archived} archived, ${view.cutover.replaced} written` : ''}`;
|
|
279
|
+
}
|
|
280
|
+
if (step === 'rollback') {
|
|
281
|
+
return `rollback call ${call}: ${view.state}${view.rollback ? `, ${view.rollback.phase}; ${view.rollback.restored} restored` : ''}`;
|
|
282
|
+
}
|
|
283
|
+
if (step === 'stage') {
|
|
284
|
+
const total = view.staging.total === null ? 'an uncounted number' : String(view.staging.total);
|
|
285
|
+
return `stage call ${call}: ${view.state}; ${view.staging.staged} of ${total} documents converted, ${view.staging.failed} could not be`;
|
|
286
|
+
}
|
|
287
|
+
return `validate call ${call}: ${view.state}; ${view.validation?.checked ?? 0} documents checked${view.validation?.complete ? ' (finished)' : ''}`;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* A call of a loop failed or was unreadable after `calls - 1` calls had been answered. The command says "unknown, run status" for a write
|
|
291
|
+
* it cannot prove did not land, and what it knows of the migration from the calls before.
|
|
292
|
+
*/
|
|
293
|
+
class MigrationLoopError extends Error {
|
|
294
|
+
error;
|
|
295
|
+
calls;
|
|
296
|
+
last;
|
|
297
|
+
unreadable;
|
|
298
|
+
constructor(input) {
|
|
299
|
+
super('a data migration call failed');
|
|
300
|
+
this.name = 'MigrationLoopError';
|
|
301
|
+
this.error = input.error;
|
|
302
|
+
this.calls = input.calls;
|
|
303
|
+
this.last = input.last;
|
|
304
|
+
this.unreadable = input.unreadable === true;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
exports.MigrationLoopError = MigrationLoopError;
|
|
308
|
+
/** What changed between two answers; an answer equal to the one before it is no progress. */
|
|
309
|
+
function progressKey(view) {
|
|
310
|
+
return [
|
|
311
|
+
view.state,
|
|
312
|
+
view.staging.staged,
|
|
313
|
+
view.staging.failed,
|
|
314
|
+
view.staging.complete,
|
|
315
|
+
view.validation?.checked ?? '-',
|
|
316
|
+
view.validation?.complete ?? '-',
|
|
317
|
+
view.validation?.ok ?? '-',
|
|
318
|
+
view.cutover?.phase ?? '-',
|
|
319
|
+
view.cutover?.archived ?? '-',
|
|
320
|
+
view.cutover?.replaced ?? '-',
|
|
321
|
+
view.rollback?.phase ?? '-',
|
|
322
|
+
view.rollback?.restored ?? '-',
|
|
323
|
+
].join('|');
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Repeat a step that carries a bounded slice of work per call. Stops at the first of: the step's goal (`done`), a `failed` migration,
|
|
327
|
+
* a validation that ended without an answer (`gap`), an answer equal to the one before it (`no_progress`), the cap (`call_cap`), or, with
|
|
328
|
+
* `once`, the first answer. The first call pins no migration unless the caller did; every later one pins the migration the first answer
|
|
329
|
+
* named, so the loop can never carry on with a different one.
|
|
330
|
+
*/
|
|
331
|
+
async function runUntilSettled(input) {
|
|
332
|
+
const maxCalls = input.maxCalls ?? exports.APPS_MIGRATION_CALLS_MAX;
|
|
333
|
+
let pinned = input.migrationId;
|
|
334
|
+
let last = null;
|
|
335
|
+
let calls = 0;
|
|
336
|
+
for (;;) {
|
|
337
|
+
calls += 1;
|
|
338
|
+
let raw;
|
|
339
|
+
try {
|
|
340
|
+
raw = await input.call(pinned);
|
|
341
|
+
}
|
|
342
|
+
catch (error) {
|
|
343
|
+
throw new MigrationLoopError({ error, calls, last });
|
|
344
|
+
}
|
|
345
|
+
const view = readMigration(raw, {
|
|
346
|
+
installationId: input.installationId,
|
|
347
|
+
...(pinned === undefined ? {} : { migrationId: pinned }),
|
|
348
|
+
});
|
|
349
|
+
if (view === null)
|
|
350
|
+
throw new MigrationLoopError({ calls, last, unreadable: true });
|
|
351
|
+
input.onProgress?.(progressLine(input.step, calls, view));
|
|
352
|
+
pinned = view.id;
|
|
353
|
+
const stop = settled(input.step, view, last, input.once);
|
|
354
|
+
if (stop !== null)
|
|
355
|
+
return { view, calls, stopped: stop };
|
|
356
|
+
if (calls >= maxCalls)
|
|
357
|
+
return { view, calls, stopped: 'call_cap' };
|
|
358
|
+
last = view;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/** Has the loop reached a place to stop, and which? `null`: another call may move it. */
|
|
362
|
+
function settled(step, view, before, once) {
|
|
363
|
+
if (view.state === 'failed')
|
|
364
|
+
return 'failed';
|
|
365
|
+
if (step === 'cutover') {
|
|
366
|
+
if (view.state === 'completed')
|
|
367
|
+
return 'done';
|
|
368
|
+
// a cutover that could not finish undid itself from the archive: it did not happen, and the app is as it was
|
|
369
|
+
if (view.state === 'rolled_back')
|
|
370
|
+
return 'undone';
|
|
371
|
+
if (view.state === 'rolling_back')
|
|
372
|
+
return once ? 'once' : null;
|
|
373
|
+
if (view.state !== 'cutting_over')
|
|
374
|
+
return 'no_progress';
|
|
375
|
+
if (once)
|
|
376
|
+
return 'once';
|
|
377
|
+
return before !== null && progressKey(before) === progressKey(view) ? 'no_progress' : null;
|
|
378
|
+
}
|
|
379
|
+
if (step === 'rollback') {
|
|
380
|
+
if (view.state === 'rolled_back')
|
|
381
|
+
return 'done';
|
|
382
|
+
if (view.state !== 'rolling_back')
|
|
383
|
+
return 'no_progress';
|
|
384
|
+
if (once)
|
|
385
|
+
return 'once';
|
|
386
|
+
return before !== null && progressKey(before) === progressKey(view) ? 'no_progress' : null;
|
|
387
|
+
}
|
|
388
|
+
if (step === 'stage') {
|
|
389
|
+
if (view.state === 'staged' || view.state === 'validated')
|
|
390
|
+
return 'done';
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
if (view.state === 'validated')
|
|
394
|
+
return 'done';
|
|
395
|
+
if (view.state === 'staged' && view.validation?.complete === true) {
|
|
396
|
+
return view.validation.gap !== undefined ? 'gap' : 'no_progress';
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
const open = step === 'stage'
|
|
400
|
+
? view.state === 'frozen' || view.state === 'staging'
|
|
401
|
+
: view.state === 'staged';
|
|
402
|
+
if (!open)
|
|
403
|
+
return 'no_progress';
|
|
404
|
+
if (once)
|
|
405
|
+
return 'once';
|
|
406
|
+
return before !== null && progressKey(before) === progressKey(view) ? 'no_progress' : null;
|
|
407
|
+
}
|
|
408
|
+
/** The exit code a loop's result has. A `once` call that moved the migration along is a step that did what it says. */
|
|
409
|
+
function loopExit(result) {
|
|
410
|
+
switch (result.stopped) {
|
|
411
|
+
case 'done':
|
|
412
|
+
case 'once':
|
|
413
|
+
return exports.APPS_MIGRATION_EXIT.ok;
|
|
414
|
+
case 'failed':
|
|
415
|
+
case 'undone':
|
|
416
|
+
return exports.APPS_MIGRATION_EXIT.failed;
|
|
417
|
+
default:
|
|
418
|
+
return exports.APPS_MIGRATION_EXIT.unknown;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/** What a loop that did not reach its goal says on stderr, so the exit code is never the only thing a person has. */
|
|
422
|
+
function loopStopNote(step, result, bin) {
|
|
423
|
+
const inst = installationRef(result.view.installationId);
|
|
424
|
+
const status = `\`${bin} apps installations migrate status ${inst}\``;
|
|
425
|
+
const again = `\`${bin} apps installations migrate ${step} ${inst}\``;
|
|
426
|
+
switch (result.stopped) {
|
|
427
|
+
case 'done':
|
|
428
|
+
return null;
|
|
429
|
+
case 'once':
|
|
430
|
+
return `Stopped after one call, as you asked (--once). The migration is ${result.view.state}; run ${again} again to go on.`;
|
|
431
|
+
case 'failed':
|
|
432
|
+
return null;
|
|
433
|
+
case 'undone':
|
|
434
|
+
return `The cutover could not finish and undid itself: the archived documents were put back and the app runs the version it ran before (${data_provider_1.APP_DATA_MIGRATION_ROLLBACK_REASON_WORDS[result.view.rollback?.reason ?? 'installation_changed']}). The cutover did not happen. This is not a success.`;
|
|
435
|
+
case 'no_progress':
|
|
436
|
+
return `Stopped: the last call moved nothing (the migration is ${result.view.state}), so ${stepNoun(step)} is not finished and it is not known why. This is not a success. Read where it stands with ${status}, and run ${again} again if the state allows it.`;
|
|
437
|
+
case 'call_cap':
|
|
438
|
+
return `Stopped after ${result.calls} calls without finishing, which is more than a migration this size should need. This is not a success. Run ${again} again to go on from where it stopped, or read where it stands with ${status}.`;
|
|
439
|
+
case 'gap':
|
|
440
|
+
return `Validation reached the end without an answer: ${result.view.validation?.gap
|
|
441
|
+
? data_provider_1.APP_DATA_MIGRATION_VALIDATION_GAP_WORDS[result.view.validation.gap]
|
|
442
|
+
: 'it could not judge every document'}. The migration stays staged and this is NOT a pass (it never says "fits" for what it could not judge). Run ${again} again, or abort the migration.`;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
const STEP_NOUNS = {
|
|
446
|
+
stage: 'staging',
|
|
447
|
+
validate: 'validation',
|
|
448
|
+
cutover: 'the cutover',
|
|
449
|
+
rollback: 'the rollback',
|
|
450
|
+
};
|
|
451
|
+
const stepNoun = (step) => STEP_NOUNS[step];
|
|
452
|
+
// ── the stage gate ───────────────────────────────────────────────────────────
|
|
453
|
+
/**
|
|
454
|
+
* What `stage` says, instead of calling the server, when the migration is only planned and the person has not said `--yes`: staging
|
|
455
|
+
* FREEZES the app, which is theirs to confirm. Names the plan's counts, the three sentences and the command that goes on.
|
|
456
|
+
*/
|
|
457
|
+
function describeFreezeGate(view, bin) {
|
|
458
|
+
const inst = installationRef(view.installationId);
|
|
459
|
+
const documents = view.plan.collections.reduce((sum, c) => sum + c.documents, 0);
|
|
460
|
+
return [
|
|
461
|
+
`Staging freezes ${say(view.installationId)}: the app stops running and nothing can write its data until you abort the migration. Nothing was changed, and nothing has been sent to the server to freeze it.`,
|
|
462
|
+
`The plan: ${plural(documents, 'document')} in ${plural(view.plan.collections.length, 'collection')}, ${say(view.fromVersionId)} -> ${say(view.targetVersionId)}.`,
|
|
463
|
+
...view.disclaimers,
|
|
464
|
+
`→ To freeze it and start staging: ${bin} apps installations migrate stage ${inst} --yes`,
|
|
465
|
+
`→ To drop this plan without freezing anything: ${bin} apps installations migrate abort ${inst}`,
|
|
466
|
+
].join('\n');
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* What `cutover` says, instead of calling the server, when the migration is validated and the person has not said `--yes`: the cutover REPLACES the
|
|
470
|
+
* installation's stored documents and moves the app to the new version, which is theirs to confirm. States what changes and what is kept.
|
|
471
|
+
*/
|
|
472
|
+
function describeCutoverGate(view, bin) {
|
|
473
|
+
const inst = installationRef(view.installationId);
|
|
474
|
+
const documents = view.plan.collections.reduce((sum, c) => sum + c.documents, 0);
|
|
475
|
+
const days = data_provider_1.APP_DATA_MIGRATION_LIMITS.archiveRetentionDays;
|
|
476
|
+
return [
|
|
477
|
+
`The cutover REPLACES the stored documents of ${say(view.installationId)} and moves the app. Nothing was changed, and nothing has been sent to the server to start it.`,
|
|
478
|
+
`What changes: ${plural(documents, 'document')} in ${plural(view.plan.collections.length, 'collection')} are replaced by the converted ones, and the app moves from ${say(view.fromVersionId)} to ${say(view.targetVersionId)}. If that version asks for more than you approved, the app lands waiting for your approval, as any move to it would (a migration never grants anything).`,
|
|
479
|
+
`What is kept: the old documents are first copied to an archive, outside your storage quota, for ${days} days after the cutover completes (the exact date is shown then). \`${bin} apps installations migrate rollback ${inst} --yes\` puts them back within that time. After it they are erased for good.`,
|
|
480
|
+
`While it runs the app stays frozen; from the moment it starts it can only finish or be rolled back, not aborted. If this terminal is closed the platform resumes it by itself.`,
|
|
481
|
+
...view.disclaimers,
|
|
482
|
+
`→ To do it: ${bin} apps installations migrate cutover ${inst} --yes`,
|
|
483
|
+
`→ To leave everything as it is and put the app back: ${bin} apps installations migrate abort ${inst}`,
|
|
484
|
+
].join('\n');
|
|
485
|
+
}
|
|
486
|
+
/** What `rollback` says, instead of calling the server, when the person has not said `--yes`. */
|
|
487
|
+
function describeRollbackGate(view, bin) {
|
|
488
|
+
const inst = installationRef(view.installationId);
|
|
489
|
+
return [
|
|
490
|
+
`The rollback REPLACES the stored documents of ${say(view.installationId)} with the archived ones and moves the app back to ${say(view.fromVersionId)}. Nothing was changed, and nothing has been sent to the server to start it.`,
|
|
491
|
+
`It freezes the app while it runs and first counts the documents written since the cutover. If there are any it refuses and tells you how many per collection, unless you add --discard-writes-since-cutover: then they are lost for good.`,
|
|
492
|
+
view.archive?.expiresAt
|
|
493
|
+
? `The archive of the old documents is kept until ${say(view.archive.expiresAt)}; after that a rollback is no longer possible.`
|
|
494
|
+
: 'The archive of the old documents is kept for a limited time after the cutover.',
|
|
495
|
+
...view.disclaimers,
|
|
496
|
+
`→ To do it: ${bin} apps installations migrate rollback ${inst} --yes`,
|
|
497
|
+
].join('\n');
|
|
498
|
+
}
|
|
499
|
+
function reasonOf(body) {
|
|
500
|
+
const value = body?.reason;
|
|
501
|
+
return typeof value === 'string' && /^[a-z0-9_]{1,64}$/.test(value) ? value : undefined;
|
|
502
|
+
}
|
|
503
|
+
const isRefusalReason = (value) => typeof value === 'string' && data_provider_1.APP_DATA_MIGRATION_REFUSALS.includes(value);
|
|
504
|
+
/** The collections a refusal names, as the store's grammar allows them (bounded), or none. */
|
|
505
|
+
function collectionsOf(body) {
|
|
506
|
+
const value = body?.collections;
|
|
507
|
+
if (!Array.isArray(value))
|
|
508
|
+
return [];
|
|
509
|
+
return value.filter(data_provider_1.isAppStoreCollectionName).slice(0, 12);
|
|
510
|
+
}
|
|
511
|
+
function nextStepForRefusal(reason, ctx) {
|
|
512
|
+
const inst = installationRef(ctx.installation);
|
|
513
|
+
const version = plain(VERSION_ID, ctx.versionId) ?? '<version>';
|
|
514
|
+
const status = `${ctx.bin} apps installations migrate status ${inst}`;
|
|
515
|
+
switch (reason) {
|
|
516
|
+
case 'data_compatible':
|
|
517
|
+
return `→ Move it with: ${ctx.bin} apps upgrade ${inst} --version ${version}`;
|
|
518
|
+
case 'no_migration_declared':
|
|
519
|
+
case 'collection_removed':
|
|
520
|
+
return `→ See what does not fit with: ${ctx.bin} apps installations compatibility ${inst} --version ${version}`;
|
|
521
|
+
case 'compatibility_unknown':
|
|
522
|
+
return `→ Check it again with: ${ctx.bin} apps installations compatibility ${inst} --version ${version}`;
|
|
523
|
+
case 'migration_active':
|
|
524
|
+
return `→ See it with: ${status}. Finish it, or end it with: ${ctx.bin} apps installations migrate abort ${inst}`;
|
|
525
|
+
case 'freeze_not_confirmed':
|
|
526
|
+
return `→ Staging freezes the app. To confirm that, run it again with --yes: ${ctx.bin} apps installations migrate stage ${inst} --yes`;
|
|
527
|
+
case 'wrong_state':
|
|
528
|
+
case 'migration_not_found':
|
|
529
|
+
return `→ See which state it is in with: ${status}`;
|
|
530
|
+
case 'no_active_migration':
|
|
531
|
+
return `→ Plan one with: ${ctx.bin} apps installations migrate plan ${inst} --version <version>`;
|
|
532
|
+
case 'cutover_not_confirmed':
|
|
533
|
+
return `→ To confirm it, run it again with --yes: ${ctx.bin} apps installations migrate cutover ${inst} --yes`;
|
|
534
|
+
case 'rollback_not_confirmed':
|
|
535
|
+
return `→ To confirm it, run it again with --yes: ${ctx.bin} apps installations migrate rollback ${inst} --yes`;
|
|
536
|
+
case 'cutover_started':
|
|
537
|
+
return `→ See where it stands with: ${status}. A cutover that has begun finishes by running \`${ctx.bin} apps installations migrate cutover ${inst} --yes\` again, or is rolled back by the platform if it cannot.`;
|
|
538
|
+
case 'archive_expired':
|
|
539
|
+
case 'moved_since_cutover':
|
|
540
|
+
return `→ See what the app runs now with: ${ctx.bin} apps installations show ${inst}`;
|
|
541
|
+
case 'writes_since_cutover':
|
|
542
|
+
return `→ To roll back anyway and lose those documents: ${ctx.bin} apps installations migrate rollback ${inst} --yes --discard-writes-since-cutover`;
|
|
543
|
+
case 'version_not_installable':
|
|
544
|
+
return ctx.step === 'rollback'
|
|
545
|
+
? `→ The version this installation ran before is delisted or rejected, so it cannot be moved back to. See: ${status}`
|
|
546
|
+
: null;
|
|
547
|
+
default:
|
|
548
|
+
return null;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
/** A `migration_refused` answer, in words: the reason's sentence from the contract, the collections it names, why a check was unknown, and what to run next. */
|
|
552
|
+
function refusedFailure(facts, ctx, unchanged) {
|
|
553
|
+
const { reason, collections, unknownReason, migrationId, status, changes } = facts;
|
|
554
|
+
const head = isRefusalReason(reason)
|
|
555
|
+
? `The server refused the ${ctx.step}: ${data_provider_1.APP_DATA_MIGRATION_REFUSAL_WORDS[reason]}`
|
|
556
|
+
: `The server refused the ${ctx.step} for a reason this client does not know${reason ? ` (${reason})` : ''}; update the CLI`;
|
|
557
|
+
const unknownWords = unknownReason === undefined
|
|
558
|
+
? ''
|
|
559
|
+
: ` The check said: ${data_provider_1.APP_COMPATIBILITY_UNKNOWN_WORDS[unknownReason] ?? `${unknownReason} (a reason this client does not know)`}.`;
|
|
560
|
+
const named = collections.length === 0
|
|
561
|
+
? ''
|
|
562
|
+
: ` (${plural(collections.length, 'collection')}: ${collections.map(say).join(', ')})`;
|
|
563
|
+
const next = isRefusalReason(reason) ? nextStepForRefusal(reason, ctx) : null;
|
|
564
|
+
const written = changes === undefined || changes.length === 0
|
|
565
|
+
? ''
|
|
566
|
+
: ` Written since the cutover: ${changes.map((entry) => `${plural(entry.documents, 'document')} in ${say(entry.collection)}`).join(', ')}.`;
|
|
567
|
+
return {
|
|
568
|
+
message: `${head}${named}.${unknownWords}${written}${unchanged}${next ? `\n${next}` : ''}`,
|
|
569
|
+
code: exports.APPS_MIGRATION_CODES.refused,
|
|
570
|
+
...(status === undefined ? {} : { status }),
|
|
571
|
+
exit: exports.APPS_MIGRATION_EXIT.refused,
|
|
572
|
+
retryable: false,
|
|
573
|
+
details: {
|
|
574
|
+
reason: reason ?? null,
|
|
575
|
+
...(collections.length > 0 ? { collections } : {}),
|
|
576
|
+
...(changes === undefined || changes.length === 0 ? {} : { changes }),
|
|
577
|
+
...(unknownReason === undefined ? {} : { unknownReason }),
|
|
578
|
+
...(migrationId === undefined ? {} : { migrationId }),
|
|
579
|
+
},
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
/** A refusal this client makes itself, from what a read of the migration showed, with the words the server's own would have. Nothing was sent. */
|
|
583
|
+
function describeLocalRefusal(reason, ctx) {
|
|
584
|
+
return refusedFailure({ reason, collections: [] }, ctx, ' Nothing was changed.');
|
|
585
|
+
}
|
|
586
|
+
/** What is said when the open migration is not the one the person named. Nothing was sent. */
|
|
587
|
+
function describeNamedMigrationNotOpen(ctx) {
|
|
588
|
+
const inst = installationRef(ctx.installation);
|
|
589
|
+
return {
|
|
590
|
+
message: `The migration you named is not the one that is open on this installation (it was closed or replaced). Nothing was changed.\n→ Read which one is open with: ${ctx.bin} apps installations migrate status ${inst}`,
|
|
591
|
+
code: exports.APPS_MIGRATION_CODES.changed,
|
|
592
|
+
exit: exports.APPS_MIGRATION_EXIT.refused,
|
|
593
|
+
retryable: false,
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
/** The words of a write whose outcome is unproven: it may have landed, so it is neither a success nor a refusal (exit 5). */
|
|
597
|
+
function unknownOutcomeFailure(ctx, status) {
|
|
598
|
+
const inst = installationRef(ctx.installation);
|
|
599
|
+
const how = status === undefined || status === 0
|
|
600
|
+
? 'no answer came back'
|
|
601
|
+
: `the server answered HTTP ${status}`;
|
|
602
|
+
const earlier = ctx.calls !== undefined && ctx.calls > 1
|
|
603
|
+
? ` ${plural(ctx.calls - 1, 'earlier call')} in this run ${ctx.calls - 1 === 1 ? 'was' : 'were'} answered${ctx.last ? ` (the migration was ${ctx.last.state} then)` : ''}.`
|
|
604
|
+
: '';
|
|
605
|
+
return {
|
|
606
|
+
message: `The ${ctx.step} request may have reached the server, but its outcome is unknown (${how}).${earlier} The migration may have moved, and the app may be frozen: do not assume it did not happen, and do not send it again blindly.\n→ Read where it stands with: ${ctx.bin} apps installations migrate status ${inst}`,
|
|
607
|
+
code: exports.APPS_MIGRATION_CODES.outcomeUnknown,
|
|
608
|
+
...(typeof status === 'number' && status > 0 ? { status } : {}),
|
|
609
|
+
exit: exports.APPS_MIGRATION_EXIT.unknown,
|
|
610
|
+
retryable: false,
|
|
611
|
+
details: {
|
|
612
|
+
outcome: 'unknown',
|
|
613
|
+
step: ctx.step,
|
|
614
|
+
...(ctx.calls === undefined ? {} : { calls: ctx.calls }),
|
|
615
|
+
},
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Say what a failed request means, in the exit table's terms, or `null` when it is the CLI's ordinary failure (a sign-in that expired, a rate
|
|
620
|
+
* limit, the server being down on a READ): the caller then hands it to the base command, which words those.
|
|
621
|
+
*
|
|
622
|
+
* `write` says the request was one that changes state (plan, stage, validate, abort): for one, a transport failure or a 5xx proves
|
|
623
|
+
* nothing, so it is UNKNOWN (5) and says to run `status`. For a read it is not.
|
|
624
|
+
*/
|
|
625
|
+
function describeMigrationFailure(error, ctx, write) {
|
|
626
|
+
const status = (0, http_error_1.httpStatusOf)(error);
|
|
627
|
+
const body = (0, http_error_1.httpBodyOf)(error);
|
|
628
|
+
const code = typeof body?.code === 'string' ? body.code : undefined;
|
|
629
|
+
const errorField = typeof body?.error === 'string' ? body.error : undefined;
|
|
630
|
+
const inst = installationRef(ctx.installation);
|
|
631
|
+
const statusCmd = `${ctx.bin} apps installations migrate status ${inst}`;
|
|
632
|
+
const earlierWork = (ctx.calls ?? 1) > 1;
|
|
633
|
+
const unchanged = earlierWork
|
|
634
|
+
? ' This refused call changed nothing; the earlier calls of this run did their work.'
|
|
635
|
+
: ' Nothing was changed.';
|
|
636
|
+
const nothing = ctx.step === 'abort' && !earlierWork ? '' : unchanged;
|
|
637
|
+
// A failure the server tags as coming AFTER its write step finished: whatever its code says, the write may have landed.
|
|
638
|
+
if (write && body?.phase === 'after_write')
|
|
639
|
+
return unknownOutcomeFailure(ctx, status);
|
|
640
|
+
// The feature is switched off on this server: a deployment setting, not an outage. Nothing was read and nothing was changed.
|
|
641
|
+
if (status === 503 &&
|
|
642
|
+
(code === data_provider_1.APP_DATA_MIGRATION_UNAVAILABLE || errorField === data_provider_1.APP_DATA_MIGRATION_UNAVAILABLE)) {
|
|
643
|
+
return {
|
|
644
|
+
message: 'Migrating an installation’s data is not enabled on this server. That is a deployment setting (SKRR_APP_DATA_MIGRATION), not a fault: nothing was read or changed, and retrying will not help. Ask whoever runs this deployment to turn it on.',
|
|
645
|
+
code: exports.APPS_MIGRATION_CODES.notEnabled,
|
|
646
|
+
status,
|
|
647
|
+
exit: exports.APPS_MIGRATION_EXIT.notEnabled,
|
|
648
|
+
retryable: false,
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
// A personal access token never reaches the route: the router refuses it first.
|
|
652
|
+
if (status === 403 && code?.startsWith('PAT_')) {
|
|
653
|
+
return {
|
|
654
|
+
message: `An access token cannot do this: only a signed-in person who is the installation’s installer can run a data migration.${nothing}\n→ Unset SKRR_TOKEN and OVERSKY_TOKEN, then sign in with \`${ctx.bin} login\` and run this again.`,
|
|
655
|
+
code: exports.APPS_MIGRATION_CODES.forbidden,
|
|
656
|
+
status,
|
|
657
|
+
exit: exports.APPS_MIGRATION_EXIT.refused,
|
|
658
|
+
retryable: false,
|
|
659
|
+
details: { reason: 'token' },
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
// The server REFUSED the request, with the closed reason of the contract. A refusal changes nothing.
|
|
663
|
+
if (status === 409 &&
|
|
664
|
+
(code === data_provider_1.APP_DATA_MIGRATION_REFUSED || errorField === data_provider_1.APP_DATA_MIGRATION_REFUSED)) {
|
|
665
|
+
const unknownReasonRaw = typeof body?.unknownReason === 'string' ? body.unknownReason : undefined;
|
|
666
|
+
return refusedFailure({
|
|
667
|
+
reason: reasonOf(body),
|
|
668
|
+
collections: collectionsOf(body),
|
|
669
|
+
...((0, data_provider_1.parseChanges)(body?.changes) === null
|
|
670
|
+
? {}
|
|
671
|
+
: {
|
|
672
|
+
changes: (0, data_provider_1.parseChanges)(body?.changes),
|
|
673
|
+
}),
|
|
674
|
+
...(unknownReasonRaw !== undefined && /^[a-z0-9_]{1,64}$/.test(unknownReasonRaw)
|
|
675
|
+
? { unknownReason: unknownReasonRaw }
|
|
676
|
+
: {}),
|
|
677
|
+
...(plain(MIGRATION_ID, body?.migrationId) === null
|
|
678
|
+
? {}
|
|
679
|
+
: { migrationId: plain(MIGRATION_ID, body?.migrationId) }),
|
|
680
|
+
...(status === undefined ? {} : { status }),
|
|
681
|
+
}, ctx, unchanged);
|
|
682
|
+
}
|
|
683
|
+
if (status === 403 && (errorField === 'forbidden' || code === 'forbidden')) {
|
|
684
|
+
const reason = reasonOf(body);
|
|
685
|
+
return {
|
|
686
|
+
message: `${FORBIDDEN_WORDS[reason ?? ''] ?? 'You may not do this.'}${nothing}`,
|
|
687
|
+
code: exports.APPS_MIGRATION_CODES.forbidden,
|
|
688
|
+
status,
|
|
689
|
+
exit: exports.APPS_MIGRATION_EXIT.refused,
|
|
690
|
+
retryable: false,
|
|
691
|
+
details: { reason: reason ?? null },
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
if (status === 404 && (errorField === 'not_found' || code === 'not_found')) {
|
|
695
|
+
return {
|
|
696
|
+
message: `There is no such installation that you can see.${nothing}`,
|
|
697
|
+
code: exports.APPS_MIGRATION_CODES.notFound,
|
|
698
|
+
status,
|
|
699
|
+
exit: exports.APPS_MIGRATION_EXIT.refused,
|
|
700
|
+
retryable: false,
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
if (status === 409 && (errorField === 'changed' || code === 'changed')) {
|
|
704
|
+
return {
|
|
705
|
+
message: `The migration is not as this call expected: the one you named is not the one that is open (it was closed or replaced), or something changed it while this ran. Read where it stands before deciding what to do next.\n→ ${statusCmd}`,
|
|
706
|
+
code: exports.APPS_MIGRATION_CODES.changed,
|
|
707
|
+
status,
|
|
708
|
+
exit: exports.APPS_MIGRATION_EXIT.refused,
|
|
709
|
+
retryable: false,
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
// Another typed answer of the installations routes (a version that is not there, a bad request, an installation that cannot be read).
|
|
713
|
+
const rejected = typeof status === 'number' && status >= 400 && status < 500 && status !== 401 && status !== 429;
|
|
714
|
+
if (rejected && errorField !== undefined && OTHER_REJECTIONS[errorField] !== undefined) {
|
|
715
|
+
return {
|
|
716
|
+
message: `${OTHER_REJECTIONS[errorField]}${nothing}`,
|
|
717
|
+
code: exports.APPS_MIGRATION_CODES.rejected,
|
|
718
|
+
status,
|
|
719
|
+
exit: exports.APPS_MIGRATION_EXIT.refused,
|
|
720
|
+
retryable: false,
|
|
721
|
+
details: { error: errorField },
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
// A write that got no answer it can stand on may have landed: say so, and say how to find out.
|
|
725
|
+
if (write && outcomeUnknown(error, status, body))
|
|
726
|
+
return unknownOutcomeFailure(ctx, status);
|
|
727
|
+
return null;
|
|
728
|
+
}
|
|
729
|
+
/** Does this failure leave a write's outcome unproven? No answer (unless the request provably never left), 408, 425, a 5xx, a body the transport could not read, or a failure the server tagged `after_write`. */
|
|
730
|
+
function outcomeUnknown(error, status, body) {
|
|
731
|
+
if (body?.phase === 'after_write')
|
|
732
|
+
return true;
|
|
733
|
+
if (error?.code === 'NON_JSON_RESPONSE')
|
|
734
|
+
return true;
|
|
735
|
+
if (status === undefined || status === 0)
|
|
736
|
+
return !(0, write_outcome_1.requestNeverSent)(error);
|
|
737
|
+
return status === 408 || status === 425 || status >= 500;
|
|
738
|
+
}
|
|
739
|
+
/** The words of `forbidden`, by the reason the server gave. */
|
|
740
|
+
const FORBIDDEN_WORDS = {
|
|
741
|
+
human_only: 'Only a signed-in person can run a data migration, not an access token or an agent’s credential.',
|
|
742
|
+
missing_permission: 'You need edit access to what this app is installed on to run a data migration.',
|
|
743
|
+
installer_only: 'Only the installer (the person whose approval this installation runs under) can run a data migration. Another editor has to approve the app again first.',
|
|
744
|
+
suspended_by_moderation: 'The platform suspended this app, not you, and only the platform can lift that. A data migration neither freezes nor unfreezes an app the platform holds.',
|
|
745
|
+
};
|
|
746
|
+
/** The answers of the installations routes that are a definite "no" and have no words of the migration's own. */
|
|
747
|
+
const OTHER_REJECTIONS = {
|
|
748
|
+
invalid_input: 'The request was not understood. Check the ids and try again.',
|
|
749
|
+
version_not_found: 'There is no such version of that app that you can use.',
|
|
750
|
+
version_not_installable: 'That version is delisted or rejected, so no migration was planned for it.',
|
|
751
|
+
manifest_invalid: 'That version’s manifest is not one this platform will run, so no migration was planned for it.',
|
|
752
|
+
installation_incomplete: 'This installation points at a version that can no longer be read, so it cannot be migrated.',
|
|
753
|
+
};
|