@solidactions/cli 3.11.0 → 3.13.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/README.md +11 -0
- package/dist/command-manifest.json +75 -1
- package/dist/commands/database-push.js +375 -0
- package/dist/index.js +23 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -95,6 +95,7 @@ solidactions database pull analytics .solidactions/databases/analytics.db --yes
|
|
|
95
95
|
solidactions database pull analytics --writable
|
|
96
96
|
solidactions database import analytics backup.sql --yes
|
|
97
97
|
solidactions database import analytics backup.sql --resume <checkpoint> --yes
|
|
98
|
+
solidactions database push analytics complete.db --yes
|
|
98
99
|
```
|
|
99
100
|
|
|
100
101
|
`--json` produces machine-readable output where supported. `delete`, `exec`,
|
|
@@ -111,6 +112,16 @@ By default, `pull` atomically publishes a read-only local replica at
|
|
|
111
112
|
`.solidactions/databases/<safe-stem>.db`, or at an explicit path. Reuse that
|
|
112
113
|
file for local analytics and transformations. `pull --writable` instead opens a
|
|
113
114
|
foreground session: writes go to the live workspace database.
|
|
115
|
+
|
|
116
|
+
`database push` replaces the complete remote database from a local SQLite file.
|
|
117
|
+
It requires `--yes`, never changes the source file, and normalizes a private
|
|
118
|
+
snapshot to UTF-8 SQLite with 4096-byte pages, no auto-vacuum, and WAL mode.
|
|
119
|
+
The normalized snapshot can have a different byte size from the source.
|
|
120
|
+
Quiesce writers before starting: replacement invalidates the old database URL
|
|
121
|
+
and credentials. Empty databases additionally require `--allow-empty`; use
|
|
122
|
+
`--idempotency-key <uuid>` to safely replay an interrupted operation.
|
|
123
|
+
Reported countable rows include ordinary user tables; internal, virtual, and
|
|
124
|
+
shadow tables are excluded.
|
|
114
125
|
No offline merge or background write-back continues after exit.
|
|
115
126
|
|
|
116
127
|
`dump` publishes only a complete SQL file through an owned temporary path and
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schema_version": 1,
|
|
3
3
|
"cli_name": "solidactions",
|
|
4
|
-
"cli_version": "3.
|
|
4
|
+
"cli_version": "3.13.0",
|
|
5
5
|
"global_options": [
|
|
6
6
|
{
|
|
7
7
|
"flags": "-V, --version",
|
|
@@ -911,6 +911,80 @@
|
|
|
911
911
|
}
|
|
912
912
|
]
|
|
913
913
|
},
|
|
914
|
+
{
|
|
915
|
+
"path": [
|
|
916
|
+
"database",
|
|
917
|
+
"push"
|
|
918
|
+
],
|
|
919
|
+
"name": "push",
|
|
920
|
+
"aliases": [],
|
|
921
|
+
"description": "Replace from a private WAL/4096-byte/auto-vacuum NONE snapshot; source file is unchanged; countable rows exclude internal, virtual, and shadow tables",
|
|
922
|
+
"hidden": false,
|
|
923
|
+
"arguments": [
|
|
924
|
+
{
|
|
925
|
+
"name": "database",
|
|
926
|
+
"required": true,
|
|
927
|
+
"variadic": false,
|
|
928
|
+
"description": "Database name"
|
|
929
|
+
},
|
|
930
|
+
{
|
|
931
|
+
"name": "file.db",
|
|
932
|
+
"required": true,
|
|
933
|
+
"variadic": false,
|
|
934
|
+
"description": "Complete SQLite database file"
|
|
935
|
+
}
|
|
936
|
+
],
|
|
937
|
+
"options": [
|
|
938
|
+
{
|
|
939
|
+
"flags": "-y, --yes",
|
|
940
|
+
"long": "--yes",
|
|
941
|
+
"short": "-y",
|
|
942
|
+
"description": "Acknowledge destructive replacement",
|
|
943
|
+
"required": true,
|
|
944
|
+
"value_required": false,
|
|
945
|
+
"value_optional": false,
|
|
946
|
+
"variadic": false,
|
|
947
|
+
"negated": false,
|
|
948
|
+
"hidden": false
|
|
949
|
+
},
|
|
950
|
+
{
|
|
951
|
+
"flags": "--allow-empty",
|
|
952
|
+
"long": "--allow-empty",
|
|
953
|
+
"short": null,
|
|
954
|
+
"description": "Allow a database with zero countable rows",
|
|
955
|
+
"required": false,
|
|
956
|
+
"value_required": false,
|
|
957
|
+
"value_optional": false,
|
|
958
|
+
"variadic": false,
|
|
959
|
+
"negated": false,
|
|
960
|
+
"hidden": false
|
|
961
|
+
},
|
|
962
|
+
{
|
|
963
|
+
"flags": "--idempotency-key <uuid>",
|
|
964
|
+
"long": "--idempotency-key",
|
|
965
|
+
"short": null,
|
|
966
|
+
"description": "Reuse a bulk-load operation UUID",
|
|
967
|
+
"required": false,
|
|
968
|
+
"value_required": true,
|
|
969
|
+
"value_optional": false,
|
|
970
|
+
"variadic": false,
|
|
971
|
+
"negated": false,
|
|
972
|
+
"hidden": false
|
|
973
|
+
},
|
|
974
|
+
{
|
|
975
|
+
"flags": "-h, --help",
|
|
976
|
+
"long": "--help",
|
|
977
|
+
"short": "-h",
|
|
978
|
+
"description": "display help for command",
|
|
979
|
+
"required": false,
|
|
980
|
+
"value_required": false,
|
|
981
|
+
"value_optional": false,
|
|
982
|
+
"variadic": false,
|
|
983
|
+
"negated": false,
|
|
984
|
+
"hidden": false
|
|
985
|
+
}
|
|
986
|
+
]
|
|
987
|
+
},
|
|
914
988
|
{
|
|
915
989
|
"path": [
|
|
916
990
|
"database",
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.normalizeDatabaseForPush = normalizeDatabaseForPush;
|
|
40
|
+
exports.assertCountableTableLimit = assertCountableTableLimit;
|
|
41
|
+
exports.databasePushWithConfig = databasePushWithConfig;
|
|
42
|
+
exports.databasePush = databasePush;
|
|
43
|
+
const axios_1 = __importDefault(require("axios"));
|
|
44
|
+
const crypto_1 = require("crypto");
|
|
45
|
+
const fs_1 = __importDefault(require("fs"));
|
|
46
|
+
const os_1 = __importDefault(require("os"));
|
|
47
|
+
const path_1 = __importDefault(require("path"));
|
|
48
|
+
const url_1 = require("url");
|
|
49
|
+
const client_1 = require("@libsql/client");
|
|
50
|
+
const database_data_plane_1 = require("../utils/database-data-plane");
|
|
51
|
+
const MAX_BYTES = 20_000_000_000;
|
|
52
|
+
const PAGE_SIZE = 4096;
|
|
53
|
+
const POLL_MS = 2_000;
|
|
54
|
+
const TERMINAL = new Set(['promoted', 'failed', 'aborted']);
|
|
55
|
+
const PHASES = new Set(['preparing', 'uploading', 'validating', 'finalizing', 'promoted_pending_ready', 'promoted', 'aborting', 'aborted', 'failed']);
|
|
56
|
+
// Laravel's unversioned `uuid` validation accepts the canonical hexadecimal
|
|
57
|
+
// 8-4-4-4-12 representation regardless of UUID version (including UUIDv7).
|
|
58
|
+
const IDEMPOTENCY_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
59
|
+
const OPERATION_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
60
|
+
const EXCLUDED_PREFIXES = ['sqlite_', '_litestream', 'libsql_', '_turso_'];
|
|
61
|
+
function sqlString(value) {
|
|
62
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
63
|
+
}
|
|
64
|
+
function sqlIdentifier(value) {
|
|
65
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
66
|
+
}
|
|
67
|
+
function scalar(rows, label) {
|
|
68
|
+
if (rows.length !== 1 || rows[0].length < 1)
|
|
69
|
+
throw new Error(`Invalid ${label} result.`);
|
|
70
|
+
return rows[0][0];
|
|
71
|
+
}
|
|
72
|
+
function integer(value, label) {
|
|
73
|
+
const parsed = Number(value);
|
|
74
|
+
if (!Number.isSafeInteger(parsed) || parsed < 0)
|
|
75
|
+
throw new Error(`Invalid ${label}.`);
|
|
76
|
+
return parsed;
|
|
77
|
+
}
|
|
78
|
+
function isExcluded(name) {
|
|
79
|
+
const folded = name.toLowerCase();
|
|
80
|
+
return EXCLUDED_PREFIXES.some((prefix) => folded.startsWith(prefix));
|
|
81
|
+
}
|
|
82
|
+
function safeNormalizationError(error) {
|
|
83
|
+
if (error instanceof database_data_plane_1.DatabaseOperationError)
|
|
84
|
+
return error;
|
|
85
|
+
if (error?.code === 'ENOENT') {
|
|
86
|
+
return new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'The source database file was not found; no source file was changed.');
|
|
87
|
+
}
|
|
88
|
+
const text = error instanceof Error ? error.message.toLowerCase() : '';
|
|
89
|
+
if (text.includes('database is locked') || text.includes('busy')) {
|
|
90
|
+
return new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'The SQLite snapshot is busy. Quiesce local writers and retry; the source database was not changed.');
|
|
91
|
+
}
|
|
92
|
+
if (text.includes('encrypted') || text.includes('not a database') || text.includes('malformed') || text.includes('corrupt')) {
|
|
93
|
+
return new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'The SQLite file is corrupt, encrypted, or not a database. Export an unencrypted SQLite database and retry; the source database was not changed.');
|
|
94
|
+
}
|
|
95
|
+
if (text.includes('no space') || text.includes('disk full')) {
|
|
96
|
+
return new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'Temporary disk space is insufficient for database normalization; the source database was not changed.');
|
|
97
|
+
}
|
|
98
|
+
return new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'The local SQLite binding could not open or normalize a temporary copy. Verify Node 20 platform support and the input file; the source database was not changed.');
|
|
99
|
+
}
|
|
100
|
+
async function sha256(file) {
|
|
101
|
+
const hash = (0, crypto_1.createHash)('sha256');
|
|
102
|
+
for await (const chunk of fs_1.default.createReadStream(file))
|
|
103
|
+
hash.update(chunk);
|
|
104
|
+
return hash.digest('hex');
|
|
105
|
+
}
|
|
106
|
+
async function removeSidecars(file) {
|
|
107
|
+
await Promise.all(['-wal', '-shm', '-journal'].map((suffix) => fs_1.default.promises.rm(`${file}${suffix}`, { force: true })));
|
|
108
|
+
}
|
|
109
|
+
async function normalizeDatabaseForPush(sourceInput, dependencies = {}) {
|
|
110
|
+
const source = path_1.default.resolve(sourceInput);
|
|
111
|
+
let directory;
|
|
112
|
+
let sourceClient;
|
|
113
|
+
let stageClient;
|
|
114
|
+
let finalClient;
|
|
115
|
+
try {
|
|
116
|
+
const sourceStat = await fs_1.default.promises.stat(source);
|
|
117
|
+
if (!sourceStat.isFile() || sourceStat.size === 0)
|
|
118
|
+
throw new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'A database push requires a non-empty regular SQLite file; the source database was not changed.');
|
|
119
|
+
if (sourceStat.size > MAX_BYTES)
|
|
120
|
+
throw new database_data_plane_1.DatabaseOperationError('bulk_load_too_large', 'The database exceeds the decimal 20,000,000,000-byte bulk-load limit; the source database was not changed.');
|
|
121
|
+
const tempRoot = dependencies.tempRoot ?? os_1.default.tmpdir();
|
|
122
|
+
const statfs = dependencies.statfs ?? fs_1.default.promises.statfs;
|
|
123
|
+
const space = await statfs(tempRoot);
|
|
124
|
+
const available = Number(space.bavail) * Number(space.bsize);
|
|
125
|
+
if (!Number.isFinite(available) || available < sourceStat.size * 2 + 16 * 1024 * 1024) {
|
|
126
|
+
throw new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'Temporary disk space is insufficient for two normalized database copies; the source database was not changed.');
|
|
127
|
+
}
|
|
128
|
+
directory = await fs_1.default.promises.mkdtemp(path_1.default.join(tempRoot, 'solidactions-database-push-'));
|
|
129
|
+
await fs_1.default.promises.chmod(directory, 0o700);
|
|
130
|
+
const snapshot = path_1.default.join(directory, 'snapshot.db');
|
|
131
|
+
const final = path_1.default.join(directory, 'upload.db');
|
|
132
|
+
sourceClient = (0, client_1.createClient)({ url: (0, url_1.pathToFileURL)(source).href, intMode: 'number' });
|
|
133
|
+
await sourceClient.execute(`VACUUM INTO ${sqlString(snapshot)}`);
|
|
134
|
+
await sourceClient.close();
|
|
135
|
+
sourceClient = undefined;
|
|
136
|
+
stageClient = (0, client_1.createClient)({ url: (0, url_1.pathToFileURL)(snapshot).href, intMode: 'number' });
|
|
137
|
+
await stageClient.execute('PRAGMA journal_mode=DELETE');
|
|
138
|
+
await stageClient.execute('PRAGMA page_size=4096');
|
|
139
|
+
await stageClient.execute('PRAGMA auto_vacuum=NONE');
|
|
140
|
+
await stageClient.execute(`VACUUM INTO ${sqlString(final)}`);
|
|
141
|
+
await stageClient.close();
|
|
142
|
+
stageClient = undefined;
|
|
143
|
+
finalClient = (0, client_1.createClient)({ url: (0, url_1.pathToFileURL)(final).href, intMode: 'number' });
|
|
144
|
+
const mode = String(scalar((await finalClient.execute('PRAGMA journal_mode=WAL')).rows, 'journal mode')).toLowerCase();
|
|
145
|
+
const checkpoint = (await finalClient.execute('PRAGMA wal_checkpoint(TRUNCATE)')).rows[0];
|
|
146
|
+
if (mode !== 'wal' || !checkpoint || Number(checkpoint[0]) !== 0)
|
|
147
|
+
throw new Error('WAL checkpoint is busy.');
|
|
148
|
+
await finalClient.close();
|
|
149
|
+
finalClient = undefined;
|
|
150
|
+
await removeSidecars(final);
|
|
151
|
+
await fs_1.default.promises.chmod(final, 0o400);
|
|
152
|
+
finalClient = (0, client_1.createClient)({ url: (0, url_1.pathToFileURL)(final).href, intMode: 'number' });
|
|
153
|
+
const encoding = String(scalar((await finalClient.execute('PRAGMA encoding')).rows, 'encoding'));
|
|
154
|
+
if (encoding.toUpperCase() !== 'UTF-8') {
|
|
155
|
+
throw new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'The database encoding is not UTF-8 and cannot be changed in place. Export or rebuild a new UTF-8 SQLite database; the source database was not changed.');
|
|
156
|
+
}
|
|
157
|
+
const pageSize = integer(scalar((await finalClient.execute('PRAGMA page_size')).rows, 'page size'), 'page size');
|
|
158
|
+
const autoVacuum = integer(scalar((await finalClient.execute('PRAGMA auto_vacuum')).rows, 'auto vacuum'), 'auto vacuum');
|
|
159
|
+
const persistedMode = String(scalar((await finalClient.execute('PRAGMA journal_mode')).rows, 'journal mode')).toLowerCase();
|
|
160
|
+
const integrity = String(scalar((await finalClient.execute('PRAGMA integrity_check(1)')).rows, 'integrity'));
|
|
161
|
+
if (pageSize !== PAGE_SIZE || autoVacuum !== 0 || persistedMode !== 'wal' || integrity !== 'ok')
|
|
162
|
+
throw new Error('Normalized SQLite invariants failed.');
|
|
163
|
+
const schemaResult = await finalClient.execute('SELECT type, name, tbl_name, sql FROM sqlite_master WHERE sql IS NOT NULL ORDER BY type COLLATE BINARY, name COLLATE BINARY, tbl_name COLLATE BINARY, sql COLLATE BINARY');
|
|
164
|
+
const schema = schemaResult.rows
|
|
165
|
+
.map((row) => Array.from(row, (value) => value === null ? null : String(value)))
|
|
166
|
+
.filter((row) => !isExcluded(String(row[1])) && !isExcluded(String(row[2])));
|
|
167
|
+
const schemaSha = (0, crypto_1.createHash)('sha256').update(JSON.stringify(schema), 'utf8').digest('hex');
|
|
168
|
+
const tableList = await finalClient.execute('PRAGMA table_list');
|
|
169
|
+
const candidates = tableList.rows
|
|
170
|
+
.filter((row) => String(row[2]) === 'table')
|
|
171
|
+
.map((row) => String(row[1]))
|
|
172
|
+
.filter((name) => !isExcluded(name));
|
|
173
|
+
const masterNames = new Set(schema.filter((row) => row[0] === 'table').map((row) => row[1]));
|
|
174
|
+
const names = candidates.filter((name) => masterNames.has(name)).sort((a, b) => Buffer.from(a).compare(Buffer.from(b)));
|
|
175
|
+
assertCountableTableLimit(names.length);
|
|
176
|
+
const tables = {};
|
|
177
|
+
let totalRows = 0;
|
|
178
|
+
for (const name of names) {
|
|
179
|
+
const count = integer(scalar((await finalClient.execute(`SELECT COUNT(*) FROM ${sqlIdentifier(name)}`)).rows, `row count for ${name}`), `row count for ${name}`);
|
|
180
|
+
tables[name] = count;
|
|
181
|
+
totalRows += count;
|
|
182
|
+
if (!Number.isSafeInteger(totalRows))
|
|
183
|
+
throw new Error('Row count overflow.');
|
|
184
|
+
}
|
|
185
|
+
const pageCount = integer(scalar((await finalClient.execute('PRAGMA page_count')).rows, 'page count'), 'page count');
|
|
186
|
+
await finalClient.close();
|
|
187
|
+
finalClient = undefined;
|
|
188
|
+
await removeSidecars(final);
|
|
189
|
+
const inputBytes = (await fs_1.default.promises.stat(final)).size;
|
|
190
|
+
if (inputBytes !== pageCount * PAGE_SIZE || inputBytes < PAGE_SIZE)
|
|
191
|
+
throw new Error('Normalized byte count mismatch.');
|
|
192
|
+
if (inputBytes > MAX_BYTES)
|
|
193
|
+
throw new database_data_plane_1.DatabaseOperationError('bulk_load_too_large', 'The normalized database exceeds the decimal 20,000,000,000-byte bulk-load limit; the source database was not changed.');
|
|
194
|
+
const digest = await sha256(final);
|
|
195
|
+
return {
|
|
196
|
+
file: final, inputBytes, pageCount, totalRows,
|
|
197
|
+
manifest: { sha256: digest, schema_sha256: schemaSha, tables },
|
|
198
|
+
cleanup: async () => { if (directory)
|
|
199
|
+
await fs_1.default.promises.rm(directory, { recursive: true, force: true }); },
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
try {
|
|
204
|
+
await sourceClient?.close();
|
|
205
|
+
}
|
|
206
|
+
catch { /* sanitized below */ }
|
|
207
|
+
try {
|
|
208
|
+
await stageClient?.close();
|
|
209
|
+
}
|
|
210
|
+
catch { /* sanitized below */ }
|
|
211
|
+
try {
|
|
212
|
+
await finalClient?.close();
|
|
213
|
+
}
|
|
214
|
+
catch { /* sanitized below */ }
|
|
215
|
+
if (directory) {
|
|
216
|
+
try {
|
|
217
|
+
await fs_1.default.promises.chmod(directory, 0o700);
|
|
218
|
+
}
|
|
219
|
+
catch { /* cleanup remains best effort */ }
|
|
220
|
+
await fs_1.default.promises.rm(directory, { recursive: true, force: true });
|
|
221
|
+
}
|
|
222
|
+
throw safeNormalizationError(error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function assertCountableTableLimit(count) {
|
|
226
|
+
if (count > 1_000) {
|
|
227
|
+
throw new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'The database has more than 1,000 countable tables and cannot be bulk loaded; the source database was not changed.');
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function hardDeadlineMs(bytes) {
|
|
231
|
+
return Math.min(4 * 60 * 60_000, 30 * 60_000 + Math.ceil(bytes / 2_000_000) * 1000);
|
|
232
|
+
}
|
|
233
|
+
// A push may keep polling past the client's own hard deadline, but only as far
|
|
234
|
+
// as the deadline the SERVER reports for the phase it is in (#1287 R18), and
|
|
235
|
+
// never unboundedly: `hard_expires_at` is the UPLOAD deadline, while validation
|
|
236
|
+
// runs on a lease of its own that legitimately outlives it. Giving up on the
|
|
237
|
+
// upload deadline abandoned pushes whose validation was perfectly healthy.
|
|
238
|
+
const MAX_POLL_EXTENSION_MS = 60 * 60_000;
|
|
239
|
+
function extendedPollDeadline(current, uploadDeadlineAt, serverDeadline) {
|
|
240
|
+
if (typeof serverDeadline !== 'string')
|
|
241
|
+
return current;
|
|
242
|
+
const parsed = Date.parse(serverDeadline);
|
|
243
|
+
if (!Number.isFinite(parsed))
|
|
244
|
+
return current;
|
|
245
|
+
return Math.max(current, Math.min(parsed + POLL_MS, uploadDeadlineAt + MAX_POLL_EXTENSION_MS));
|
|
246
|
+
}
|
|
247
|
+
function stableOperation(data) {
|
|
248
|
+
const operation = data?.operation;
|
|
249
|
+
if (!operation || typeof operation.id !== 'string' || !OPERATION_UUID.test(operation.id) || typeof operation.phase !== 'string' || !PHASES.has(operation.phase))
|
|
250
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'The database bulk-load response was invalid.');
|
|
251
|
+
return operation;
|
|
252
|
+
}
|
|
253
|
+
async function databasePushWithConfig(name, file, options, config, dependencies = {}) {
|
|
254
|
+
if (!options.yes)
|
|
255
|
+
throw new database_data_plane_1.DatabaseOperationError('confirmation_required', 'Database push is destructive and requires --yes.');
|
|
256
|
+
const key = options.idempotencyKey ?? (0, crypto_1.randomUUID)();
|
|
257
|
+
if (!IDEMPOTENCY_UUID.test(key))
|
|
258
|
+
throw new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', '--idempotency-key must be a UUID.');
|
|
259
|
+
const stdout = dependencies.stdout ?? console.log;
|
|
260
|
+
const sleep = dependencies.sleep ?? ((milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds)));
|
|
261
|
+
const normalized = await normalizeDatabaseForPush(file, dependencies);
|
|
262
|
+
let operationId;
|
|
263
|
+
let promoteAccepted = false;
|
|
264
|
+
let terminalObserved = false;
|
|
265
|
+
try {
|
|
266
|
+
if (normalized.totalRows === 0 && !options.allowEmpty)
|
|
267
|
+
throw new database_data_plane_1.DatabaseOperationError('invalid_bulk_database', 'This database has zero countable rows. Retry with --allow-empty only if replacing the remote database with an empty database is intended; the source database was not changed.');
|
|
268
|
+
const deadlineMs = hardDeadlineMs(normalized.inputBytes);
|
|
269
|
+
const clock = dependencies.now ?? Date.now;
|
|
270
|
+
const deadlineAt = clock() + deadlineMs;
|
|
271
|
+
const remaining = () => {
|
|
272
|
+
const value = deadlineAt - clock();
|
|
273
|
+
if (value <= 0)
|
|
274
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Database replacement did not finish before the displayed hard deadline. Retry with the same idempotency key.');
|
|
275
|
+
return value;
|
|
276
|
+
};
|
|
277
|
+
const controlRemaining = () => Math.min(database_data_plane_1.DEFAULT_DATABASE_CONTROL_PLANE_TIMEOUT_MS, remaining());
|
|
278
|
+
stdout(`WARNING: This destructively replaces database "${name}". Quiesce all writers; old URLs and credentials will become invalid. A private snapshot is normalized to WAL, 4096-byte pages, and auto-vacuum NONE; the source file is unchanged.`);
|
|
279
|
+
stdout(`Hard deadline: ${Math.ceil(deadlineMs / 60_000)} minutes for ${normalized.inputBytes} bytes. Idempotency key: ${key}`);
|
|
280
|
+
const prepareBody = {
|
|
281
|
+
operation: 'bulk_load_prepare', name, bulk_mode: 'replace', input_bytes: normalized.inputBytes,
|
|
282
|
+
page_count: normalized.pageCount, manifest: normalized.manifest, allow_empty: options.allowEmpty === true, idempotency_key: key,
|
|
283
|
+
};
|
|
284
|
+
const prepare = await (0, database_data_plane_1.requestDatabaseOperation)(config, prepareBody, { post: dependencies.post, controlPlaneTimeoutMs: controlRemaining() });
|
|
285
|
+
const operation = stableOperation(prepare);
|
|
286
|
+
operationId = String(operation.id);
|
|
287
|
+
const upload = prepare?.upload;
|
|
288
|
+
if (typeof upload?.url !== 'string' || typeof upload?.token !== 'string')
|
|
289
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'The database upload credential response was invalid.');
|
|
290
|
+
const uploader = dependencies.upload ?? axios_1.default.post;
|
|
291
|
+
let uploaded;
|
|
292
|
+
let renewalFailure;
|
|
293
|
+
let renewing = Promise.resolve();
|
|
294
|
+
const renewalTimer = setInterval(() => {
|
|
295
|
+
renewing = renewing.then(async () => {
|
|
296
|
+
const replay = await (0, database_data_plane_1.requestDatabaseOperation)(config, prepareBody, { post: dependencies.post, controlPlaneTimeoutMs: controlRemaining() });
|
|
297
|
+
const replayOperation = stableOperation(replay);
|
|
298
|
+
if (replayOperation.id !== operation.id)
|
|
299
|
+
throw new database_data_plane_1.DatabaseOperationError('bulk_load_conflict', 'The bulk-load replay returned a different operation.');
|
|
300
|
+
// A replay is a LEASE renewal only (#1287 R9b): the server
|
|
301
|
+
// returns no credential, because an in-progress direct upload
|
|
302
|
+
// cannot swap tokens mid-flight. The original single-use
|
|
303
|
+
// credential stays valid until the hard deadline. Should any
|
|
304
|
+
// server ever include one, it is neither retained nor printed.
|
|
305
|
+
}).catch((error) => { renewalFailure = error; });
|
|
306
|
+
}, 5 * 60_000);
|
|
307
|
+
try {
|
|
308
|
+
uploaded = await uploader(upload.url, fs_1.default.createReadStream(normalized.file), {
|
|
309
|
+
headers: { Authorization: `Bearer ${upload.token}`, 'Content-Type': 'application/octet-stream', 'Content-Length': String(normalized.inputBytes) },
|
|
310
|
+
maxBodyLength: normalized.inputBytes, maxContentLength: 1024, timeout: remaining(),
|
|
311
|
+
validateStatus: (status) => (status >= 200 && status < 300) || status === 413,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Candidate upload failed. The upstream response was hidden to protect credentials; the source database was not changed.');
|
|
316
|
+
}
|
|
317
|
+
finally {
|
|
318
|
+
clearInterval(renewalTimer);
|
|
319
|
+
await renewing;
|
|
320
|
+
}
|
|
321
|
+
if (renewalFailure)
|
|
322
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'The database upload lease could not be renewed; the source database was not changed.');
|
|
323
|
+
if (uploaded.status < 200 || uploaded.status > 299) {
|
|
324
|
+
if (uploaded.status === 413)
|
|
325
|
+
throw new database_data_plane_1.DatabaseOperationError('bulk_load_too_large', 'The upstream database service rejected the upload size; the source database was not changed.');
|
|
326
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Candidate upload failed. The upstream response was hidden to protect credentials; the source database was not changed.');
|
|
327
|
+
}
|
|
328
|
+
await (0, database_data_plane_1.requestDatabaseOperation)(config, { operation: 'bulk_load_promote', operation_id: operation.id, idempotency_key: key, upload_http_status: uploaded.status }, { post: dependencies.post, controlPlaneTimeoutMs: controlRemaining() });
|
|
329
|
+
promoteAccepted = true;
|
|
330
|
+
let pollDeadlineAt = deadlineAt;
|
|
331
|
+
const pollControlRemaining = () => {
|
|
332
|
+
const value = pollDeadlineAt - clock();
|
|
333
|
+
if (value <= 0)
|
|
334
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Database replacement did not finish before the displayed hard deadline. Retry with the same idempotency key.');
|
|
335
|
+
return Math.min(database_data_plane_1.DEFAULT_DATABASE_CONTROL_PLANE_TIMEOUT_MS, value);
|
|
336
|
+
};
|
|
337
|
+
while (clock() <= pollDeadlineAt) {
|
|
338
|
+
const response = await (0, database_data_plane_1.requestDatabaseOperation)(config, { operation: 'bulk_load_status', operation_id: operation.id }, { post: dependencies.post, controlPlaneTimeoutMs: pollControlRemaining() });
|
|
339
|
+
const status = stableOperation(response);
|
|
340
|
+
pollDeadlineAt = extendedPollDeadline(pollDeadlineAt, deadlineAt, status.deadline_at);
|
|
341
|
+
if (!TERMINAL.has(String(status.phase))) {
|
|
342
|
+
await sleep(POLL_MS);
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
terminalObserved = true;
|
|
346
|
+
if (status.phase !== 'promoted')
|
|
347
|
+
throw new database_data_plane_1.DatabaseOperationError(typeof status.failure_code === 'string' ? status.failure_code : 'upstream_unavailable', `Database replacement ended in ${status.phase}.`);
|
|
348
|
+
stdout(`Database "${name}" replaced successfully (${String(status.rows_loaded ?? normalized.totalRows)} countable rows). Reacquire database credentials before reconnecting.`);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Database replacement did not finish before the displayed hard deadline. Retry status with the same idempotency key.');
|
|
352
|
+
}
|
|
353
|
+
catch (error) {
|
|
354
|
+
if (operationId && !promoteAccepted) {
|
|
355
|
+
try {
|
|
356
|
+
await (0, database_data_plane_1.requestDatabaseOperation)(config, { operation: 'bulk_load_abort', operation_id: operationId, idempotency_key: key }, { post: dependencies.post });
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
// Preserve the original credential-safe failure. The durable
|
|
360
|
+
// operation deadline and server reconciliation own cleanup.
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (operationId && promoteAccepted && !terminalObserved) {
|
|
364
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', `Promotion was accepted, but status could not be confirmed. Operation ${operationId} remains active; retry with idempotency key ${key}.`);
|
|
365
|
+
}
|
|
366
|
+
throw error;
|
|
367
|
+
}
|
|
368
|
+
finally {
|
|
369
|
+
await normalized.cleanup();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
async function databasePush(name, file, options) {
|
|
373
|
+
const { requireConfigWithWorkspace } = await Promise.resolve().then(() => __importStar(require('../utils/api')));
|
|
374
|
+
await databasePushWithConfig(name, file, options, await requireConfigWithWorkspace());
|
|
375
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -93,6 +93,7 @@ const crew_env_map_database_1 = require("./commands/crew-env-map-database");
|
|
|
93
93
|
const state_1 = require("./commands/state");
|
|
94
94
|
const workflow_view_1 = require("./commands/workflow-view");
|
|
95
95
|
const database_1 = require("./commands/database");
|
|
96
|
+
const database_push_1 = require("./commands/database-push");
|
|
96
97
|
const config_1 = require("./utils/config");
|
|
97
98
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
98
99
|
const pkg = require('../package.json');
|
|
@@ -215,7 +216,12 @@ exports.program
|
|
|
215
216
|
// =============================================================================
|
|
216
217
|
// Register the complete public surface up front so help and the generated
|
|
217
218
|
// command manifest stay authoritative while handlers land in focused slices.
|
|
218
|
-
const database = exports.program
|
|
219
|
+
const database = exports.program
|
|
220
|
+
.command('database')
|
|
221
|
+
.description('Manage workspace databases')
|
|
222
|
+
.addHelpText('after', `
|
|
223
|
+
Connect to a Workspace Database directly (any HTTP client or libSQL-compatible
|
|
224
|
+
driver): https://www.solidactions.com/docs/workspace-databases/`);
|
|
219
225
|
database
|
|
220
226
|
.command('list')
|
|
221
227
|
.description('List workspace databases')
|
|
@@ -305,6 +311,22 @@ database
|
|
|
305
311
|
.action(async (name, file, options) => {
|
|
306
312
|
await (0, database_1.databaseImport)(name, file, options);
|
|
307
313
|
});
|
|
314
|
+
database
|
|
315
|
+
.command('push')
|
|
316
|
+
.description('Replace from a private WAL/4096-byte/auto-vacuum NONE snapshot; source file is unchanged; countable rows exclude internal, virtual, and shadow tables')
|
|
317
|
+
.argument('<database>', 'Database name')
|
|
318
|
+
.argument('<file.db>', 'Complete SQLite database file')
|
|
319
|
+
.requiredOption('-y, --yes', 'Acknowledge destructive replacement')
|
|
320
|
+
.option('--allow-empty', 'Allow a database with zero countable rows')
|
|
321
|
+
.option('--idempotency-key <uuid>', 'Reuse a bulk-load operation UUID')
|
|
322
|
+
.addHelpText('after', `
|
|
323
|
+
The source file is unchanged. Push uploads a private normalized snapshot using
|
|
324
|
+
WAL mode, 4096-byte pages, and auto-vacuum NONE; its byte size can differ from
|
|
325
|
+
the source. Countable rows include ordinary user tables only. Internal,
|
|
326
|
+
virtual, and shadow tables are excluded from the count.`)
|
|
327
|
+
.action(async (name, file, options) => {
|
|
328
|
+
await (0, database_push_1.databasePush)(name, file, options);
|
|
329
|
+
});
|
|
308
330
|
// =============================================================================
|
|
309
331
|
// project <subcommand>
|
|
310
332
|
// =============================================================================
|