diff --git a/src/lib/data/sql-export/export-sql-script.ts b/src/lib/data/sql-export/export-sql-script.ts index 826b0f79..fc4a043c 100644 --- a/src/lib/data/sql-export/export-sql-script.ts +++ b/src/lib/data/sql-export/export-sql-script.ts @@ -381,6 +381,16 @@ export const exportBaseSQL = ({ fieldDefault = `now()`; } + // Fix CURRENT_DATE() for PostgreSQL in DBML flow - PostgreSQL uses CURRENT_DATE without parentheses + if ( + isDBMLFlow && + targetDatabaseType === DatabaseType.POSTGRESQL + ) { + if (fieldDefault.toUpperCase() === 'CURRENT_DATE()') { + fieldDefault = 'CURRENT_DATE'; + } + } + sqlScript += ` DEFAULT ${fieldDefault}`; } } diff --git a/src/lib/dbml/dbml-export/__tests__/cases/4.dbml b/src/lib/dbml/dbml-export/__tests__/cases/4.dbml new file mode 100644 index 00000000..3b2bcd98 --- /dev/null +++ b/src/lib/dbml/dbml-export/__tests__/cases/4.dbml @@ -0,0 +1,7 @@ +Table "public"."orders" { + "order_id" integer [pk, not null] + "customer_id" integer [not null] + "order_date" date [not null, default: `CURRENT_DATE`] + "total_amount" numeric [not null, default: 0] + "status" varchar(50) [not null, default: 'Pending'] +} diff --git a/src/lib/dbml/dbml-export/__tests__/cases/4.json b/src/lib/dbml/dbml-export/__tests__/cases/4.json new file mode 100644 index 00000000..4157868e --- /dev/null +++ b/src/lib/dbml/dbml-export/__tests__/cases/4.json @@ -0,0 +1 @@ +{"id":"6b81a1787207","name":"SQL Import (postgresql)","createdAt":"2025-09-15T08:46:26.747Z","updatedAt":"2025-09-17T11:32:13.876Z","databaseType":"postgresql","tables":[{"id":"5ytf0yj9etpmm7mhmhvpu8kfj","name":"orders","schema":"public","order":1,"fields":[{"id":"w7l77cy9hylvlitdovt4ktdmk","name":"order_id","type":{"id":"integer","name":"integer"},"nullable":false,"primaryKey":true,"unique":false,"default":"","createdAt":1757925986747,"increment":true},{"id":"vz7747t5fxrb62v1eepmahv9v","name":"customer_id","type":{"id":"integer","name":"integer"},"nullable":false,"primaryKey":false,"unique":false,"default":"","createdAt":1757925986747,"increment":false},{"id":"geq9qy6sv4ozl2lg9fvcyzxpf","name":"order_date","type":{"name":"date","id":"date","usageLevel":1},"nullable":false,"primaryKey":false,"unique":false,"default":"CURRENT_DATE()","createdAt":1757925986747,"increment":false},{"id":"z928n7umvpec79t2eif7kmde9","name":"total_amount","type":{"name":"numeric","id":"numeric","fieldAttributes":{"precision":{"max":999,"min":1,"default":10},"scale":{"max":999,"min":0,"default":2}}},"nullable":false,"primaryKey":false,"unique":false,"default":"0","createdAt":1757925986747,"increment":false},{"id":"7bkrd0rp1s17bi1lnle6pesc7","name":"status","type":{"name":"varchar","id":"varchar","fieldAttributes":{"hasCharMaxLength":true},"usageLevel":1},"nullable":false,"primaryKey":false,"unique":false,"default":"'Pending'","createdAt":1757925986747,"increment":false,"characterMaximumLength":"50"}],"indexes":[],"x":113,"y":747,"color":"#8eb7ff","isView":false,"createdAt":1757925986747,"diagramId":"6b81a1787207","parentAreaId":null}],"relationships":[],"dependencies":[],"storageMode":"project","lastProjectSavedAt":"2025-09-17T11:32:13.876Z","areas":[],"creationMethod":"imported","customTypes":[]} \ No newline at end of file diff --git a/src/lib/dbml/dbml-export/__tests__/export-sql-dbml-cases.test.ts b/src/lib/dbml/dbml-export/__tests__/export-sql-dbml-cases.test.ts index b4f89317..69bf2bec 100644 --- a/src/lib/dbml/dbml-export/__tests__/export-sql-dbml-cases.test.ts +++ b/src/lib/dbml/dbml-export/__tests__/export-sql-dbml-cases.test.ts @@ -4,64 +4,40 @@ import { generateDBMLFromDiagram } from '../dbml-export'; import * as fs from 'fs'; import * as path from 'path'; -describe('DBML Export - Diagram Case 1 Tests', () => { +const testCase = (caseNumber: string) => { + // Read the JSON file + const jsonPath = path.join(__dirname, 'cases', `${caseNumber}.json`); + const jsonContent = fs.readFileSync(jsonPath, 'utf-8'); + + // Parse the JSON and convert to diagram + const diagram = diagramFromJSONInput(jsonContent); + + // Generate DBML from the diagram + const result = generateDBMLFromDiagram(diagram); + const generatedDBML = result.standardDbml; + + // Read the expected DBML file + const dbmlPath = path.join(__dirname, 'cases', `${caseNumber}.dbml`); + const expectedDBML = fs.readFileSync(dbmlPath, 'utf-8'); + + // Compare the generated DBML with the expected DBML + expect(generatedDBML).toBe(expectedDBML); +}; + +describe('DBML Export cases', () => { it('should handle case 1 diagram', { timeout: 30000 }, async () => { - // Read the JSON file - const jsonPath = path.join(__dirname, 'cases', '1.json'); - const jsonContent = fs.readFileSync(jsonPath, 'utf-8'); - - // Parse the JSON and convert to diagram - const diagram = diagramFromJSONInput(jsonContent); - - // Generate DBML from the diagram - const result = generateDBMLFromDiagram(diagram); - const generatedDBML = result.standardDbml; - - // Read the expected DBML file - const dbmlPath = path.join(__dirname, 'cases', '1.dbml'); - const expectedDBML = fs.readFileSync(dbmlPath, 'utf-8'); - - // Compare the generated DBML with the expected DBML - expect(generatedDBML).toBe(expectedDBML); + testCase('1'); }); it('should handle case 2 diagram', { timeout: 30000 }, async () => { - // Read the JSON file - const jsonPath = path.join(__dirname, 'cases', '2.json'); - const jsonContent = fs.readFileSync(jsonPath, 'utf-8'); - - // Parse the JSON and convert to diagram - const diagram = diagramFromJSONInput(jsonContent); - - // Generate DBML from the diagram - const result = generateDBMLFromDiagram(diagram); - const generatedDBML = result.standardDbml; - - // Read the expected DBML file - const dbmlPath = path.join(__dirname, 'cases', '2.dbml'); - const expectedDBML = fs.readFileSync(dbmlPath, 'utf-8'); - - // Compare the generated DBML with the expected DBML - expect(generatedDBML).toBe(expectedDBML); + testCase('2'); }); it('should handle case 3 diagram', { timeout: 30000 }, async () => { - // Read the JSON file - const jsonPath = path.join(__dirname, 'cases', '3.json'); - const jsonContent = fs.readFileSync(jsonPath, 'utf-8'); + testCase('3'); + }); - // Parse the JSON and convert to diagram - const diagram = diagramFromJSONInput(jsonContent); - - // Generate DBML from the diagram - const result = generateDBMLFromDiagram(diagram); - const generatedDBML = result.standardDbml; - - // Read the expected DBML file - const dbmlPath = path.join(__dirname, 'cases', '3.dbml'); - const expectedDBML = fs.readFileSync(dbmlPath, 'utf-8'); - - // Compare the generated DBML with the expected DBML - expect(generatedDBML).toBe(expectedDBML); + it('should handle case 4 diagram', { timeout: 30000 }, async () => { + testCase('4'); }); });