columns diff

This commit is contained in:
Jan Prochazka
2021-10-24 21:43:37 +02:00
parent 819fdac8ab
commit b474bd109b
2 changed files with 43 additions and 17 deletions

View File

@@ -1,21 +1,15 @@
import { DbDiffOptions, testEqualTables } from 'dbgate-tools';
import { DatabaseInfo, EngineDriver } from 'dbgate-types';
import { DbDiffOptions, testEqualColumns, testEqualTables } from 'dbgate-tools';
import { DatabaseInfo, EngineDriver, TableInfo } from 'dbgate-types';
export function computeDiffRows(
sourceDb: DatabaseInfo,
targetDb: DatabaseInfo,
opts: DbDiffOptions,
driver: EngineDriver
) {
if (!sourceDb || !targetDb || !driver) return [];
export function computeDiffRowsCore(sourceList, targetList, testEqual) {
const res = [];
for (const obj of sourceDb.tables) {
const paired = targetDb.tables.find(x => x.pairingId == obj.pairingId);
for (const obj of sourceList) {
const paired = targetList.find(x => x.pairingId == obj.pairingId);
if (paired) {
res.push({
source: obj,
target: paired,
state: testEqualTables(obj, paired, opts, targetDb, driver) ? 'equal' : 'changed',
state: testEqual(obj, paired) ? 'equal' : 'changed',
});
} else {
res.push({
@@ -24,8 +18,8 @@ export function computeDiffRows(
});
}
}
for (const obj of targetDb.tables) {
const paired = sourceDb.tables.find(x => x.pairingId == obj.pairingId);
for (const obj of targetList) {
const paired = sourceList.find(x => x.pairingId == obj.pairingId);
if (!paired) {
res.push({
target: obj,
@@ -34,7 +28,19 @@ export function computeDiffRows(
}
}
return res.map(row => ({
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,
@@ -42,3 +48,23 @@ export function computeDiffRows(
targetPureName: row?.target?.pureName,
}));
}
export function computeTableDiffColumns(
sourceTable: TableInfo,
targetTable: TableInfo,
opts: DbDiffOptions,
driver: EngineDriver
) {
if (!sourceTable || !sourceTable || !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,
}));
}