refactor all main scripts

This commit is contained in:
Ulf Gebhardt 2023-03-10 00:21:27 +01:00
parent 66c09d0615
commit 7d54896801
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
3 changed files with 28 additions and 55 deletions

View File

@ -1,34 +0,0 @@
import CONFIG from './config'
import { createPool, PoolConfig } from 'mysql'
import { Migration } from 'ts-mysql-migrate'
import path from 'path'
const poolConfig: PoolConfig = {
host: CONFIG.DB_HOST,
port: CONFIG.DB_PORT,
user: CONFIG.DB_USER,
password: CONFIG.DB_PASSWORD,
database: CONFIG.DB_DATABASE,
}
// Pool?
const pool = createPool(poolConfig)
// Create & Initialize Migrations
const migration = new Migration({
conn: pool,
tableName: CONFIG.MIGRATIONS_TABLE,
silent: true,
dir: path.join(__dirname, '..', 'migrations'),
})
const initialize = async (): Promise<void> => {
await migration.initialize()
}
const resetDB = async (closePool = false): Promise<void> => {
await migration.reset() // use for resetting database
if (closePool) pool.end()
}
export { resetDB, pool, migration, initialize }

View File

@ -1,18 +1,28 @@
import 'reflect-metadata'
import prepare from './prepare'
import connection from './typeorm/connection'
import { resetDB, pool, migration } from './helpers'
import { createDatabase } from './prepare'
import CONFIG from './config'
import { createPool, Pool } from 'mysql'
import { Migration } from 'ts-mysql-migrate'
import path from 'path'
const run = async (command: string) => {
// Database actions not supported by our migration library
await prepare()
// Database connection for TypeORM
const con = await connection()
if (!con || !con.isConnected) {
throw new Error(`Couldn't open connection to database`)
}
await createDatabase()
const pool = createPool({
host: CONFIG.DB_HOST,
port: CONFIG.DB_PORT,
user: CONFIG.DB_USER,
password: CONFIG.DB_PASSWORD,
database: CONFIG.DB_DATABASE,
})
const migration = new Migration({
conn: pool,
tableName: CONFIG.MIGRATIONS_TABLE,
silent: true,
dir: path.join(__dirname, '..', 'migrations'),
})
await migration.initialize()
// Execute command
@ -25,14 +35,13 @@ const run = async (command: string) => {
break
case 'reset':
// TODO protect from production
await resetDB() // use for resetting database
await migration.reset()
break
default:
throw new Error(`Unsupported command ${command}`)
}
// Terminate connections gracefully
await con.close()
pool.end()
}

View File

@ -1,15 +1,8 @@
/* PREPARE SCRIPT
*
* This file ensures operations our migration library
* can not take care of are done.
* This applies to all Database Operations like
* creating, deleting, renaming Databases.
*/
import { createConnection } from 'mysql2/promise'
import { createConnection, RowDataPacket } from 'mysql2/promise'
import CONFIG from './config'
export default async (): Promise<void> => {
const createDatabase = async (): Promise<void> => {
const con = await createConnection({
host: CONFIG.DB_HOST,
port: CONFIG.DB_PORT,
@ -25,6 +18,8 @@ export default async (): Promise<void> => {
DEFAULT CHARACTER SET utf8mb4
DEFAULT COLLATE utf8mb4_unicode_ci;`)
/* LEGACY CODE
import { RowDataPacket } from 'mysql2/promise'
// Check if old migration table is present, delete if needed
const [rows] = await con.query(`SHOW TABLES FROM \`${CONFIG.DB_DATABASE}\` LIKE 'migrations';`)
if ((<RowDataPacket>rows).length > 0) {
@ -37,6 +32,9 @@ export default async (): Promise<void> => {
console.log('Found and dropped old migrations table')
}
}
*/
await con.end()
}
export { createDatabase }