Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

fix: add areas width and height + table width to diff check (#931)

Guy Ben-Aharon98f6edd5c8a8
diff --git a/src/lib/domain/diff/area-diff.ts b/src/lib/domain/diff/area-diff.ts
index d4685de0..02bea1dc 100644
--- a/src/lib/domain/diff/area-diff.ts
+++ b/src/lib/domain/diff/area-diff.ts
@@ -1,13 +1,18 @@
import { z } from 'zod';
import type { Area } from '../area';
-export type AreaDiffAttribute = keyof Pick<Area, 'name' | 'color' | 'x' | 'y'>;
+export type AreaDiffAttribute = keyof Pick<
+ Area,
+ 'name' | 'color' | 'x' | 'y' | 'width' | 'height'
+>;
const areaDiffAttributeSchema: z.ZodType<AreaDiffAttribute> = z.union([
z.literal('name'),
z.literal('color'),
z.literal('x'),
z.literal('y'),
+ z.literal('width'),
+ z.literal('height'),
]);
export interface AreaDiffChanged {
diff --git a/src/lib/domain/diff/diff-check/__tests__/diff-check.test.ts b/src/lib/domain/diff/diff-check/__tests__/diff-check.test.ts
index b0b3521a..b161b27e 100644
--- a/src/lib/domain/diff/diff-check/__tests__/diff-check.test.ts
+++ b/src/lib/domain/diff/diff-check/__tests__/diff-check.test.ts
@@ -9,6 +9,7 @@ import type { Area } from '@/lib/domain/area';
import { DatabaseType } from '@/lib/domain/database-type';
import type { TableDiffChanged } from '../../table-diff';
import type { FieldDiffChanged } from '../../field-diff';
+import type { AreaDiffChanged } from '../../area-diff';
// Helper function to create a mock diagram
function createMockDiagram(overrides?: Partial<Diagram>): Diagram {
@@ -160,6 +161,62 @@ describe('generateDiff', () => {
expect(result.diffMap.has('table-x-table-1')).toBe(true);
expect(result.diffMap.has('table-y-table-1')).toBe(true);
});
+
+ it('should detect table width changes', () => {
+ const oldDiagram = createMockDiagram({
+ tables: [createMockTable({ width: 150 })],
+ });
+ const newDiagram = createMockDiagram({
+ tables: [createMockTable({ width: 250 })],
+ });
+
+ const result = generateDiff({
+ diagram: oldDiagram,
+ newDiagram,
+ options: {
+ attributes: {
+ tables: ['width'],
+ },
+ },
+ });
+
+ expect(result.diffMap.size).toBe(1);
+ const diff = result.diffMap.get('table-width-table-1');
+ expect(diff).toBeDefined();
+ expect(diff?.type).toBe('changed');
+ expect((diff as TableDiffChanged)?.attribute).toBe('width');
+ expect((diff as TableDiffChanged)?.oldValue).toBe(150);
+ expect((diff as TableDiffChanged)?.newValue).toBe(250);
+ });
+
+ it('should detect multiple table dimension changes', () => {
+ const oldDiagram = createMockDiagram({
+ tables: [createMockTable({ x: 0, y: 0, width: 100 })],
+ });
+ const newDiagram = createMockDiagram({
+ tables: [createMockTable({ x: 50, y: 75, width: 200 })],
+ });
+
+ const result = generateDiff({
+ diagram: oldDiagram,
+ newDiagram,
+ options: {
+ attributes: {
+ tables: ['x', 'y', 'width'],
+ },
+ },
+ });
+
+ expect(result.diffMap.size).toBe(3);
+ expect(result.diffMap.has('table-x-table-1')).toBe(true);
+ expect(result.diffMap.has('table-y-table-1')).toBe(true);
+ expect(result.diffMap.has('table-width-table-1')).toBe(true);
+
+ const widthDiff = result.diffMap.get('table-width-table-1');
+ expect(widthDiff?.type).toBe('changed');
+ expect((widthDiff as TableDiffChanged)?.oldValue).toBe(100);
+ expect((widthDiff as TableDiffChanged)?.newValue).toBe(200);
+ });
});
describe('Field Diffing', () => {
@@ -321,6 +378,92 @@ describe('generateDiff', () => {
expect(result.diffMap.size).toBe(0);
});
+
+ it('should detect area width changes', () => {
+ const oldDiagram = createMockDiagram({
+ areas: [createMockArea({ width: 100 })],
+ });
+ const newDiagram = createMockDiagram({
+ areas: [createMockArea({ width: 200 })],
+ });
+
+ const result = generateDiff({
+ diagram: oldDiagram,
+ newDiagram,
+ options: {
+ includeAreas: true,
+ attributes: {
+ areas: ['width'],
+ },
+ },
+ });
+
+ expect(result.diffMap.size).toBe(1);
+ const diff = result.diffMap.get('area-width-area-1');
+ expect(diff).toBeDefined();
+ expect(diff?.type).toBe('changed');
+ expect((diff as AreaDiffChanged)?.attribute).toBe('width');
+ expect((diff as AreaDiffChanged)?.oldValue).toBe(100);
+ expect((diff as AreaDiffChanged)?.newValue).toBe(200);
+ });
+
+ it('should detect area height changes', () => {
+ const oldDiagram = createMockDiagram({
+ areas: [createMockArea({ height: 100 })],
+ });
+ const newDiagram = createMockDiagram({
+ areas: [createMockArea({ height: 300 })],
+ });
+
+ const result = generateDiff({
+ diagram: oldDiagram,
+ newDiagram,
+ options: {
+ includeAreas: true,
+ attributes: {
+ areas: ['height'],
+ },
+ },
+ });
+
+ expect(result.diffMap.size).toBe(1);
+ const diff = result.diffMap.get('area-height-area-1');
+ expect(diff).toBeDefined();
+ expect(diff?.type).toBe('changed');
+ expect((diff as AreaDiffChanged)?.attribute).toBe('height');
+ expect((diff as AreaDiffChanged)?.oldValue).toBe(100);
+ expect((diff as AreaDiffChanged)?.newValue).toBe(300);
+ });
+
+ it('should detect multiple area dimension changes', () => {
+ const oldDiagram = createMockDiagram({
+ areas: [
+ createMockArea({ x: 0, y: 0, width: 100, height: 100 }),
+ ],
+ });
+ const newDiagram = createMockDiagram({
+ areas: [
+ createMockArea({ x: 50, y: 50, width: 200, height: 300 }),
+ ],
+ });
+
+ const result = generateDiff({
+ diagram: oldDiagram,
+ newDiagram,
+ options: {
+ includeAreas: true,
+ attributes: {
+ areas: ['x', 'y', 'width', 'height'],
+ },
+ },
+ });
+
+ expect(result.diffMap.size).toBe(4);
+ expect(result.diffMap.has('area-x-area-1')).toBe(true);
+ expect(result.diffMap.has('area-y-area-1')).toBe(true);
+ expect(result.diffMap.has('area-width-area-1')).toBe(true);
+ expect(result.diffMap.has('area-height-area-1')).toBe(true);
+ });
});
describe('Custom Matchers', () => {
@@ -565,6 +708,84 @@ describe('generateDiff', () => {
});
describe('Complex Scenarios', () => {
+ it('should detect all dimensional changes for tables and areas', () => {
+ const oldDiagram = createMockDiagram({
+ tables: [
+ createMockTable({
+ id: 'table-1',
+ x: 0,
+ y: 0,
+ width: 100,
+ }),
+ ],
+ areas: [
+ createMockArea({
+ id: 'area-1',
+ x: 0,
+ y: 0,
+ width: 200,
+ height: 150,
+ }),
+ ],
+ });
+
+ const newDiagram = createMockDiagram({
+ tables: [
+ createMockTable({
+ id: 'table-1',
+ x: 10,
+ y: 20,
+ width: 120,
+ }),
+ ],
+ areas: [
+ createMockArea({
+ id: 'area-1',
+ x: 25,
+ y: 35,
+ width: 250,
+ height: 175,
+ }),
+ ],
+ });
+
+ const result = generateDiff({
+ diagram: oldDiagram,
+ newDiagram,
+ options: {
+ includeAreas: true,
+ attributes: {
+ tables: ['x', 'y', 'width'],
+ areas: ['x', 'y', 'width', 'height'],
+ },
+ },
+ });
+
+ // Table dimensional changes
+ expect(result.diffMap.has('table-x-table-1')).toBe(true);
+ expect(result.diffMap.has('table-y-table-1')).toBe(true);
+ expect(result.diffMap.has('table-width-table-1')).toBe(true);
+
+ // Area dimensional changes
+ expect(result.diffMap.has('area-x-area-1')).toBe(true);
+ expect(result.diffMap.has('area-y-area-1')).toBe(true);
+ expect(result.diffMap.has('area-width-area-1')).toBe(true);
+ expect(result.diffMap.has('area-height-area-1')).toBe(true);
+
+ // Verify the correct values
+ const tableWidthDiff = result.diffMap.get('table-width-table-1');
+ expect((tableWidthDiff as TableDiffChanged)?.oldValue).toBe(100);
+ expect((tableWidthDiff as TableDiffChanged)?.newValue).toBe(120);
+
+ const areaWidthDiff = result.diffMap.get('area-width-area-1');
+ expect((areaWidthDiff as AreaDiffChanged)?.oldValue).toBe(200);
+ expect((areaWidthDiff as AreaDiffChanged)?.newValue).toBe(250);
+
+ const areaHeightDiff = result.diffMap.get('area-height-area-1');
+ expect((areaHeightDiff as AreaDiffChanged)?.oldValue).toBe(150);
+ expect((areaHeightDiff as AreaDiffChanged)?.newValue).toBe(175);
+ });
+
it('should handle multiple simultaneous changes', () => {
const oldDiagram = createMockDiagram({
tables: [
diff --git a/src/lib/domain/diff/diff-check/diff-check.ts b/src/lib/domain/diff/diff-check/diff-check.ts
index a530178f..62c949e4 100644
--- a/src/lib/domain/diff/diff-check/diff-check.ts
+++ b/src/lib/domain/diff/diff-check/diff-check.ts
@@ -342,6 +342,29 @@ function compareTables({
changedTables.set(oldTable.id, true);
}
+
+ if (
+ attributesToCheck.includes('width') &&
+ oldTable.width !== newTable.width
+ ) {
+ diffMap.set(
+ getDiffMapKey({
+ diffObject: 'table',
+ objectId: oldTable.id,
+ attribute: 'width',
+ }),
+ {
+ object: 'table',
+ type: 'changed',
+ tableId: oldTable.id,
+ attribute: 'width',
+ newValue: newTable.width,
+ oldValue: oldTable.width,
+ }
+ );
+
+ changedTables.set(oldTable.id, true);
+ }
}
}
}
@@ -918,6 +941,50 @@ function compareAreas({
);
changedAreas.set(oldArea.id, true);
}
+
+ if (
+ attributesToCheck.includes('width') &&
+ oldArea.width !== newArea.width
+ ) {
+ diffMap.set(
+ getDiffMapKey({
+ diffObject: 'area',
+ objectId: oldArea.id,
+ attribute: 'width',
+ }),
+ {
+ object: 'area',
+ type: 'changed',
+ areaId: oldArea.id,
+ attribute: 'width',
+ newValue: newArea.width,
+ oldValue: oldArea.width,
+ }
+ );
+ changedAreas.set(oldArea.id, true);
+ }
+
+ if (
+ attributesToCheck.includes('height') &&
+ oldArea.height !== newArea.height
+ ) {
+ diffMap.set(
+ getDiffMapKey({
+ diffObject: 'area',
+ objectId: oldArea.id,
+ attribute: 'height',
+ }),
+ {
+ object: 'area',
+ type: 'changed',
+ areaId: oldArea.id,
+ attribute: 'height',
+ newValue: newArea.height,
+ oldValue: oldArea.height,
+ }
+ );
+ changedAreas.set(oldArea.id, true);
+ }
}
}
}
diff --git a/src/lib/domain/diff/table-diff.ts b/src/lib/domain/diff/table-diff.ts
index 7a8f1363..faf22506 100644
--- a/src/lib/domain/diff/table-diff.ts
+++ b/src/lib/domain/diff/table-diff.ts
@@ -3,7 +3,7 @@ import type { DBTable } from '../db-table';
export type TableDiffAttribute = keyof Pick<
DBTable,
- 'name' | 'comments' | 'color' | 'x' | 'y'
+ 'name' | 'comments' | 'color' | 'x' | 'y' | 'width'
>;
const tableDiffAttributeSchema: z.ZodType<TableDiffAttribute> = z.union([
@@ -12,6 +12,7 @@ const tableDiffAttributeSchema: z.ZodType<TableDiffAttribute> = z.union([
z.literal('color'),
z.literal('x'),
z.literal('y'),
+ z.literal('width'),
]);
export interface TableDiffChanged {