({
databaseMetadata,
databaseType,
}: {
databaseMetadata: DatabaseMetadata;
databaseType: DatabaseType;
})
| 40 | }; |
| 41 | |
| 42 | export const createTablesFromMetadata = ({ |
| 43 | databaseMetadata, |
| 44 | databaseType, |
| 45 | }: { |
| 46 | databaseMetadata: DatabaseMetadata; |
| 47 | databaseType: DatabaseType; |
| 48 | }): DBTable[] => { |
| 49 | const { |
| 50 | tables: tableInfos, |
| 51 | pk_info: primaryKeys, |
| 52 | columns, |
| 53 | indexes, |
| 54 | views: views, |
| 55 | check_constraints: checkConstraints, |
| 56 | } = databaseMetadata; |
| 57 | |
| 58 | // Pre-compute view names for faster lookup if there are views |
| 59 | const viewNamesSet = new Set<string>(); |
| 60 | const materializedViewNamesSet = new Set<string>(); |
| 61 | |
| 62 | if (views && views.length > 0) { |
| 63 | views.forEach((view) => { |
| 64 | const key = generateTableKey({ |
| 65 | schemaName: view.schema, |
| 66 | tableName: view.view_name, |
| 67 | }); |
| 68 | viewNamesSet.add(key); |
| 69 | |
| 70 | if ( |
| 71 | view.view_definition && |
| 72 | decodeViewDefinition(databaseType, view.view_definition) |
| 73 | .toLowerCase() |
| 74 | .includes('materialized') |
| 75 | ) { |
| 76 | materializedViewNamesSet.add(key); |
| 77 | } |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | // Pre-compute lookup maps for better performance |
| 82 | const columnsByTable = new Map<string, (typeof columns)[0][]>(); |
| 83 | const indexesByTable = new Map<string, (typeof indexes)[0][]>(); |
| 84 | const primaryKeysByTable = new Map<string, (typeof primaryKeys)[0][]>(); |
| 85 | |
| 86 | // Group columns by table |
| 87 | columns.forEach((col) => { |
| 88 | const key = generateTableKey({ |
| 89 | schemaName: col.schema, |
| 90 | tableName: col.table, |
| 91 | }); |
| 92 | if (!columnsByTable.has(key)) { |
| 93 | columnsByTable.set(key, []); |
| 94 | } |
| 95 | columnsByTable.get(key)!.push(col); |
| 96 | }); |
| 97 | |
| 98 | // Group indexes by table |
| 99 | indexes.forEach((idx) => { |
no test coverage detected