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 lastStatus = null;
let analysedTime = 0;
let serverVersion;
async function checkedAsyncCall(promise) {
try {
@ -36,7 +37,7 @@ async function handleFullRefresh() {
loadingModel = true;
const driver = requireEngineDriver(storedConnection);
setStatusName('loadStructure');
analysedStructure = await checkedAsyncCall(driver.analyseFull(systemConnection));
analysedStructure = await checkedAsyncCall(driver.analyseFull(systemConnection, serverVersion));
analysedTime = new Date().getTime();
process.send({ msgtype: 'structure', structure: analysedStructure });
process.send({ msgtype: 'structureTime', analysedTime });
@ -48,7 +49,7 @@ async function handleIncrementalRefresh(forceSend) {
loadingModel = true;
const driver = requireEngineDriver(storedConnection);
setStatusName('checkStructure');
const newStructure = await checkedAsyncCall(driver.analyseIncremental(systemConnection, analysedStructure));
const newStructure = await checkedAsyncCall(driver.analyseIncremental(systemConnection, analysedStructure, serverVersion));
analysedTime = new Date().getTime();
if (newStructure != null) {
analysedStructure = newStructure;
@ -84,6 +85,7 @@ async function readVersion() {
const driver = requireEngineDriver(storedConnection);
const version = await driver.getVersion(systemConnection);
process.send({ msgtype: 'version', version });
serverVersion = version;
}
async function handleConnect({ connection, structure, globalSettings }) {
@ -93,7 +95,7 @@ async function handleConnect({ connection, structure, globalSettings }) {
if (!structure) setStatusName('pending');
const driver = requireEngineDriver(storedConnection);
systemConnection = await checkedAsyncCall(connectUtility(driver, storedConnection));
readVersion();
await checkedAsyncCall(readVersion());
if (structure) {
analysedStructure = structure;
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 _groupBy from 'lodash/groupBy';
import _pick from 'lodash/pick';
@ -13,8 +13,11 @@ export class DatabaseAnalyser {
modifications: DatabaseModification[];
singleObjectFilter: any;
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() {
return DatabaseAnalyser.createEmptyStructure();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -133,10 +133,15 @@ const drivers = driverBases.map(driverBase => ({
const m = version.match(/([\d\.]+)/);
let versionText = null;
let versionMajor = null;
let versionMinor = null;
if (m) {
if (isCockroach) versionText = `CockroachDB ${m[1]}`;
if (isRedshift) versionText = `Redshift ${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 {
@ -145,6 +150,8 @@ const drivers = driverBases.map(driverBase => ({
isPostgres,
isCockroach,
isRedshift,
versionMajor,
versionMinor,
};
},
async readQuery(client, sql, structure) {

View File

@ -57,6 +57,20 @@ const postgresDriver = {
...dialect,
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} */

View File

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