From cb22a0f271451bfcc80c766bfe9b7ebf758dcf59 Mon Sep 17 00:00:00 2001 From: 1ilit <1ilit@proton.me> Date: Wed, 19 Mar 2025 02:01:20 +0400 Subject: [PATCH 1/2] Fix syntax errors in types for pg export --- src/utils/exportSQL/postgres.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/utils/exportSQL/postgres.js b/src/utils/exportSQL/postgres.js index d6b81078..fb35f451 100644 --- a/src/utils/exportSQL/postgres.js +++ b/src/utils/exportSQL/postgres.js @@ -13,17 +13,17 @@ export function toPostgres(diagram) { const typeStatements = diagram.types .map( (type) => - `\nCREATE TYPE ${type.name} AS (\n${type.fields + `CREATE TYPE ${type.name} AS (\n${type.fields .map((f) => `\t${f.name} ${f.type}`) - .join("\n")}\n);\n\n${ - type.comment.trim() !== "" + .join(",\n")}\n);\n\n${ + type.comment && type.comment.trim() !== "" ? `\nCOMMENT ON TYPE "${type.name}" IS '${type.comment}';\n\n` : "" }`, ) .join("\n"); - return `${enumStatements}${typeStatements}${diagram.tables + return `${enumStatements}${enumStatements.trim() !== "" ? `\n${typeStatements}` : typeStatements}${diagram.tables .map( (table) => `CREATE TABLE "${table.name}" (\n${table.fields @@ -32,7 +32,9 @@ export function toPostgres(diagram) { `${exportFieldComment(field.comment)}\t"${ field.name }" ${field.type}${ - field.size !== undefined && field.size !== "" ? "(" + field.size + ")" : "" + field.size !== undefined && field.size !== "" + ? "(" + field.size + ")" + : "" }${field.isArray ? " ARRAY" : ""}${field.notNull ? " NOT NULL" : ""}${field.unique ? " UNIQUE" : ""}${ field.increment ? " GENERATED BY DEFAULT AS IDENTITY" : "" }${ @@ -53,7 +55,7 @@ export function toPostgres(diagram) { .map((f) => `"${f.name}"`) .join(", ")})` : "" - }\n);\n${ + }\n);${ table.comment.trim() !== "" ? `\nCOMMENT ON TABLE "${table.name}" IS '${table.comment}';\n` : "" @@ -74,10 +76,10 @@ export function toPostgres(diagram) { ) .join("\n")}\n`, ) - .join("\n")}\n${diagram.references + .join("\n")}${diagram.references .map( (r) => - `ALTER TABLE "${diagram.tables[r.startTableId].name}"\nADD FOREIGN KEY("${ + `\nALTER TABLE "${diagram.tables[r.startTableId].name}"\nADD FOREIGN KEY("${ diagram.tables[r.startTableId].fields[r.startFieldId].name }") REFERENCES "${diagram.tables[r.endTableId].name}"("${ diagram.tables[r.endTableId].fields[r.endFieldId].name From 55f12e27cd8d2aa5733089278dcb37894b5f383d Mon Sep 17 00:00:00 2001 From: 1ilit <1ilit@proton.me> Date: Wed, 19 Mar 2025 02:12:36 +0400 Subject: [PATCH 2/2] Fix syntax errors in types for generic pg export --- src/utils/exportSQL/generic.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils/exportSQL/generic.js b/src/utils/exportSQL/generic.js index cfb09227..a3d17be4 100644 --- a/src/utils/exportSQL/generic.js +++ b/src/utils/exportSQL/generic.js @@ -220,9 +220,11 @@ export function jsonToPostgreSQL(obj) { } else { return `CREATE TYPE ${type.name} AS (\n${type.fields .map((f) => `\t${f.name} ${getTypeString(f, obj.database, "postgres")}`) - .join( - "\n", - )}\n);\n${type.comment != "" ? `\nCOMMENT ON TYPE ${type.name} IS '${type.comment}';\n` : ""}`; + .join(",\n")}\n);\n${ + type.comment && type.comment.trim() != "" + ? `\nCOMMENT ON TYPE ${type.name} IS '${type.comment}';\n` + : "" + }`; } })}\n${obj.tables .map(