mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-20 05:26:00 +00:00
db deploy fixes
This commit is contained in:
@@ -686,7 +686,8 @@ export class SqlDumper implements AlterProcessor {
|
||||
}
|
||||
|
||||
createSqlObject(obj: SqlObjectInfo) {
|
||||
this.putCmd(obj.createSql);
|
||||
this.putRaw(obj.createSql);
|
||||
this.endCommand();
|
||||
}
|
||||
|
||||
getSqlObjectSqlName(ojectTypeField: string) {
|
||||
|
||||
@@ -498,22 +498,25 @@ export class AlterPlan {
|
||||
return this.operations;
|
||||
}
|
||||
const fks = [];
|
||||
const res = this.operations.map(op => {
|
||||
if (op.operationType == 'createTable') {
|
||||
fks.push(...(op.newObject.foreignKeys || []));
|
||||
return {
|
||||
...op,
|
||||
newObject: {
|
||||
...op.newObject,
|
||||
foreignKeys: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
return op;
|
||||
});
|
||||
const res = this.operations
|
||||
.filter(op => op.operationType != 'createConstraint')
|
||||
.map(op => {
|
||||
if (op.operationType == 'createTable') {
|
||||
fks.push(...(op.newObject.foreignKeys || []));
|
||||
return {
|
||||
...op,
|
||||
newObject: {
|
||||
...op.newObject,
|
||||
foreignKeys: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
return op;
|
||||
});
|
||||
|
||||
return [
|
||||
...res,
|
||||
...this.operations.filter(op => op.operationType == 'createConstraint'),
|
||||
...fks.map(
|
||||
fk =>
|
||||
({
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
SqlDialect,
|
||||
SqlObjectInfo,
|
||||
TableInfo,
|
||||
ViewInfo,
|
||||
} from 'dbgate-types';
|
||||
import uuidv1 from 'uuid/v1';
|
||||
import { AlterPlan } from './alterPlan';
|
||||
@@ -15,6 +16,8 @@ import _omit from 'lodash/omit';
|
||||
import _cloneDeep from 'lodash/cloneDeep';
|
||||
import _isEqual from 'lodash/isEqual';
|
||||
import _pick from 'lodash/pick';
|
||||
import _compact from 'lodash/compact';
|
||||
import toposort from 'toposort';
|
||||
|
||||
type DbDiffSchemaMode = 'strict' | 'ignore' | 'ignoreImplicit';
|
||||
|
||||
@@ -504,6 +507,40 @@ export function createAlterTablePlan(
|
||||
return plan;
|
||||
}
|
||||
|
||||
function sortViewsByDependency(views: ViewInfo[]): ViewInfo[] {
|
||||
const viewNames: string[] = [];
|
||||
const viewDict: { [name: string]: string } = {};
|
||||
|
||||
for (const view of views) {
|
||||
if (!viewNames.includes(view.pureName)) {
|
||||
viewNames.push(view.pureName);
|
||||
}
|
||||
viewDict[view.pureName] = viewDict[view.pureName]
|
||||
? `${viewDict[view.pureName]} ${view.createSql}}`
|
||||
: view.createSql;
|
||||
}
|
||||
|
||||
const edges = [];
|
||||
for (const viewName of viewNames) {
|
||||
edges.push([viewName, null]);
|
||||
const viewText = viewDict[viewName];
|
||||
for (const otherView of viewNames) {
|
||||
if (otherView === viewName) continue;
|
||||
if ((' ' + viewText + ' ').match('[\\W]' + otherView + '[\\W]')) {
|
||||
edges.push([otherView, viewName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ordered: string[] = _compact(toposort(edges));
|
||||
|
||||
const res: ViewInfo[] = [];
|
||||
for (const viewName of ordered) {
|
||||
res.push(...views.filter(x => x.pureName == viewName));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export function createAlterDatabasePlan(
|
||||
oldDb: DatabaseInfo,
|
||||
newDb: DatabaseInfo,
|
||||
@@ -539,7 +576,13 @@ export function createAlterDatabasePlan(
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const newobj of newDb[objectTypeField] || []) {
|
||||
|
||||
let newList = newDb[objectTypeField] || [];
|
||||
if (objectTypeField == 'views') {
|
||||
newList = sortViewsByDependency(newList);
|
||||
}
|
||||
|
||||
for (const newobj of newList) {
|
||||
const oldobj = (oldDb[objectTypeField] || []).find(x => x.pairingId == newobj.pairingId);
|
||||
if (objectTypeField == 'tables') {
|
||||
if (oldobj == null) {
|
||||
|
||||
@@ -4,6 +4,10 @@ import { splitQuery } from 'dbgate-query-splitter';
|
||||
import { dumpSqlSelect } from 'dbgate-sqltree';
|
||||
import { EngineDriver, QueryResult, RunScriptOptions } from 'dbgate-types';
|
||||
import { detectSqlFilterBehaviour } from './detectSqlFilterBehaviour';
|
||||
import { getLogger } from './getLogger';
|
||||
import { getLimitedQuery } from './stringTools';
|
||||
|
||||
const logger = getLogger('driverBase');
|
||||
|
||||
const dialect = {
|
||||
limitSelect: true,
|
||||
@@ -71,6 +75,9 @@ export const driverBase = {
|
||||
}
|
||||
for (const sqlItem of splitQuery(sql, this.getQuerySplitterOptions('script'))) {
|
||||
try {
|
||||
if (options?.logScriptItems) {
|
||||
logger.info({ sql: getLimitedQuery(sqlItem as string) }, `Execute script item`);
|
||||
}
|
||||
await this.query(pool, sqlItem, { discardResult: true, ...options?.queryOptions });
|
||||
} catch (err) {
|
||||
if (options?.useTransaction && this.supportsTransactions) {
|
||||
|
||||
@@ -511,3 +511,13 @@ export function safeFormatDate(date) {
|
||||
return date?.toString();
|
||||
}
|
||||
}
|
||||
|
||||
export function getLimitedQuery(sql: string): string {
|
||||
if (!sql) {
|
||||
return sql;
|
||||
}
|
||||
if (sql.length > 1000) {
|
||||
return sql.substring(0, 1000) + '...';
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
@@ -74,10 +74,11 @@ function columnInfoFromYaml(column: ColumnInfoYaml, table: TableInfoYaml): Colum
|
||||
const res: ColumnInfo = {
|
||||
pureName: table.name,
|
||||
columnName: column.name,
|
||||
dataType: column.length ? `${column.type}(${column.length})` : column.type,
|
||||
dataType: column.length ? `${column.type}(${column.length < 0 ? 'max' : column.length})` : column.type,
|
||||
autoIncrement: column.autoIncrement,
|
||||
notNull: column.notNull || (table.primaryKey && table.primaryKey.includes(column.name)),
|
||||
defaultValue: column.default,
|
||||
defaultConstraint: column.default != null ? `DF_${table.name}_${column.name}` : undefined,
|
||||
};
|
||||
return res;
|
||||
}
|
||||
@@ -108,6 +109,7 @@ function convertForeignKeyFromYaml(
|
||||
if (!refTable || !refTable.primaryKey) return null;
|
||||
return {
|
||||
constraintType: 'foreignKey',
|
||||
constraintName: `FK_${table.name}_${col.name}`,
|
||||
pureName: table.name,
|
||||
refTableName: col.references,
|
||||
deleteAction: col.refDeleteAction,
|
||||
@@ -133,6 +135,7 @@ export function tableInfoFromYaml(table: TableInfoYaml, allTables: TableInfoYaml
|
||||
res.primaryKey = {
|
||||
pureName: table.name,
|
||||
constraintType: 'primaryKey',
|
||||
constraintName: `PK_${table.name}`,
|
||||
columns: table.primaryKey.map(columnName => ({ columnName })),
|
||||
};
|
||||
}
|
||||
@@ -140,6 +143,7 @@ export function tableInfoFromYaml(table: TableInfoYaml, allTables: TableInfoYaml
|
||||
res.sortingKey = {
|
||||
pureName: table.name,
|
||||
constraintType: 'sortingKey',
|
||||
constraintName: `SK_${table.name}`,
|
||||
columns: table.sortingKey.map(columnName => ({ columnName })),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user