(
schemaName: string,
tableName: string
)
| 289 | } |
| 290 | |
| 291 | async tableSchema( |
| 292 | schemaName: string, |
| 293 | tableName: string |
| 294 | ): Promise<DatabaseTableSchema> { |
| 295 | const columnsResult = ( |
| 296 | await this.query( |
| 297 | `SELECT * FROM information_schema.columns WHERE table_schema = ${this.escapeValue(schemaName)} AND table_name = ${this.escapeValue(tableName)}` |
| 298 | ) |
| 299 | ).rows as unknown as PostgresColumnRow[]; |
| 300 | |
| 301 | const constraintResult = ( |
| 302 | await this.query(`SELECT |
| 303 | tc.constraint_name, |
| 304 | tc.table_schema, |
| 305 | tc.table_name, |
| 306 | tc.constraint_type, |
| 307 | kcu.column_name, |
| 308 | ccu.table_schema AS reference_table_schema, |
| 309 | ccu.table_name AS reference_table_name, |
| 310 | ccu.column_name AS reference_column_name |
| 311 | FROM |
| 312 | information_schema.table_constraints AS tc |
| 313 | LEFT JOIN information_schema.key_column_usage AS kcu |
| 314 | ON ( |
| 315 | tc.table_schema = kcu.table_schema AND |
| 316 | tc.table_name = kcu.table_name AND |
| 317 | tc.constraint_name = kcu.constraint_name |
| 318 | ) |
| 319 | LEFT JOIN information_schema.constraint_column_usage AS ccu |
| 320 | ON ( |
| 321 | ccu.table_schema = kcu.table_schema AND |
| 322 | ccu.constraint_name = kcu.constraint_name |
| 323 | ) |
| 324 | WHERE |
| 325 | tc.table_schema = ${this.escapeValue(schemaName)} AND tc.table_name = ${this.escapeValue(tableName)}`) |
| 326 | ).rows as unknown as PostgresConstraintRow[]; |
| 327 | |
| 328 | const constraintRecord: Record<string, DatabaseTableColumnConstraint> = {}; |
| 329 | for (const constraint of constraintResult.filter( |
| 330 | (f) => f.column_name !== null |
| 331 | )) { |
| 332 | const key = constraint.constraint_name; |
| 333 | const constraintItem = constraintRecord[key] || { |
| 334 | name: constraint.constraint_name, |
| 335 | primaryKey: false, |
| 336 | notNull: false, |
| 337 | unique: false, |
| 338 | checkExpression: "", |
| 339 | defaultValue: null, |
| 340 | }; |
| 341 | |
| 342 | if (constraint.constraint_type === "PRIMARY KEY") { |
| 343 | constraintItem.primaryKey = true; |
| 344 | constraintItem.primaryColumns = [ |
| 345 | ...(constraintItem?.primaryColumns ?? []), |
| 346 | constraint.column_name, |
| 347 | ]; |
| 348 | } else if (constraint.constraint_type === "FOREIGN KEY") { |
nothing calls this directly
no test coverage detected