mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
console/mssql: fix fetching information about primary key and unique key columns
GitOrigin-RevId: b2796d12223c60374c553f580acc08a02df71a88
This commit is contained in:
parent
21215dad1b
commit
c46b256fff
@ -59,8 +59,10 @@ type MSSqlFk = {
|
||||
type MSSqlConstraint = {
|
||||
table_name: string;
|
||||
table_schema: string;
|
||||
constraint_name: string;
|
||||
columns: { name: string }[];
|
||||
constraints: {
|
||||
constraint_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);
|
||||
}
|
||||
|
@ -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;
|
||||
AND pk.is_primary_key = 1
|
||||
${whereClause}
|
||||
GROUP BY
|
||||
tab.name,
|
||||
tab.schema_id,
|
||||
tab.object_id
|
||||
FOR JSON PATH;
|
||||
`;
|
||||
},
|
||||
checkConstraintsSql: () => {
|
||||
@ -429,13 +434,13 @@ FROM sys.objects as obj
|
||||
.join(',')})`;
|
||||
}
|
||||
return `
|
||||
SELECT
|
||||
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: [],
|
||||
|
Loading…
Reference in New Issue
Block a user