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