mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31: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 = {
|
type MSSqlConstraint = {
|
||||||
table_name: string;
|
table_name: string;
|
||||||
table_schema: string;
|
table_schema: string;
|
||||||
|
constraints: {
|
||||||
constraint_name: string;
|
constraint_name: string;
|
||||||
columns: { name: string }[];
|
name: string;
|
||||||
|
}[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const mergeDataMssql = (
|
export const mergeDataMssql = (
|
||||||
@ -91,23 +93,58 @@ export const mergeDataMssql = (
|
|||||||
? (JSON.parse(data[1].result?.slice(1).join('')) as MSSqlFk[])
|
? (JSON.parse(data[1].result?.slice(1).join('')) as MSSqlFk[])
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
primaryKeys = data[2].result
|
// one row per table
|
||||||
? (JSON.parse(
|
const parsedPKs: MSSqlConstraint[] = data[2].result
|
||||||
data[2].result?.slice(1).join('')
|
? JSON.parse(data[2].result?.slice(1).join(''))
|
||||||
) as MSSqlConstraint[]).map(pk => ({
|
|
||||||
...pk,
|
|
||||||
columns: pk.columns.map(({ name }) => name),
|
|
||||||
}))
|
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
uniqueKeys = data[3].result
|
primaryKeys = parsedPKs.reduce((acc: Table['primary_key'][], pk) => {
|
||||||
? (JSON.parse(
|
const { table_name, table_schema, constraints } = pk;
|
||||||
data[3].result?.slice(1).join('')
|
|
||||||
) as MSSqlConstraint[]).map(pk => ({
|
const columnsByConstraintName: { [name: string]: string[] } = {};
|
||||||
...pk,
|
constraints.forEach(c => {
|
||||||
columns: pk.columns.map(({ name }) => name),
|
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) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
@ -397,11 +397,11 @@ FROM sys.objects as obj
|
|||||||
return `
|
return `
|
||||||
SELECT
|
SELECT
|
||||||
schema_name (tab.schema_id) AS table_schema,
|
schema_name (tab.schema_id) AS table_schema,
|
||||||
pk.name AS constraint_name,
|
|
||||||
tab.name AS table_name,
|
tab.name AS table_name,
|
||||||
(
|
(
|
||||||
SELECT
|
SELECT
|
||||||
col.name
|
col.name,
|
||||||
|
pk.name AS constraint_name
|
||||||
FROM
|
FROM
|
||||||
sys.indexes pk
|
sys.indexes pk
|
||||||
INNER JOIN sys.index_columns ic ON ic.object_id = pk.object_id
|
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
|
INNER JOIN sys.columns col ON pk.object_id = col.object_id
|
||||||
AND col.column_id = ic.column_id
|
AND col.column_id = ic.column_id
|
||||||
WHERE
|
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
|
FROM
|
||||||
sys.tables tab
|
sys.tables tab
|
||||||
INNER JOIN sys.indexes pk ON tab.object_id = pk.object_id
|
INNER JOIN sys.indexes pk ON tab.object_id = pk.object_id
|
||||||
AND pk.is_primary_key = 1
|
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: () => {
|
checkConstraintsSql: () => {
|
||||||
@ -431,11 +436,11 @@ FROM sys.objects as obj
|
|||||||
return `
|
return `
|
||||||
SELECT
|
SELECT
|
||||||
schema_name (tab.schema_id) AS table_schema,
|
schema_name (tab.schema_id) AS table_schema,
|
||||||
idx.name AS constraint_name,
|
|
||||||
tab.name AS table_name,
|
tab.name AS table_name,
|
||||||
(
|
(
|
||||||
SELECT
|
SELECT
|
||||||
col.name
|
col.name,
|
||||||
|
idx.name AS constraint_name
|
||||||
FROM
|
FROM
|
||||||
sys.indexes idx
|
sys.indexes idx
|
||||||
INNER JOIN sys.index_columns ic ON ic.object_id = idx.object_id
|
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
|
INNER JOIN sys.columns col ON idx.object_id = col.object_id
|
||||||
AND col.column_id = ic.column_id
|
AND col.column_id = ic.column_id
|
||||||
WHERE
|
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
|
FROM
|
||||||
sys.tables tab
|
sys.tables tab
|
||||||
INNER JOIN sys.indexes idx ON tab.object_id = idx.object_id AND idx.is_unique_constraint = 1
|
INNER JOIN sys.indexes idx ON tab.object_id = idx.object_id
|
||||||
${whereClause} FOR JSON PATH;
|
AND idx.is_unique_constraint = 1
|
||||||
|
${whereClause}
|
||||||
|
GROUP BY
|
||||||
|
tab.name,
|
||||||
|
tab.schema_id,
|
||||||
|
tab.object_id
|
||||||
|
FOR JSON PATH;
|
||||||
`;
|
`;
|
||||||
},
|
},
|
||||||
frequentlyUsedColumns: [],
|
frequentlyUsedColumns: [],
|
||||||
|
Loading…
Reference in New Issue
Block a user