mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-28 22:16:01 +00:00
preloaded rows fixes
This commit is contained in:
@@ -25,6 +25,7 @@ import _isEqual from 'lodash/isEqual';
|
|||||||
import _pick from 'lodash/pick';
|
import _pick from 'lodash/pick';
|
||||||
import _compact from 'lodash/compact';
|
import _compact from 'lodash/compact';
|
||||||
import _isString from 'lodash/isString';
|
import _isString from 'lodash/isString';
|
||||||
|
import { detectChangesInPreloadedRows } from './structureTools';
|
||||||
|
|
||||||
type DbDiffSchemaMode = 'strict' | 'ignore' | 'ignoreImplicit';
|
type DbDiffSchemaMode = 'strict' | 'ignore' | 'ignoreImplicit';
|
||||||
|
|
||||||
@@ -507,7 +508,7 @@ function createPairs(oldList, newList, additionalCondition = null) {
|
|||||||
|
|
||||||
function planTablePreload(plan: AlterPlan, oldTable: TableInfo, newTable: TableInfo) {
|
function planTablePreload(plan: AlterPlan, oldTable: TableInfo, newTable: TableInfo) {
|
||||||
const key = newTable.preloadedRowsKey || newTable.primaryKey?.columns?.map(x => x.columnName);
|
const key = newTable.preloadedRowsKey || newTable.primaryKey?.columns?.map(x => x.columnName);
|
||||||
if (newTable.preloadedRows?.length > 0 && key?.length > 0) {
|
if (newTable.preloadedRows?.length > 0 && key?.length > 0 && detectChangesInPreloadedRows(oldTable, newTable)) {
|
||||||
plan.fillPreloadedRows(
|
plan.fillPreloadedRows(
|
||||||
newTable,
|
newTable,
|
||||||
oldTable?.preloadedRows,
|
oldTable?.preloadedRows,
|
||||||
@@ -597,8 +598,6 @@ function planAlterTable(plan: AlterPlan, oldTable: TableInfo, newTable: TableInf
|
|||||||
|
|
||||||
constraintPairs.filter(x => x[0] == null).forEach(x => plan.createConstraint(x[1]));
|
constraintPairs.filter(x => x[0] == null).forEach(x => plan.createConstraint(x[1]));
|
||||||
|
|
||||||
planTablePreload(plan, oldTable, newTable);
|
|
||||||
|
|
||||||
planChangeTableOptions(plan, oldTable, newTable, opts);
|
planChangeTableOptions(plan, oldTable, newTable, opts);
|
||||||
|
|
||||||
// console.log('oldTable', oldTable);
|
// console.log('oldTable', oldTable);
|
||||||
@@ -635,7 +634,15 @@ export function testEqualTables(
|
|||||||
// if (plan.operations.length > 0) {
|
// if (plan.operations.length > 0) {
|
||||||
// console.log('************** plan.operations', a, b, plan.operations);
|
// console.log('************** plan.operations', a, b, plan.operations);
|
||||||
// }
|
// }
|
||||||
return plan.operations.length == 0;
|
if (plan.operations.length > 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (detectChangesInPreloadedRows(a, b)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function testEqualSqlObjects(a: SqlObjectInfo, b: SqlObjectInfo, opts: DbDiffOptions) {
|
export function testEqualSqlObjects(a: SqlObjectInfo, b: SqlObjectInfo, opts: DbDiffOptions) {
|
||||||
@@ -658,6 +665,7 @@ export function createAlterTablePlan(
|
|||||||
plan.dropTable(oldTable);
|
plan.dropTable(oldTable);
|
||||||
} else {
|
} else {
|
||||||
planAlterTable(plan, oldTable, newTable, opts);
|
planAlterTable(plan, oldTable, newTable, opts);
|
||||||
|
planTablePreload(plan, oldTable, newTable);
|
||||||
}
|
}
|
||||||
plan.transformPlan();
|
plan.transformPlan();
|
||||||
return plan;
|
return plan;
|
||||||
@@ -719,6 +727,7 @@ export function createAlterDatabasePlan(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
planAlterTable(plan, oldobj, newobj, opts);
|
planAlterTable(plan, oldobj, newobj, opts);
|
||||||
|
planTablePreload(plan, oldobj, newobj);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (newobj == null) {
|
if (newobj == null) {
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import type { DatabaseInfo, TableInfo, ApplicationDefinition, ViewInfo, CollectionInfo } from 'dbgate-types';
|
import type {
|
||||||
|
DatabaseInfo,
|
||||||
|
TableInfo,
|
||||||
|
ApplicationDefinition,
|
||||||
|
ViewInfo,
|
||||||
|
CollectionInfo,
|
||||||
|
NamedObjectInfo,
|
||||||
|
} from 'dbgate-types';
|
||||||
import _flatten from 'lodash/flatten';
|
import _flatten from 'lodash/flatten';
|
||||||
import _uniq from 'lodash/uniq';
|
import _uniq from 'lodash/uniq';
|
||||||
|
import _keys from 'lodash/keys';
|
||||||
|
|
||||||
export function addTableDependencies(db: DatabaseInfo): DatabaseInfo {
|
export function addTableDependencies(db: DatabaseInfo): DatabaseInfo {
|
||||||
if (!db.tables) {
|
if (!db.tables) {
|
||||||
@@ -220,3 +228,61 @@ export function skipNamesInStructureByRegex(db: DatabaseInfo, regex: RegExp) {
|
|||||||
triggers: (db.triggers || []).filter(tbl => !regex.test(tbl.pureName)),
|
triggers: (db.triggers || []).filter(tbl => !regex.test(tbl.pureName)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function detectChangesInPreloadedRows(oldTable: TableInfo, newTable: TableInfo): boolean {
|
||||||
|
const key =
|
||||||
|
newTable.preloadedRowsKey ||
|
||||||
|
oldTable.preloadedRowsKey ||
|
||||||
|
newTable.primaryKey?.columns?.map(x => x.columnName) ||
|
||||||
|
oldTable.primaryKey?.columns?.map(x => x.columnName);
|
||||||
|
const oldRows = oldTable?.preloadedRows || [];
|
||||||
|
const newRows = newTable?.preloadedRows || [];
|
||||||
|
const insertOnly = newTable.preloadedRowsInsertOnly || oldTable.preloadedRowsInsertOnly;
|
||||||
|
|
||||||
|
if (newRows.length != oldRows.length) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const row of newRows) {
|
||||||
|
const old = oldRows?.find(r => key.every(col => r[col] == row[col]));
|
||||||
|
const rowKeys = _keys(row);
|
||||||
|
if (old) {
|
||||||
|
const updated = [];
|
||||||
|
for (const col of rowKeys) {
|
||||||
|
if (row[col] != old[col] && !insertOnly?.includes(col)) {
|
||||||
|
updated.push(col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (updated.length > 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const row of oldRows || []) {
|
||||||
|
const newr = oldRows?.find(r => key.every(col => r[col] == row[col]));
|
||||||
|
if (!newr) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removePreloadedRowsFromStructure(db: DatabaseInfo): DatabaseInfo {
|
||||||
|
if (!db) {
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...db,
|
||||||
|
tables: (db.tables || []).map(tbl => ({
|
||||||
|
...tbl,
|
||||||
|
preloadedRows: undefined,
|
||||||
|
preloadedRowsKey: undefined,
|
||||||
|
preloadedRowsInsertOnly: undefined,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user