Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
fix: normalize over-escaped default values in DBML export (#1059)
Jonathan Fishner6aadab174a9e
diff --git a/src/lib/data/sql-export/__tests__/export-sql-dbml.test.ts b/src/lib/data/sql-export/__tests__/export-sql-dbml.test.tsindex a489d9e1..8b617c28 100644--- a/src/lib/data/sql-export/__tests__/export-sql-dbml.test.ts+++ b/src/lib/data/sql-export/__tests__/export-sql-dbml.test.ts@@ -943,5 +943,77 @@ describe('DBML Export - SQL Generation Tests', () => {// Should include precision only when scale is not providedexpect(sql).toContain('"interest_rate" numeric(5)');});++ it('should normalize over-escaped default values', () => {+ const diagram: Diagram = createDiagram({+ id: testId(),+ name: 'Corrupted Defaults Test',+ databaseType: DatabaseType.POSTGRESQL,+ tables: [+ createTable({+ id: testId(),+ name: 'settings',+ fields: [+ createField({+ id: testId(),+ name: 'id',+ type: { id: 'bigint', name: 'bigint' },+ primaryKey: true,+ nullable: false,+ unique: false,+ }),+ createField({+ id: testId(),+ name: 'color',+ type: { id: 'text', name: 'text' },+ primaryKey: false,+ nullable: false,+ unique: false,+ // Over-escaped: '''#999999'' should become '#999999'+ default: "'''#999999''",+ }),+ createField({+ id: testId(),+ name: 'status',+ type: { id: 'text', name: 'text' },+ primaryKey: false,+ nullable: false,+ unique: false,+ // Over-escaped: '''open'' should become 'open'+ default: "'''open''",+ }),+ createField({+ id: testId(),+ name: 'plan_tier',+ type: { id: 'text', name: 'text' },+ primaryKey: false,+ nullable: false,+ unique: false,+ // Double-escaped: ''free'' should become 'free'+ default: "''free''",+ }),+ ],+ indexes: [],+ color: '#888888',+ }),+ ],+ relationships: [],+ });++ const sql = exportBaseSQL({+ diagram,+ targetDatabaseType: DatabaseType.POSTGRESQL,+ isDBMLFlow: true,+ });++ // Should normalize over-escaped quotes to proper single quotes+ expect(sql).toContain("DEFAULT '#999999'");+ expect(sql).toContain("DEFAULT 'open'");+ expect(sql).toContain("DEFAULT 'free'");++ // Should NOT contain the malformed triple/double quotes+ expect(sql).not.toContain("'''");+ expect(sql).not.toContain("''free''");+ });});});diff --git a/src/lib/data/sql-export/export-sql-script.ts b/src/lib/data/sql-export/export-sql-script.tsindex bba6ea62..806d6133 100644--- a/src/lib/data/sql-export/export-sql-script.ts+++ b/src/lib/data/sql-export/export-sql-script.ts@@ -19,6 +19,26 @@ import {supportsCheckConstraints,} from '@/lib/domain/database-capabilities';+// Function to normalize over-escaped default values+// Handles cases like '''value'' which should be 'value'+const normalizeQuotedDefault = (value: string): string => {+ // Check for over-escaped patterns: '''value'', ''value'', etc.+ // These happen when a quoted string gets re-quoted during import/export cycles+ const overEscapedMatch = value.match(/^('{2,})(.*?)('{1,})$/);+ if (overEscapedMatch) {+ const [, leadingQuotes, innerValue, trailingQuotes] = overEscapedMatch;+ // If we have more than one leading/trailing quote, it's over-escaped+ if (leadingQuotes.length > 1 || trailingQuotes.length > 1) {+ // Extract the actual value and re-quote properly+ // First, unescape any doubled quotes in the inner value+ const unescaped = innerValue.replace(/''/g, "'");+ // Return properly quoted string+ return `'${unescaped.replace(/'/g, "''")}'`;+ }+ }+ return value;+};+// Function to format default values with proper quotingconst formatDefaultValue = (value: string): string => {const trimmed = value.trim();@@ -50,12 +70,13 @@ const formatDefaultValue = (value: string): string => {return trimmed;}- // Already quoted strings - keep as is+ // Already quoted strings - normalize and returnif ((trimmed.startsWith("'") && trimmed.endsWith("'")) ||(trimmed.startsWith('"') && trimmed.endsWith('"'))) {- return trimmed;+ // Normalize over-escaped quotes (e.g., '''value'' -> 'value')+ return normalizeQuotedDefault(trimmed);}// Check if it's a simple identifier (alphanumeric, no spaces) that might be a currency or enum