2
0
mirror of https://github.com/frappe/books.git synced 2025-02-02 12:08:27 +00:00

fix: db async issue

This commit is contained in:
18alantom 2022-10-16 17:07:06 +05:30
parent 058d5dc24e
commit 2efc2908fc

View File

@ -354,37 +354,25 @@ export default class DatabaseCore extends DatabaseBase {
return (res?.count ?? 0) > 0; return (res?.count ?? 0) > 0;
} }
async #removeColumns(schemaName: string, targetColumns: string[]) { async #dropColumns(schemaName: string, targetColumns: string[]) {
const fields = this.schemaMap[schemaName]?.fields await this.knex!.schema.table(schemaName, (table) => {
.filter((f) => f.fieldtype !== FieldTypeEnum.Table && !f.computed) table.dropColumns(...targetColumns);
.map((f) => f.fieldname); });
const tableRows = await this.getAll(schemaName, { fields });
this.prestigeTheTable(schemaName, tableRows);
} }
async prestigeTheTable(schemaName: string, tableRows: FieldValueMap[]) { async prestigeTheTable(schemaName: string, tableRows: FieldValueMap[]) {
const max = 200;
// Alter table hacx for sqlite in case of schema change. // Alter table hacx for sqlite in case of schema change.
const tempName = `__${schemaName}`; const tempName = `__${schemaName}`;
await this.knex!.schema.dropTableIfExists(tempName);
// Create replacement table
await this.knex!.schema.dropTableIfExists(tempName);
await this.knex!.raw('PRAGMA foreign_keys=OFF'); await this.knex!.raw('PRAGMA foreign_keys=OFF');
await this.#createTable(schemaName, tempName); await this.#createTable(schemaName, tempName);
if (tableRows.length > 200) { // Insert rows from source table into the replacement table
const fi = Math.floor(tableRows.length / max); await this.knex!.batchInsert(tempName, tableRows, 200);
for (let i = 0; i <= fi; i++) {
const rowSlice = tableRows.slice(i * max, i + 1 * max);
if (rowSlice.length === 0) {
break;
}
await this.knex!.batchInsert(tempName, rowSlice);
}
} else {
await this.knex!.batchInsert(tempName, tableRows);
}
// Replace with the replacement table
await this.knex!.schema.dropTable(schemaName); await this.knex!.schema.dropTable(schemaName);
await this.knex!.schema.renameTable(tempName, schemaName); await this.knex!.schema.renameTable(tempName, schemaName);
await this.knex!.raw('PRAGMA foreign_keys=ON'); await this.knex!.raw('PRAGMA foreign_keys=ON');
@ -593,21 +581,23 @@ export default class DatabaseCore extends DatabaseBase {
const diff: ColumnDiff = await this.#getColumnDiff(schemaName); const diff: ColumnDiff = await this.#getColumnDiff(schemaName);
const newForeignKeys: Field[] = await this.#getNewForeignKeys(schemaName); const newForeignKeys: Field[] = await this.#getNewForeignKeys(schemaName);
return this.knex!.schema.table(schemaName, (table) => { await this.knex!.schema.table(schemaName, (table) => {
if (diff.added.length) { if (!diff.added.length) {
for (const field of diff.added) { return;
this.#buildColumnForTable(table, field);
}
} }
if (diff.removed.length) { for (const field of diff.added) {
this.#removeColumns(schemaName, diff.removed); this.#buildColumnForTable(table, field);
}
}).then(() => {
if (newForeignKeys.length) {
return this.#addForeignKeys(schemaName, newForeignKeys);
} }
}); });
if (diff.removed.length) {
await this.#dropColumns(schemaName, diff.removed);
}
if (newForeignKeys.length) {
await this.#addForeignKeys(schemaName, newForeignKeys);
}
} }
async #createTable(schemaName: string, tableName?: string) { async #createTable(schemaName: string, tableName?: string) {
@ -687,34 +677,8 @@ export default class DatabaseCore extends DatabaseBase {
} }
async #addForeignKeys(schemaName: string, newForeignKeys: Field[]) { async #addForeignKeys(schemaName: string, newForeignKeys: Field[]) {
await this.knex!.raw('PRAGMA foreign_keys=OFF'); const tableRows = await this.knex!.select().from(schemaName);
await this.knex!.raw('BEGIN TRANSACTION'); await this.prestigeTheTable(schemaName, tableRows);
const tempName = 'TEMP' + schemaName;
// create temp table
await this.#createTable(schemaName, tempName);
try {
// copy from old to new table
await this.knex!(tempName).insert(this.knex!.select().from(schemaName));
} catch (err) {
await this.knex!.raw('ROLLBACK');
await this.knex!.raw('PRAGMA foreign_keys=ON');
const rows = await this.knex!.select().from(schemaName);
await this.prestigeTheTable(schemaName, rows);
return;
}
// drop old table
await this.knex!.schema.dropTable(schemaName);
// rename new table
await this.knex!.schema.renameTable(tempName, schemaName);
await this.knex!.raw('COMMIT');
await this.knex!.raw('PRAGMA foreign_keys=ON');
} }
async #loadChildren( async #loadChildren(