Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
update diff types (#636)
Guy Ben-Aharon874aa5ab7531
diff --git a/src/context/diff-context/diff-check/diff-check.ts b/src/context/diff-context/diff-check/diff-check.tsindex ccf0bc0c..463386f2 100644--- a/src/context/diff-context/diff-check/diff-check.ts+++ b/src/context/diff-context/diff-check/diff-check.ts@@ -1,12 +1,8 @@import type { Diagram } from '@/lib/domain/diagram';-import type {- ChartDBDiff,- DiffMap,- DiffObject,- FieldDiffAttribute,-} from '../types';import type { DBField } from '@/lib/domain/db-field';import type { DBIndex } from '@/lib/domain/db-index';+import type { ChartDBDiff, DiffMap, DiffObject } from '@/lib/domain/diff/diff';+import type { FieldDiffAttribute } from '@/lib/domain/diff/field-diff';export function getDiffMapKey({diffObject,@@ -78,7 +74,7 @@ function compareTables({{object: 'table',type: 'added',- tableId: newTable.id,+ tableAdded: newTable,});changedTables.set(newTable.id, true);@@ -221,7 +217,7 @@ function compareFields({{object: 'field',type: 'added',- fieldId: newField.id,+ newField,tableId,});@@ -327,8 +323,8 @@ function compareFieldProperties({fieldId: oldField.id,tableId,attribute,- oldValue: oldField[attribute],- newValue: newField[attribute],+ oldValue: oldField[attribute] ?? '',+ newValue: newField[attribute] ?? '',});}@@ -362,7 +358,7 @@ function compareIndexes({{object: 'index',type: 'added',- indexId: newIndex.id,+ newIndex,tableId,});@@ -414,7 +410,7 @@ function compareRelationships({{object: 'relationship',type: 'added',- relationshipId: newRelationship.id,+ newRelationship,});}diff --git a/src/context/diff-context/diff-context.tsx b/src/context/diff-context/diff-context.tsxindex 497962e2..43fd24d8 100644--- a/src/context/diff-context/diff-context.tsx+++ b/src/context/diff-context/diff-context.tsx@@ -1,11 +1,11 @@import { createContext } from 'react';-import type { DiffMap } from './types';import type { Diagram } from '@/lib/domain/diagram';import type { DBTable } from '@/lib/domain/db-table';import type { EventEmitter } from 'ahooks/lib/useEventEmitter';import type { DBField } from '@/lib/domain/db-field';import type { DataType } from '@/lib/data/data-types/data-types';import type { DBRelationship } from '@/lib/domain/db-relationship';+import type { DiffMap } from '@/lib/domain/diff/diff';export type DiffEventType = 'diff_calculated';diff --git a/src/context/diff-context/diff-provider.tsx b/src/context/diff-context/diff-provider.tsxindex 049d72c2..50361f1e 100644--- a/src/context/diff-context/diff-provider.tsx+++ b/src/context/diff-context/diff-provider.tsx@@ -5,13 +5,14 @@ import type {DiffEvent,} from './diff-context';import { diffContext } from './diff-context';-import type { ChartDBDiff, DiffMap } from './types';+import { generateDiff, getDiffMapKey } from './diff-check/diff-check';import type { Diagram } from '@/lib/domain/diagram';import { useEventEmitter } from 'ahooks';import type { DBField } from '@/lib/domain/db-field';import type { DataType } from '@/lib/data/data-types/data-types';import type { DBRelationship } from '@/lib/domain/db-relationship';+import type { ChartDBDiff, DiffMap } from '@/lib/domain/diff/diff';export const DiffProvider: React.FC<React.PropsWithChildren> = ({children,@@ -45,7 +46,7 @@ export const DiffProvider: React.FC<React.PropsWithChildren> = ({if (diff.object === 'field' && diff.type === 'added') {const field = newDiagram?.tables?.find((table) => table.id === diff.tableId)- ?.fields.find((f) => f.id === diff.fieldId);+ ?.fields.find((f) => f.id === diff.newField.id);if (field) {newFieldsMap.set(diff.tableId, [@@ -73,7 +74,7 @@ export const DiffProvider: React.FC<React.PropsWithChildren> = ({diffMap.forEach((diff) => {if (diff.object === 'relationship' && diff.type === 'added') {const relationship = newDiagram?.relationships?.find(- (rel) => rel.id === diff.relationshipId+ (rel) => rel.id === diff.newRelationship.id);if (relationship) {diff --git a/src/context/diff-context/types.ts b/src/context/diff-context/types.tsdeleted file mode 100644index 7c63d9cf..00000000--- a/src/context/diff-context/types.ts+++ /dev/null@@ -1,53 +0,0 @@-import type { DataType } from '@/lib/data/data-types/data-types';--export type TableDiffAttribute = 'name' | 'comments';--export interface TableDiff {- object: 'table';- type: 'added' | 'removed' | 'changed';- tableId: string;- attribute?: TableDiffAttribute;- oldValue?: string;- newValue?: string;-}--export interface RelationshipDiff {- object: 'relationship';- type: 'added' | 'removed';- relationshipId: string;-}--export type FieldDiffAttribute =- | 'name'- | 'type'- | 'primaryKey'- | 'unique'- | 'nullable'- | 'comments';--export interface FieldDiff {- object: 'field';- type: 'added' | 'removed' | 'changed';- fieldId: string;- tableId: string;- attribute?: FieldDiffAttribute;- oldValue?: string | boolean | DataType;- newValue?: string | boolean | DataType;-}--export interface IndexDiff {- object: 'index';- type: 'added' | 'removed';- indexId: string;- tableId: string;-}--export type ChartDBDiff = TableDiff | FieldDiff | IndexDiff | RelationshipDiff;--export type DiffMap = Map<string, ChartDBDiff>;--export type DiffObject =- | TableDiff['object']- | FieldDiff['object']- | IndexDiff['object']- | RelationshipDiff['object'];diff --git a/src/lib/domain/diff/diff.ts b/src/lib/domain/diff/diff.tsnew file mode 100644index 00000000..2d158fd5--- /dev/null+++ b/src/lib/domain/diff/diff.ts@@ -0,0 +1,27 @@+import { z } from 'zod';++import type { FieldDiff } from './field-diff';+import { fieldDiffSchema } from './field-diff';+import type { IndexDiff } from './index-diff';+import { indexDiffSchema } from './index-diff';+import type { RelationshipDiff } from './relationship-diff';+import { relationshipDiffSchema } from './relationship-diff';+import type { TableDiff } from './table-diff';+import { tableDiffSchema } from './table-diff';++export type ChartDBDiff = TableDiff | FieldDiff | IndexDiff | RelationshipDiff;++export const chartDBDiffSchema: z.ZodType<ChartDBDiff> = z.union([+ tableDiffSchema,+ fieldDiffSchema,+ indexDiffSchema,+ relationshipDiffSchema,+]);++export type DiffMap = Map<string, ChartDBDiff>;++export type DiffObject =+ | TableDiff['object']+ | FieldDiff['object']+ | IndexDiff['object']+ | RelationshipDiff['object'];diff --git a/src/lib/domain/diff/field-diff.ts b/src/lib/domain/diff/field-diff.tsnew file mode 100644index 00000000..ba217e52--- /dev/null+++ b/src/lib/domain/diff/field-diff.ts@@ -0,0 +1,81 @@+import { z } from 'zod';++import {+ type DataType,+ dataTypeSchema,+} from '@/lib/data/data-types/data-types';+import type { DBField } from '../db-field';+import { dbFieldSchema } from '../db-field';++export type FieldDiffAttribute =+ | 'name'+ | 'type'+ | 'primaryKey'+ | 'unique'+ | 'nullable'+ | 'comments';++export const fieldDiffAttributeSchema: z.ZodType<FieldDiffAttribute> = z.union([+ z.literal('name'),+ z.literal('type'),+ z.literal('primaryKey'),+ z.literal('unique'),+ z.literal('nullable'),+ z.literal('comments'),+]);++export interface FieldDiffAdded {+ object: 'field';+ type: 'added';+ tableId: string;+ newField: DBField;+}++export const fieldDiffAddedSchema: z.ZodType<FieldDiffAdded> = z.object({+ object: z.literal('field'),+ type: z.literal('added'),+ tableId: z.string(),+ newField: dbFieldSchema,+});++export interface FieldDiffRemoved {+ object: 'field';+ type: 'removed';+ fieldId: string;+ tableId: string;+}++export const fieldDiffRemovedSchema: z.ZodType<FieldDiffRemoved> = z.object({+ object: z.literal('field'),+ type: z.literal('removed'),+ fieldId: z.string(),+ tableId: z.string(),+});++export interface FieldDiffChanged {+ object: 'field';+ type: 'changed';+ fieldId: string;+ tableId: string;+ attribute: FieldDiffAttribute;+ oldValue: string | boolean | DataType;+ newValue: string | boolean | DataType;+}++export const fieldDiffChangedSchema: z.ZodType<FieldDiffChanged> = z.object({+ object: z.literal('field'),+ type: z.literal('changed'),+ fieldId: z.string(),+ tableId: z.string(),+ attribute: fieldDiffAttributeSchema,+ oldValue: z.union([z.string(), z.boolean(), dataTypeSchema]),+ newValue: z.union([z.string(), z.boolean(), dataTypeSchema]),+});++export type FieldDiff = FieldDiffAdded | FieldDiffRemoved | FieldDiffChanged;++export const fieldDiffSchema: z.ZodType<FieldDiff> = z.union([+ fieldDiffRemovedSchema,+ fieldDiffChangedSchema,+ fieldDiffAddedSchema,+]);diff --git a/src/lib/domain/diff/index-diff.ts b/src/lib/domain/diff/index-diff.tsnew file mode 100644index 00000000..4c398828--- /dev/null+++ b/src/lib/domain/diff/index-diff.ts@@ -0,0 +1,38 @@+import { z } from 'zod';+import type { DBIndex } from '../db-index';+import { dbIndexSchema } from '../db-index';++export interface IndexDiffAdded {+ object: 'index';+ type: 'added';+ tableId: string;+ newIndex: DBIndex;+}++export const indexDiffAddedSchema: z.ZodType<IndexDiffAdded> = z.object({+ object: z.literal('index'),+ type: z.literal('added'),+ tableId: z.string(),+ newIndex: dbIndexSchema,+});++export interface IndexDiffRemoved {+ object: 'index';+ type: 'removed';+ indexId: string;+ tableId: string;+}++export const indexDiffRemovedSchema: z.ZodType<IndexDiffRemoved> = z.object({+ object: z.literal('index'),+ type: z.literal('removed'),+ indexId: z.string(),+ tableId: z.string(),+});++export type IndexDiff = IndexDiffAdded | IndexDiffRemoved;++export const indexDiffSchema: z.ZodType<IndexDiff> = z.union([+ indexDiffAddedSchema,+ indexDiffRemovedSchema,+]);diff --git a/src/lib/domain/diff/relationship-diff.ts b/src/lib/domain/diff/relationship-diff.tsnew file mode 100644index 00000000..d342fbdb--- /dev/null+++ b/src/lib/domain/diff/relationship-diff.ts@@ -0,0 +1,36 @@+import { z } from 'zod';+import type { DBRelationship } from '../db-relationship';+import { dbRelationshipSchema } from '../db-relationship';++export interface RelationshipDiffAdded {+ object: 'relationship';+ type: 'added';+ newRelationship: DBRelationship;+}++export const relationshipDiffAddedSchema: z.ZodType<RelationshipDiffAdded> =+ z.object({+ object: z.literal('relationship'),+ type: z.literal('added'),+ newRelationship: dbRelationshipSchema,+ });++export interface RelationshipDiffRemoved {+ object: 'relationship';+ type: 'removed';+ relationshipId: string;+}++export const relationshipDiffRemovedSchema: z.ZodType<RelationshipDiffRemoved> =+ z.object({+ object: z.literal('relationship'),+ type: z.literal('removed'),+ relationshipId: z.string(),+ });++export type RelationshipDiff = RelationshipDiffAdded | RelationshipDiffRemoved;++export const relationshipDiffSchema: z.ZodType<RelationshipDiff> = z.union([+ relationshipDiffAddedSchema,+ relationshipDiffRemovedSchema,+]);diff --git a/src/lib/domain/diff/table-diff.ts b/src/lib/domain/diff/table-diff.tsnew file mode 100644index 00000000..d325b5d4--- /dev/null+++ b/src/lib/domain/diff/table-diff.ts@@ -0,0 +1,60 @@+import { z } from 'zod';+import type { DBTable } from '../db-table';+import { dbTableSchema } from '../db-table';++export type TableDiffAttribute = 'name' | 'comments';++const tableDiffAttributeSchema: z.ZodType<TableDiffAttribute> = z.union([+ z.literal('name'),+ z.literal('comments'),+]);++export interface TableDiffChanged {+ object: 'table';+ type: 'changed';+ tableId: string;+ attribute: TableDiffAttribute;+ oldValue?: string | null;+ newValue?: string | null;+}++export const TableDiffChangedSchema: z.ZodType<TableDiffChanged> = z.object({+ object: z.literal('table'),+ type: z.literal('changed'),+ tableId: z.string(),+ attribute: tableDiffAttributeSchema,+ oldValue: z.string().or(z.null()).optional(),+ newValue: z.string().or(z.null()).optional(),+});++export interface TableDiffRemoved {+ object: 'table';+ type: 'removed';+ tableId: string;+}++export const TableDiffRemovedSchema: z.ZodType<TableDiffRemoved> = z.object({+ object: z.literal('table'),+ type: z.literal('removed'),+ tableId: z.string(),+});++export interface TableDiffAdded {+ object: 'table';+ type: 'added';+ tableAdded: DBTable;+}++export const TableDiffAddedSchema: z.ZodType<TableDiffAdded> = z.object({+ object: z.literal('table'),+ type: z.literal('added'),+ tableAdded: dbTableSchema,+});++export type TableDiff = TableDiffChanged | TableDiffRemoved | TableDiffAdded;++export const tableDiffSchema: z.ZodType<TableDiff> = z.union([+ TableDiffChangedSchema,+ TableDiffRemovedSchema,+ TableDiffAddedSchema,+]);