Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed PostgreSQL schema handling in getAllTables and getAllViews methods #1065

Open
wants to merge 2 commits into
base: 21.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion commands/db_truncate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ export default class DbTruncate extends BaseCommand {
*/
private async performTruncate(client: QueryClientContract, schemas: string[]) {
let tables = await client.getAllTables(schemas)
tables = tables.filter((table) => !['adonis_schema', 'adonis_schema_versions'].includes(table))
const extractTableName = (table: string) =>
client.dialect.name === 'postgres' ? table.split('.')[1].replace(/"/g, '') : table
tables = tables.filter(
(table) => !['adonis_schema', 'adonis_schema_versions'].includes(extractTableName(table))
)

await Promise.all(tables.map((table) => client.truncate(table, true)))
this.logger.success('Truncated tables successfully')
Expand Down
21 changes: 12 additions & 9 deletions src/dialects/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export class PgDialect implements DialectContract {
const tables = await this.client
.query()
.from('pg_catalog.pg_tables')
.select('tablename as table_name')
.select('schemaname AS schema', 'tablename AS table')
.whereIn('schemaname', schemas)
.orderBy('tablename', 'asc')

return tables.map(({ table_name }) => table_name)
return tables.map(({ schema, table }) => `"${schema}"."${table}"`)
}

/**
Expand All @@ -57,11 +57,11 @@ export class PgDialect implements DialectContract {
const views = await this.client
.query()
.from('pg_catalog.pg_views')
.select('viewname as view_name')
.select('schemaname AS schema', 'viewname AS view')
.whereIn('schemaname', schemas)
.orderBy('viewname', 'asc')

return views.map(({ view_name }) => view_name)
return views.map(({ schema, view }) => `"${schema}"."${view}"`)
}

/**
Expand Down Expand Up @@ -97,9 +97,10 @@ export class PgDialect implements DialectContract {
* Truncate pg table with option to cascade and restart identity
*/
async truncate(table: string, cascade: boolean = false) {
const quotedTable = table.startsWith('"') ? table : `"${table}"`
return cascade
? this.client.rawQuery(`TRUNCATE "${table}" RESTART IDENTITY CASCADE;`)
: this.client.rawQuery(`TRUNCATE "${table}";`)
? this.client.rawQuery(`TRUNCATE ${quotedTable} RESTART IDENTITY CASCADE;`)
: this.client.rawQuery(`TRUNCATE ${quotedTable};`)
}

/**
Expand All @@ -111,15 +112,17 @@ export class PgDialect implements DialectContract {
/**
* Filter out tables that are not allowed to be dropped
*/
const extractTableName = (table: string) => table.split('.')[1].replace(/"/g, '')
tables = tables.filter(
(table) => !(this.config.wipe?.ignoreTables || ['spatial_ref_sys']).includes(table)
(table) =>
!(this.config.wipe?.ignoreTables || ['spatial_ref_sys']).includes(extractTableName(table))
)

if (!tables.length) {
return
}

await this.client.rawQuery(`DROP TABLE "${tables.join('", "')}" CASCADE;`)
await this.client.rawQuery(`DROP TABLE ${tables.join(', ')} CASCADE;`)
}

/**
Expand All @@ -129,7 +132,7 @@ export class PgDialect implements DialectContract {
const views = await this.getAllViews(schemas)
if (!views.length) return

await this.client.rawQuery(`DROP VIEW "${views.join('", "')}" CASCADE;`)
await this.client.rawQuery(`DROP VIEW ${views.join(', ')} CASCADE;`)
}

/**
Expand Down
6 changes: 5 additions & 1 deletion test/database/query_client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,11 @@ test.group('Query client | get tables', (group) => {
connection.connect()

const client = new QueryClient('dual', connection, createEmitter())
const tables = await client.getAllTables(['public'])
let tables = await client.getAllTables(['public'])
if (client.dialect.name === 'postgres') {
const extractTableName = (table: string) => table.split('.')[1].replace(/"/g, '')
tables = tables.map((table) => extractTableName(table))
}
if (process.env.DB !== 'mysql_legacy') {
assert.deepEqual(tables, [
'comments',
Expand Down
10 changes: 9 additions & 1 deletion test/database/views_types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ test.group('Query client | Views, types and domains', (group) => {
view.as(connection.client!('follows').select('user_id'))
})

const allViews = await client.getAllViews(['public'])
let allViews = await client.getAllViews(['public'])
if (client.dialect.name === 'postgres') {
const extractViewName = (view: string) => view.split('.')[1].replace(/"/g, '')
allViews = allViews.map((view) => extractViewName(view))
}
assert.deepEqual(allViews.sort(), ['users_view', 'follows_view'].sort())

await client.dropAllViews()
Expand All @@ -62,6 +66,10 @@ test.group('Query client | Views, types and domains', (group) => {
})

let allViews = await client.getAllViews(['public'])
if (client.dialect.name === 'postgres') {
const extractViewName = (view: string) => view.split('.')[1].replace(/"/g, '')
allViews = allViews.map((view) => extractViewName(view))
}
assert.deepEqual(allViews.sort(), ['users_view', 'follows_view'].sort())

await client.dropAllViews()
Expand Down