save structure changes to jsonl file

This commit is contained in:
Jan Prochazka
2023-02-16 13:33:28 +01:00
parent edf0637a35
commit 3114a05c3b
4 changed files with 65 additions and 4 deletions

View File

@@ -2,7 +2,7 @@ const fs = require('fs');
const _ = require('lodash'); const _ = require('lodash');
const stream = require('stream'); const stream = require('stream');
const byline = require('byline'); const byline = require('byline');
const { getLogger } = require('dbgate-tools'); const { getLogger, processJsonDataUpdateCommands, removeTablePairingId } = require('dbgate-tools');
const logger = getLogger('modifyJsonLinesReader'); const logger = getLogger('modifyJsonLinesReader');
const stableStringify = require('json-stable-stringify'); const stableStringify = require('json-stable-stringify');
@@ -11,6 +11,7 @@ class ParseStream extends stream.Transform {
super({ objectMode: true }); super({ objectMode: true });
this.limitRows = limitRows; this.limitRows = limitRows;
this.changeSet = changeSet; this.changeSet = changeSet;
this.wasHeader = false;
this.currentRowIndex = 0; this.currentRowIndex = 0;
if (mergeMode == 'merge') { if (mergeMode == 'merge') {
if (mergedRows && mergeKey) { if (mergedRows && mergeKey) {
@@ -28,12 +29,28 @@ class ParseStream extends stream.Transform {
_transform(chunk, encoding, done) { _transform(chunk, encoding, done) {
let obj = JSON.parse(chunk); let obj = JSON.parse(chunk);
if (obj.__isStreamHeader) { if (obj.__isStreamHeader) {
if (this.changeSet && this.changeSet.structure) {
this.push({
...removeTablePairingId(this.changeSet.structure),
__isStreamHeader: true,
});
} else {
this.push(obj); this.push(obj);
}
this.wasHeader = true;
done(); done();
return; return;
} }
if (this.changeSet) { if (this.changeSet) {
if (!this.wasHeader && this.changeSet.structure) {
this.push({
...removeTablePairingId(this.changeSet.structure),
__isStreamHeader: true,
});
this.wasHeader = true;
}
if (!this.limitRows || this.currentRowIndex < this.limitRows) { if (!this.limitRows || this.currentRowIndex < this.limitRows) {
if (this.changeSet.deletes.find(x => x.existingRowIndex == this.currentRowIndex)) { if (this.changeSet.deletes.find(x => x.existingRowIndex == this.currentRowIndex)) {
obj = null; obj = null;
@@ -48,6 +65,9 @@ class ParseStream extends stream.Transform {
} }
if (obj) { if (obj) {
if (this.changeSet.dataUpdateCommands) {
obj = processJsonDataUpdateCommands(obj, this.changeSet.dataUpdateCommands);
}
this.push(obj); this.push(obj);
} }
this.currentRowIndex += 1; this.currentRowIndex += 1;

View File

@@ -72,6 +72,34 @@ export function generateTablePairingId(table: TableInfo): TableInfo {
return table; return table;
} }
export function removeTablePairingId(table: TableInfo): TableInfo {
if (!table) return table;
return {
...table,
columns: table.columns?.map(col => ({
...col,
pairingId: undefined,
})),
foreignKeys: table.foreignKeys?.map(cnt => ({
...cnt,
pairingId: undefined,
})),
checks: table.checks?.map(cnt => ({
...cnt,
pairingId: undefined,
})),
indexes: table.indexes?.map(cnt => ({
...cnt,
pairingId: undefined,
})),
uniques: table.uniques?.map(cnt => ({
...cnt,
pairingId: undefined,
})),
pairingId: undefined,
};
}
function generateObjectPairingId(obj) { function generateObjectPairingId(obj) {
if (obj.objectTypeField) if (obj.objectTypeField)
return { return {
@@ -346,7 +374,13 @@ 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) {
plan.fillPreloadedRows(newTable, oldTable?.preloadedRows, newTable.preloadedRows, key, newTable.preloadedRowsInsertOnly); plan.fillPreloadedRows(
newTable,
oldTable?.preloadedRows,
newTable.preloadedRows,
key,
newTable.preloadedRowsInsertOnly
);
} }
} }

View File

@@ -18,11 +18,12 @@
export let dispatchChangeSet = null; export let dispatchChangeSet = null;
export let allowChangeChangeSetStructure = false; export let allowChangeChangeSetStructure = false;
export let infoLoadCounter = 0;
let loadedRows; let loadedRows;
let infoCounter = 0; let infoCounter = 0;
$: info = useApiCall('jsldata/get-info', { jslid, infoCounter }, {}); $: info = useApiCall('jsldata/get-info', { jslid, infoCounter, infoLoadCounter }, {});
// $: columns = ($info && $info.columns) || []; // $: columns = ($info && $info.columns) || [];
const config = writable(createGridConfig()); const config = writable(createGridConfig());

View File

@@ -42,6 +42,7 @@
export let jslid = undefined; export let jslid = undefined;
export let tabid; export let tabid;
let infoLoadCounter = 0;
const quickExportHandlerRef = createQuickExportHandlerRef(); const quickExportHandlerRef = createQuickExportHandlerRef();
@@ -73,7 +74,11 @@
file: archiveFile, file: archiveFile,
changeSet: $changeSetStore.value, changeSet: $changeSetStore.value,
}); });
const structureChanged = !!$changeSetStore.value?.structure;
dispatchChangeSet({ type: 'reset', value: createChangeSet() }); dispatchChangeSet({ type: 'reset', value: createChangeSet() });
if (structureChanged) {
infoLoadCounter += 1;
}
await tick(); await tick();
runCommand('dataGrid.refresh'); runCommand('dataGrid.refresh');
} }
@@ -92,6 +97,7 @@
focusOnVisible focusOnVisible
{changeSetStore} {changeSetStore}
{dispatchChangeSet} {dispatchChangeSet}
{infoLoadCounter}
/> />
<svelte:fragment slot="toolstrip"> <svelte:fragment slot="toolstrip">
<ToolStripCommandButton command="dataGrid.refresh" /> <ToolStripCommandButton command="dataGrid.refresh" />