Skip to content

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

fix(dbml import): fix dbml import types + schemas (#808)

Guy Ben-Aharon00bd535b3c62
diff --git a/src/lib/dbml/dbml-export/__tests__/dbml-export-issue-fix.test.ts b/src/lib/dbml/dbml-export/__tests__/dbml-export-issue-fix.test.ts
index ad72fd25..bd36c4a0 100644
--- a/src/lib/dbml/dbml-export/__tests__/dbml-export-issue-fix.test.ts
+++ b/src/lib/dbml/dbml-export/__tests__/dbml-export-issue-fix.test.ts
@@ -845,4 +845,118 @@ describe('DBML Export - Issue Fixes', () => {
// The duplicate store.products (table3) should be removed
expect(result.standardDbml).not.toContain('duplicate_field');
});
+
+ it('should export index names correctly in DBML', () => {
+ const diagram: Diagram = {
+ id: 'test-diagram',
+ name: 'Test',
+ databaseType: DatabaseType.POSTGRESQL,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ tables: [
+ {
+ id: 'table1',
+ name: 'users',
+ schema: 'public',
+ x: 0,
+ y: 0,
+ fields: [
+ {
+ id: 'field1',
+ name: 'id',
+ type: { id: 'bigint', name: 'bigint' },
+ primaryKey: true,
+ nullable: false,
+ unique: false,
+ collation: null,
+ default: null,
+ characterMaximumLength: null,
+ createdAt: Date.now(),
+ },
+ {
+ id: 'field2',
+ name: 'email',
+ type: { id: 'varchar', name: 'varchar' },
+ primaryKey: false,
+ nullable: false,
+ unique: false,
+ collation: null,
+ default: null,
+ characterMaximumLength: '255',
+ createdAt: Date.now(),
+ },
+ {
+ id: 'field3',
+ name: 'created_at',
+ type: { id: 'timestamp', name: 'timestamp' },
+ primaryKey: false,
+ nullable: false,
+ unique: false,
+ collation: null,
+ default: null,
+ characterMaximumLength: null,
+ createdAt: Date.now(),
+ },
+ ],
+ indexes: [
+ {
+ id: 'idx1',
+ name: 'idx_email',
+ unique: true,
+ fieldIds: ['field2'],
+ createdAt: Date.now(),
+ },
+ {
+ id: 'idx2',
+ name: 'idx_created_at',
+ unique: false,
+ fieldIds: ['field3'],
+ createdAt: Date.now(),
+ },
+ {
+ id: 'idx3',
+ name: 'idx_email_created',
+ unique: false,
+ fieldIds: ['field2', 'field3'],
+ createdAt: Date.now(),
+ },
+ ],
+ color: 'blue',
+ isView: false,
+ createdAt: Date.now(),
+ },
+ ],
+ relationships: [],
+ };
+
+ const result = generateDBMLFromDiagram(diagram);
+
+ // Check that table is properly formatted with schema
+ expect(result.standardDbml).toContain('Table "public"."users"');
+
+ // Check that indexes are properly formatted with names
+ // Note: When a table has a schema, index names are prefixed with the schema
+ expect(result.standardDbml).toContain(
+ 'email [unique, name: "public_idx_email"]'
+ );
+ expect(result.standardDbml).toContain(
+ 'created_at [name: "public_idx_created_at"]'
+ );
+ expect(result.standardDbml).toContain(
+ '(email, created_at) [name: "public_idx_email_created"]'
+ );
+
+ // Verify proper index syntax in the table
+ const indexSection = result.standardDbml.match(/Indexes \{[\s\S]*?\}/);
+ expect(indexSection).toBeTruthy();
+ expect(indexSection![0]).toContain(
+ 'email [unique, name: "public_idx_email"]'
+ );
+ expect(indexSection![0]).toContain(
+ 'created_at [name: "public_idx_created_at"]'
+ );
+ expect(indexSection![0]).toContain(
+ '(email, created_at) [name: "public_idx_email_created"]'
+ );
+ });
});
diff --git a/src/lib/dbml/dbml-import/__tests__/dbml-import-fantasy-examples.test.ts b/src/lib/dbml/dbml-import/__tests__/dbml-import-fantasy-examples.test.ts
index 15b42f48..3632fa08 100644
--- a/src/lib/dbml/dbml-import/__tests__/dbml-import-fantasy-examples.test.ts
+++ b/src/lib/dbml/dbml-import/__tests__/dbml-import-fantasy-examples.test.ts
@@ -695,13 +695,34 @@ Note dragon_note {
// Check that table header color was removed
expect(hoardsTable).toBeDefined();
+ // Verify all indexes are imported correctly
+ expect(hoardsTable?.indexes).toHaveLength(3); // Should have 3 indexes as defined in DBML
+
// Verify named indexes
const uniqueDragonIndex = hoardsTable?.indexes.find(
(idx) => idx.name === 'idx_unique_dragon'
);
+ expect(uniqueDragonIndex).toBeDefined();
+ expect(uniqueDragonIndex?.name).toBe('idx_unique_dragon'); // Verify exact name from DBML
expect(uniqueDragonIndex?.unique).toBe(true);
expect(uniqueDragonIndex?.fieldIds).toHaveLength(1);
+ const hoardValueIndex = hoardsTable?.indexes.find(
+ (idx) => idx.name === 'idx_hoard_value'
+ );
+ expect(hoardValueIndex).toBeDefined();
+ expect(hoardValueIndex?.name).toBe('idx_hoard_value'); // Verify exact name from DBML
+ expect(hoardValueIndex?.unique).toBe(false);
+ expect(hoardValueIndex?.fieldIds).toHaveLength(1);
+
+ const dragonActiveIndex = hoardsTable?.indexes.find(
+ (idx) => idx.name === 'idx_dragon_active'
+ );
+ expect(dragonActiveIndex).toBeDefined();
+ expect(dragonActiveIndex?.name).toBe('idx_dragon_active'); // Verify exact name from DBML
+ expect(dragonActiveIndex?.unique).toBe(false);
+ expect(dragonActiveIndex?.fieldIds).toHaveLength(2);
+
// Check relationship
expect(diagram.relationships).toHaveLength(1);
const relationship = diagram.relationships?.[0];
@@ -741,5 +762,263 @@ Table empty_table {
expect(diagram.tables?.[0]?.fields).toHaveLength(1);
expect(diagram.tables?.[0]?.name).toBe('empty_table');
});
+
+ it('should import tables with same name but different schemas', async () => {
+ const dbml = `
+Table "aa"."users" {
+ id integer [primary key]
+}
+
+Table "bb"."users" {
+ id integer [primary key]
+}`;
+ const diagram = await importDBMLToDiagram(dbml);
+
+ expect(diagram.tables).toHaveLength(2);
+
+ const aaUsersTable = diagram.tables?.find(
+ (t) => t.name === 'users' && t.schema === 'aa'
+ );
+ const bbUsersTable = diagram.tables?.find(
+ (t) => t.name === 'users' && t.schema === 'bb'
+ );
+
+ expect(aaUsersTable).toBeDefined();
+ expect(bbUsersTable).toBeDefined();
+
+ expect(aaUsersTable?.schema).toBe('aa');
+ expect(bbUsersTable?.schema).toBe('bb');
+
+ expect(aaUsersTable?.fields).toHaveLength(1);
+ expect(bbUsersTable?.fields).toHaveLength(1);
+
+ expect(aaUsersTable?.fields[0].name).toBe('id');
+ expect(aaUsersTable?.fields[0].type.id).toBe('int');
+ expect(aaUsersTable?.fields[0].primaryKey).toBe(true);
+
+ expect(bbUsersTable?.fields[0].name).toBe('id');
+ expect(bbUsersTable?.fields[0].type.id).toBe('int');
+ expect(bbUsersTable?.fields[0].primaryKey).toBe(true);
+ });
+
+ it('should import complex multi-schema DBML with inline refs and various indexes', async () => {
+ // This test validates:
+ // - 3 tables across different schemas (public, public_2, public_3)
+ // - Table-level notes (Note: 'my comment' on users table)
+ // - 3 indexes:
+ // * Composite unique index: (content, user_id) on posts table
+ // * Single non-unique index: created_at on posts table
+ // * Single unique index: id on comments table
+ // - 3 inline foreign key relationships:
+ // * posts.user_id -> users.id
+ // * comments.post_id -> posts.id
+ // * comments.user_id -> users.id
+ // - Quoted identifiers for all table and field names
+
+ const dbml = `
+Table "public"."users" {
+ "id" varchar(500) [pk]
+ "name" varchar(500)
+ "email" varchar(500)
+ Note: 'my comment'
+}
+
+Table "public_2"."posts" {
+ "id" varchar(500) [pk]
+ "title" varchar(500)
+ "content" text
+ "user_id" varchar(500) [ref: < "public"."users"."id"]
+ "created_at" timestamp
+
+ Indexes {
+ (content, user_id) [unique, name: "public_2_content_user_id_idx"]
+ created_at [name: "public_2_index_2"]
+ }
+}
+
+Table "public_3"."comments" {
+ "id" varchar(500) [pk]
+ "content" text
+ "post_id" varchar(500) [ref: < "public_2"."posts"."id"]
+ "user_id" varchar(500) [ref: < "public"."users"."id"]
+ "created_at" timestamp
+
+ Indexes {
+ id [unique, name: "public_3_index_1"]
+ }
+}`;
+ const diagram = await importDBMLToDiagram(dbml);
+
+ // Verify tables
+ expect(diagram.tables).toHaveLength(3);
+
+ const usersTable = diagram.tables?.find(
+ (t) => t.name === 'users' && t.schema === 'public'
+ );
+ const postsTable = diagram.tables?.find(
+ (t) => t.name === 'posts' && t.schema === 'public_2'
+ );
+ const commentsTable = diagram.tables?.find(
+ (t) => t.name === 'comments' && t.schema === 'public_3'
+ );
+
+ expect(usersTable).toBeDefined();
+ expect(postsTable).toBeDefined();
+ expect(commentsTable).toBeDefined();
+
+ // Check users table
+ expect(usersTable?.fields).toHaveLength(3);
+ expect(
+ usersTable?.fields.find((f) => f.name === 'id')?.primaryKey
+ ).toBe(true);
+ expect(
+ usersTable?.fields.find((f) => f.name === 'id')?.type.id
+ ).toBe('varchar');
+ expect(
+ usersTable?.fields.find((f) => f.name === 'name')?.type.id
+ ).toBe('varchar');
+ expect(
+ usersTable?.fields.find((f) => f.name === 'email')?.type.id
+ ).toBe('varchar');
+
+ // Check if table note is preserved
+ expect(usersTable?.comments).toBe('my comment');
+
+ // Check posts table
+ expect(postsTable?.fields).toHaveLength(5);
+ expect(
+ postsTable?.fields.find((f) => f.name === 'content')?.type.id
+ ).toBe('text');
+ expect(
+ postsTable?.fields.find((f) => f.name === 'created_at')?.type.id
+ ).toBe('timestamp');
+
+ // Check posts indexes thoroughly
+ expect(postsTable?.indexes).toHaveLength(2);
+
+ // Index 1: Composite unique index on (content, user_id)
+ const compositeIndex = postsTable?.indexes.find(
+ (idx) => idx.name === 'public_2_content_user_id_idx'
+ );
+ expect(compositeIndex).toBeDefined();
+ expect(compositeIndex?.name).toBe('public_2_content_user_id_idx'); // Verify exact name from DBML
+ expect(compositeIndex?.unique).toBe(true);
+ expect(compositeIndex?.fieldIds).toHaveLength(2);
+ // Verify it includes the correct fields
+ const contentFieldId = postsTable?.fields.find(
+ (f) => f.name === 'content'
+ )?.id;
+ const userIdFieldId = postsTable?.fields.find(
+ (f) => f.name === 'user_id'
+ )?.id;
+ expect(compositeIndex?.fieldIds).toContain(contentFieldId);
+ expect(compositeIndex?.fieldIds).toContain(userIdFieldId);
+
+ // Index 2: Non-unique index on created_at
+ const singleIndex = postsTable?.indexes.find(
+ (idx) => idx.name === 'public_2_index_2'
+ );
+ expect(singleIndex).toBeDefined();
+ expect(singleIndex?.name).toBe('public_2_index_2'); // Verify exact name from DBML
+ expect(singleIndex?.unique).toBe(false);
+ expect(singleIndex?.fieldIds).toHaveLength(1);
+ const createdAtFieldId = postsTable?.fields.find(
+ (f) => f.name === 'created_at'
+ )?.id;
+ expect(singleIndex?.fieldIds[0]).toBe(createdAtFieldId);
+
+ // Check comments table
+ expect(commentsTable?.fields).toHaveLength(5);
+ expect(commentsTable?.indexes).toHaveLength(1);
+
+ // Index: Unique index on id
+ const idIndex = commentsTable?.indexes.find(
+ (idx) => idx.name === 'public_3_index_1'
+ );
+ expect(idIndex).toBeDefined();
+ expect(idIndex?.name).toBe('public_3_index_1'); // Verify exact name from DBML
+ expect(idIndex?.unique).toBe(true);
+ expect(idIndex?.fieldIds).toHaveLength(1);
+ const idFieldId = commentsTable?.fields.find(
+ (f) => f.name === 'id'
+ )?.id;
+ expect(idIndex?.fieldIds[0]).toBe(idFieldId);
+
+ // Verify relationships (inline refs should create relationships)
+ // From DBML:
+ // 1. posts.user_id -> users.id
+ // 2. comments.post_id -> posts.id
+ // 3. comments.user_id -> users.id
+ expect(diagram.relationships).toHaveLength(3);
+
+ // Find relationships - check the actual field references
+ const findRelationshipByFields = (
+ sourceTableId: string,
+ sourceFieldName: string,
+ targetTableId: string,
+ targetFieldName: string
+ ) => {
+ const sourceField = diagram.tables
+ ?.find((t) => t.id === sourceTableId)
+ ?.fields.find((f) => f.name === sourceFieldName);
+ const targetField = diagram.tables
+ ?.find((t) => t.id === targetTableId)
+ ?.fields.find((f) => f.name === targetFieldName);
+
+ return diagram.relationships?.find(
+ (r) =>
+ (r.sourceFieldId === sourceField?.id &&
+ r.targetFieldId === targetField?.id) ||
+ (r.sourceFieldId === targetField?.id &&
+ r.targetFieldId === sourceField?.id)
+ );
+ };
+
+ // Relationship 1: posts.user_id -> users.id
+ const postsUsersRel = findRelationshipByFields(
+ postsTable!.id,
+ 'user_id',
+ usersTable!.id,
+ 'id'
+ );
+ expect(postsUsersRel).toBeDefined();
+ expect(postsUsersRel?.sourceSchema).toBeDefined();
+ expect(postsUsersRel?.targetSchema).toBeDefined();
+
+ // Relationship 2: comments.post_id -> posts.id
+ const commentsPostsRel = findRelationshipByFields(
+ commentsTable!.id,
+ 'post_id',
+ postsTable!.id,
+ 'id'
+ );
+ expect(commentsPostsRel).toBeDefined();
+
+ // Relationship 3: comments.user_id -> users.id
+ const commentsUsersRel = findRelationshipByFields(
+ commentsTable!.id,
+ 'user_id',
+ usersTable!.id,
+ 'id'
+ );
+ expect(commentsUsersRel).toBeDefined();
+
+ // Verify all relationships have the expected cardinality
+ // In DBML, inline refs create relationships where the referenced table (with PK)
+ // is the "one" side and the referencing table (with FK) is the "many" side
+ const allOneToMany = diagram.relationships?.every(
+ (r) =>
+ r.sourceCardinality === 'one' &&
+ r.targetCardinality === 'many'
+ );
+ expect(allOneToMany).toBe(true);
+
+ // Verify schemas are preserved in relationships
+ const relationshipsHaveSchemas = diagram.relationships?.every(
+ (r) =>
+ r.sourceSchema !== undefined && r.targetSchema !== undefined
+ );
+ expect(relationshipsHaveSchemas).toBe(true);
+ });
});
});
diff --git a/src/lib/dbml/dbml-import/dbml-import.ts b/src/lib/dbml/dbml-import/dbml-import.ts
index 55abcb01..64007029 100644
--- a/src/lib/dbml/dbml-import/dbml-import.ts
+++ b/src/lib/dbml/dbml-import/dbml-import.ts
@@ -32,8 +32,9 @@ export const preprocessDBML = (content: string): string => {
);
// Handle Table headers with color attributes
+ // This regex handles both simple table names and schema.table patterns with quotes
processed = processed.replace(
- /Table\s+(\w+)\s*\[[^\]]*\]\s*\{/g,
+ /Table\s+((?:"[^"]+"\."[^"]+")|(?:\w+))\s*\[[^\]]*\]\s*\{/g,
'Table $1 {'
);
@@ -86,7 +87,7 @@ interface DBMLIndexColumn {
}
interface DBMLIndex {
- columns: string | (string | DBMLIndexColumn)[];
+ columns: (string | DBMLIndexColumn)[];
unique?: boolean;
name?: string;
}
@@ -96,6 +97,7 @@ interface DBMLTable {
schema?: string | { name: string };
fields: DBMLField[];
indexes?: DBMLIndex[];
+ note?: string | { value: string } | null;
}
interface DBMLEndpoint {
@@ -113,9 +115,11 @@ const mapDBMLTypeToGenericType = (dbmlType: string): DataType => {
const matchedType = genericDataTypes.find((t) => t.id === normalizedType);
if (matchedType) return matchedType;
const typeMap: Record<string, string> = {
- int: 'integer',
+ int: 'int',
+ integer: 'int',
varchar: 'varchar',
bool: 'boolean',
+ boolean: 'boolean',
number: 'numeric',
string: 'varchar',
text: 'text',
@@ -194,10 +198,9 @@ export const importDBMLToDiagram = async (
}
const parsedData = parser.parse(sanitizedContent, 'dbml');
- const dbmlData = parsedData.schemas[0];
- // Handle case where no schema is found
- if (!dbmlData || !dbmlData.tables) {
+ // Handle case where no schemas are found
+ if (!parsedData.schemas || parsedData.schemas.length === 0) {
return {
id: generateDiagramId(),
name: 'DBML Import',
@@ -209,71 +212,117 @@ export const importDBMLToDiagram = async (
};
}
- // Extract only the necessary data from the parsed DBML
- const extractedData = {
- tables: (dbmlData.tables as unknown as DBMLTable[]).map(
- (table) => ({
- name: table.name,
- schema: table.schema,
- fields: table.fields.map((field: DBMLField) => ({
- name: field.name,
- type: field.type,
- unique: field.unique,
- pk: field.pk,
- not_null: field.not_null,
- increment: field.increment,
- })),
- indexes:
- table.indexes?.map((dbmlIndex) => {
- let indexColumns: string[];
-
- // Handle composite index case "(col1, col2)"
- if (typeof dbmlIndex.columns === 'string') {
- if (dbmlIndex.columns.includes('(')) {
- // Composite index
- const columnsStr =
- dbmlIndex.columns.replace(/[()]/g, '');
- indexColumns = columnsStr
- .split(',')
- .map((c) => c.trim());
+ // Process all schemas, not just the first one
+ const allTables: DBMLTable[] = [];
+ const allRefs: DBMLRef[] = [];
+
+ parsedData.schemas.forEach((schema) => {
+ if (schema.tables) {
+ schema.tables.forEach((table) => {
+ // For tables with explicit schema, use the schema name
+ // For tables without explicit schema, use empty string
+ const schemaName =
+ typeof table.schema === 'string'
+ ? table.schema
+ : table.schema?.name || '';
+
+ allTables.push({
+ name: table.name,
+ schema: schemaName,
+ note: table.note,
+ fields: table.fields.map(
+ (field) =>
+ ({
+ name: field.name,
+ type: field.type,
+ unique: field.unique,
+ pk: field.pk,
+ not_null: field.not_null,
+ increment: field.increment,
+ }) satisfies DBMLField
+ ),
+ indexes:
+ table.indexes?.map((dbmlIndex) => {
+ let indexColumns: string[];
+
+ // Handle both string and array formats
+ if (typeof dbmlIndex.columns === 'string') {
+ // Handle composite index case "(col1, col2)"
+ // @ts-expect-error "columns" can be a string in some DBML versions
+ if (dbmlIndex.columns.includes('(')) {
+ const columnsStr: string =
+ // @ts-expect-error "columns" can be a string in some DBML versions
+ dbmlIndex.columns.replace(
+ /[()]/g,
+ ''
+ );
+ indexColumns = columnsStr
+ .split(',')
+ .map((c) => c.trim());
+ } else {
+ // Single column as string
+
+ indexColumns = [
+ // @ts-expect-error "columns" can be a string in some DBML versions
+ dbmlIndex.columns.trim(),
+ ];
+ }
} else {
- // Single column
- indexColumns = [dbmlIndex.columns.trim()];
+ // Handle array of columns
+ indexColumns = dbmlIndex.columns.map(
+ (col) => {
+ if (typeof col === 'string') {
+ // @ts-expect-error "columns" can be a string in some DBML versions
+ return col.trim();
+ } else if (
+ typeof col === 'object' &&
+ 'value' in col
+ ) {
+ return col.value.trim();
+ } else {
+ return String(col).trim();
+ }
+ }
+ );
}
- } else {
- // Handle array of columns
- indexColumns = Array.isArray(dbmlIndex.columns)
- ? dbmlIndex.columns.map((col) =>
- typeof col === 'object' &&
- 'value' in col
- ? (col.value as string).trim()
- : (col as string).trim()
- )
- : [String(dbmlIndex.columns).trim()];
- }
-
- // Generate a consistent index name
- const indexName =
- dbmlIndex.name ||
- `idx_${table.name}_${indexColumns.join('_')}`;
-
- return {
- columns: indexColumns,
- unique: dbmlIndex.unique || false,
- name: indexName,
- };
- }) || [],
- })
- ),
- refs: (dbmlData.refs as unknown as DBMLRef[]).map((ref) => ({
- endpoints: (ref.endpoints as [DBMLEndpoint, DBMLEndpoint]).map(
- (endpoint) => ({
- tableName: endpoint.tableName,
- fieldNames: endpoint.fieldNames,
- relation: endpoint.relation,
- })
- ),
- })),
+
+ // Generate a consistent index name
+ const indexName =
+ dbmlIndex.name ||
+ `idx_${table.name}_${indexColumns.join('_')}`;
+
+ return {
+ columns: indexColumns,
+ unique: dbmlIndex.unique || false,
+ name: indexName,
+ };
+ }) || [],
+ });
+ });
+ }
+
+ if (schema.refs) {
+ schema.refs.forEach((ref) => {
+ // Convert the ref to ensure it has exactly two endpoints
+ if (ref.endpoints && ref.endpoints.length >= 2) {
+ allRefs.push({
+ endpoints: [ref.endpoints[0], ref.endpoints[1]] as [
+ DBMLEndpoint,
+ DBMLEndpoint,
+ ],
+ });
+ }
+ });
+ }
+ });
+
+ // Extract only the necessary data from the parsed DBML
+ const extractedData: {
+ tables: DBMLTable[];
+ refs: DBMLRef[];
+ } = {
+ tables: allTables,
+ refs: allRefs,
};
// Convert DBML tables to ChartDB table objects
@@ -310,13 +359,26 @@ export const importDBMLToDiagram = async (
id: generateId(),
name:
dbmlIndex.name ||
- `idx_${table.name}_${dbmlIndex.columns.join('_')}`,
+ `idx_${table.name}_${(dbmlIndex.columns as string[]).join('_')}`,
fieldIds,
unique: dbmlIndex.unique || false,
createdAt: Date.now(),
};
}) || [];
+ // Extract table note/comment
+ let tableComment: string | undefined;
+ if (table.note) {
+ if (typeof table.note === 'string') {
+ tableComment = table.note;
+ } else if (
+ typeof table.note === 'object' &&
+ 'value' in table.note
+ ) {
+ tableComment = table.note.value;
+ }
+ }
+
return {
id: generateId(),
name: table.name.replace(/['"]/g, ''),
@@ -332,6 +394,7 @@ export const importDBMLToDiagram = async (
color: randomColor(),
isView: false,
createdAt: Date.now(),
+ comments: tableComment,
};
});