1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-12-07 23:23:49 +02:00

improving db scheme. Adding Mysql tests

This commit is contained in:
Patrik J. Braun
2019-01-27 14:36:42 -05:00
parent 8ed202b53d
commit d92003eeee
22 changed files with 342 additions and 150 deletions

View File

@@ -17,16 +17,17 @@ import {DataStructureVersion} from '../../../common/DataStructureVersion';
import {FileEntity} from './enitites/FileEntity';
import {FaceRegionEntry} from './enitites/FaceRegionEntry';
import {PersonEntry} from './enitites/PersonEntry';
import {Utils} from '../../../common/Utils';
export class SQLConnection {
private static connection: Connection = null;
constructor() {
}
private static connection: Connection = null;
public static async getConnection(): Promise<Connection> {
if (this.connection == null) {
const options: any = this.getDriver(Config.Server.database);
@@ -44,8 +45,10 @@ export class SQLConnection {
VersionEntity
];
options.synchronize = false;
//options.logging = 'all';
this.connection = await createConnection(options);
// options.logging = 'all';
this.connection = await this.createConnection(options);
await SQLConnection.schemeSync(this.connection);
}
return this.connection;
@@ -72,7 +75,7 @@ export class SQLConnection {
];
options.synchronize = false;
// options.logging = "all";
const conn = await createConnection(options);
const conn = await this.createConnection(options);
await SQLConnection.schemeSync(conn);
await conn.close();
return true;
@@ -92,6 +95,38 @@ export class SQLConnection {
}
public static async close() {
try {
if (this.connection != null) {
await this.connection.close();
this.connection = null;
}
} catch (err) {
console.error(err);
}
}
private static async createConnection(options: ConnectionOptions) {
if (options.type === 'sqlite') {
return await createConnection(options);
}
try {
return await createConnection(options);
} catch (e) {
if (e.sqlMessage === 'Unknown database \'' + options.database + '\'') {
Logger.debug('creating database: ' + options.database);
const tmpOption = Utils.clone(options);
// @ts-ignore
delete tmpOption.database;
const tmpConn = await createConnection(tmpOption);
await tmpConn.query('CREATE DATABASE IF NOT EXISTS ' + options.database);
await tmpConn.close();
return await createConnection(options);
}
throw e;
}
}
private static async schemeSync(connection: Connection) {
let version = null;
try {
@@ -145,16 +180,5 @@ export class SQLConnection {
return driver;
}
public static async close() {
try {
if (this.connection != null) {
await this.connection.close();
this.connection = null;
}
} catch (err) {
console.error(err);
}
}
}