console/mssql: fix fetching information about primary key and unique key columns

GitOrigin-RevId: b2796d12223c60374c553f580acc08a02df71a88
This commit is contained in:
Aleksandra Sikora 2021-05-07 16:06:40 +02:00 committed by hasura-bot
parent 21215dad1b
commit c46b256fff
2 changed files with 76 additions and 27 deletions

View File

@ -59,8 +59,10 @@ type MSSqlFk = {
type MSSqlConstraint = {
table_name: string;
table_schema: string;
constraints: {
constraint_name: string;
columns: { name: string }[];
name: string;
}[];
};
export const mergeDataMssql = (
@ -91,23 +93,58 @@ export const mergeDataMssql = (
? (JSON.parse(data[1].result?.slice(1).join('')) as MSSqlFk[])
: [];
primaryKeys = data[2].result
? (JSON.parse(
data[2].result?.slice(1).join('')
) as MSSqlConstraint[]).map(pk => ({
...pk,
columns: pk.columns.map(({ name }) => name),
}))
// one row per table
const parsedPKs: MSSqlConstraint[] = data[2].result
? JSON.parse(data[2].result?.slice(1).join(''))
: [];
uniqueKeys = data[3].result
? (JSON.parse(
data[3].result?.slice(1).join('')
) as MSSqlConstraint[]).map(pk => ({
...pk,
columns: pk.columns.map(({ name }) => name),
}))
primaryKeys = parsedPKs.reduce((acc: Table['primary_key'][], pk) => {
const { table_name, table_schema, constraints } = pk;
const columnsByConstraintName: { [name: string]: string[] } = {};
constraints.forEach(c => {
columnsByConstraintName[c.constraint_name] = [
...(columnsByConstraintName[c.constraint_name] || []),
c.name,
];
});
const constraintInfo = Object.keys(columnsByConstraintName).map(
pkName => ({
table_schema,
table_name,
constraint_name: pkName,
columns: columnsByConstraintName[pkName],
})
);
return [...acc, ...constraintInfo];
}, []);
const parsedUKs: MSSqlConstraint[] = data[3].result
? JSON.parse(data[3].result?.slice(1).join(''))
: [];
uniqueKeys = parsedUKs.reduce((acc, uk) => {
const { table_name, table_schema, constraints } = uk;
const columnsByConstraintName: { [name: string]: string[] } = {};
constraints.forEach(c => {
columnsByConstraintName[c.constraint_name] = [
...(columnsByConstraintName[c.constraint_name] || []),
c.name,
];
});
const constraintInfo = Object.keys(columnsByConstraintName).map(
pkName => ({
table_schema,
table_name,
constraint_name: pkName,
columns: columnsByConstraintName[pkName],
})
);
return [...acc, ...constraintInfo];
}, [] as Exclude<Table['unique_constraints'], null>);
} catch (e) {
console.error(e);
}

View File

@ -397,11 +397,11 @@ FROM sys.objects as obj
return `
SELECT
schema_name (tab.schema_id) AS table_schema,
pk.name AS constraint_name,
tab.name AS table_name,
(
SELECT
col.name
col.name,
pk.name AS constraint_name
FROM
sys.indexes pk
INNER JOIN sys.index_columns ic ON ic.object_id = pk.object_id
@ -409,12 +409,17 @@ FROM sys.objects as obj
INNER JOIN sys.columns col ON pk.object_id = col.object_id
AND col.column_id = ic.column_id
WHERE
tab.object_id = pk.object_id FOR json path) AS columns
tab.object_id = pk.object_id AND pk.is_primary_key = 1 FOR json path) AS constraints
FROM
sys.tables tab
INNER JOIN sys.indexes pk ON tab.object_id = pk.object_id
AND pk.is_primary_key = 1
${whereClause} FOR JSON PATH;
${whereClause}
GROUP BY
tab.name,
tab.schema_id,
tab.object_id
FOR JSON PATH;
`;
},
checkConstraintsSql: () => {
@ -431,11 +436,11 @@ FROM sys.objects as obj
return `
SELECT
schema_name (tab.schema_id) AS table_schema,
idx.name AS constraint_name,
tab.name AS table_name,
(
SELECT
col.name
col.name,
idx.name AS constraint_name
FROM
sys.indexes idx
INNER JOIN sys.index_columns ic ON ic.object_id = idx.object_id
@ -443,11 +448,18 @@ FROM sys.objects as obj
INNER JOIN sys.columns col ON idx.object_id = col.object_id
AND col.column_id = ic.column_id
WHERE
tab.object_id = idx.object_id for json path) AS columns
tab.object_id = idx.object_id
AND idx.is_unique_constraint = 1 FOR json path) AS constraints
FROM
sys.tables tab
INNER JOIN sys.indexes idx ON tab.object_id = idx.object_id AND idx.is_unique_constraint = 1
${whereClause} FOR JSON PATH;
INNER JOIN sys.indexes idx ON tab.object_id = idx.object_id
AND idx.is_unique_constraint = 1
${whereClause}
GROUP BY
tab.name,
tab.schema_id,
tab.object_id
FOR JSON PATH;
`;
},
frequentlyUsedColumns: [],