mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
check db version with typeORM
This commit is contained in:
parent
922b823459
commit
d6dbc50c92
@ -1,18 +0,0 @@
|
||||
import { createConnection, Connection } from 'mysql2/promise'
|
||||
import CONFIG from '../config'
|
||||
|
||||
const connection = async (): Promise<Connection> => {
|
||||
const con = await createConnection({
|
||||
host: CONFIG.DB_HOST,
|
||||
port: CONFIG.DB_PORT,
|
||||
user: CONFIG.DB_USER,
|
||||
password: CONFIG.DB_PASSWORD,
|
||||
database: CONFIG.DB_DATABASE,
|
||||
})
|
||||
|
||||
await con.connect()
|
||||
|
||||
return con
|
||||
}
|
||||
|
||||
export default connection
|
||||
@ -5,12 +5,12 @@ import express from 'express'
|
||||
import cors from 'cors'
|
||||
import { buildSchema } from 'type-graphql'
|
||||
import { ApolloServer } from 'apollo-server-express'
|
||||
import { RowDataPacket } from 'mysql2/promise'
|
||||
|
||||
import connection from './database/connection'
|
||||
import typeOrmConnection from './typeorm/connection'
|
||||
import CONFIG from './config'
|
||||
|
||||
import connection from './typeorm/connection'
|
||||
import getDBVersion from './typeorm/getDBVersion'
|
||||
|
||||
// TODO move to extern
|
||||
import { UserResolver } from './graphql/resolvers/UserResolver'
|
||||
import { BalanceResolver } from './graphql/resolvers/BalanceResolver'
|
||||
@ -39,20 +39,20 @@ const context = (args: any) => {
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// check for correct database version
|
||||
// open mysql connection
|
||||
const con = await connection()
|
||||
const [rows] = await con.query(`SELECT * FROM migrations ORDER BY version DESC LIMIT 1;`)
|
||||
if (
|
||||
(<RowDataPacket>rows).length === 0 ||
|
||||
!(<RowDataPacket>rows)[0].fileName ||
|
||||
(<RowDataPacket>rows)[0].fileName.indexOf(DB_VERSION) === -1
|
||||
) {
|
||||
throw new Error(`Wrong database version - the backend requires '${DB_VERSION}'`)
|
||||
if (!con.isConnected) {
|
||||
throw new Error(`Couldn't open connection to database`)
|
||||
}
|
||||
|
||||
const toCon = await typeOrmConnection()
|
||||
if (!toCon.isConnected) {
|
||||
throw new Error(`Couldn't open typeorm db connection`)
|
||||
// check for correct database version
|
||||
const dbVersion = await getDBVersion()
|
||||
if (!dbVersion || dbVersion.indexOf(DB_VERSION) === -1) {
|
||||
throw new Error(
|
||||
`Wrong database version - the backend requires '${DB_VERSION}' but found '${
|
||||
dbVersion || 'None'
|
||||
}'`,
|
||||
)
|
||||
}
|
||||
|
||||
const schema = await buildSchema({
|
||||
@ -61,10 +61,7 @@ async function main() {
|
||||
})
|
||||
|
||||
// Graphiql interface
|
||||
let playground = false
|
||||
if (CONFIG.GRAPHIQL) {
|
||||
playground = true
|
||||
}
|
||||
const playground = CONFIG.GRAPHIQL
|
||||
|
||||
// Express Server
|
||||
const server = express()
|
||||
|
||||
21
backend/src/typeorm/entity/Migration.ts
Normal file
21
backend/src/typeorm/entity/Migration.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
|
||||
|
||||
@Entity('migrations')
|
||||
export class Migration extends BaseEntity {
|
||||
@PrimaryGeneratedColumn() // This is actually not a primary column
|
||||
version: number
|
||||
|
||||
@Column({ length: 256, nullable: true, default: null })
|
||||
fileName: string
|
||||
|
||||
@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
|
||||
date: Date
|
||||
|
||||
/*
|
||||
static findByUser(userId: number): Promise<Balance> {
|
||||
return this.createQueryBuilder('balance')
|
||||
.where('balance.userId = :userId', { userId })
|
||||
.getOneOrFail()
|
||||
}
|
||||
*/
|
||||
}
|
||||
15
backend/src/typeorm/getDBVersion.ts
Normal file
15
backend/src/typeorm/getDBVersion.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { getConnection } from 'typeorm'
|
||||
import { Migration } from './entity/Migration'
|
||||
|
||||
const getDBVersion = async (): Promise<string | null> => {
|
||||
const connection = getConnection()
|
||||
const migrations = connection.getRepository(Migration)
|
||||
try {
|
||||
const dbVersion = await migrations.findOne({ order: { version: 'DESC' } })
|
||||
return dbVersion ? dbVersion.fileName : null
|
||||
} catch (error) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export default getDBVersion
|
||||
Loading…
x
Reference in New Issue
Block a user