-
Notifications
You must be signed in to change notification settings - Fork 0
Add Jest test database manager #26
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
Open
PatrickDinh
wants to merge
2
commits into
main
Choose a base branch
from
feature/jest-test-db
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,235 +1 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| // eslint-disable-next-line node/shebang | ||
| import { requestText, runChildProc, writeError, writeText, writeWarning, yeahNah } from './helpers' | ||
| import { Client } from 'pg' | ||
|
|
||
| type commands = | ||
| | 'migration-generate' | ||
| | 'migration-create' | ||
| | 'migration-check' | ||
| | 'migration-revert' | ||
| | 'snapshot-create' | ||
| | 'snapshot-restore' | ||
| | 'snapshot-clean' | ||
| // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents | ||
| | unknown | ||
|
|
||
| const databaseConfig = { | ||
| host: process.env.TYPEORM_TOOLKIT_DATABASE_HOST!, | ||
| port: Number(process.env.TYPEORM_TOOLKIT_DATABASE_PORT!), | ||
| user: process.env.TYPEORM_TOOLKIT_DATABASE_USERNAME!, | ||
| password: process.env.TYPEORM_TOOLKIT_DATABASE_PASSWORD!, | ||
| database: process.env.TYPEORM_TOOLKIT_DATABASE_NAME!, | ||
| } | ||
|
|
||
| run(process.argv[2]) | ||
| .then((code) => process.exit(code)) | ||
| .catch((e) => { | ||
| writeError(e instanceof Error ? e.message : `${e}`) | ||
| process.exit(-1) | ||
| }) | ||
|
|
||
| async function run(command: commands): Promise<number> { | ||
| switch (command) { | ||
| case 'snapshot-create': { | ||
| let snapshotName = process.argv[3] | ||
| if (!snapshotName) { | ||
| snapshotName = requestText('Enter a name for the snapshot: ') | ||
| } | ||
| await createSnapshot(snapshotName) | ||
| return 0 | ||
| } | ||
| case 'snapshot-restore': { | ||
| await restoreSnapshot(process.argv[3]) | ||
| return 0 | ||
| } | ||
| case 'snapshot-clean': | ||
| await cleanSnapshots() | ||
| return 0 | ||
| case 'migration-generate': | ||
| generateMigration(process.argv[3]) | ||
| return 0 | ||
| case 'migration-create': | ||
| createMigration(process.argv[3]) | ||
| return 0 | ||
| case 'migration-check': | ||
| return checkMigration() ?? 0 | ||
| case 'migration-revert': | ||
| revertMigration() | ||
| return 0 | ||
| default: | ||
| throw new Error('Missing command: Expected "create" or "restore"') | ||
| } | ||
| } | ||
|
|
||
| async function createPgClient(): Promise<Client> { | ||
| const client = new Client({ ...databaseConfig, database: 'postgres' }) | ||
| writeText('Connecting to postgres') | ||
| await client.connect() | ||
| return client | ||
| } | ||
|
|
||
| async function cleanSnapshots() { | ||
| const client = await createPgClient() | ||
| const databases = await getDatabases(client, databaseConfig.database) | ||
| if (databases.length === 0) { | ||
| writeText('There are no snapshot databases to remove.') | ||
| return | ||
| } | ||
| writeWarning(`This will drop the following snapshot databases: \n${databases.map((db) => ` - ${db}`).join('\n')}`) | ||
|
|
||
| const confirmationText = `Yes I'm sure` | ||
| const confirmation = requestText(`Enter "${confirmationText}" to confirm this action: `) | ||
| if (confirmation !== confirmationText) throw new Error('Aborted by user') | ||
| for (const db of databases) await dropDatabase(client, db) | ||
| } | ||
|
|
||
| async function createSnapshot(snapshotName: string | undefined) { | ||
| if (snapshotName === undefined || !/^[a-z_]+$/i.test(snapshotName)) { | ||
| throw new Error(`Invalid snapshot name ${snapshotName ?? '<undefined>'}. Snapshot name must only contain letters and underscores`) | ||
| } | ||
| const snapshotDbName = `${databaseConfig.database}_${snapshotName}` | ||
|
|
||
| const client = await createPgClient() | ||
|
|
||
| const existingDatabases = await getDatabases(client, databaseConfig.database) | ||
|
|
||
| if (existingDatabases.some((db) => db === snapshotDbName)) { | ||
| writeWarning(`Snapshot db with the name ${snapshotDbName} already exists.`) | ||
| if (yeahNah('Would you like to override this snapshot database?')) { | ||
| await dropDatabase(client, snapshotDbName) | ||
| } else throw new Error('Aborted by user') | ||
| } | ||
|
|
||
| await closeOtherConnections(client, databaseConfig.database) | ||
|
|
||
| writeText(`Creating snapshot database ${snapshotDbName}`) | ||
|
|
||
| await client.query( | ||
| ` | ||
| create database "${snapshotDbName}" with template = "${databaseConfig.database}" | ||
| ` | ||
| ) | ||
| } | ||
|
|
||
| async function restoreSnapshot(snapshotName: string | undefined) { | ||
| const client = await createPgClient() | ||
|
|
||
| const snapshotDatabaseName = await getSnapshotDatabaseName(client, snapshotName) | ||
|
|
||
| writeWarning('This will drop the main database and override it with the specified snapshot.') | ||
|
|
||
| if (!yeahNah('Are you sure you want to continue?')) throw new Error('Aborted by user') | ||
|
|
||
| await closeOtherConnections(client, databaseConfig.database) | ||
| await closeOtherConnections(client, snapshotDatabaseName) | ||
|
|
||
| await dropDatabase(client, databaseConfig.database) | ||
|
|
||
| writeText(`Restoring snapshot from ${snapshotDatabaseName}`) | ||
| await client.query(`CREATE DATABASE ${databaseConfig.database} WITH TEMPLATE = ${snapshotDatabaseName}`) | ||
|
|
||
| if (yeahNah('Would you like to remove the snapshot?')) { | ||
| await dropDatabase(client, snapshotDatabaseName) | ||
| } | ||
| } | ||
|
|
||
| async function getSnapshotDatabaseName(client: Client, snapshotName: string | undefined): Promise<string> { | ||
| const databases = await getDatabases(client, databaseConfig.database) | ||
|
|
||
| if (snapshotName) { | ||
| const matchedDbName = databases.find((db) => db === `${databaseConfig.database}_${snapshotName}`) | ||
| if (matchedDbName) { | ||
| return matchedDbName | ||
| } | ||
| writeWarning(`Couldn't find snapshot with the name "${snapshotName}"`) | ||
| } | ||
| writeText( | ||
| `Available snapshots: \n${databases.map((db, i) => ` ${i + 1}: ${db.substring(databaseConfig.database.length + 1)}`).join('\n')}` | ||
| ) | ||
|
|
||
| const snapshotNumber = Number(requestText('Enter the number of the snapshot to restore: ')) | ||
|
|
||
| if (isNaN(snapshotNumber) || !databases[snapshotNumber - 1]) throw new Error('Invalid snapshot selection') | ||
| return databases[snapshotNumber - 1]! | ||
| } | ||
|
|
||
| async function dropDatabase(client: Client, databaseName: string) { | ||
| writeText(`Dropping snapshot database ${databaseName}`) | ||
| await client.query(`DROP DATABASE ${databaseName}`) | ||
| } | ||
|
|
||
| async function getDatabases(client: Client, baseName: string) { | ||
| const data = await client.query<{ datname: string }>( | ||
| ` | ||
| SELECT datname | ||
| FROM pg_database | ||
| WHERE datname like $1 and datname <> $2 | ||
| ORDER BY datname | ||
| `, | ||
| [`${baseName}_%`, baseName] | ||
| ) | ||
|
|
||
| return data.rows.map((r) => r.datname) | ||
| } | ||
|
|
||
| async function closeOtherConnections(client: Client, dbName: string) { | ||
| writeText(`Closing active connections to ${dbName}`) | ||
| const query = ` | ||
| SELECT pg_terminate_backend(pg_stat_activity.pid) | ||
| FROM pg_stat_activity | ||
| WHERE pg_stat_activity.datname = $1::text | ||
| AND pid <> pg_backend_pid();` | ||
|
|
||
| await client.query(query, [dbName]) | ||
| } | ||
|
|
||
| function generateMigration(name: string) { | ||
| if (!name) { | ||
| writeText( | ||
| `No migration name provided. You can provide one by invoking 'npm run typeorm-pg-toolkit migration-generate -- MIGRATION_NAME'` | ||
| ) | ||
| name = requestText('Enter a name for the migrations: ') | ||
| } | ||
|
|
||
| writeText(`Generating migration with name: ${name}`) | ||
|
|
||
| runChildProc('typeorm-ts-node-commonjs', [ | ||
| `migration:generate`, | ||
| '--dataSource', | ||
| process.env.TYPEORM_TOOLKIT_MIGRATION_DATASOURCE_CONFIG!, | ||
| '--pretty', | ||
| `${process.env.TYPEORM_TOOLKIT_MIGRATION_ROOT_DIR}/${name}`, | ||
| ]) | ||
| } | ||
|
|
||
| function createMigration(name: string) { | ||
| if (!name) { | ||
| writeText(`No migration name provided. You can provide one by invoking 'npm run typeorm-pg-toolkit migration-create -- MIGRATION_NAME'`) | ||
| name = requestText('Enter a name for the migrations: ') | ||
| } | ||
|
|
||
| writeText(`Creating migration with name: ${name}`) | ||
|
|
||
| runChildProc('typeorm-ts-node-commonjs', [`migration:create`, `${process.env.TYPEORM_TOOLKIT_MIGRATION_ROOT_DIR}/${name}`]) | ||
| } | ||
|
|
||
| function checkMigration() { | ||
| writeText(`Checking if migration is needed`) | ||
|
|
||
| return runChildProc('typeorm-ts-node-commonjs', [ | ||
| `migration:generate`, | ||
| '--dryrun', | ||
| '--dataSource', | ||
| process.env.TYPEORM_TOOLKIT_MIGRATION_DATASOURCE_CONFIG!, | ||
| '--check', | ||
| 'some/path', | ||
| ]) | ||
| } | ||
|
|
||
| function revertMigration() { | ||
| writeText(`Reverting the latest migration`) | ||
|
|
||
| runChildProc('typeorm-ts-node-commonjs', [`migration:revert`, '--dataSource', process.env.TYPEORM_TOOLKIT_MIGRATION_DATASOURCE_CONFIG!]) | ||
| } | ||
| export * from './test-database-manager' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be more reflective of real life and easier to understand at a glance to assign both the DataSource + dispose rather than just assigning the dispose function?