velocious 1.0.535 → 1.0.536
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/build/database/drivers/base.js +17 -0
- package/build/database/drivers/mysql/index.js +15 -0
- package/build/database/drivers/mysql/query-stream.js +43 -0
- package/build/database/tenants/data-copier.js +169 -0
- package/build/src/database/drivers/base.d.ts +10 -0
- package/build/src/database/drivers/base.d.ts.map +1 -1
- package/build/src/database/drivers/base.js +16 -1
- package/build/src/database/drivers/mysql/index.d.ts +8 -0
- package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mysql/index.js +16 -1
- package/build/src/database/drivers/mysql/query-stream.d.ts +14 -0
- package/build/src/database/drivers/mysql/query-stream.d.ts.map +1 -0
- package/build/src/database/drivers/mysql/query-stream.js +43 -0
- package/build/src/database/tenants/data-copier.d.ts +66 -0
- package/build/src/database/tenants/data-copier.d.ts.map +1 -1
- package/build/src/database/tenants/data-copier.js +145 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/database/drivers/base.js +17 -0
- package/src/database/drivers/mysql/index.js +15 -0
- package/src/database/drivers/mysql/query-stream.js +43 -0
- package/src/database/tenants/data-copier.js +169 -0
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const DEFAULT_INSERT_CHUNK_SIZE = 100
|
|
4
4
|
const DEFAULT_QUERY_CHUNK_SIZE = 500
|
|
5
|
+
const DEFAULT_STREAM_BATCH_SIZE = 1000
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Splits an array into chunks of at most `chunkSize` items.
|
|
@@ -204,6 +205,174 @@ export default class DataCopier {
|
|
|
204
205
|
return {idsByTableName, rowsByTableName}
|
|
205
206
|
}
|
|
206
207
|
|
|
208
|
+
/**
|
|
209
|
+
* The plan tables referenced as a parent by some child entry. Their ids must be retained
|
|
210
|
+
* while streaming so their children can be scoped; leaf tables — typically the high-volume
|
|
211
|
+
* ones — are never in this set and so are never accumulated in memory.
|
|
212
|
+
* @returns {Set<string>} - Table names that are a parent of another plan entry.
|
|
213
|
+
*/
|
|
214
|
+
parentTableNames() {
|
|
215
|
+
/** @type {Set<string>} */
|
|
216
|
+
const names = new Set()
|
|
217
|
+
|
|
218
|
+
for (const tableConfig of this.tablePlan) {
|
|
219
|
+
if (tableConfig.parentTableName) {
|
|
220
|
+
names.add(tableConfig.parentTableName)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return names
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Streams every plan table's source rows scoped to `keyValue` in bounded `batchSize` batches,
|
|
229
|
+
* following parent/child chaining. Each table is read through a real
|
|
230
|
+
* {@link import("../drivers/base.js").default#queryStream} cursor, so a large table is never
|
|
231
|
+
* buffered; only the ids of tables that are themselves a parent are retained (to scope their
|
|
232
|
+
* children). `selectColumns` bounds each row's projection — pass `[idColumn]` for a light
|
|
233
|
+
* id-only scan, or omit it to stream full rows — and must include the id column for any table
|
|
234
|
+
* that has children. Memory stays bounded to one batch plus the retained parent-table ids.
|
|
235
|
+
* @param {import("../drivers/base.js").default} db - Source database to stream.
|
|
236
|
+
* @param {string} keyValue - Tenant key selecting the root plan rows.
|
|
237
|
+
* @param {{batchSize: number, selectColumns?: string[]}} options - Batch size and optional projection.
|
|
238
|
+
* @yields {{rows: Record<string, unknown>[], tableName: string}} - Successive row batches per table.
|
|
239
|
+
*/
|
|
240
|
+
async *streamPlanSourceBatches(db, keyValue, {batchSize, selectColumns}) {
|
|
241
|
+
/** @type {Map<string, string[]>} */
|
|
242
|
+
const retainedIdsByTableName = new Map()
|
|
243
|
+
const parentTableNames = this.parentTableNames()
|
|
244
|
+
|
|
245
|
+
for (const tableConfig of this.tablePlan) {
|
|
246
|
+
/** @type {string} */
|
|
247
|
+
let scopeColumn
|
|
248
|
+
/** @type {string[]} */
|
|
249
|
+
let scopeValues
|
|
250
|
+
|
|
251
|
+
if (tableConfig.keyColumn) {
|
|
252
|
+
scopeColumn = tableConfig.keyColumn
|
|
253
|
+
scopeValues = [keyValue]
|
|
254
|
+
} else {
|
|
255
|
+
if (!tableConfig.parentColumn || !tableConfig.parentTableName) {
|
|
256
|
+
throw new Error(`Expected keyColumn or parentTableName+parentColumn for table ${tableConfig.tableName} in the tenant table plan.`)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (!retainedIdsByTableName.has(tableConfig.parentTableName)) {
|
|
260
|
+
throw new Error(`Tenant table plan entry ${tableConfig.tableName} references parent table ${tableConfig.parentTableName}, which has not been streamed; parent tables must appear before their children in the plan.`)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
scopeColumn = tableConfig.parentColumn
|
|
264
|
+
scopeValues = retainedIdsByTableName.get(tableConfig.parentTableName) || []
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** @type {string[] | null} */
|
|
268
|
+
const retainedIds = parentTableNames.has(tableConfig.tableName) ? [] : null
|
|
269
|
+
const quotedTable = db.quoteTable(tableConfig.tableName)
|
|
270
|
+
const quotedScope = db.quoteColumn(scopeColumn)
|
|
271
|
+
const selectList = selectColumns
|
|
272
|
+
? selectColumns.map((column) => `${quotedTable}.${db.quoteColumn(column)}`).join(", ")
|
|
273
|
+
: `${quotedTable}.*`
|
|
274
|
+
/** @type {Record<string, unknown>[]} */
|
|
275
|
+
let batch = []
|
|
276
|
+
|
|
277
|
+
for (const scopeChunk of chunks(uniqueStrings(scopeValues), this.queryChunkSize)) {
|
|
278
|
+
const sql = `SELECT ${selectList} FROM ${quotedTable} WHERE ${quotedScope} IN (${this.quotedValuesSql(db, scopeChunk)})`
|
|
279
|
+
|
|
280
|
+
for await (const row of db.queryStream(sql)) {
|
|
281
|
+
batch.push(row)
|
|
282
|
+
|
|
283
|
+
if (retainedIds) {
|
|
284
|
+
retainedIds.push(String(row[this.idColumn]))
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (batch.length >= batchSize) {
|
|
288
|
+
yield {rows: batch, tableName: tableConfig.tableName}
|
|
289
|
+
batch = []
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (batch.length > 0) {
|
|
295
|
+
yield {rows: batch, tableName: tableConfig.tableName}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
retainedIdsByTableName.set(tableConfig.tableName, retainedIds || [])
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Returns which of `ids` currently exist in `tableName` in `db`. `ids` is one already-bounded
|
|
304
|
+
* batch, so it is probed with a single `IN (...)` lookup.
|
|
305
|
+
* @param {{db: import("../drivers/base.js").default, ids: string[], tableName: string}} args - Database, ids to probe, and table.
|
|
306
|
+
* @returns {Promise<Set<string>>} - The subset of `ids` present in the table.
|
|
307
|
+
*/
|
|
308
|
+
async queryExistingIds({db, ids, tableName}) {
|
|
309
|
+
if (ids.length <= 0) {
|
|
310
|
+
return new Set()
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const quotedTable = db.quoteTable(tableName)
|
|
314
|
+
const quotedId = db.quoteColumn(this.idColumn)
|
|
315
|
+
const rows = await this.executeQuietQuery(db, `SELECT ${quotedTable}.${quotedId} FROM ${quotedTable} WHERE ${quotedId} IN (${this.quotedValuesSql(db, ids)})`)
|
|
316
|
+
|
|
317
|
+
return new Set(rows.map((row) => String(row[this.idColumn])))
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Streams the source ids for `keyValue` and returns the first table found to be missing rows in
|
|
322
|
+
* the target, with that batch's missing ids — stopping at the first shortfall instead of
|
|
323
|
+
* enumerating every missing row. Callers verifying a tenant already holds every source row (for
|
|
324
|
+
* example before deleting the source copies) treat an empty result as the go-ahead and any entry
|
|
325
|
+
* as a hard stop; failing fast keeps memory bounded to a single batch even when the target is
|
|
326
|
+
* far behind.
|
|
327
|
+
* @param {string} keyValue - Tenant key selecting the source rows to check.
|
|
328
|
+
* @param {{batchSize?: number}} [options] - Streaming batch size.
|
|
329
|
+
* @returns {Promise<Map<string, string[]>>} - The first table missing rows and that batch's missing ids, or empty when the target holds everything.
|
|
330
|
+
*/
|
|
331
|
+
async findMissingRowIds(keyValue, {batchSize = DEFAULT_STREAM_BATCH_SIZE} = {}) {
|
|
332
|
+
/** @type {Map<string, string[]>} */
|
|
333
|
+
const missingByTableName = new Map()
|
|
334
|
+
|
|
335
|
+
for await (const {rows, tableName} of this.streamPlanSourceBatches(this.sourceDb, keyValue, {batchSize, selectColumns: [this.idColumn]})) {
|
|
336
|
+
const ids = rows.map((row) => String(row[this.idColumn]))
|
|
337
|
+
const existingIds = await this.queryExistingIds({db: this.targetDb, ids, tableName})
|
|
338
|
+
const missingIds = ids.filter((id) => !existingIds.has(id))
|
|
339
|
+
|
|
340
|
+
if (missingIds.length > 0) {
|
|
341
|
+
missingByTableName.set(tableName, missingIds)
|
|
342
|
+
break
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return missingByTableName
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Streams the source rows for `keyValue` and copies into the target, batch by batch, only the
|
|
351
|
+
* rows missing there. Because the full rows travel in the stream, each batch's missing rows are
|
|
352
|
+
* inserted as it arrives — nothing is accumulated, and no second source query runs while the
|
|
353
|
+
* source connection is held by the stream — so a table with large columns stays bounded to one
|
|
354
|
+
* batch even when the target is empty. Intended for single-table plans (or plans whose per-table,
|
|
355
|
+
* parent-first insert order is foreign-key safe).
|
|
356
|
+
* @param {string} keyValue - Tenant key selecting the source rows to reconcile.
|
|
357
|
+
* @param {{batchSize?: number}} [options] - Streaming batch size.
|
|
358
|
+
* @returns {Promise<number>} - The number of rows copied into the target.
|
|
359
|
+
*/
|
|
360
|
+
async copyMissingRows(keyValue, {batchSize = DEFAULT_STREAM_BATCH_SIZE} = {}) {
|
|
361
|
+
let copiedCount = 0
|
|
362
|
+
|
|
363
|
+
for await (const {rows, tableName} of this.streamPlanSourceBatches(this.sourceDb, keyValue, {batchSize})) {
|
|
364
|
+
const existingIds = await this.queryExistingIds({db: this.targetDb, ids: rows.map((row) => String(row[this.idColumn])), tableName})
|
|
365
|
+
const missingRows = rows.filter((row) => !existingIds.has(String(row[this.idColumn])))
|
|
366
|
+
|
|
367
|
+
if (missingRows.length > 0) {
|
|
368
|
+
await this.insertTargetRows(new Map([[tableName, missingRows]]))
|
|
369
|
+
copiedCount += missingRows.length
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return copiedCount
|
|
374
|
+
}
|
|
375
|
+
|
|
207
376
|
/**
|
|
208
377
|
* Selects `tableName` rows in `db` whose `columnName` is in `values`, chunked. `selectColumns`
|
|
209
378
|
* bounds the projection and defaults to every column.
|