mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #2808 from gradido/cleanup-database
refactor(database): cleanup database
This commit is contained in:
commit
e5319b7772
@ -6,7 +6,6 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
|
||||||
import { initialize } from '@dbTools/helpers'
|
|
||||||
import { entities } from '@entity/index'
|
import { entities } from '@entity/index'
|
||||||
import { createTestClient } from 'apollo-server-testing'
|
import { createTestClient } from 'apollo-server-testing'
|
||||||
|
|
||||||
@ -40,7 +39,6 @@ export const testEnvironment = async (testLogger: any = logger, testI18n: any =
|
|||||||
const testClient = createTestClient(server.apollo)
|
const testClient = createTestClient(server.apollo)
|
||||||
const mutate = testClient.mutate
|
const mutate = testClient.mutate
|
||||||
const query = testClient.query
|
const query = testClient.query
|
||||||
await initialize()
|
|
||||||
return { mutate, query, con }
|
return { mutate, query, con }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,5 +4,3 @@ DB_USER=root
|
|||||||
DB_PASSWORD=
|
DB_PASSWORD=
|
||||||
DB_DATABASE=gradido_community
|
DB_DATABASE=gradido_community
|
||||||
MIGRATIONS_TABLE=migrations
|
MIGRATIONS_TABLE=migrations
|
||||||
|
|
||||||
TYPEORM_SEEDING_FACTORIES=src/factories/**/*{.ts,.js}
|
|
||||||
|
|||||||
@ -6,5 +6,3 @@ DB_USER=$DB_USER
|
|||||||
DB_PASSWORD=$DB_PASSWORD
|
DB_PASSWORD=$DB_PASSWORD
|
||||||
DB_DATABASE=gradido_community
|
DB_DATABASE=gradido_community
|
||||||
MIGRATIONS_TABLE=migrations
|
MIGRATIONS_TABLE=migrations
|
||||||
|
|
||||||
TYPEORM_SEEDING_FACTORIES=src/factories/**/*{.ts,.js}
|
|
||||||
|
|||||||
@ -1,15 +0,0 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
||||||
|
|
||||||
const CONFIG = require('./src/config')
|
|
||||||
|
|
||||||
module.export = {
|
|
||||||
name: 'default',
|
|
||||||
type: 'mysql',
|
|
||||||
host: CONFIG.DB_HOST,
|
|
||||||
port: CONFIG.DB_PORT,
|
|
||||||
username: CONFIG.DB_USER,
|
|
||||||
password: CONFIG.DB_PASSWORD,
|
|
||||||
database: CONFIG.DB_DATABASE,
|
|
||||||
seeds: ['src/seeds/**/*{.ts,.js}'],
|
|
||||||
factories: ['src/factories/**/*{.ts,.js}'],
|
|
||||||
}
|
|
||||||
@ -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 }
|
|
||||||
@ -1,18 +1,29 @@
|
|||||||
import 'reflect-metadata'
|
import 'reflect-metadata'
|
||||||
import prepare from './prepare'
|
import { createDatabase } from './prepare'
|
||||||
import connection from './typeorm/connection'
|
import CONFIG from './config'
|
||||||
import { resetDB, pool, migration } from './helpers'
|
|
||||||
|
import { createPool } from 'mysql'
|
||||||
|
import { Migration } from 'ts-mysql-migrate'
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
const run = async (command: string) => {
|
const run = async (command: string) => {
|
||||||
// Database actions not supported by our migration library
|
// Database actions not supported by our migration library
|
||||||
await prepare()
|
await createDatabase()
|
||||||
|
|
||||||
// Database connection for TypeORM
|
|
||||||
const con = await connection()
|
|
||||||
if (!con || !con.isConnected) {
|
|
||||||
throw new Error(`Couldn't open connection to database`)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Initialize Migrations
|
||||||
|
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()
|
await migration.initialize()
|
||||||
|
|
||||||
// Execute command
|
// Execute command
|
||||||
@ -25,14 +36,13 @@ const run = async (command: string) => {
|
|||||||
break
|
break
|
||||||
case 'reset':
|
case 'reset':
|
||||||
// TODO protect from production
|
// TODO protect from production
|
||||||
await resetDB() // use for resetting database
|
await migration.reset()
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unsupported command ${command}`)
|
throw new Error(`Unsupported command ${command}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Terminate connections gracefully
|
// Terminate connections gracefully
|
||||||
await con.close()
|
|
||||||
pool.end()
|
pool.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +0,0 @@
|
|||||||
import Decimal from 'decimal.js-light'
|
|
||||||
|
|
||||||
export interface TransactionContext {
|
|
||||||
typeId: number
|
|
||||||
userId: number
|
|
||||||
balance: Decimal
|
|
||||||
balanceDate: Date
|
|
||||||
amount: Decimal
|
|
||||||
memo: string
|
|
||||||
creationDate?: Date
|
|
||||||
sendReceiverUserId?: number
|
|
||||||
}
|
|
||||||
@ -1,26 +0,0 @@
|
|||||||
export interface UserContext {
|
|
||||||
pubKey?: Buffer
|
|
||||||
email?: string
|
|
||||||
firstName?: string
|
|
||||||
lastName?: string
|
|
||||||
deletedAt?: Date
|
|
||||||
password?: BigInt
|
|
||||||
privKey?: Buffer
|
|
||||||
emailHash?: Buffer
|
|
||||||
createdAt?: Date
|
|
||||||
emailChecked?: boolean
|
|
||||||
language?: string
|
|
||||||
publisherId?: number
|
|
||||||
passphrase?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ServerUserContext {
|
|
||||||
username?: string
|
|
||||||
password?: string
|
|
||||||
email?: string
|
|
||||||
role?: string
|
|
||||||
activated?: number
|
|
||||||
lastLogin?: Date
|
|
||||||
created?: Date
|
|
||||||
modified?: Date
|
|
||||||
}
|
|
||||||
@ -1,32 +0,0 @@
|
|||||||
import Decimal from 'decimal.js-light'
|
|
||||||
|
|
||||||
export interface UserInterface {
|
|
||||||
// from user
|
|
||||||
email?: string
|
|
||||||
firstName?: string
|
|
||||||
lastName?: string
|
|
||||||
password?: BigInt
|
|
||||||
pubKey?: Buffer
|
|
||||||
privKey?: Buffer
|
|
||||||
emailHash?: Buffer
|
|
||||||
createdAt?: Date
|
|
||||||
emailChecked?: boolean
|
|
||||||
language?: string
|
|
||||||
deletedAt?: Date
|
|
||||||
publisherId?: number
|
|
||||||
passphrase?: string
|
|
||||||
// from server user
|
|
||||||
serverUserPassword?: string
|
|
||||||
role?: string
|
|
||||||
activated?: number
|
|
||||||
lastLogin?: Date
|
|
||||||
modified?: Date
|
|
||||||
// flag for admin
|
|
||||||
isAdmin?: boolean
|
|
||||||
// flag for balance (creation of 1000 GDD)
|
|
||||||
addBalance?: boolean
|
|
||||||
// balance
|
|
||||||
recordDate?: Date
|
|
||||||
creationDate?: Date
|
|
||||||
amount?: Decimal
|
|
||||||
}
|
|
||||||
@ -1,15 +1,8 @@
|
|||||||
/* PREPARE SCRIPT
|
import { createConnection } from 'mysql2/promise'
|
||||||
*
|
|
||||||
* 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, RowDataPacket } from 'mysql2/promise'
|
|
||||||
import CONFIG from './config'
|
import CONFIG from './config'
|
||||||
|
|
||||||
export default async (): Promise<void> => {
|
export const createDatabase = async (): Promise<void> => {
|
||||||
const con = await createConnection({
|
const con = await createConnection({
|
||||||
host: CONFIG.DB_HOST,
|
host: CONFIG.DB_HOST,
|
||||||
port: CONFIG.DB_PORT,
|
port: CONFIG.DB_PORT,
|
||||||
@ -25,6 +18,8 @@ export default async (): Promise<void> => {
|
|||||||
DEFAULT CHARACTER SET utf8mb4
|
DEFAULT CHARACTER SET utf8mb4
|
||||||
DEFAULT COLLATE utf8mb4_unicode_ci;`)
|
DEFAULT COLLATE utf8mb4_unicode_ci;`)
|
||||||
|
|
||||||
|
/* LEGACY CODE
|
||||||
|
import { RowDataPacket } from 'mysql2/promise'
|
||||||
// Check if old migration table is present, delete if needed
|
// Check if old migration table is present, delete if needed
|
||||||
const [rows] = await con.query(`SHOW TABLES FROM \`${CONFIG.DB_DATABASE}\` LIKE 'migrations';`)
|
const [rows] = await con.query(`SHOW TABLES FROM \`${CONFIG.DB_DATABASE}\` LIKE 'migrations';`)
|
||||||
if ((<RowDataPacket>rows).length > 0) {
|
if ((<RowDataPacket>rows).length > 0) {
|
||||||
@ -37,6 +32,7 @@ export default async (): Promise<void> => {
|
|||||||
console.log('Found and dropped old migrations table')
|
console.log('Found and dropped old migrations table')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
await con.end()
|
await con.end()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,27 +0,0 @@
|
|||||||
import { createConnection, Connection } from 'typeorm'
|
|
||||||
import CONFIG from '../config'
|
|
||||||
import { entities } from '../../entity/index'
|
|
||||||
|
|
||||||
const connection = async (): Promise<Connection | null> => {
|
|
||||||
let con = null
|
|
||||||
try {
|
|
||||||
con = await createConnection({
|
|
||||||
name: 'default',
|
|
||||||
type: 'mysql',
|
|
||||||
host: CONFIG.DB_HOST,
|
|
||||||
port: CONFIG.DB_PORT,
|
|
||||||
username: CONFIG.DB_USER,
|
|
||||||
password: CONFIG.DB_PASSWORD,
|
|
||||||
database: CONFIG.DB_DATABASE,
|
|
||||||
entities,
|
|
||||||
synchronize: false,
|
|
||||||
})
|
|
||||||
} catch (error) {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.log(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
return con
|
|
||||||
}
|
|
||||||
|
|
||||||
export default connection
|
|
||||||
@ -4,7 +4,6 @@
|
|||||||
import CONFIG from '@/config'
|
import CONFIG from '@/config'
|
||||||
import connection from '@/typeorm/connection'
|
import connection from '@/typeorm/connection'
|
||||||
import { checkDBVersion } from '@/typeorm/DBVersion'
|
import { checkDBVersion } from '@/typeorm/DBVersion'
|
||||||
import { initialize } from '@dbTools/helpers'
|
|
||||||
import { entities } from '@entity/index'
|
import { entities } from '@entity/index'
|
||||||
import { logger } from './testSetup'
|
import { logger } from './testSetup'
|
||||||
|
|
||||||
@ -42,7 +41,6 @@ export const testEnvironment = async () => {
|
|||||||
logger.fatal('Fatal: Database Version incorrect')
|
logger.fatal('Fatal: Database Version incorrect')
|
||||||
throw new Error('Fatal: Database Version incorrect')
|
throw new Error('Fatal: Database Version incorrect')
|
||||||
}
|
}
|
||||||
await initialize()
|
|
||||||
return { con }
|
return { con }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user