Skip to content

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

fix(dbml-editor): add inline refs mode + fix issues with DBML syntax (#687)

Jonathan Fishnerfbf2fe919c21
diff --git a/src/components/code-snippet/code-snippet.tsx b/src/components/code-snippet/code-snippet.tsx
index 8d1fa627..0ece3369 100644
--- a/src/components/code-snippet/code-snippet.tsx
+++ b/src/components/code-snippet/code-snippet.tsx
@@ -5,6 +5,7 @@ import { useTheme } from '@/hooks/use-theme';
import { useMonaco } from '@monaco-editor/react';
import { useToast } from '@/components/toast/use-toast';
import { Button } from '../button/button';
+import type { LucideIcon } from 'lucide-react';
import { Copy, CopyCheck } from 'lucide-react';
import { Tooltip, TooltipContent, TooltipTrigger } from '../tooltip/tooltip';
import { useTranslation } from 'react-i18next';
@@ -26,6 +27,12 @@ export const DiffEditor = lazy(() =>
type EditorType = typeof Editor;
+export interface CodeSnippetAction {
+ label: string;
+ icon: LucideIcon;
+ onClick: () => void;
+}
+
export interface CodeSnippetProps {
className?: string;
code: string;
@@ -35,6 +42,7 @@ export interface CodeSnippetProps {
autoScroll?: boolean;
isComplete?: boolean;
editorProps?: React.ComponentProps<EditorType>;
+ actions?: CodeSnippetAction[];
}
export const CodeSnippet: React.FC<CodeSnippetProps> = React.memo(
@@ -47,6 +55,7 @@ export const CodeSnippet: React.FC<CodeSnippetProps> = React.memo(
autoScroll = false,
isComplete = true,
editorProps,
+ actions,
}) => {
const { t } = useTranslation();
const monaco = useMonaco();
@@ -119,36 +128,58 @@ export const CodeSnippet: React.FC<CodeSnippetProps> = React.memo(
) : (
<Suspense fallback={<Spinner />}>
{isComplete ? (
- <Tooltip
- onOpenChange={setTooltipOpen}
- open={isCopied || tooltipOpen}
- >
- <TooltipTrigger
- asChild
- className="absolute right-1 top-1 z-10"
+ <div className="absolute right-1 top-1 z-10 flex flex-col gap-1">
+ <Tooltip
+ onOpenChange={setTooltipOpen}
+ open={isCopied || tooltipOpen}
>
- <span>
- <Button
- className=" h-fit p-1.5"
- variant="outline"
- onClick={copyToClipboard}
- >
- {isCopied ? (
- <CopyCheck size={16} />
- ) : (
- <Copy size={16} />
- )}
- </Button>
- </span>
- </TooltipTrigger>
- <TooltipContent>
- {t(
- isCopied
- ? 'copied'
- : 'copy_to_clipboard'
- )}
- </TooltipContent>
- </Tooltip>
+ <TooltipTrigger asChild>
+ <span>
+ <Button
+ className="h-fit p-1.5"
+ variant="outline"
+ onClick={copyToClipboard}
+ >
+ {isCopied ? (
+ <CopyCheck size={16} />
+ ) : (
+ <Copy size={16} />
+ )}
+ </Button>
+ </span>
+ </TooltipTrigger>
+ <TooltipContent>
+ {t(
+ isCopied
+ ? 'copied'
+ : 'copy_to_clipboard'
+ )}
+ </TooltipContent>
+ </Tooltip>
+
+ {actions &&
+ actions.length > 0 &&
+ actions.map((action, index) => (
+ <Tooltip key={index}>
+ <TooltipTrigger asChild>
+ <span>
+ <Button
+ className="h-fit p-1.5"
+ variant="outline"
+ onClick={action.onClick}
+ >
+ <action.icon
+ size={16}
+ />
+ </Button>
+ </span>
+ </TooltipTrigger>
+ <TooltipContent>
+ {action.label}
+ </TooltipContent>
+ </Tooltip>
+ ))}
+ </div>
) : null}
<Editor
diff --git a/src/lib/data/export-metadata/export-sql-script.ts b/src/lib/data/export-metadata/export-sql-script.ts
index 327d72c8..f63d7ff7 100644
--- a/src/lib/data/export-metadata/export-sql-script.ts
+++ b/src/lib/data/export-metadata/export-sql-script.ts
@@ -9,6 +9,29 @@ import { exportPostgreSQL } from './export-per-type/postgresql';
import { exportSQLite } from './export-per-type/sqlite';
import { exportMySQL } from './export-per-type/mysql';
+// Function to simplify verbose data type names
+const simplifyDataType = (typeName: string): string => {
+ const typeMap: Record<string, string> = {
+ 'character varying': 'varchar',
+ 'char varying': 'varchar',
+ integer: 'int',
+ int4: 'int',
+ int8: 'bigint',
+ serial4: 'serial',
+ serial8: 'bigserial',
+ float8: 'double precision',
+ float4: 'real',
+ bool: 'boolean',
+ character: 'char',
+ 'timestamp without time zone': 'timestamp',
+ 'timestamp with time zone': 'timestamptz',
+ 'time without time zone': 'time',
+ 'time with time zone': 'timetz',
+ };
+
+ return typeMap[typeName.toLowerCase()] || typeName;
+};
+
export const exportBaseSQL = ({
diagram,
targetDatabaseType,
@@ -93,11 +116,12 @@ export const exportBaseSQL = ({
sqlScript += `CREATE TABLE ${tableName} (\n`;
table.fields.forEach((field, index) => {
- let typeName = field.type.name;
+ let typeName = simplifyDataType(field.type.name);
// Handle ENUM type
if (typeName.toLowerCase() === 'enum') {
- typeName = 'varchar';
+ // Map enum to TEXT for broader compatibility, especially with DBML importer
+ typeName = 'text';
}
// Temp fix for 'array' to be text[]
@@ -116,6 +140,7 @@ export const exportBaseSQL = ({
if (field.characterMaximumLength) {
sqlScript += `(${field.characterMaximumLength})`;
} else if (field.type.name.toLowerCase().includes('varchar')) {
+ // Keep varchar sizing, but don't apply to TEXT (previously enum)
sqlScript += `(500)`;
}
diff --git a/src/pages/editor-page/side-panel/tables-section/table-dbml/table-dbml.tsx b/src/pages/editor-page/side-panel/tables-section/table-dbml/table-dbml.tsx
index 70e8bbe5..717cea99 100644
--- a/src/pages/editor-page/side-panel/tables-section/table-dbml/table-dbml.tsx
+++ b/src/pages/editor-page/side-panel/tables-section/table-dbml/table-dbml.tsx
@@ -1,4 +1,4 @@
-import React, { useMemo } from 'react';
+import React, { useMemo, useState, useEffect } from 'react';
import type { DBTable } from '@/lib/domain/db-table';
import { useChartDB } from '@/hooks/use-chartdb';
import { useTheme } from '@/hooks/use-theme';
@@ -10,6 +10,8 @@ import type { Diagram } from '@/lib/domain/diagram';
import { useToast } from '@/components/toast/use-toast';
import { setupDBMLLanguage } from '@/components/code-snippet/languages/dbml-language';
import { DatabaseType } from '@/lib/domain/database-type';
+import { ArrowLeftRight } from 'lucide-react';
+import { type DBField } from '@/lib/domain/db-field';
export interface TableDBMLProps {
filteredTables: DBTable[];
@@ -37,29 +39,268 @@ const databaseTypeToImportFormat = (
}
};
+// Fix problematic field names in the diagram before passing to SQL generator
+const fixProblematicFieldNames = (diagram: Diagram): Diagram => {
+ const fixedTables =
+ diagram.tables?.map((table) => {
+ // Deep clone the table to avoid modifying the original
+ const newTable = { ...table };
+
+ // Fix field names if this is the "relation" table
+ if (table.name === 'relation') {
+ newTable.fields = table.fields.map((field) => {
+ // Create a new field to avoid modifying the original
+ const newField = { ...field };
+
+ // Fix the 'from' and 'to' fields which are SQL keywords
+ if (field.name === 'from') {
+ newField.name = 'source';
+ } else if (field.name === 'to') {
+ newField.name = 'target';
+ }
+
+ return newField;
+ });
+ }
+
+ return newTable;
+ }) || [];
+
+ // Update relationships to point to the renamed fields
+ const fixedRelationships =
+ diagram.relationships?.map((rel) => {
+ const relationTable = diagram.tables?.find(
+ (t) => t.name === 'relation'
+ );
+ if (!relationTable) return rel;
+
+ const newRel = { ...rel };
+
+ // Fix relationships that were pointing to the 'from' field
+ const fromField = relationTable.fields.find(
+ (f) => f.name === 'from'
+ );
+ if (fromField && rel.targetFieldId === fromField.id) {
+ // We need to look up the renamed field in our fixed tables
+ const fixedRelationTable = fixedTables.find(
+ (t) => t.name === 'relation'
+ );
+ const sourceField = fixedRelationTable?.fields.find(
+ (f) => f.name === 'source'
+ );
+ if (sourceField) {
+ newRel.targetFieldId = sourceField.id;
+ }
+ }
+
+ // Fix relationships that were pointing to the 'to' field
+ const toField = relationTable.fields.find((f) => f.name === 'to');
+ if (toField && rel.targetFieldId === toField.id) {
+ // We need to look up the renamed field in our fixed tables
+ const fixedRelationTable = fixedTables.find(
+ (t) => t.name === 'relation'
+ );
+ const targetField = fixedRelationTable?.fields.find(
+ (f) => f.name === 'target'
+ );
+ if (targetField) {
+ newRel.targetFieldId = targetField.id;
+ }
+ }
+
+ return newRel;
+ }) || [];
+
+ // Return a new diagram with the fixes
+ return {
+ ...diagram,
+ tables: fixedTables,
+ relationships: fixedRelationships,
+ };
+};
+
+// Function to sanitize SQL before passing to the importer
+const sanitizeSQLforDBML = (sql: string): string => {
+ // Replace special characters in identifiers
+ let sanitized = sql;
+
+ // Handle duplicate constraint names
+ const constraintNames = new Set<string>();
+ let constraintCounter = 0;
+
+ sanitized = sanitized.replace(
+ /ADD CONSTRAINT (\w+) FOREIGN KEY/g,
+ (match, name) => {
+ if (constraintNames.has(name)) {
+ return `ADD CONSTRAINT ${name}_${++constraintCounter} FOREIGN KEY`;
+ } else {
+ constraintNames.add(name);
+ return match;
+ }
+ }
+ );
+
+ // Replace any remaining problematic characters
+ sanitized = sanitized.replace(/\?\?/g, '__');
+
+ return sanitized;
+};
+
+// Post-process DBML to convert separate Ref statements to inline refs
+const convertToInlineRefs = (dbml: string): string => {
+ // Extract all Ref statements - Corrected pattern
+ const refPattern =
+ /Ref\s+"([^"]+)"\s*:\s*"([^"]+)"\."([^"]+)"\s*([<>*])\s*"([^"]+)"\."([^"]+)"/g;
+ const refs: Array<{
+ refName: string;
+ sourceTable: string;
+ sourceField: string;
+ direction: string;
+ targetTable: string;
+ targetField: string;
+ }> = [];
+
+ let match;
+ while ((match = refPattern.exec(dbml)) !== null) {
+ refs.push({
+ refName: match[1], // Reference name
+ sourceTable: match[2], // Source table
+ sourceField: match[3], // Source field
+ direction: match[4], // Direction (<, >)
+ targetTable: match[5], // Target table
+ targetField: match[6], // Target field
+ });
+ }
+
+ // Extract all table definitions - Corrected pattern and handling
+ const tables: {
+ [key: string]: { start: number; end: number; content: string };
+ } = {};
+ const tablePattern = /Table\s+"([^"]+)"\s*{([^}]*)}/g; // Simpler pattern, assuming content doesn't have {}
+
+ let tableMatch;
+ while ((tableMatch = tablePattern.exec(dbml)) !== null) {
+ const tableName = tableMatch[1];
+ tables[tableName] = {
+ start: tableMatch.index,
+ end: tableMatch.index + tableMatch[0].length,
+ content: tableMatch[2],
+ };
+ }
+
+ if (refs.length === 0 || Object.keys(tables).length === 0) {
+ return dbml; // Return original if parsing failed
+ }
+
+ // Create a map for faster table lookup
+ const tableMap = new Map(Object.entries(tables));
+
+ // 1. Add inline refs to table contents
+ refs.forEach((ref) => {
+ let targetTableName, fieldNameToModify, inlineRefSyntax;
+
+ if (ref.direction === '<') {
+ targetTableName = ref.targetTable;
+ fieldNameToModify = ref.targetField;
+ inlineRefSyntax = `[ref: < "${ref.sourceTable}"."${ref.sourceField}"]`;
+ } else {
+ targetTableName = ref.sourceTable;
+ fieldNameToModify = ref.sourceField;
+ inlineRefSyntax = `[ref: > "${ref.targetTable}"."${ref.targetField}"]`;
+ }
+
+ const tableData = tableMap.get(targetTableName);
+ if (tableData) {
+ const fieldPattern = new RegExp(
+ `("(${fieldNameToModify})"[^\n]*?)([ \t]*[[].*?[]])?([ \t]*//.*)?$`,
+ 'm'
+ );
+ let newContent = tableData.content;
+
+ newContent = newContent.replace(
+ fieldPattern,
+ (
+ lineMatch,
+ fieldPart,
+ _fieldName,
+ existingAttributes,
+ commentPart
+ ) => {
+ // Avoid adding duplicate refs
+ if (lineMatch.includes('[ref:')) {
+ return lineMatch;
+ }
+
+ return `${fieldPart.trim()} ${inlineRefSyntax}${existingAttributes || ''}${commentPart || ''}`;
+ }
+ );
+
+ // Update the table content if modified
+ if (newContent !== tableData.content) {
+ tableData.content = newContent;
+ tableMap.set(targetTableName, tableData);
+ }
+ }
+ });
+
+ // 2. Reconstruct DBML with modified tables
+ let reconstructedDbml = '';
+ let lastIndex = 0;
+ const sortedTables = Object.entries(tables).sort(
+ ([, a], [, b]) => a.start - b.start
+ );
+
+ for (const [tableName, tableData] of sortedTables) {
+ reconstructedDbml += dbml.substring(lastIndex, tableData.start);
+ reconstructedDbml += `Table "${tableName}" {${tableData.content}}`;
+ lastIndex = tableData.end;
+ }
+ reconstructedDbml += dbml.substring(lastIndex);
+
+ // 3. Remove original Ref lines
+ const finalLines = reconstructedDbml
+ .split('\n')
+ .filter((line) => !line.trim().startsWith('Ref '));
+ const finalDbml = finalLines.join('\n').trim();
+
+ return finalDbml;
+};
+
+// Function to check for SQL keywords (add more if needed)
+const isSQLKeyword = (name: string): boolean => {
+ const keywords = new Set(['CASE', 'ORDER', 'GROUP', 'FROM', 'TO', 'USER']); // Add common keywords
+ return keywords.has(name.toUpperCase());
+};
+
+// Fix DBML formatting to ensure consistent display of char and varchar types
+const normalizeCharTypeFormat = (dbml: string): string => {
+ // Replace "char (N)" with "char(N)" to match varchar's formatting
+ return dbml
+ .replace(/"char "/g, 'char')
+ .replace(/"char \(([0-9]+)\)"/g, 'char($1)')
+ .replace(/"character \(([0-9]+)\)"/g, 'character($1)');
+};
+
export const TableDBML: React.FC<TableDBMLProps> = ({ filteredTables }) => {
const { currentDiagram } = useChartDB();
const { effectiveTheme } = useTheme();
const { toast } = useToast();
+ const [dbmlFormat, setDbmlFormat] = useState<'inline' | 'standard'>(
+ 'standard'
+ );
- const generateDBML = useMemo(() => {
- // Filter out fields with empty names and track if any were found
+ // --- Effect for handling empty field name warnings ---
+ useEffect(() => {
let foundInvalidFields = false;
- const invalidTableNames = new Set<string>(); // Use a Set to store unique table names
+ const invalidTableNames = new Set<string>();
- const sanitizedTables = filteredTables.map((table) => {
- const validFields = table.fields.filter((field) => {
+ filteredTables.forEach((table) => {
+ table.fields.forEach((field) => {
if (field.name === '') {
foundInvalidFields = true;
- invalidTableNames.add(table.name); // Add table name to the set
- return false; // Exclude this field
+ invalidTableNames.add(table.name);
}
- return true; // Keep this field
});
- return {
- ...table,
- fields: validFields,
- };
});
if (foundInvalidFields) {
@@ -70,20 +311,33 @@ export const TableDBML: React.FC<TableDBMLProps> = ({ filteredTables }) => {
variant: 'default',
});
}
+ }, [filteredTables, toast]); // Depend on filteredTables and toast
+
+ // Generate both standard and inline DBML formats
+ const { standardDbml, inlineDbml } = useMemo(() => {
+ // Filter out fields with empty names
+ const sanitizedTables = filteredTables.map((table) => {
+ const validFields = table.fields.filter(
+ (field) => field.name !== ''
+ );
+ return {
+ ...table,
+ fields: validFields,
+ };
+ });
+ // Create the base filtered diagram structure
const filteredDiagram: Diagram = {
...currentDiagram,
- tables: sanitizedTables, // Use sanitized tables
+ tables: sanitizedTables,
relationships:
currentDiagram.relationships?.filter((rel) => {
- // Update relationship filtering to use sanitizedTables
const sourceTable = sanitizedTables.find(
(t) => t.id === rel.sourceTableId
);
const targetTable = sanitizedTables.find(
(t) => t.id === rel.targetTableId
);
- // Also check if the related fields still exist after sanitization
const sourceFieldExists = sourceTable?.fields.some(
(f) => f.id === rel.sourceFieldId
);
@@ -98,54 +352,171 @@ export const TableDBML: React.FC<TableDBMLProps> = ({ filteredTables }) => {
targetFieldExists
);
}) ?? [],
- } satisfies Diagram;
+ };
- const filteredDiagramWithoutSpaces: Diagram = {
- ...filteredDiagram,
+ // Sanitize field names ('from'/'to' in 'relation' table)
+ const cleanDiagram = fixProblematicFieldNames(filteredDiagram);
+
+ // --- Final sanitization and renaming pass ---
+ // Track tables renamed due to SQL keyword conflicts
+ const sqlRenamedTables = new Map<string, string>();
+ // Track fields renamed due to SQL keyword conflicts
+ const fieldRenames: Array<{
+ table: string;
+ originalName: string;
+ newName: string;
+ }> = [];
+ const finalDiagramForExport: Diagram = {
+ ...cleanDiagram,
tables:
- filteredDiagram.tables?.map((table) => ({
- ...table,
- name: table.name.replace(/\s/g, '_'),
- fields: table.fields.map((field) => ({
- ...field,
- name: field.name.replace(/\s/g, '_'),
- })),
- indexes: table.indexes?.map((index) => ({
- ...index,
- name: index.name.replace(/\s/g, '_'),
- })),
+ cleanDiagram.tables?.map((table) => {
+ const originalName = table.name;
+ // Sanitize table name
+ let safeTableName = originalName.replace(/[^\w]/g, '_');
+ // Rename if SQL keyword
+ if (isSQLKeyword(safeTableName)) {
+ const newName = `${safeTableName}_table`;
+ sqlRenamedTables.set(newName, originalName);
+ safeTableName = newName;
+ }
+
+ const fieldNameCounts = new Map<string, number>();
+ const processedFields = table.fields.map((field) => {
+ const originalSafeName = field.name.replace(
+ /[^\w]/g,
+ '_'
+ );
+ let finalSafeName = originalSafeName;
+ const count =
+ fieldNameCounts.get(originalSafeName) || 0;
+
+ if (count > 0) {
+ finalSafeName = `${originalSafeName}_${count + 1}`; // Rename duplicate
+ }
+ fieldNameCounts.set(originalSafeName, count + 1);
+
+ // Create a copy and remove comments
+ const sanitizedField: DBField = {
+ ...field,
+ name: finalSafeName,
+ };
+ delete sanitizedField.comments;
+
+ // Rename if SQL keyword
+ if (isSQLKeyword(finalSafeName)) {
+ const newFieldName = `${finalSafeName}_field`;
+ fieldRenames.push({
+ table: safeTableName,
+ originalName: finalSafeName,
+ newName: newFieldName,
+ });
+ sanitizedField.name = newFieldName;
+ }
+ return sanitizedField;
+ });
+
+ return {
+ ...table,
+ name: safeTableName,
+ fields: processedFields, // Use fields with renamed duplicates
+ indexes: (table.indexes || []).map((index) => ({
+ ...index,
+ name: index.name
+ ? index.name.replace(/[^\w]/g, '_')
+ : `idx_${Math.random().toString(36).substring(2, 8)}`,
+ })),
+ };
+ }) ?? [],
+ relationships:
+ cleanDiagram.relationships?.map((rel, index) => ({
+ ...rel,
+ name: `fk_${index}_${rel.name ? rel.name.replace(/[^\w]/g, '_') : Math.random().toString(36).substring(2, 8)}`,
})) ?? [],
- } satisfies Diagram;
+ } as Diagram;
- const baseScript = exportBaseSQL({
- diagram: filteredDiagramWithoutSpaces,
- targetDatabaseType: currentDiagram.databaseType,
- isDBMLFlow: true,
- });
+ let standard = '';
+ let inline = '';
+ let baseScript = ''; // Define baseScript outside try
try {
- const importFormat = databaseTypeToImportFormat(
- currentDiagram.databaseType
- );
- return importer.import(baseScript, importFormat);
- } catch (e) {
- console.error(e);
+ baseScript = exportBaseSQL({
+ diagram: finalDiagramForExport, // Use final diagram
+ targetDatabaseType: currentDiagram.databaseType,
+ isDBMLFlow: true,
+ });
- toast({
- title: 'Error',
- description:
- 'Failed to generate DBML. We would appreciate if you could report this issue!',
- variant: 'destructive',
+ baseScript = sanitizeSQLforDBML(baseScript);
+
+ // Append COMMENTS for tables renamed due to SQL keywords
+ sqlRenamedTables.forEach((originalName, newName) => {
+ const escapedOriginal = originalName.replace(/'/g, "\\'");
+ baseScript += `\nCOMMENT ON TABLE "${newName}" IS 'Original name was "${escapedOriginal}" (renamed due to SQL keyword conflict).';`;
+ });
+
+ // Append COMMENTS for fields renamed due to SQL keyword conflicts
+ fieldRenames.forEach(({ table, originalName, newName }) => {
+ const escapedOriginal = originalName.replace(/'/g, "\\'");
+ baseScript += `\nCOMMENT ON COLUMN "${table}"."${newName}" IS 'Original name was "${escapedOriginal}" (renamed due to SQL keyword conflict).';`;
});
- return '';
+ standard = normalizeCharTypeFormat(
+ importer.import(
+ baseScript,
+ databaseTypeToImportFormat(currentDiagram.databaseType)
+ )
+ );
+
+ inline = normalizeCharTypeFormat(convertToInlineRefs(standard));
+ } catch (error: unknown) {
+ console.error(
+ 'Error during DBML generation process:',
+ error,
+ 'Input SQL was:',
+ baseScript // Log the SQL that caused the error
+ );
+
+ const errorMessage = `// Error generating DBML: ${error instanceof Error ? error.message : 'Unknown error'}`;
+ standard = errorMessage;
+ inline = errorMessage;
+
+ // Handle different error types for toast
+ if (error instanceof Error) {
+ toast({
+ title: 'DBML Export Error',
+ description: `Could not generate DBML: ${error.message.substring(0, 100)}${error.message.length > 100 ? '...' : ''}`,
+ variant: 'destructive',
+ });
+ } else {
+ toast({
+ title: 'DBML Export Error',
+ description:
+ 'Could not generate DBML due to an unknown error',
+ variant: 'destructive',
+ });
+ }
}
- }, [currentDiagram, filteredTables, toast]);
+ return { standardDbml: standard, inlineDbml: inline };
+ }, [currentDiagram, filteredTables, toast]); // Keep toast dependency for now, although direct call is removed
+
+ // Determine which DBML string to display
+ const dbmlToDisplay = dbmlFormat === 'inline' ? inlineDbml : standardDbml;
+
+ // Toggle function
+ const toggleFormat = () => {
+ setDbmlFormat((prev) => (prev === 'inline' ? 'standard' : 'inline'));
+ };
return (
<CodeSnippet
- code={generateDBML}
+ code={dbmlToDisplay}
className="my-0.5"
+ actions={[
+ {
+ label: `Show ${dbmlFormat === 'inline' ? 'Standard' : 'Inline'} Refs`,
+ icon: ArrowLeftRight,
+ onClick: toggleFormat,
+ },
+ ]}
editorProps={{
height: '100%',
defaultLanguage: 'dbml',