db alter plan improvements

This commit is contained in:
SPRINX0\prochazka
2024-11-11 11:07:57 +01:00
parent a069093f6b
commit 75b4f49e31
14 changed files with 64 additions and 12 deletions

View File

@@ -515,6 +515,9 @@ export class SqlDumper implements AlterProcessor {
this.put('%i %k', col.columnName, col.isDescending == true ? 'DESC' : 'ASC');
});
this.put('&<&n)');
if (ix.filterDefinition && this.dialect.filteredIndexes) {
this.put('&n^where %s', ix.filterDefinition);
}
this.endCommand();
}

View File

@@ -81,6 +81,7 @@ interface AlterOperation_ChangeConstraint {
interface AlterOperation_DropConstraint {
operationType: 'dropConstraint';
oldObject: ConstraintInfo;
isRecreate?: boolean;
}
interface AlterOperation_RenameConstraint {
@@ -337,15 +338,16 @@ export class AlterPlan {
if (op.operationType == testedOperationType) {
const constraints = this._getDependendColumnConstraints(testedObject as ColumnInfo, testedDependencies);
if (constraints.length > 0 && this.opts.noDropConstraint) {
return [];
}
// if (constraints.length > 0 && this.opts.noDropConstraint) {
// return [];
// }
const res: AlterOperation[] = [
...constraints.map(oldObject => {
const opRes: AlterOperation = {
operationType: 'dropConstraint',
oldObject,
isRecreate: true,
};
return opRes;
}),
@@ -382,15 +384,16 @@ export class AlterPlan {
}
if (op.operationType == 'changeConstraint') {
if (this.opts.noDropConstraint) {
// skip constraint recreate
return [];
}
// if (this.opts.noDropConstraint) {
// // skip constraint recreate
// return [];
// }
this.recreates.constraints += 1;
const opDrop: AlterOperation = {
operationType: 'dropConstraint',
oldObject: op.oldObject,
isRecreate: true,
};
const opCreate: AlterOperation = {
operationType: 'createConstraint',
@@ -547,7 +550,7 @@ export class AlterPlan {
if (this.opts.noDropColumn && op.operationType == 'dropColumn') return false;
if (this.opts.noDropTable && op.operationType == 'dropTable') return false;
if (this.opts.noDropTable && op.operationType == 'recreateTable') return false;
if (this.opts.noDropConstraint && op.operationType == 'dropConstraint') return false;
if (this.opts.noDropConstraint && op.operationType == 'dropConstraint' && !op.isRecreate) return false;
// if (
// this.opts.noDropSqlObject &&
// op.operationType == 'dropSqlObject' &&

View File

@@ -127,6 +127,13 @@ export function removeTablePairingId(table: TableInfo): TableInfo {
};
}
function simplifySqlExpression(sql: string) {
return (sql || '')
.replace(/[\s\(\)\[\]\"]/g, '')
.toLowerCase()
.trim();
}
function generateObjectPairingId(obj) {
if (obj.objectTypeField)
return {
@@ -375,6 +382,7 @@ function testEqualForeignKeys(a: ForeignKeyInfo, b: ForeignKeyInfo, opts: DbDiff
function testEqualIndex(a: IndexInfo, b: IndexInfo, opts: DbDiffOptions) {
if (!testEqualColumnRefs(a.columns, b.columns, opts)) return false;
if (!!a.isUnique != !!b.isUnique) return false;
if (simplifySqlExpression(a.filterDefinition) != simplifySqlExpression(b.filterDefinition)) return false;
if (!opts.ignoreConstraintNames && !testEqualNames(a.constraintName, b.constraintName, opts)) return false;
return true;
@@ -456,7 +464,7 @@ export function testEqualTypes(a: ColumnInfo, b: ColumnInfo, opts: DbDiffOptions
return true;
}
if ((a.dataType || '').toLowerCase() != (b.dataType || '').toLowerCase()) {
if (simplifySqlExpression(a.dataType) != simplifySqlExpression(b.dataType)) {
console.debug(
`Column ${a.pureName}.${a.columnName}, ${b.pureName}.${b.columnName}: different data type: ${a.dataType}, ${b.dataType}`
);

View File

@@ -159,6 +159,7 @@ export function tableInfoFromYaml(table: TableInfoYaml, allTables: TableInfoYaml
pureName: table.name,
isUnique: index.unique,
constraintType: 'index',
filterDefinition: index.filter,
columns: [
...index.columns.map(columnName => ({ columnName })),
...(index.included || []).map(columnName => ({ columnName, isIncludedColumn: true })),