diff --git a/backend/src/database/connection.ts b/backend/src/database/connection.ts deleted file mode 100644 index 584b657d2..000000000 --- a/backend/src/database/connection.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { createConnection, Connection } from 'mysql2/promise' -import CONFIG from '../config' - -const connection = async (): Promise => { - 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 diff --git a/backend/src/index.ts b/backend/src/index.ts index bd58f8c86..36442a4ca 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -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 ( - (rows).length === 0 || - !(rows)[0].fileName || - (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() diff --git a/backend/src/typeorm/entity/Migration.ts b/backend/src/typeorm/entity/Migration.ts new file mode 100644 index 000000000..fbeeca14d --- /dev/null +++ b/backend/src/typeorm/entity/Migration.ts @@ -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 { + return this.createQueryBuilder('balance') + .where('balance.userId = :userId', { userId }) + .getOneOrFail() + } + */ +} diff --git a/backend/src/typeorm/getDBVersion.ts b/backend/src/typeorm/getDBVersion.ts new file mode 100644 index 000000000..497a6da5e --- /dev/null +++ b/backend/src/typeorm/getDBVersion.ts @@ -0,0 +1,15 @@ +import { getConnection } from 'typeorm' +import { Migration } from './entity/Migration' + +const getDBVersion = async (): Promise => { + 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