mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2026-02-02 07:40:43 +03:00
Make schemas work with postgres
This commit is contained in:
@@ -4,27 +4,25 @@ import { Pool } from 'pg';
|
||||
|
||||
import fs from "fs";
|
||||
|
||||
export class Mysql implements IDatabase {
|
||||
export class Postgres implements IDatabase {
|
||||
private pool: Pool;
|
||||
|
||||
constructor(private config: any) {}
|
||||
|
||||
init(): void {
|
||||
this.pool = new Pool();
|
||||
async init(): Promise<void> {
|
||||
this.pool = new Pool(this.config.postgres);
|
||||
|
||||
if (!this.config.readOnly) {
|
||||
// Upgrade database if required
|
||||
this.upgradeDB(this.config.fileNamePrefix, this.config.dbSchemaFolder);
|
||||
|
||||
if (this.config.createDbIfNotExists && !this.config.readOnly && fs.existsSync(this.config.dbSchemaFileName)) {
|
||||
this.pool.query(this.processUpgradeQuery(fs.readFileSync(this.config.dbSchemaFileName).toString()));
|
||||
await this.pool.query(this.processUpgradeQuery(fs.readFileSync(this.config.dbSchemaFileName).toString()));
|
||||
}
|
||||
|
||||
// Upgrade database if required
|
||||
await this.upgradeDB(this.config.fileNamePrefix, this.config.dbSchemaFolder);
|
||||
}
|
||||
}
|
||||
|
||||
async prepare(type: QueryType, query: string, params?: any[]) {
|
||||
Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`);
|
||||
|
||||
// Convert query to use numbered parameters
|
||||
let count = 1;
|
||||
for (let char = 0; char < query.length; char++) {
|
||||
@@ -34,7 +32,9 @@ export class Mysql implements IDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
const queryResult = await this.pool.query(query, params);
|
||||
Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`);
|
||||
|
||||
const queryResult = await this.pool.query({text: query, values: params});
|
||||
|
||||
switch (type) {
|
||||
case 'get': {
|
||||
@@ -51,7 +51,7 @@ export class Mysql implements IDatabase {
|
||||
|
||||
private async upgradeDB(fileNamePrefix: string, schemaFolder: string) {
|
||||
const versionCodeInfo = await this.pool.query("SELECT value FROM config WHERE key = 'version'");
|
||||
let versionCode = versionCodeInfo ? versionCodeInfo.rows[0].value : 0;
|
||||
let versionCode = versionCodeInfo.rows[0] ? versionCodeInfo.rows[0].value : 0;
|
||||
|
||||
let path = schemaFolder + "/_upgrade_" + fileNamePrefix + "_" + (parseInt(versionCode) + 1) + ".sql";
|
||||
Logger.debug('db update: trying ' + path);
|
||||
@@ -67,7 +67,9 @@ export class Mysql implements IDatabase {
|
||||
}
|
||||
|
||||
private processUpgradeQuery(query: string): string {
|
||||
let result = query.replace(/sha256\((.*?)\)/gm, "digest($1, 'sha256')");
|
||||
let result = query.toLocaleLowerCase();
|
||||
result = result.replace(/sha256\((.*?)\)/gm, "digest($1, 'sha256')");
|
||||
result = result.replace(/integer/gm, "numeric");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user