preloaded rows fixes

This commit is contained in:
SPRINX0\prochazka
2024-11-11 13:05:19 +01:00
parent 75b4f49e31
commit fa72d9a39f
2 changed files with 80 additions and 5 deletions

View File

@@ -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 _uniq from 'lodash/uniq';
import _keys from 'lodash/keys';
export function addTableDependencies(db: DatabaseInfo): DatabaseInfo {
if (!db.tables) {
@@ -220,3 +228,61 @@ export function skipNamesInStructureByRegex(db: DatabaseInfo, regex: RegExp) {
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,
})),
};
}