Add setup-fresh-db script to reset dev database and run migrations.
This commit is contained in:
@@ -13,6 +13,8 @@
|
|||||||
"typeorm:generate-migration": "npm run typeorm -- -d ./src/database/DataSource.ts migration:generate ./src/database/migrations/$npm_config_name",
|
"typeorm:generate-migration": "npm run typeorm -- -d ./src/database/DataSource.ts migration:generate ./src/database/migrations/$npm_config_name",
|
||||||
"typeorm:revert-migration": "npm run typeorm -- -d ./src/database/DataSource.ts migration:revert",
|
"typeorm:revert-migration": "npm run typeorm -- -d ./src/database/DataSource.ts migration:revert",
|
||||||
"typeorm:create-migration": "npm run typeorm -- migration:create ./src/database/migrations/$npm_config_name",
|
"typeorm:create-migration": "npm run typeorm -- migration:create ./src/database/migrations/$npm_config_name",
|
||||||
|
"truncate-db": "ts-node -r tsconfig-paths/register ./src/database/utils/drop",
|
||||||
|
"setup-fresh-db": "npm run truncate-db && npm run typeorm:run-migrations",
|
||||||
"test": "jest"
|
"test": "jest"
|
||||||
},
|
},
|
||||||
"jest": {
|
"jest": {
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import dataSource from '../DataSource';
|
||||||
|
import { getAppConfig } from '../../config';
|
||||||
|
import { getErrorMessage } from '../../utils/getErrorMessage';
|
||||||
|
import type { InformationSchemaTableRow } from '../../types/database/InformationSchemaTableRow';
|
||||||
|
import type { PgEnumTypeRow } from '../../types/database/PgEnumTypeRow';
|
||||||
|
import { NodeEnv } from '../../types/NodeEnv';
|
||||||
|
|
||||||
|
const {
|
||||||
|
nodeEnv
|
||||||
|
} = getAppConfig();
|
||||||
|
|
||||||
|
void (async () => {
|
||||||
|
if (nodeEnv === NodeEnv.Development) {
|
||||||
|
console.info('Connecting to the database...');
|
||||||
|
|
||||||
|
const initializedDataSource = await dataSource.initialize();
|
||||||
|
|
||||||
|
console.info('Connected to the database.');
|
||||||
|
|
||||||
|
const tables = await initializedDataSource.query<InformationSchemaTableRow[]>(
|
||||||
|
`SELECT table_name
|
||||||
|
FROM information_schema.tables
|
||||||
|
WHERE table_schema = 'public';`
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const table of tables) {
|
||||||
|
try {
|
||||||
|
await initializedDataSource.query(`DROP TABLE IF EXISTS "${table.table_name}" CASCADE;`);
|
||||||
|
|
||||||
|
console.info(`Dropped table: ${table.table_name}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to drop table: ${table.table_name}: ${getErrorMessage(error)}`);
|
||||||
|
|
||||||
|
return process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const enums = await initializedDataSource.query<PgEnumTypeRow[]>(
|
||||||
|
`SELECT typname FROM pg_type WHERE typtype = 'e';`
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const enumType of enums) {
|
||||||
|
try {
|
||||||
|
await initializedDataSource.query(`DROP TYPE IF EXISTS ${enumType.typname} CASCADE;`);
|
||||||
|
|
||||||
|
console.info(`Dropped enum: ${enumType.typname}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to drop enum: ${enumType.typname}: ${getErrorMessage(error)}`);
|
||||||
|
|
||||||
|
return process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.info('All tables and enums have been dropped.');
|
||||||
|
|
||||||
|
return process.exit(0);
|
||||||
|
}
|
||||||
|
})();
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export type InformationSchemaTableRow = {
|
||||||
|
table_name: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export type PgEnumTypeRow = {
|
||||||
|
typname: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export const getErrorMessage = (error: unknown): string => (error instanceof Error ? error.message : String(error));
|
||||||
Reference in New Issue
Block a user