From c097e78dd09a2b927485a2d68af89448fafd6ee0 Mon Sep 17 00:00:00 2001 From: "SPRINX0\\prochazka" Date: Thu, 29 Aug 2024 10:53:56 +0200 Subject: [PATCH] preloaded rows works with autoinc columns (fix for mssql) --- packages/tools/src/SqlDumper.ts | 13 +++++++++++-- packages/tools/src/alterPlan.ts | 13 +++++++++++-- packages/tools/src/database-info-alter-processor.ts | 9 ++++++++- packages/tools/src/diffTools.ts | 3 ++- packages/types/alter-processor.d.ts | 9 ++++++++- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/packages/tools/src/SqlDumper.ts b/packages/tools/src/SqlDumper.ts index 38161bda..5e54f0dd 100644 --- a/packages/tools/src/SqlDumper.ts +++ b/packages/tools/src/SqlDumper.ts @@ -210,7 +210,6 @@ export class SqlDumper implements AlterProcessor { } else { this.putRaw(SqlDumper.convertKeywordCase(type)); } - } columnDefinition(column: ColumnInfo, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) { @@ -653,7 +652,14 @@ export class SqlDumper implements AlterProcessor { this.putCmd('^drop %s %f', this.getSqlObjectSqlName(obj.objectTypeField), obj); } - fillPreloadedRows(table: NamedObjectInfo, oldRows: any[], newRows: any[], key: string[], insertOnly: string[]) { + fillPreloadedRows( + table: NamedObjectInfo, + oldRows: any[], + newRows: any[], + key: string[], + insertOnly: string[], + autoIncrementColumn: string + ) { let was = false; for (const row of newRows) { const old = oldRows?.find(r => key.every(col => r[col] == row[col])); @@ -676,12 +682,15 @@ export class SqlDumper implements AlterProcessor { } else { if (was) this.put(';\n'); was = true; + const autoinc = rowKeys.includes(autoIncrementColumn); + if (autoinc) this.allowIdentityInsert(table, true); this.put( '^insert ^into %f (%,i) ^values (%,v)', table, rowKeys, rowKeys.map(x => row[x]) ); + if (autoinc) this.allowIdentityInsert(table, false); } } if (was) { diff --git a/packages/tools/src/alterPlan.ts b/packages/tools/src/alterPlan.ts index dae56bf8..c47ecdc9 100644 --- a/packages/tools/src/alterPlan.ts +++ b/packages/tools/src/alterPlan.ts @@ -94,6 +94,7 @@ interface AlterOperation_FillPreloadedRows { newRows: any[]; key: string[]; insertOnly: string[]; + autoIncrementColumn: string; } type AlterOperation = @@ -233,7 +234,14 @@ export class AlterPlan { this.recreates.tables += 1; } - fillPreloadedRows(table: NamedObjectInfo, oldRows: any[], newRows: any[], key: string[], insertOnly: string[]) { + fillPreloadedRows( + table: NamedObjectInfo, + oldRows: any[], + newRows: any[], + key: string[], + insertOnly: string[], + autoIncrementColumn: string + ) { this.operations.push({ operationType: 'fillPreloadedRows', table, @@ -241,6 +249,7 @@ export class AlterPlan { newRows, key, insertOnly, + autoIncrementColumn, }); } @@ -567,7 +576,7 @@ export function runAlterOperation(op: AlterOperation, processor: AlterProcessor) processor.dropSqlObject(op.oldObject); break; case 'fillPreloadedRows': - processor.fillPreloadedRows(op.table, op.oldRows, op.newRows, op.key, op.insertOnly); + processor.fillPreloadedRows(op.table, op.oldRows, op.newRows, op.key, op.insertOnly, op.autoIncrementColumn); break; case 'recreateTable': { diff --git a/packages/tools/src/database-info-alter-processor.ts b/packages/tools/src/database-info-alter-processor.ts index 046b04c0..2b8c2b23 100644 --- a/packages/tools/src/database-info-alter-processor.ts +++ b/packages/tools/src/database-info-alter-processor.ts @@ -116,7 +116,14 @@ export class DatabaseInfoAlterProcessor { throw new Error('recreateTable not implemented for DatabaseInfoAlterProcessor'); } - fillPreloadedRows(table: NamedObjectInfo, oldRows: any[], newRows: any[], key: string[], insertOnly: string[]) { + fillPreloadedRows( + table: NamedObjectInfo, + oldRows: any[], + newRows: any[], + key: string[], + insertOnly: string[], + autoIncrementColumn: string + ) { const tableInfo = this.db.tables.find(x => x.pureName == table.pureName && x.schemaName == table.schemaName); tableInfo.preloadedRows = newRows; tableInfo.preloadedRowsKey = key; diff --git a/packages/tools/src/diffTools.ts b/packages/tools/src/diffTools.ts index 545fbb4a..6f939cb8 100644 --- a/packages/tools/src/diffTools.ts +++ b/packages/tools/src/diffTools.ts @@ -368,7 +368,8 @@ function planTablePreload(plan: AlterPlan, oldTable: TableInfo, newTable: TableI oldTable?.preloadedRows, newTable.preloadedRows, key, - newTable.preloadedRowsInsertOnly + newTable.preloadedRowsInsertOnly, + newTable.columns.find(x => x.autoIncrement)?.columnName ); } } diff --git a/packages/types/alter-processor.d.ts b/packages/types/alter-processor.d.ts index 3e5cd3cc..e2d1d6d7 100644 --- a/packages/types/alter-processor.d.ts +++ b/packages/types/alter-processor.d.ts @@ -15,5 +15,12 @@ export interface AlterProcessor { recreateTable(oldTable: TableInfo, newTable: TableInfo); createSqlObject(obj: SqlObjectInfo); dropSqlObject(obj: SqlObjectInfo); - fillPreloadedRows(table: NamedObjectInfo, oldRows: any[], newRows: any[], key: string[], insertOnly: string[]); + fillPreloadedRows( + table: NamedObjectInfo, + oldRows: any[], + newRows: any[], + key: string[], + insertOnly: string[], + autoIncrementColumn: string + ); }