Skip to content

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

fix: preserve MySQL column notes in DBML export (#979)

Jonathan Fishner39eebe5e2ae9
diff --git a/src/lib/data/import-metadata/scripts/maria-script.ts b/src/lib/data/import-metadata/scripts/maria-script.ts
index 8ad70966..0cd4d15f 100644
--- a/src/lib/data/import-metadata/scripts/maria-script.ts
+++ b/src/lib/data/import-metadata/scripts/maria-script.ts
@@ -71,7 +71,7 @@ SELECT CAST(CONCAT(
',"default":"', ${withExtras ? withDefault : withoutDefault},
'","collation":"', IFNULL(cols.collation_name, ''),
'","is_identity":', IF(cols.extra LIKE '%auto_increment%', 'true', 'false'),
- '"}')
+ ',"comment":"', REPLACE(REPLACE(IFNULL(cols.column_comment, ''), '"', '\\"'), '\\n', ' '), '"}')
) FROM (
SELECT cols.table_schema,
cols.table_name,
@@ -84,7 +84,8 @@ SELECT CAST(CONCAT(
cols.is_nullable,
cols.column_default,
cols.collation_name,
- cols.extra
+ cols.extra,
+ cols.column_comment
FROM information_schema.columns cols
WHERE cols.table_schema = DATABASE()
) AS cols), ''),
@@ -127,14 +128,16 @@ SELECT CAST(CONCAT(
'","rows":', IFNULL(tbls.TABLE_ROWS, 0),
',"type":"', IFNULL(tbls.TABLE_TYPE, ''),
'","engine":"', IFNULL(tbls.ENGINE, ''),
- '","collation":"', IFNULL(tbls.TABLE_COLLATION, ''), '"}')
+ '","collation":"', IFNULL(tbls.TABLE_COLLATION, ''),
+ '","comment":"', REPLACE(REPLACE(IFNULL(tbls.TABLE_COMMENT, ''), '"', '\\"'), '\\n', ' '), '"}')
) FROM (
SELECT \`TABLE_SCHEMA\`,
\`TABLE_NAME\`,
\`TABLE_ROWS\`,
\`TABLE_TYPE\`,
\`ENGINE\`,
- \`TABLE_COLLATION\`
+ \`TABLE_COLLATION\`,
+ \`TABLE_COMMENT\`
FROM information_schema.tables tbls
WHERE tbls.table_schema = DATABASE()
) AS tbls), ''),
diff --git a/src/lib/data/import-metadata/scripts/mysql-script.ts b/src/lib/data/import-metadata/scripts/mysql-script.ts
index 9839c838..7c7b3c47 100644
--- a/src/lib/data/import-metadata/scripts/mysql-script.ts
+++ b/src/lib/data/import-metadata/scripts/mysql-script.ts
@@ -94,7 +94,8 @@ export const getMySQLQuery = (
',"default":"', ${withExtras ? withDefault : withoutDefault},
'","collation":"', IFNULL(cols.collation_name, ''),
'","is_identity":', IF(cols.extra LIKE '%auto_increment%', 'true', 'false'),
- '}'
+ ',"comment":"', REPLACE(REPLACE(IFNULL(cols.column_comment, ''), '"', '\\\\"'), '\\n', ' '),
+ '"}'
)))))
), indexes as (
(SELECT (@indexes:=NULL),
@@ -131,7 +132,8 @@ export const getMySQLQuery = (
'"rows":', IFNULL(\`TABLE_ROWS\`, 0),
', "type":"', IFNULL(\`TABLE_TYPE\`, ''), '",',
'"engine":"', IFNULL(\`ENGINE\`, ''), '",',
- '"collation":"', IFNULL(\`TABLE_COLLATION\`, ''), '"}')))))
+ '"collation":"', IFNULL(\`TABLE_COLLATION\`, ''), '",',
+ '"comment":"', REPLACE(REPLACE(IFNULL(\`TABLE_COMMENT\`, ''), '"', '\\\\"'), '\\\\n', ' '), '"}')))))
), views as (
(SELECT (@views:=NULL),
(SELECT (0)
@@ -219,7 +221,8 @@ export const getMySQLQuery = (
',"ordinal_position":', cols.ordinal_position,
',"nullable":', IF(cols.is_nullable = 'YES', 'true', 'false'),
',"default":"', ${withExtras ? withDefault : withoutDefault},
- '","collation":"', IFNULL(cols.collation_name, ''), '"}')
+ '","collation":"', IFNULL(cols.collation_name, ''),
+ '","comment":"', REPLACE(REPLACE(IFNULL(cols.column_comment, ''), '"', '\\"'), '\\n', ' '), '"}')
) FROM (
SELECT cols.table_schema,
cols.table_name,
@@ -231,7 +234,8 @@ export const getMySQLQuery = (
cols.ordinal_position,
cols.is_nullable,
cols.column_default,
- cols.collation_name
+ cols.collation_name,
+ cols.column_comment
FROM information_schema.columns cols
WHERE cols.table_schema = DATABASE()
) AS cols), ''),
@@ -274,14 +278,16 @@ export const getMySQLQuery = (
'","rows":', IFNULL(tbls.TABLE_ROWS, 0),
',"type":"', IFNULL(tbls.TABLE_TYPE, ''),
'","engine":"', IFNULL(tbls.ENGINE, ''),
- '","collation":"', IFNULL(tbls.TABLE_COLLATION, ''), '"}')
+ '","collation":"', IFNULL(tbls.TABLE_COLLATION, ''),
+ '","comment":"', REPLACE(REPLACE(IFNULL(tbls.TABLE_COMMENT, ''), '"', '\\\\"'), '\\\\n', ' '), '"}')
) FROM (
SELECT \`TABLE_SCHEMA\`,
\`TABLE_NAME\`,
\`TABLE_ROWS\`,
\`TABLE_TYPE\`,
\`ENGINE\`,
- \`TABLE_COLLATION\`
+ \`TABLE_COLLATION\`,
+ \`TABLE_COMMENT\`
FROM information_schema.tables tbls
WHERE tbls.table_schema = DATABASE()
) AS tbls), ''),
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 213cc063..c479fb7c 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
@@ -349,7 +349,7 @@ describe('DBML Export - Issue Fixes', () => {
);
});
- it('should export table and field comments to DBML', () => {
+ it('should export table and field comments to DBML for PostgreSQL', () => {
const diagram: Diagram = {
id: 'test-diagram',
name: 'Test',
@@ -519,6 +519,239 @@ describe('DBML Export - Issue Fixes', () => {
);
});
+ it('should export table and field comments to DBML for MySQL', () => {
+ const diagram: Diagram = {
+ id: 'test-diagram',
+ name: 'Test',
+ databaseType: DatabaseType.MYSQL,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ tables: [
+ {
+ id: 'table1',
+ name: 'pl_a_cmsn',
+ comments: 'Commission table',
+ x: 0,
+ y: 0,
+ fields: [
+ {
+ id: 'field1',
+ name: 'mandt',
+ type: { id: 'char', name: 'char' },
+ primaryKey: true,
+ nullable: false,
+ unique: false,
+ comments: 'Mandant',
+ collation: null,
+ default: null,
+ characterMaximumLength: '3',
+ createdAt: Date.now(),
+ },
+ {
+ id: 'field2',
+ name: 'policy_no',
+ type: { id: 'char', name: 'char' },
+ primaryKey: false,
+ nullable: false,
+ unique: false,
+ comments: 'Policennummer',
+ collation: null,
+ default: null,
+ characterMaximumLength: '25',
+ createdAt: Date.now(),
+ },
+ {
+ id: 'field3',
+ name: 'validity_from',
+ type: { id: 'date', name: 'date' },
+ primaryKey: false,
+ nullable: false,
+ unique: false,
+ comments: 'Gültigkeitsdatum bis',
+ collation: null,
+ default: null,
+ characterMaximumLength: null,
+ createdAt: Date.now(),
+ },
+ ],
+ indexes: [],
+ color: 'blue',
+ isView: false,
+ createdAt: Date.now(),
+ },
+ ],
+ relationships: [],
+ };
+
+ const result = generateDBMLFromDiagram(diagram);
+
+ // Check table exists in DBML
+ expect(result.standardDbml).toContain('Table "pl_a_cmsn" {');
+
+ // Check table comments are preserved for MySQL
+ expect(result.standardDbml).toContain("Note: 'Commission table'");
+
+ // Check field comments are preserved with note: syntax
+ expect(result.standardDbml).toContain(
+ '"mandt" char(3) [pk, not null, note: \'Mandant\']'
+ );
+ expect(result.standardDbml).toContain(
+ '"policy_no" char(25) [not null, note: \'Policennummer\']'
+ );
+ expect(result.standardDbml).toContain(
+ '"validity_from" date [not null, note: \'Gültigkeitsdatum bis\']'
+ );
+
+ // Also check inline DBML
+ expect(result.inlineDbml).toContain('Table "pl_a_cmsn" {');
+ expect(result.inlineDbml).toContain("Note: 'Commission table'");
+ expect(result.inlineDbml).toContain(
+ '"mandt" char(3) [pk, not null, note: \'Mandant\']'
+ );
+ expect(result.inlineDbml).toContain(
+ '"policy_no" char(25) [not null, note: \'Policennummer\']'
+ );
+ expect(result.inlineDbml).toContain(
+ '"validity_from" date [not null, note: \'Gültigkeitsdatum bis\']'
+ );
+ });
+
+ it('should handle multiline comments for MySQL tables and fields', () => {
+ const diagram: Diagram = {
+ id: 'test-diagram',
+ name: 'Test',
+ databaseType: DatabaseType.MYSQL,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ tables: [
+ {
+ id: 'table1',
+ name: 'users',
+ comments:
+ 'This is a multiline\ntable comment\nwith multiple lines',
+ x: 0,
+ y: 0,
+ fields: [
+ {
+ id: 'field1',
+ name: 'id',
+ type: { id: 'bigint', name: 'bigint' },
+ primaryKey: true,
+ nullable: false,
+ unique: false,
+ comments: 'This is a\nmultiline field\ncomment',
+ collation: null,
+ default: null,
+ characterMaximumLength: null,
+ createdAt: Date.now(),
+ },
+ {
+ id: 'field2',
+ name: 'description',
+ type: { id: 'text', name: 'text' },
+ primaryKey: false,
+ nullable: true,
+ unique: false,
+ comments:
+ 'Field with\n\ntabs\tand\n spaces \nand newlines',
+ collation: null,
+ default: null,
+ characterMaximumLength: null,
+ createdAt: Date.now(),
+ },
+ ],
+ indexes: [],
+ color: 'blue',
+ isView: false,
+ createdAt: Date.now(),
+ },
+ ],
+ relationships: [],
+ };
+
+ const result = generateDBMLFromDiagram(diagram);
+
+ // Check that multiline table comment is preserved as single line at the end
+ expect(result.standardDbml).toContain('Table "users" {');
+ expect(result.standardDbml).toContain(
+ "Note: 'This is a multiline table comment with multiple lines'"
+ );
+ // Note should be at the end of the table, before closing brace
+ expect(result.standardDbml).toMatch(
+ /Table "users" \{[\s\S]*Note: 'This is a multiline table comment with multiple lines'\s*\}/m
+ );
+
+ // Check that multiline field comments are preserved as single line
+ expect(result.standardDbml).toContain(
+ '"id" bigint [pk, not null, note: \'This is a multiline field comment\']'
+ );
+ expect(result.standardDbml).toContain(
+ '"description" text [note: \'Field with tabs and spaces and newlines\']'
+ );
+
+ // Also verify in inline DBML
+ expect(result.inlineDbml).toContain(
+ "Note: 'This is a multiline table comment with multiple lines'"
+ );
+ expect(result.inlineDbml).toContain(
+ '"id" bigint [pk, not null, note: \'This is a multiline field comment\']'
+ );
+ });
+
+ it('should handle multiline comments for PostgreSQL tables and fields', () => {
+ const diagram: Diagram = {
+ id: 'test-diagram',
+ name: 'Test',
+ databaseType: DatabaseType.POSTGRESQL,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ tables: [
+ {
+ id: 'table1',
+ name: 'products',
+ comments: 'Product catalog\nwith detailed\ninformation',
+ x: 0,
+ y: 0,
+ fields: [
+ {
+ id: 'field1',
+ name: 'sku',
+ type: { id: 'varchar', name: 'varchar' },
+ primaryKey: true,
+ nullable: false,
+ unique: false,
+ comments: 'Stock Keeping Unit\nUnique identifier',
+ collation: null,
+ default: null,
+ characterMaximumLength: '50',
+ createdAt: Date.now(),
+ },
+ ],
+ indexes: [],
+ color: 'blue',
+ isView: false,
+ createdAt: Date.now(),
+ },
+ ],
+ relationships: [],
+ };
+
+ const result = generateDBMLFromDiagram(diagram);
+
+ // Check that multiline comments are flattened for PostgreSQL too
+ expect(result.standardDbml).toContain('Table "products" {');
+ expect(result.standardDbml).toContain(
+ "Note: 'Product catalog with detailed information'"
+ );
+ // Note should be at the end of the table
+ expect(result.standardDbml).toMatch(
+ /Table "products" \{[\s\S]*Note: 'Product catalog with detailed information'\s*\}/m
+ );
+ expect(result.standardDbml).toContain(
+ '"sku" varchar(50) [pk, not null, note: \'Stock Keeping Unit Unique identifier\']'
+ );
+ });
+
it('should preserve tables with same name but different schemas', () => {
const diagram: Diagram = {
id: 'test-diagram',
diff --git a/src/lib/dbml/dbml-export/dbml-export.ts b/src/lib/dbml/dbml-export/dbml-export.ts
index 231f2011..9ffd8d68 100644
--- a/src/lib/dbml/dbml-export/dbml-export.ts
+++ b/src/lib/dbml/dbml-export/dbml-export.ts
@@ -631,6 +631,107 @@ const restoreIncrementAttribute = (dbml: string, tables: DBTable[]): string => {
return result;
};
+// Restore table and field notes/comments that may have been lost during DBML export
+// This handles databases where @dbml/core doesn't recognize the comment syntax
+// (e.g., MySQL's inline COMMENT syntax). For databases like PostgreSQL where
+// notes are already preserved, this function detects existing notes and skips them.
+const restoreNotes = (dbml: string, tables: DBTable[]): string => {
+ if (!tables || tables.length === 0) return dbml;
+
+ let result = dbml;
+
+ // Helper function to escape comments for DBML
+ const escapeComment = (comment: string): string => {
+ return comment
+ .replace(/\r?\n/g, ' ') // Replace newlines with spaces
+ .replace(/\s+/g, ' ') // Normalize multiple spaces
+ .trim() // Remove leading/trailing whitespace
+ .replace(/\\/g, '\\\\')
+ .replace(/'/g, "\\'")
+ .replace(/"/g, '\\"');
+ };
+
+ tables.forEach((table) => {
+ // Build the table identifier pattern once for this table
+ const tableIdentifier = table.schema
+ ? `"${table.schema.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"\\."${table.name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"`
+ : `"${table.name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"`;
+
+ // Restore table-level notes
+ if (table.comments) {
+ const escapedComment = escapeComment(table.comments);
+
+ // Pattern to match the entire table block
+ const tableBlockPattern = new RegExp(
+ `(Table ${tableIdentifier} \\{)([\\s\\S]*?)(^\\})`,
+ 'gm'
+ );
+
+ result = result.replace(
+ tableBlockPattern,
+ (match, tableStart, tableContent, tableEnd) => {
+ // Check if a Note already exists ANYWHERE in this table
+ if (/^\s*Note:\s*'/m.test(tableContent)) {
+ // Note already exists, don't add another one
+ return match;
+ }
+
+ // Add the Note at the end, before the closing brace (like PostgreSQL)
+ return `${tableStart}${tableContent}\n Note: '${escapedComment}'\n${tableEnd}`;
+ }
+ );
+ }
+
+ // Restore field-level notes
+ const fieldsWithComments = table.fields.filter((f) => f.comments);
+
+ fieldsWithComments.forEach((field) => {
+ // Escape field name for regex
+ const escapedFieldName = field.name.replace(
+ /[.*+?^${}()|[\]\\]/g,
+ '\\$&'
+ );
+
+ // Escape the comment text for use in the replacement
+ const escapedComment = escapeComment(field.comments!);
+
+ // Pattern to match the field line
+ // We need to match the complete field definition including array types
+ // Format: "field_name" type_with_size_and_arrays [attributes]
+ // Examples: "id" bigint [pk], "items" text[] [not null], "name" varchar(100) [unique]
+ const fieldPattern = new RegExp(
+ `(Table ${tableIdentifier} \\{[^}]*?^\\s*"${escapedFieldName}"\\s+\\S+(?:\\([^)]*\\))?(?:\\[\\])?)(\\s*\\[[^\\]]*\\])?`,
+ 'gms'
+ );
+
+ result = result.replace(
+ fieldPattern,
+ (match, fieldPart, brackets) => {
+ // Check if note already exists
+ if (brackets && brackets.includes('note:')) {
+ return match;
+ }
+
+ // Add note to the attributes
+ if (brackets) {
+ // If brackets exist, add note to them
+ const newBrackets = brackets.replace(
+ ']',
+ `, note: '${escapedComment}']`
+ );
+ return fieldPart + newBrackets;
+ } else {
+ // If no brackets, create new ones with note
+ return fieldPart + ` [note: '${escapedComment}']`;
+ }
+ }
+ );
+ });
+ });
+
+ return result;
+};
+
// Restore composite primary key names in the DBML
const restoreCompositePKNames = (dbml: string, tables: DBTable[]): string => {
if (!tables || tables.length === 0) return dbml;
@@ -945,6 +1046,9 @@ export function generateDBMLFromDiagram(diagram: Diagram): DBMLExportResult {
// Restore increment attribute for auto-incrementing fields
standard = restoreIncrementAttribute(standard, tablesWithFields);
+ // Restore table and field notes/comments that may have been lost during DBML export
+ standard = restoreNotes(standard, tablesWithFields);
+
// Prepend Enum DBML to the standard output
if (enumsDBML) {
standard = enumsDBML + '\n\n' + standard;