Skip to content

Commit

Permalink
fix: mention search paths from config during db:wipe and db:truncate …
Browse files Browse the repository at this point in the history
…commands

Fixes: #926
  • Loading branch information
thetutlage committed Dec 15, 2023
1 parent 8a6f894 commit 5e99253
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
14 changes: 10 additions & 4 deletions commands/db_truncate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export default class DbTruncate extends BaseCommand {
/**
* Truncate all tables except adonis migrations table
*/
private async performTruncate(client: QueryClientContract) {
let tables = await client.getAllTables(['public'])
private async performTruncate(client: QueryClientContract, schemas: string[]) {
let tables = await client.getAllTables(schemas)
tables = tables.filter((table) => !['adonis_schema', 'adonis_schema_versions'].includes(table))

await Promise.all(tables.map((table) => client.truncate(table, true)))
Expand Down Expand Up @@ -91,13 +91,19 @@ export default class DbTruncate extends BaseCommand {
/**
* Invalid database connection
*/
if (!db.manager.has(this.connection)) {
const managerConnection = db.manager.get(this.connection)
if (!managerConnection) {
this.printNotAValidConnection(this.connection)
this.exitCode = 1
return
}

await this.performTruncate(connection)
let schemas: string[] = ['public']
if ('searchPath' in managerConnection.config && managerConnection.config.searchPath) {
schemas = managerConnection.config.searchPath
}

await this.performTruncate(connection, schemas)
}

/**
Expand Down
26 changes: 16 additions & 10 deletions commands/db_wipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class DbWipe extends BaseCommand {
/**
* Drop all views (if asked for and supported)
*/
private async performDropViews(client: QueryClientContract) {
private async performDropViews(client: QueryClientContract, schemas: string[]) {
if (!this.dropViews) {
return
}
Expand All @@ -76,22 +76,22 @@ export default class DbWipe extends BaseCommand {
this.logger.warning(`Dropping views is not supported by "${client.dialect.name}"`)
}

await client.dropAllViews()
await client.dropAllViews(schemas)
this.logger.success('Dropped views successfully')
}

/**
* Drop all tables
*/
private async performDropTables(client: QueryClientContract) {
await client.dropAllTables()
private async performDropTables(client: QueryClientContract, schemas: string[]) {
await client.dropAllTables(schemas)
this.logger.success('Dropped tables successfully')
}

/**
* Drop all types (if asked for and supported)
*/
private async performDropTypes(client: QueryClientContract) {
private async performDropTypes(client: QueryClientContract, schemas: string[]) {
if (!this.dropTypes) {
return
}
Expand All @@ -100,7 +100,7 @@ export default class DbWipe extends BaseCommand {
this.logger.warning(`Dropping types is not supported by "${client.dialect.name}"`)
}

await client.dropAllTypes()
await client.dropAllTypes(schemas)
this.logger.success('Dropped types successfully')
}

Expand Down Expand Up @@ -132,15 +132,21 @@ export default class DbWipe extends BaseCommand {
/**
* Invalid database connection
*/
if (!db.manager.has(this.connection)) {
const managerConnection = db.manager.get(this.connection)
if (!managerConnection) {
this.printNotAValidConnection(this.connection)
this.exitCode = 1
return
}

await this.performDropViews(connection)
await this.performDropTables(connection)
await this.performDropTypes(connection)
let schemas: string[] = ['public']
if ('searchPath' in managerConnection.config && managerConnection.config.searchPath) {
schemas = managerConnection.config.searchPath
}

await this.performDropViews(connection, schemas)
await this.performDropTables(connection, schemas)
await this.performDropTypes(connection, schemas)
}

/**
Expand Down
1 change: 1 addition & 0 deletions test-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export function getConfig(): ConnectionConfig {
user: process.env.PG_USER as string,
password: process.env.PG_PASSWORD as string,
},
searchPath: ['custom', 'public'],
debug: !!process.env.DEBUG,
useNullAsDefault: true,
}
Expand Down

0 comments on commit 5e99253

Please sign in to comment.