Skip to content

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

fix(dbml): don't corrupt index names containing 'default' (#1127) and fix SQLite autoincrement DBML generation (#1165) (#1170)

Jonathan Fishnerff65341df107
diff --git a/src/lib/data/sql-export/export-sql-script.ts b/src/lib/data/sql-export/export-sql-script.ts
index f3202934..ae6efedf 100644
--- a/src/lib/data/sql-export/export-sql-script.ts
+++ b/src/lib/data/sql-export/export-sql-script.ts
@@ -515,17 +515,9 @@ export const exportBaseSQL = ({
// Never use named constraints to avoid duplicate constraint name issues
if (field.primaryKey && !hasCompositePrimaryKey) {
sqlScript += ' PRIMARY KEY';
-
- // For SQLite with DBML flow, add AUTOINCREMENT after PRIMARY KEY
- if (
- isDBMLFlow &&
- field.increment &&
- targetDatabaseType === DatabaseType.SQLITE &&
- (typeName.toLowerCase() === 'integer' ||
- typeName.toLowerCase() === 'int')
- ) {
- sqlScript += ' AUTOINCREMENT';
- }
+ // No SQLite AUTOINCREMENT here: the DBML flow parses this SQL
+ // with the postgres dialect, which rejects it (#1165). The
+ // increment attribute is restored by restoreIncrementAttribute.
}
// Add a comma after each field except the last one (or before composite PK constraint)
diff --git a/src/lib/dbml/dbml-export/__tests__/dbml-export-open-issues.test.ts b/src/lib/dbml/dbml-export/__tests__/dbml-export-open-issues.test.ts
new file mode 100644
index 00000000..6d6a4498
--- /dev/null
+++ b/src/lib/dbml/dbml-export/__tests__/dbml-export-open-issues.test.ts
@@ -0,0 +1,107 @@
+import { describe, it, expect } from 'vitest';
+import { generateDBMLFromDiagram, sanitizeSQLforDBML } from '../dbml-export';
+import { DatabaseType } from '@/lib/domain/database-type';
+import type { Diagram } from '@/lib/domain/diagram';
+import type { DBField } from '@/lib/domain/db-field';
+
+const field = (overrides: Partial<DBField>): DBField => ({
+ id: overrides.name ?? 'f',
+ name: 'f',
+ type: { id: 'integer', name: 'integer' },
+ primaryKey: false,
+ nullable: true,
+ unique: false,
+ collation: null,
+ default: null,
+ characterMaximumLength: null,
+ createdAt: Date.now(),
+ ...overrides,
+});
+
+const diagramWith = (
+ databaseType: DatabaseType,
+ fields: DBField[],
+ indexes: Diagram['tables'] extends (infer T)[] | undefined
+ ? T extends { indexes: infer I }
+ ? I
+ : never
+ : never = []
+): Diagram => ({
+ id: 'd',
+ name: 'Test',
+ databaseType,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ tables: [
+ {
+ id: 't1',
+ name: 'payment_methods',
+ x: 0,
+ y: 0,
+ fields,
+ indexes,
+ color: 'blue',
+ isView: false,
+ createdAt: Date.now(),
+ },
+ ],
+ relationships: [],
+});
+
+describe('DBML export - open issues', () => {
+ // https://github.com/chartdb/chartdb/issues/1127
+ it('does not corrupt index names containing "default"', () => {
+ const sql =
+ 'CREATE INDEX idx_payment_methods_is_default ON "payment_methods" ("is_default");';
+ expect(sanitizeSQLforDBML(sql)).toBe(sql);
+
+ const result = generateDBMLFromDiagram(
+ diagramWith(
+ DatabaseType.POSTGRESQL,
+ [
+ field({ id: 'id', name: 'id', primaryKey: true }),
+ field({
+ id: 'is_default',
+ name: 'is_default',
+ type: { id: 'boolean', name: 'boolean' },
+ default: 'false',
+ }),
+ ],
+ [
+ {
+ id: 'i1',
+ name: 'idx_payment_methods_is_default',
+ unique: false,
+ fieldIds: ['is_default'],
+ createdAt: Date.now(),
+ },
+ ]
+ )
+ );
+ expect(result.error).toBeUndefined();
+ expect(result.standardDbml).toContain('idx_payment_methods_is_default');
+ });
+
+ it('still quotes bare identifier defaults', () => {
+ expect(sanitizeSQLforDBML('"status" varchar(10) DEFAULT active,')).toBe(
+ `"status" varchar(10) DEFAULT 'active',`
+ );
+ });
+
+ // https://github.com/chartdb/chartdb/issues/1165
+ it('generates DBML for SQLite autoincrement primary key', () => {
+ const result = generateDBMLFromDiagram(
+ diagramWith(DatabaseType.SQLITE, [
+ field({
+ id: 'id',
+ name: 'id',
+ primaryKey: true,
+ nullable: false,
+ increment: true,
+ }),
+ ])
+ );
+ expect(result.error).toBeUndefined();
+ expect(result.standardDbml).toMatch(/"id" integer \[[^\]]*increment/);
+ });
+});
diff --git a/src/lib/dbml/dbml-export/dbml-export.ts b/src/lib/dbml/dbml-export/dbml-export.ts
index 007c935d..426baec3 100644
--- a/src/lib/dbml/dbml-export/dbml-export.ts
+++ b/src/lib/dbml/dbml-export/dbml-export.ts
@@ -245,7 +245,8 @@ export const sanitizeSQLforDBML = (sql: string): string => {
'UUID',
]);
sanitized = sanitized.replace(
- /DEFAULT\s+([a-zA-Z_]\w*)(?=[\s,);]|$)/gi,
+ // \b prevents matching inside identifiers like idx_is_default (#1127)
+ /\bDEFAULT\s+([a-zA-Z_]\w*)(?=[\s,);]|$)/gi,
(match, identifier) => {
if (sqlDefaultKeywords.has(identifier.toUpperCase())) {
return match;
@@ -256,7 +257,7 @@ export const sanitizeSQLforDBML = (sql: string): string => {
// Fix DEFAULT NOW by replacing with NOW()
sanitized = sanitized.replace(
- /DEFAULT\s+NOW(?=[\s,);]|$)/gi,
+ /\bDEFAULT\s+NOW(?=[\s,);]|$)/gi,
'DEFAULT NOW()'
);