This commit is contained in:
Jan Prochazka 2021-09-26 20:25:01 +02:00
parent f85460cce8
commit 7e82e83faa
11 changed files with 47 additions and 21 deletions

View File

@ -14,6 +14,7 @@ let analysedStructure = null;
let lastPing = null; let lastPing = null;
let lastStatus = null; let lastStatus = null;
let analysedTime = 0; let analysedTime = 0;
let serverVersion;
async function checkedAsyncCall(promise) { async function checkedAsyncCall(promise) {
try { try {
@ -36,7 +37,7 @@ async function handleFullRefresh() {
loadingModel = true; loadingModel = true;
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
setStatusName('loadStructure'); setStatusName('loadStructure');
analysedStructure = await checkedAsyncCall(driver.analyseFull(systemConnection)); analysedStructure = await checkedAsyncCall(driver.analyseFull(systemConnection, serverVersion));
analysedTime = new Date().getTime(); analysedTime = new Date().getTime();
process.send({ msgtype: 'structure', structure: analysedStructure }); process.send({ msgtype: 'structure', structure: analysedStructure });
process.send({ msgtype: 'structureTime', analysedTime }); process.send({ msgtype: 'structureTime', analysedTime });
@ -48,7 +49,7 @@ async function handleIncrementalRefresh(forceSend) {
loadingModel = true; loadingModel = true;
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
setStatusName('checkStructure'); setStatusName('checkStructure');
const newStructure = await checkedAsyncCall(driver.analyseIncremental(systemConnection, analysedStructure)); const newStructure = await checkedAsyncCall(driver.analyseIncremental(systemConnection, analysedStructure, serverVersion));
analysedTime = new Date().getTime(); analysedTime = new Date().getTime();
if (newStructure != null) { if (newStructure != null) {
analysedStructure = newStructure; analysedStructure = newStructure;
@ -84,6 +85,7 @@ async function readVersion() {
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
const version = await driver.getVersion(systemConnection); const version = await driver.getVersion(systemConnection);
process.send({ msgtype: 'version', version }); process.send({ msgtype: 'version', version });
serverVersion = version;
} }
async function handleConnect({ connection, structure, globalSettings }) { async function handleConnect({ connection, structure, globalSettings }) {
@ -93,7 +95,7 @@ async function handleConnect({ connection, structure, globalSettings }) {
if (!structure) setStatusName('pending'); if (!structure) setStatusName('pending');
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
systemConnection = await checkedAsyncCall(connectUtility(driver, storedConnection)); systemConnection = await checkedAsyncCall(connectUtility(driver, storedConnection));
readVersion(); await checkedAsyncCall(readVersion());
if (structure) { if (structure) {
analysedStructure = structure; analysedStructure = structure;
handleIncrementalRefresh(true); handleIncrementalRefresh(true);

View File

@ -1,4 +1,4 @@
import { DatabaseInfo, DatabaseModification, EngineDriver } from 'dbgate-types'; import { DatabaseInfo, DatabaseModification, EngineDriver, SqlDialect } from 'dbgate-types';
import _sortBy from 'lodash/sortBy'; import _sortBy from 'lodash/sortBy';
import _groupBy from 'lodash/groupBy'; import _groupBy from 'lodash/groupBy';
import _pick from 'lodash/pick'; import _pick from 'lodash/pick';
@ -13,8 +13,11 @@ export class DatabaseAnalyser {
modifications: DatabaseModification[]; modifications: DatabaseModification[];
singleObjectFilter: any; singleObjectFilter: any;
singleObjectId: string = null; singleObjectId: string = null;
dialect: SqlDialect;
constructor(public pool, public driver: EngineDriver) {} constructor(public pool, public driver: EngineDriver, version) {
this.dialect = (driver?.dialectByVersion && driver?.dialectByVersion(version)) || driver?.dialect;
}
async _runAnalysis() { async _runAnalysis() {
return DatabaseAnalyser.createEmptyStructure(); return DatabaseAnalyser.createEmptyStructure();

View File

@ -17,8 +17,8 @@ export const driverBase = {
dumperClass: SqlDumper, dumperClass: SqlDumper,
dialect, dialect,
async analyseFull(pool) { async analyseFull(pool, version) {
const analyser = new this.analyserClass(pool, this); const analyser = new this.analyserClass(pool, this, version);
return analyser.fullAnalysis(); return analyser.fullAnalysis();
}, },
async analyseSingleObject(pool, name, typeField = 'tables') { async analyseSingleObject(pool, name, typeField = 'tables') {
@ -28,8 +28,8 @@ export const driverBase = {
analyseSingleTable(pool, name) { analyseSingleTable(pool, name) {
return this.analyseSingleObject(pool, name, 'tables'); return this.analyseSingleObject(pool, name, 'tables');
}, },
async analyseIncremental(pool, structure) { async analyseIncremental(pool, structure, version) {
const analyser = new this.analyserClass(pool, this); const analyser = new this.analyserClass(pool, this, version);
return analyser.incrementalAnalysis(structure); return analyser.incrementalAnalysis(structure);
}, },
createDumper() { createDumper() {

View File

@ -67,8 +67,8 @@ export interface EngineDriver {
name: string; name: string;
}[] }[]
>; >;
analyseFull(pool: any): Promise<DatabaseInfo>; analyseFull(pool: any, serverVersion): Promise<DatabaseInfo>;
analyseIncremental(pool: any, structure: DatabaseInfo): Promise<DatabaseInfo>; analyseIncremental(pool: any, structure: DatabaseInfo, serverVersion): Promise<DatabaseInfo>;
dialect: SqlDialect; dialect: SqlDialect;
dialectByVersion(version): SqlDialect; dialectByVersion(version): SqlDialect;
createDumper(): SqlDumper; createDumper(): SqlDumper;

View File

@ -1,8 +1,8 @@
const { DatabaseAnalyser } = require('dbgate-tools'); const { DatabaseAnalyser } = require('dbgate-tools');
class Analyser extends DatabaseAnalyser { class Analyser extends DatabaseAnalyser {
constructor(pool, driver) { constructor(pool, driver, version) {
super(pool, driver); super(pool, driver, version);
} }
async _runAnalysis() { async _runAnalysis() {

View File

@ -50,8 +50,8 @@ function getColumnInfo({
} }
class MsSqlAnalyser extends DatabaseAnalyser { class MsSqlAnalyser extends DatabaseAnalyser {
constructor(pool, driver) { constructor(pool, driver, version) {
super(pool, driver); super(pool, driver, version);
} }
createQuery(resFileName, typeFields) { createQuery(resFileName, typeFields) {

View File

@ -29,8 +29,8 @@ function getColumnInfo({
} }
class Analyser extends DatabaseAnalyser { class Analyser extends DatabaseAnalyser {
constructor(pool, driver) { constructor(pool, driver, version) {
super(pool, driver); super(pool, driver, version);
} }
createQuery(resFileName, typeFields) { createQuery(resFileName, typeFields) {

View File

@ -36,8 +36,8 @@ function getColumnInfo({
} }
class Analyser extends DatabaseAnalyser { class Analyser extends DatabaseAnalyser {
constructor(pool, driver) { constructor(pool, driver, version) {
super(pool, driver); super(pool, driver, version);
} }
createQuery(resFileName, typeFields) { createQuery(resFileName, typeFields) {

View File

@ -133,10 +133,15 @@ const drivers = driverBases.map(driverBase => ({
const m = version.match(/([\d\.]+)/); const m = version.match(/([\d\.]+)/);
let versionText = null; let versionText = null;
let versionMajor = null;
let versionMinor = null;
if (m) { if (m) {
if (isCockroach) versionText = `CockroachDB ${m[1]}`; if (isCockroach) versionText = `CockroachDB ${m[1]}`;
if (isRedshift) versionText = `Redshift ${m[1]}`; if (isRedshift) versionText = `Redshift ${m[1]}`;
if (isPostgres) versionText = `PostgreSQL ${m[1]}`; if (isPostgres) versionText = `PostgreSQL ${m[1]}`;
const numbers = m[1].split('.');
if (numbers[0]) versionMajor = parseInt(numbers[0]);
if (numbers[1]) versionMinor = parseInt(numbers[1]);
} }
return { return {
@ -145,6 +150,8 @@ const drivers = driverBases.map(driverBase => ({
isPostgres, isPostgres,
isCockroach, isCockroach,
isRedshift, isRedshift,
versionMajor,
versionMinor,
}; };
}, },
async readQuery(client, sql, structure) { async readQuery(client, sql, structure) {

View File

@ -57,6 +57,20 @@ const postgresDriver = {
...dialect, ...dialect,
materializedViews: true, materializedViews: true,
}, },
dialectByVersion(version) {
if (version) {
return {
...dialect,
materializedViews:
version &&
version.versionMajor != null &&
version.versionMinor != null &&
(version.versionMajor > 9 || version.versionMajor == 9 || version.versionMinor >= 3),
};
}
return dialect;
},
}; };
/** @type {import('dbgate-types').EngineDriver} */ /** @type {import('dbgate-types').EngineDriver} */

View File

@ -16,8 +16,8 @@ SELECT
`; `;
class Analyser extends DatabaseAnalyser { class Analyser extends DatabaseAnalyser {
constructor(pool, driver) { constructor(pool, driver, version) {
super(pool, driver); super(pool, driver, version);
} }
async _getFastSnapshot() { async _getFastSnapshot() {