generated from AthennaIO/Template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db): add
db:wipe
and db:fresh
commands
- Loading branch information
Showing
14 changed files
with
216 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@athenna/database", | ||
"version": "4.42.0", | ||
"version": "4.43.0", | ||
"description": "The Athenna database handler for SQL/NoSQL.", | ||
"license": "MIT", | ||
"author": "João Lenon <[email protected]>", | ||
|
@@ -50,7 +50,9 @@ | |
"./package": "./package.json", | ||
"./package.json": "./package.json", | ||
"./testing/plugins": "./src/testing/plugins/index.js", | ||
"./commands/DbFreshCommand": "./src/commands/DbFreshCommand.js", | ||
"./commands/DbSeedCommand": "./src/commands/DbSeedCommand.js", | ||
"./commands/DbWipeCommand": "./src/commands/DbWipeCommand.js", | ||
"./commands/MakeMigrationCommand": "./src/commands/MakeMigrationCommand.js", | ||
"./commands/MakeModelCommand": "./src/commands/MakeModelCommand.js", | ||
"./commands/MakeSeederCommand": "./src/commands/MakeSeederCommand.js", | ||
|
@@ -203,9 +205,16 @@ | |
"migration:revert": { | ||
"path": "#src/commands/MigrationRevertCommand" | ||
}, | ||
"db:fresh": { | ||
"path": "#src/commands/DbFreshCommand", | ||
"loadAllCommands": true | ||
}, | ||
"db:seed": { | ||
"path": "#src/commands/DbSeedCommand" | ||
}, | ||
"db:wipe": { | ||
"path": "#src/commands/DbWipeCommand" | ||
}, | ||
"make:model": { | ||
"path": "#src/commands/MakeModelCommand" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @athenna/database | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Artisan, BaseCommand, Option } from '@athenna/artisan' | ||
|
||
export class DbWipeCommand extends BaseCommand { | ||
@Option({ | ||
default: 'default', | ||
signature: '-c, --connection <connection>', | ||
description: 'Set the the database connection.' | ||
}) | ||
public connection: string | ||
|
||
public static signature(): string { | ||
return 'db:fresh' | ||
} | ||
|
||
public static description(): string { | ||
return 'Drop all the tables of your database and run migrations again.' | ||
} | ||
|
||
public async handle(): Promise<void> { | ||
await Artisan.call(`db:wipe --connection ${this.connection}`) | ||
await Artisan.call(`migration:run --connection ${this.connection}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @athenna/database | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Exec } from '@athenna/common' | ||
import { Database } from '#src/facades/Database' | ||
import { BaseCommand, Option } from '@athenna/artisan' | ||
|
||
export class DbWipeCommand extends BaseCommand { | ||
@Option({ | ||
default: 'default', | ||
signature: '-c, --connection <connection>', | ||
description: 'Set the the database connection.' | ||
}) | ||
public connection: string | ||
|
||
public static signature(): string { | ||
return 'db:wipe' | ||
} | ||
|
||
public static description(): string { | ||
return 'Drop all the tables of your database.' | ||
} | ||
|
||
public async handle(): Promise<void> { | ||
this.logger.simple('({bold,green} [ WIPING DATABASE ])\n') | ||
|
||
const DB = Database.connection(this.connection) | ||
const task = this.logger.task() | ||
|
||
if (this.getConfig('driver') === 'mongo') { | ||
const tables = await DB.getTables() | ||
|
||
await Exec.concurrently(tables, table => DB.dropTable(table)) | ||
} else { | ||
const migrationsTable = this.getConfig( | ||
'migrations.tableName', | ||
'migrations' | ||
) | ||
|
||
await DB.revertMigrations() | ||
await DB.dropTable(migrationsTable) | ||
} | ||
|
||
const dbName = await DB.getCurrentDatabase() | ||
|
||
await task.run().finally(() => DB.close()) | ||
|
||
this.logger.success(`Database ({yellow} "${dbName}") successfully wiped.`) | ||
} | ||
|
||
private getConfig(name: string, defaultValue?: any) { | ||
return Config.get( | ||
`database.connections.${this.connection}.${name}`, | ||
defaultValue | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @athenna/database | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Path } from '@athenna/common' | ||
import { Test, type Context } from '@athenna/test' | ||
import { BaseCommandTest } from '#tests/helpers/BaseCommandTest' | ||
|
||
export default class DbFreshCommandTest extends BaseCommandTest { | ||
@Test() | ||
public async shouldBeAbleToRunDbFreshCommand({ command }: Context) { | ||
const output = await command.run('db:fresh --connection=fake', { | ||
path: Path.fixtures('consoles/db-console.ts') | ||
}) | ||
|
||
output.assertSucceeded() | ||
output.assertLogged('[ WIPING DATABASE ]') | ||
output.assertLogged('[ RUNNING MIGRATIONS ]') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @athenna/database | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Path } from '@athenna/common' | ||
import { Test, type Context } from '@athenna/test' | ||
import { BaseCommandTest } from '#tests/helpers/BaseCommandTest' | ||
|
||
export default class DbWipeCommandTest extends BaseCommandTest { | ||
@Test() | ||
public async shouldBeAbleToWipeTheEntireDatabase({ command }: Context) { | ||
const output = await command.run('db:wipe --connection=fake', { | ||
path: Path.fixtures('consoles/db-console.ts') | ||
}) | ||
|
||
output.assertSucceeded() | ||
output.assertLogged('[ WIPING DATABASE ]') | ||
output.assertLogged('Database "fake" successfully wiped.') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters