You've already forked pigallery2
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user