mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
implement batching
This commit is contained in:
parent
0a541a91e8
commit
bffb802ba2
|
@ -38,32 +38,42 @@ export class ExportAllCommand extends BaseCommand {
|
||||||
await fs.promises.mkdir(backupPath, { recursive: true });
|
await fs.promises.mkdir(backupPath, { recursive: true });
|
||||||
|
|
||||||
for (const { name: tableName, columns } of tables) {
|
for (const { name: tableName, columns } of tables) {
|
||||||
// TODO: implement batching
|
const totalRowsCount = await connection
|
||||||
|
.query(`SELECT COUNT(*) AS count FROM ${tableName}`)
|
||||||
const rows = await connection.query(`SELECT * from ${tableName}`);
|
.then((rows: Array<{ count: number }>) => rows[0].count);
|
||||||
|
|
||||||
const stream = fs.createWriteStream(join(backupPath, `${tableName}.jsonl`));
|
const stream = fs.createWriteStream(join(backupPath, `${tableName}.jsonl`));
|
||||||
|
|
||||||
for (const row of rows) {
|
let cursor = 0;
|
||||||
// Our sqlite setup has some quirks. The following code normalizes the exported data so that it can be imported into a new postgres or sqlite database.
|
const batchSize = 10;
|
||||||
if (this.globalConfig.database.type === 'sqlite') {
|
while (cursor < totalRowsCount) {
|
||||||
for (const { type: columnType, propertyName } of columns) {
|
const rows = await connection.query(
|
||||||
if (propertyName in row) {
|
`SELECT * from ${tableName} LIMIT ${cursor}, ${batchSize}`,
|
||||||
// Our sqlite setup used `simple-json` for JSON columns, which is stored as strings.
|
);
|
||||||
// This is because when we wrote this code, sqlite did not support native JSON column types.
|
|
||||||
if (columnType === jsonColumnType) {
|
for (const row of rows) {
|
||||||
row[propertyName] = JSON.parse(row[propertyName]);
|
// Our sqlite setup has some quirks. The following code normalizes the exported data so that it can be imported into a new postgres or sqlite database.
|
||||||
}
|
if (this.globalConfig.database.type === 'sqlite') {
|
||||||
// Sqlite does not have a separate Boolean data type, and uses integers 0/1 to mark values as boolean
|
for (const { type: columnType, propertyName } of columns) {
|
||||||
else if (columnType === Boolean) {
|
if (propertyName in row) {
|
||||||
row[propertyName] = Boolean(row[propertyName]);
|
// Our sqlite setup used `simple-json` for JSON columns, which is stored as strings.
|
||||||
|
// This is because when we wrote this code, sqlite did not support native JSON column types.
|
||||||
|
if (columnType === jsonColumnType) {
|
||||||
|
row[propertyName] = JSON.parse(row[propertyName]);
|
||||||
|
}
|
||||||
|
// Sqlite does not have a separate Boolean data type, and uses integers 0/1 to mark values as boolean
|
||||||
|
else if (columnType === Boolean) {
|
||||||
|
row[propertyName] = Boolean(row[propertyName]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stream.write(JSON.stringify(row));
|
||||||
|
stream.write('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.write(JSON.stringify(row));
|
cursor += batchSize;
|
||||||
stream.write('\n');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.end();
|
stream.end();
|
||||||
|
|
Loading…
Reference in a new issue