Skip to content

Commit

Permalink
Update .tables()
Browse files Browse the repository at this point in the history
  • Loading branch information
rijkvanzanten committed Jan 22, 2022
1 parent 79c114c commit ac1d5b7
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/dialects/postgres.ts
Expand Up @@ -88,11 +88,24 @@ export default class Postgres implements SchemaInspector {
* List all existing tables in the current schema/database
*/
async tables() {
const records = await this.knex
.select<{ tablename: string }[]>('tablename')
.from('pg_catalog.pg_tables')
.whereIn('schemaname', this.explodedSchema);
return records.map(({ tablename }) => tablename);
const schemaIn = this.explodedSchema.map(
(schemaName) => `${this.knex.raw('?', [schemaName])}::regnamespace`
);

const result = await this.knex.raw(
`
SELECT
rel.relname AS name
FROM
pg_class rel
WHERE
rel.relnamespace IN (${schemaIn})
AND rel.relkind = 'r'
ORDER BY rel.relname
`
);

return result.rows.map((row: { name: string }) => row.name);
}

/**
Expand Down

0 comments on commit ac1d5b7

Please sign in to comment.