fixed mssql primary key respects column order

This commit is contained in:
SPRINX0\prochazka
2024-11-08 12:13:00 +01:00
parent c750bd04ad
commit 319580554f
7 changed files with 62 additions and 14 deletions

View File

@@ -8,7 +8,7 @@ const autoIndexForeignKeysTransform = () => database => {
...(table.indexes || []),
...table.foreignKeys.map(fk => ({
constraintName: `IX_${fk.constraintName}`,
columns: fk.columns,
columns: fk.columns.map(x => ({ columnName: x.columnName })),
})),
],
};

View File

@@ -624,7 +624,9 @@ export function testEqualTables(
) {
const plan = new AlterPlan(wholeOldDb, wholeNewDb, driver.dialect, opts);
planAlterTable(plan, a, b, opts);
// console.log('plan.operations', a, b, plan.operations);
// if (plan.operations.length > 0) {
// console.log('************** plan.operations', a, b, plan.operations);
// }
return plan.operations.length == 0;
}
@@ -856,7 +858,6 @@ export function matchPairedObjects(db1: DatabaseInfo, db2: DatabaseInfo, opts: D
export const modelCompareDbDiffOptions: DbDiffOptions = {
ignoreCase: true,
schemaMode: 'ignore',
ignoreConstraintNames: true,
ignoreForeignKeyActions: true,
ignoreDataTypes: true,

View File

@@ -145,6 +145,10 @@ export function isCollectionInfo(obj: { objectTypeField?: string }): obj is Coll
}
export function filterStructureBySchema(db: DatabaseInfo, schema: string) {
if (!db) {
return db;
}
return {
...db,
tables: (db.tables || []).filter(x => x.schemaName == schema),
@@ -158,6 +162,10 @@ export function filterStructureBySchema(db: DatabaseInfo, schema: string) {
}
export function getSchemasUsedByStructure(db: DatabaseInfo) {
if (!db) {
return db;
}
return _uniq([
...(db.tables || []).map(x => x.schemaName),
...(db.views || []).map(x => x.schemaName),
@@ -168,3 +176,30 @@ export function getSchemasUsedByStructure(db: DatabaseInfo) {
...(db.triggers || []).map(x => x.schemaName),
]);
}
export function replaceSchemaInStructure(db: DatabaseInfo, schema: string) {
if (!db) {
return db;
}
return {
...db,
tables: (db.tables || []).map(tbl => ({
...tbl,
schemaName: schema,
columns: (tbl.columns || []).map(column => ({ ...column, schemaName: schema })),
primaryKey: tbl.primaryKey ? { ...tbl.primaryKey, schemaName: schema } : undefined,
sortingKey: tbl.sortingKey ? { ...tbl.sortingKey, schemaName: schema } : undefined,
foreignKeys: (tbl.foreignKeys || []).map(fk => ({ ...fk, refSchemaName: schema, schemaName: schema })),
indexes: (tbl.indexes || []).map(idx => ({ ...idx, schemaName: schema })),
uniques: (tbl.uniques || []).map(idx => ({ ...idx, schemaName: schema })),
checks: (tbl.checks || []).map(idx => ({ ...idx, schemaName: schema })),
})),
views: (db.views || []).map(x => ({ ...x, schemaName: schema })),
collections: (db.collections || []).map(x => ({ ...x, schemaName: schema })),
matviews: (db.matviews || []).map(x => ({ ...x, schemaName: schema })),
procedures: (db.procedures || []).map(x => ({ ...x, schemaName: schema })),
functions: (db.functions || []).map(x => ({ ...x, schemaName: schema })),
triggers: (db.triggers || []).map(x => ({ ...x, schemaName: schema })),
};
}

View File

@@ -35,6 +35,7 @@ export interface SqlDialect {
createCheck?: boolean;
dropCheck?: boolean;
renameSqlObject?: boolean;
multipleSchema?: boolean;
specificNullabilityImplementation?: boolean;
omitForeignKeys?: boolean;

View File

@@ -1,15 +1,24 @@
module.exports = `
select o.object_id, pureName = t.Table_Name, schemaName = t.Table_Schema, columnName = c.Column_Name, constraintName=t.constraint_name from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS t,
sys.objects o,
sys.schemas s,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c
where
c.Constraint_Name = t.Constraint_Name
and t.table_name = o.name
and o.schema_id = s.schema_id and t.Table_Schema = s.name
and c.Table_Name = t.Table_Name
and Constraint_Type = 'PRIMARY KEY'
SELECT
i.object_id AS objectId,
o.name AS pureName,
s.name AS schemaName,
c.name AS columnName,
i.name AS constraintName
FROM
sys.indexes i
INNER JOIN
sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN
sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
INNER JOIN
sys.objects o ON i.object_id = o.object_id
INNER JOIN
sys.schemas s ON o.schema_id = s.schema_id
WHERE
i.is_primary_key = 1
and o.object_id =OBJECT_ID_CONDITION
and s.name =SCHEMA_NAME_CONDITION
ORDER BY
ic.key_ordinal
`;

View File

@@ -12,6 +12,7 @@ const dialect = {
offsetFetchRangeSyntax: true,
rowNumberOverPaging: true,
defaultSchemaName: 'dbo',
multipleSchema: true,
stringEscapeChar: "'",
fallbackDataType: 'nvarchar(max)',
explicitDropConstraint: false,

View File

@@ -9,6 +9,7 @@ const dialect = {
rangeSelect: true,
ilike: true,
defaultSchemaName: 'public',
multipleSchema: true,
// stringEscapeChar: '\\',
stringEscapeChar: "'",
fallbackDataType: 'varchar',