migrating to typescript
This commit is contained in:
parent
2a836839e7
commit
d01a8f3ccc
|
@ -47,12 +47,7 @@ async function getIsStarted(conversion: Conversion, dataPoolId: number): Promise
|
|||
|
||||
const dbAccess: DBAccess = new DBAccess(conversion);
|
||||
const result: DBAccessQueryResult = await dbAccess.query(logTitle, sql, DBVendors.PG, false, false);
|
||||
|
||||
if ('rows' in result.data) {
|
||||
return Array.isArray(result.data.rows) ? !!result.data.rows[0].is_started : false;
|
||||
}
|
||||
|
||||
return false;
|
||||
return result.error ? false : !!result.data.rows[0].is_started;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,12 +63,7 @@ async function hasCurrentChunkLoaded(conversion: Conversion, chunk: any): Promis
|
|||
|
||||
const dbAccess: DBAccess = new DBAccess(conversion);
|
||||
const result: DBAccessQueryResult = await dbAccess.query(logTitle, sql, DBVendors.PG, false, false);
|
||||
|
||||
if ('rows' in result.data) {
|
||||
return Array.isArray(result.data.rows) ? !!result.data.rows[0].exists : true;
|
||||
}
|
||||
|
||||
return true;
|
||||
return result.error ? true : !!result.data.rows[0].exists;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -178,10 +178,10 @@ export default class DBAccess {
|
|||
if (error) {
|
||||
// An error occurred during DB querying.
|
||||
generateError(this._conversion, `\t--[${ caller }] ${ error }`, sql);
|
||||
return processExitOnError ? process.exit() : reject({ client: client, data: error });
|
||||
return processExitOnError ? process.exit() : reject({ client: client, data: undefined, error: error });
|
||||
}
|
||||
|
||||
return resolve({ client: client, data: data });
|
||||
return resolve({ client: client, data: data, error: undefined });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -207,11 +207,11 @@ export default class DBAccess {
|
|||
client = undefined;
|
||||
}
|
||||
|
||||
return { client: client, data: data };
|
||||
return { client: client, data: data, error: undefined };
|
||||
} catch (error) {
|
||||
// An error occurred during DB querying.
|
||||
generateError(this._conversion, `\t--[${ caller }] ${ error }`, sql);
|
||||
return processExitOnError ? process.exit() : { client: client, data: error };
|
||||
return processExitOnError ? process.exit() : { client: client, data: undefined, error: error };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,13 @@ export default interface DBAccessQueryResult {
|
|||
|
||||
/**
|
||||
* Query result.
|
||||
* The data may be undefined.
|
||||
*/
|
||||
data: any;
|
||||
data?: any;
|
||||
|
||||
/**
|
||||
* Query error.
|
||||
* The data may be undefined.
|
||||
*/
|
||||
error?: any;
|
||||
}
|
||||
|
|
|
@ -18,40 +18,30 @@
|
|||
*
|
||||
* @author Anatoly Khaytovich <anatolyuss@gmail.com>
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const log = require('./Logger');
|
||||
const generateError = require('./ErrorGenerator');
|
||||
const migrationStateManager = require('./MigrationStateManager');
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import log from './Logger';
|
||||
import Conversion from './Conversion';
|
||||
import DBAccess from './DBAccess';
|
||||
import DBVendors from './DBVendors';
|
||||
import * as extraConfigProcessor from './ExtraConfigProcessor';
|
||||
|
||||
/**
|
||||
* Attempts to convert MySQL view to PostgreSQL view.
|
||||
*
|
||||
* @param {String} schema
|
||||
* @param {String} viewName
|
||||
* @param {String} mysqlViewCode
|
||||
*
|
||||
* @returns {String}
|
||||
*/
|
||||
const generateView = (schema, viewName, mysqlViewCode) => {
|
||||
mysqlViewCode = mysqlViewCode.split('`').join('"');
|
||||
const queryStart = mysqlViewCode.indexOf('AS');
|
||||
mysqlViewCode = mysqlViewCode.slice(queryStart);
|
||||
const arrMysqlViewCode = mysqlViewCode.split(' ');
|
||||
function generateView(schema: string, viewName: string, mysqlViewCode: string): string {
|
||||
mysqlViewCode = mysqlViewCode.split('`').join('"');
|
||||
const queryStart: number = mysqlViewCode.indexOf('AS');
|
||||
mysqlViewCode = mysqlViewCode.slice(queryStart);
|
||||
const arrMysqlViewCode: string[] = mysqlViewCode.split(' ');
|
||||
|
||||
for (let i = 0; i < arrMysqlViewCode.length; ++i) {
|
||||
if (
|
||||
arrMysqlViewCode[i].toLowerCase() === 'from'
|
||||
|| arrMysqlViewCode[i].toLowerCase() === 'join'
|
||||
&& i + 1 < arrMysqlViewCode.length
|
||||
) {
|
||||
arrMysqlViewCode[i + 1] = '"' + schema + '".' + arrMysqlViewCode[i + 1];
|
||||
arrMysqlViewCode.forEach((str: string, index: number) => {
|
||||
if (str.toLowerCase() === 'from' || str.toLowerCase() === 'join' && index + 1 < arrMysqlViewCode.length) {
|
||||
arrMysqlViewCode[index + 1] = `"${ schema }".${ arrMysqlViewCode[index + 1] }`;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return 'CREATE OR REPLACE VIEW "' + schema + '"."' + viewName + '" ' + arrMysqlViewCode.join(' ') + ';';
|
||||
return `CREATE OR REPLACE VIEW "${ schema }"."${ viewName }" ${ arrMysqlViewCode.join(' ') };`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -61,9 +51,9 @@ const generateView = (schema, viewName, mysqlViewCode) => {
|
|||
* @param {String} viewName
|
||||
* @param {String} sql
|
||||
*
|
||||
* @returns {undefined}
|
||||
* @returns {undefined} TODO: refactor to return Promise<void>!!!
|
||||
*/
|
||||
const logNotCreatedView = (self, viewName, sql) => {
|
||||
const logNotCreatedView = (conversion: Conversion, viewName: string, sql: string) => {
|
||||
fs.stat(self._notCreatedViewsPath, (directoryDoesNotExist, stat) => {
|
||||
if (directoryDoesNotExist) {
|
||||
fs.mkdir(self._notCreatedViewsPath, self._0777, e => {
|
||||
|
@ -169,4 +159,4 @@ module.exports = self => {
|
|||
Promise.all(createViewPromises).then(() => resolve());
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue