mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-17 23:45:59 +00:00
backend bundle refactor
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
const mssql = require('mssql');
|
||||
const mysql = require('mysql');
|
||||
const pg = require('pg');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
const nativeModules = {
|
||||
mssql,
|
||||
mysql,
|
||||
pg,
|
||||
fs,
|
||||
path,
|
||||
};
|
||||
|
||||
function driverConnect(driver, connection) {
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
const fp = require('lodash/fp');
|
||||
const _ = require('lodash');
|
||||
const sql = require('./sql')
|
||||
|
||||
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
||||
|
||||
/** @returns {Promise<string>} */
|
||||
async function loadQuery(pool, name) {
|
||||
return await pool._nativeModules.fs.readFile(pool._nativeModules.path.join(__dirname, name), 'utf-8');
|
||||
}
|
||||
|
||||
const byTableFilter = table => x => x.pureName == table.pureName && x.schemaName == x.schemaName;
|
||||
|
||||
function extractPrimaryKeys(table, pkColumns) {
|
||||
@@ -184,15 +180,15 @@ class MsSqlAnalyser extends DatabaseAnalayser {
|
||||
functions = false,
|
||||
triggers = false
|
||||
) {
|
||||
let res = await loadQuery(this.pool, resFileName);
|
||||
let res = sql[resFileName];
|
||||
res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null');
|
||||
return res;
|
||||
}
|
||||
async runAnalysis() {
|
||||
const tables = await this.driver.query(this.pool, await this.createQuery('tables.sql'));
|
||||
const columns = await this.driver.query(this.pool, await this.createQuery('columns.sql'));
|
||||
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'));
|
||||
const tables = await this.driver.query(this.pool, await this.createQuery('tables'));
|
||||
const columns = await this.driver.query(this.pool, await this.createQuery('columns'));
|
||||
const pkColumns = await this.driver.query(this.pool, await this.createQuery('primaryKeys'));
|
||||
const fkColumns = await this.driver.query(this.pool, await this.createQuery('foreignKeys'));
|
||||
|
||||
this.result.tables = tables.rows.map(table => ({
|
||||
...table,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
module.exports = `
|
||||
select c.name as columnName, t.name as dataType, c.object_id as objectId, c.is_identity as isIdentity,
|
||||
c.max_length as maxLength, c.precision, c.scale, c.is_nullable as isNullable,
|
||||
d.definition as defaultValue, d.name as defaultConstraint,
|
||||
@@ -11,3 +12,4 @@ left join sys.default_constraints d on c.default_object_id = d.object_id
|
||||
left join sys.computed_columns m on m.object_id = c.object_id and m.column_id = c.column_id
|
||||
where o.type = 'U' and o.object_id =[OBJECT_ID_CONDITION]
|
||||
order by c.column_id
|
||||
`;
|
||||
@@ -1,3 +1,4 @@
|
||||
module.exports = `
|
||||
SELECT
|
||||
schemaName = FK.TABLE_SCHEMA,
|
||||
pureName = FK.TABLE_NAME,
|
||||
@@ -36,3 +37,4 @@ inner join sys.objects o on FK.TABLE_NAME = o.name
|
||||
inner join sys.schemas s on o.schema_id = s.schema_id and FK.TABLE_SCHEMA = s.name
|
||||
|
||||
where o.object_id =[OBJECT_ID_CONDITION]
|
||||
`;
|
||||
11
packages/engines/mssql/sql/index.js
Normal file
11
packages/engines/mssql/sql/index.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const columns = require('./columns');
|
||||
const foreignKeys = require('./foreignKeys');
|
||||
const primaryKeys = require('./primaryKeys');
|
||||
const tables = require('./tables');
|
||||
|
||||
module.exports = {
|
||||
columns,
|
||||
tables,
|
||||
foreignKeys,
|
||||
primaryKeys,
|
||||
};
|
||||
@@ -1,3 +1,4 @@
|
||||
module.exports = `
|
||||
select o.object_id, pureName = t.Table_Name, schemaName = t.Table_Schema, columnName = c.Column_Name, constraintName=t.constraint_name from
|
||||
INFORMATION_SCHEMA.TABLE_CONSTRAINTS t,
|
||||
sys.objects o,
|
||||
@@ -10,3 +11,4 @@ where
|
||||
and c.Table_Name = t.Table_Name
|
||||
and Constraint_Type = 'PRIMARY KEY'
|
||||
and o.object_id =[OBJECT_ID_CONDITION]
|
||||
`;
|
||||
@@ -1,6 +1,8 @@
|
||||
module.exports = `
|
||||
select
|
||||
o.name as pureName, s.name as schemaName, o.object_id as objectId,
|
||||
o.create_date, o.modify_date
|
||||
from sys.tables o
|
||||
inner join sys.schemas s on o.schema_id = s.schema_id
|
||||
where o.object_id =[OBJECT_ID_CONDITION]
|
||||
`;
|
||||
@@ -1,15 +1,8 @@
|
||||
const fp = require("lodash/fp");
|
||||
const _ = require("lodash");
|
||||
const fp = require('lodash/fp');
|
||||
const _ = require('lodash');
|
||||
const sql = require('./sql');
|
||||
|
||||
const DatabaseAnalayser = require("../default/DatabaseAnalyser");
|
||||
|
||||
/** @returns {Promise<string>} */
|
||||
async function loadQuery(pool, name) {
|
||||
return await pool._nativeModules.fs.readFile(
|
||||
pool._nativeModules.path.join(__dirname, name),
|
||||
"utf-8"
|
||||
);
|
||||
}
|
||||
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
||||
|
||||
class MySqlAnalyser extends DatabaseAnalayser {
|
||||
constructor(pool, driver) {
|
||||
@@ -24,20 +17,14 @@ class MySqlAnalyser extends DatabaseAnalayser {
|
||||
functions = false,
|
||||
triggers = false
|
||||
) {
|
||||
let res = await loadQuery(this.pool, resFileName);
|
||||
res = res.replace("=[OBJECT_NAME_CONDITION]", " is not null");
|
||||
res = res.replace("#DATABASE#", this.pool._database_name);
|
||||
let res = sql[resFileName];
|
||||
res = res.replace('=[OBJECT_NAME_CONDITION]', ' is not null');
|
||||
res = res.replace('#DATABASE#', this.pool._database_name);
|
||||
return res;
|
||||
}
|
||||
async runAnalysis() {
|
||||
const tables = await this.driver.query(
|
||||
this.pool,
|
||||
await this.createQuery("tables.sql")
|
||||
);
|
||||
const columns = await this.driver.query(
|
||||
this.pool,
|
||||
await this.createQuery("columns.sql")
|
||||
);
|
||||
const tables = await this.driver.query(this.pool, await this.createQuery('tables'));
|
||||
const columns = await this.driver.query(this.pool, await this.createQuery('columns'));
|
||||
// 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'));
|
||||
|
||||
@@ -48,9 +35,9 @@ class MySqlAnalyser extends DatabaseAnalayser {
|
||||
.map(({ isNullable, extra, ...col }) => ({
|
||||
...col,
|
||||
notNull: !isNullable,
|
||||
autoIncrement: extra && extra.toLowerCase().includes("auto_increment")
|
||||
autoIncrement: extra && extra.toLowerCase().includes('auto_increment'),
|
||||
})),
|
||||
foreignKeys: []
|
||||
foreignKeys: [],
|
||||
// primaryKey: extractPrimaryKeys(table, pkColumns.rows),
|
||||
// foreignKeys: extractForeignKeys(table, fkColumns.rows),
|
||||
}));
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
module.exports = `
|
||||
select
|
||||
TABLE_NAME as pureName,
|
||||
COLUMN_NAME as columnName,
|
||||
@@ -11,3 +12,4 @@ select
|
||||
from INFORMATION_SCHEMA.COLUMNS
|
||||
where TABLE_SCHEMA = '#DATABASE#' and TABLE_NAME =[OBJECT_NAME_CONDITION]
|
||||
order by ORDINAL_POSITION
|
||||
`;
|
||||
7
packages/engines/mysql/sql/index.js
Normal file
7
packages/engines/mysql/sql/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const columns = require('./columns');
|
||||
const tables = require('./tables');
|
||||
|
||||
module.exports = {
|
||||
columns,
|
||||
tables,
|
||||
};
|
||||
@@ -1,5 +1,7 @@
|
||||
module.exports = `
|
||||
select
|
||||
TABLE_NAME as pureName,
|
||||
case when ENGINE='InnoDB' then CREATE_TIME else coalesce(UPDATE_TIME, CREATE_TIME) end as alterTime
|
||||
from information_schema.tables
|
||||
where TABLE_SCHEMA = '#DATABASE#' and TABLE_NAME =[OBJECT_NAME_CONDITION];
|
||||
`;
|
||||
@@ -1,16 +1,9 @@
|
||||
const fp = require("lodash/fp");
|
||||
const _ = require("lodash");
|
||||
const sql = require('./sql')
|
||||
|
||||
const DatabaseAnalayser = require("../default/DatabaseAnalyser");
|
||||
|
||||
/** @returns {Promise<string>} */
|
||||
async function loadQuery(pool, name) {
|
||||
return await pool._nativeModules.fs.readFile(
|
||||
pool._nativeModules.path.join(__dirname, name),
|
||||
"utf-8"
|
||||
);
|
||||
}
|
||||
|
||||
class MySqlAnalyser extends DatabaseAnalayser {
|
||||
constructor(pool, driver) {
|
||||
super(pool, driver);
|
||||
@@ -24,18 +17,18 @@ class MySqlAnalyser extends DatabaseAnalayser {
|
||||
functions = false,
|
||||
triggers = false
|
||||
) {
|
||||
let res = await loadQuery(this.pool, resFileName);
|
||||
let res = sql[resFileName];
|
||||
res = res.replace("=[OBJECT_ID_CONDITION]", " is not null");
|
||||
return res;
|
||||
}
|
||||
async runAnalysis() {
|
||||
const tables = await this.driver.query(
|
||||
this.pool,
|
||||
await this.createQuery("table_modifications.psql")
|
||||
await this.createQuery("tableModifications")
|
||||
);
|
||||
const columns = await this.driver.query(
|
||||
this.pool,
|
||||
await this.createQuery("columns.psql")
|
||||
await this.createQuery("columns")
|
||||
);
|
||||
// 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'));
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
module.exports = `
|
||||
select
|
||||
table_schema as "schemaName",
|
||||
table_name as "pureName",
|
||||
@@ -15,3 +16,4 @@ where
|
||||
and table_schema !~ '^pg_toast'
|
||||
and 'table:' || table_schema || '.' || table_name =[OBJECT_ID_CONDITION]
|
||||
order by ordinal_position
|
||||
`;
|
||||
7
packages/engines/postgres/sql/index.js
Normal file
7
packages/engines/postgres/sql/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const columns = require('./columns');
|
||||
const tableModifications = require('./tableModifications');
|
||||
|
||||
module.exports = {
|
||||
columns,
|
||||
tableModifications,
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
module.exports = `
|
||||
with pkey as
|
||||
(
|
||||
select cc.conrelid, format(E'create constraint %I primary key(%s);\n', cc.conname,
|
||||
select cc.conrelid, format(E'create constraint %I primary key(%s);\\n', cc.conname,
|
||||
string_agg(a.attname, ', '
|
||||
order by array_position(cc.conkey, a.attnum))) pkey
|
||||
from pg_catalog.pg_constraint cc
|
||||
@@ -13,13 +14,13 @@ with pkey as
|
||||
|
||||
|
||||
SELECT oid as "objectId", nspname as "schemaName", relname as "pureName",
|
||||
md5('CREATE TABLE ' || nspname || '.' || relname || E'\n(\n' ||
|
||||
md5('CREATE TABLE ' || nspname || '.' || relname || E'\\n(\\n' ||
|
||||
array_to_string(
|
||||
array_agg(
|
||||
' ' || column_name || ' ' || type || ' '|| not_null
|
||||
)
|
||||
, E',\n'
|
||||
) || E'\n);\n' || (select pkey from pkey where pkey.conrelid = oid)) as "hash"
|
||||
, E',\\n'
|
||||
) || E'\\n);\\n' || (select pkey from pkey where pkey.conrelid = oid)) as "hash"
|
||||
from
|
||||
(
|
||||
SELECT
|
||||
@@ -48,3 +49,4 @@ from
|
||||
) as tabledefinition
|
||||
where 'table:' || nspname || '.' || relname =[OBJECT_ID_CONDITION]
|
||||
group by relname, nspname, oid
|
||||
`;
|
||||
Reference in New Issue
Block a user