2020-01-06 21:33:20 +00:00
|
|
|
const fs = require('fs-extra');
|
2020-02-02 15:43:41 +00:00
|
|
|
const fp = require('lodash/fp');
|
2020-01-06 21:33:20 +00:00
|
|
|
const path = require('path');
|
2020-02-02 10:31:41 +00:00
|
|
|
const _ = require('lodash');
|
2020-01-06 21:33:20 +00:00
|
|
|
|
|
|
|
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
|
|
|
|
2020-01-19 20:01:48 +00:00
|
|
|
/** @returns {Promise<string>} */
|
2020-01-06 21:33:20 +00:00
|
|
|
async function loadQuery(name) {
|
|
|
|
return await fs.readFile(path.join(__dirname, name), 'utf-8');
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:43:41 +00:00
|
|
|
const byTableFilter = table => x => x.pureName == table.pureName && x.schemaName == x.schemaName;
|
|
|
|
|
|
|
|
function extractPrimaryKeys(table, pkColumns) {
|
|
|
|
const filtered = pkColumns.filter(byTableFilter(table));
|
|
|
|
if (filtered.length == 0) return undefined;
|
|
|
|
return {
|
2020-02-02 16:27:33 +00:00
|
|
|
..._.pick(filtered[0], ['constraintName', 'schemaName', 'pureName']),
|
2020-02-02 15:43:41 +00:00
|
|
|
constraintType: 'primaryKey',
|
|
|
|
columns: filtered.map(fp.pick('columnName')),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractForeignKeys(table, fkColumns) {
|
|
|
|
const grouped = _.groupBy(fkColumns.filter(byTableFilter(table)), 'constraintName');
|
|
|
|
return _.keys(grouped).map(constraintName => ({
|
|
|
|
constraintName,
|
|
|
|
constraintType: 'foreignKey',
|
2020-02-02 16:27:33 +00:00
|
|
|
..._.pick(grouped[constraintName][0], [
|
|
|
|
'constraintName',
|
|
|
|
'schemaName',
|
|
|
|
'pureName',
|
|
|
|
'refSchemaName',
|
|
|
|
'refTableName',
|
|
|
|
'updateAction',
|
|
|
|
'deleteAction',
|
|
|
|
]),
|
2020-02-02 15:43:41 +00:00
|
|
|
columns: grouped[constraintName].map(fp.pick(['columnName', 'refColumnName'])),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-01-06 21:33:20 +00:00
|
|
|
class MsSqlAnalyser extends DatabaseAnalayser {
|
|
|
|
constructor(pool, driver) {
|
|
|
|
super(pool, driver);
|
|
|
|
}
|
|
|
|
|
2020-01-19 20:01:48 +00:00
|
|
|
async createQuery(
|
|
|
|
resFileName,
|
|
|
|
tables = false,
|
|
|
|
views = false,
|
|
|
|
procedures = false,
|
|
|
|
functions = false,
|
|
|
|
triggers = false
|
|
|
|
) {
|
|
|
|
let res = await loadQuery(resFileName);
|
|
|
|
res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null');
|
|
|
|
return res;
|
|
|
|
}
|
2020-01-06 21:33:20 +00:00
|
|
|
async runAnalysis() {
|
2020-01-19 20:01:48 +00:00
|
|
|
const tables = await this.driver.query(this.pool, await this.createQuery('tables.sql'));
|
2020-02-02 10:31:41 +00:00
|
|
|
const columns = await this.driver.query(this.pool, await this.createQuery('columns.sql'));
|
2020-02-02 15:43:41 +00:00
|
|
|
const pkColumns = await this.driver.query(this.pool, await this.createQuery('primary_keys.sql'));
|
|
|
|
const fkColumns = await this.driver.query(this.pool, await this.createQuery('foreign_keys.sql'));
|
2020-02-02 10:31:41 +00:00
|
|
|
|
|
|
|
this.result.tables = tables.rows.map(table => ({
|
|
|
|
...table,
|
|
|
|
columns: columns.rows
|
|
|
|
.filter(col => col.objectId == table.objectId)
|
|
|
|
.map(({ isNullable, isIdentity, ...col }) => ({
|
|
|
|
...col,
|
2020-02-02 12:18:56 +00:00
|
|
|
notNull: !isNullable,
|
|
|
|
autoIncrement: !!isIdentity,
|
2020-02-02 10:31:41 +00:00
|
|
|
})),
|
2020-02-02 15:43:41 +00:00
|
|
|
primaryKey: extractPrimaryKeys(table, pkColumns.rows),
|
|
|
|
foreignKeys: extractForeignKeys(table, fkColumns.rows),
|
2020-02-02 10:31:41 +00:00
|
|
|
}));
|
2020-01-06 21:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MsSqlAnalyser;
|