mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-21 17:36:01 +00:00
db diff report
This commit is contained in:
@@ -22,7 +22,7 @@
|
||||
|
||||
{#if isNative}
|
||||
<select
|
||||
value={value || defaultValue}
|
||||
value={options.find(x => x.value == value) ? value : defaultValue}
|
||||
{...$$restProps}
|
||||
on:change={e => {
|
||||
dispatch('change', e.target['value']);
|
||||
|
||||
@@ -74,6 +74,7 @@
|
||||
'icon menu': 'mdi mdi-menu',
|
||||
'icon add-column': 'mdi mdi-table-column-plus-after',
|
||||
'icon add-key': 'mdi mdi-key-plus',
|
||||
'icon report': 'mdi mdi-file-chart',
|
||||
|
||||
'img ok': 'mdi mdi-check-circle color-icon-green',
|
||||
'img ok-inv': 'mdi mdi-check-circle color-icon-inv-green',
|
||||
|
||||
@@ -1,12 +1,35 @@
|
||||
<script lang="ts" context="module">
|
||||
export const matchingProps = [];
|
||||
|
||||
const getCurrentEditor = () => getActiveComponent('CompareModelTab');
|
||||
|
||||
registerCommand({
|
||||
id: 'compareModels.reportDiff',
|
||||
category: 'Compare models',
|
||||
toolbarName: 'Report',
|
||||
name: 'Report diff',
|
||||
icon: 'icon report',
|
||||
toolbar: true,
|
||||
isRelatedToTab: true,
|
||||
onClick: () => getCurrentEditor().showReport(),
|
||||
testEnabled: () => getCurrentEditor() != null,
|
||||
});
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { findEngineDriver, generateDbPairingId, getAlterTableScript, matchPairedObjects } from 'dbgate-tools';
|
||||
import {
|
||||
findEngineDriver,
|
||||
generateDbPairingId,
|
||||
getAlterTableScript,
|
||||
matchPairedObjects,
|
||||
computeDbDiffRows,
|
||||
computeTableDiffColumns,
|
||||
getCreateObjectScript,
|
||||
} from 'dbgate-tools';
|
||||
|
||||
import _ from 'lodash';
|
||||
import { derived, writable } from 'svelte/store';
|
||||
import registerCommand from '../commands/registerCommand';
|
||||
import DiffView from '../elements/DiffView.svelte';
|
||||
import ScrollableTableControl from '../elements/ScrollableTableControl.svelte';
|
||||
import TabControl from '../elements/TabControl.svelte';
|
||||
@@ -20,13 +43,17 @@
|
||||
import SqlEditor from '../query/SqlEditor.svelte';
|
||||
import useEditorData from '../query/useEditorData';
|
||||
import { extensions } from '../stores';
|
||||
import { computeDbDiffRows, computeTableDiffColumns, getCreateTableScript } from '../utility/computeDiffRows';
|
||||
import axiosInstance from '../utility/axiosInstance';
|
||||
import createActivator, { getActiveComponent } from '../utility/createActivator';
|
||||
import { useConnectionInfo, useDatabaseInfo } from '../utility/metadataLoaders';
|
||||
import resolveApi from '../utility/resolveApi';
|
||||
|
||||
export let tabid;
|
||||
|
||||
let pairIndex = 0;
|
||||
|
||||
export const activator = createActivator('CompareModelTab', true);
|
||||
|
||||
// let values = writable({
|
||||
// sourceConid: null,
|
||||
// sourceDatabase: null,
|
||||
@@ -64,6 +91,24 @@
|
||||
driver
|
||||
).sql;
|
||||
|
||||
export async function showReport() {
|
||||
const resp = await axiosInstance.post('database-connections/generate-db-diff-report', {
|
||||
sourceConid: $values?.sourceConid,
|
||||
sourceDatabase: $values?.sourceDatabase,
|
||||
targetConid: $values?.targetConid,
|
||||
targetDatabase: $values?.targetDatabase,
|
||||
});
|
||||
|
||||
window.open(`${resolveApi()}/uploads/get?file=${resp.data}`, '_blank');
|
||||
|
||||
// window.open(
|
||||
// `${resolveApi()}/database-connections/get-diff-html?sourceConid=` +
|
||||
// `${$values?.sourceConid}&sourceDatabase=${$values?.sourceDatabase}&` +
|
||||
// `targetConid=${$values?.targetConid}&targetDatabase=${$values?.targetDatabase}`,
|
||||
// '_blank'
|
||||
// );
|
||||
}
|
||||
|
||||
const { editorState, editorValue, setEditorData } = useEditorData({
|
||||
tabid,
|
||||
// onInitialData: value => {
|
||||
@@ -162,8 +207,8 @@
|
||||
<DiffView
|
||||
leftTitle={diffRows[pairIndex]?.source?.pureName}
|
||||
rightTitle={diffRows[pairIndex]?.source?.pureName}
|
||||
leftText={getCreateTableScript(diffRows[pairIndex]?.source, driver)}
|
||||
rightText={getCreateTableScript(diffRows[pairIndex]?.target, driver)}
|
||||
leftText={getCreateObjectScript(diffRows[pairIndex]?.source, driver)}
|
||||
rightText={getCreateObjectScript(diffRows[pairIndex]?.target, driver)}
|
||||
/>
|
||||
</svelte:fragment>
|
||||
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
import { DbDiffOptions, testEqualColumns, testEqualTables } from 'dbgate-tools';
|
||||
import { DatabaseInfo, EngineDriver, TableInfo } from 'dbgate-types';
|
||||
|
||||
export function computeDiffRowsCore(sourceList, targetList, testEqual) {
|
||||
const res = [];
|
||||
for (const obj of sourceList) {
|
||||
const paired = targetList.find(x => x.pairingId == obj.pairingId);
|
||||
if (paired) {
|
||||
res.push({
|
||||
source: obj,
|
||||
target: paired,
|
||||
state: testEqual(obj, paired) ? 'equal' : 'changed',
|
||||
});
|
||||
} else {
|
||||
res.push({
|
||||
source: obj,
|
||||
state: 'removed',
|
||||
});
|
||||
}
|
||||
}
|
||||
for (const obj of targetList) {
|
||||
const paired = sourceList.find(x => x.pairingId == obj.pairingId);
|
||||
if (!paired) {
|
||||
res.push({
|
||||
target: obj,
|
||||
state: 'added',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function computeDbDiffRows(
|
||||
sourceDb: DatabaseInfo,
|
||||
targetDb: DatabaseInfo,
|
||||
opts: DbDiffOptions,
|
||||
driver: EngineDriver
|
||||
) {
|
||||
if (!sourceDb || !targetDb || !driver) return [];
|
||||
return computeDiffRowsCore(sourceDb.tables, targetDb.tables, (a, b) =>
|
||||
testEqualTables(a, b, opts, targetDb, driver)
|
||||
).map(row => ({
|
||||
...row,
|
||||
sourceSchemaName: row?.source?.schemaName,
|
||||
sourcePureName: row?.source?.pureName,
|
||||
targetSchemaName: row?.target?.schemaName,
|
||||
targetPureName: row?.target?.pureName,
|
||||
}));
|
||||
}
|
||||
|
||||
export function computeTableDiffColumns(
|
||||
sourceTable: TableInfo,
|
||||
targetTable: TableInfo,
|
||||
opts: DbDiffOptions,
|
||||
driver: EngineDriver
|
||||
) {
|
||||
if (!driver) return [];
|
||||
return computeDiffRowsCore(sourceTable?.columns || [], targetTable?.columns || [], (a, b) =>
|
||||
testEqualColumns(a, b, true, true, opts)
|
||||
).map(row => ({
|
||||
...row,
|
||||
sourceColumnName: row?.source?.columnName,
|
||||
targetColumnName: row?.target?.columnName,
|
||||
sourceDataType: row?.source?.dataType,
|
||||
targetDataType: row?.target?.dataType,
|
||||
sourceNotNull: row?.source?.notNull,
|
||||
targetNotNull: row?.target?.notNull,
|
||||
}));
|
||||
}
|
||||
|
||||
export function getCreateTableScript(table: TableInfo, driver: EngineDriver) {
|
||||
if (!table || !driver) return '';
|
||||
const dmp = driver.createDumper();
|
||||
dmp.createTable(table);
|
||||
return dmp.s;
|
||||
}
|
||||
Reference in New Issue
Block a user