Skip to content

Free, simple, and intuitive online database diagram editor and SQL generator.

Clean up postgres export

1ilitc1f651cb545b
diff --git a/src/utils/exportSQL/postgres.js b/src/utils/exportSQL/postgres.js
index 7e3846af..d8b138ae 100644
--- a/src/utils/exportSQL/postgres.js
+++ b/src/utils/exportSQL/postgres.js
@@ -5,57 +5,33 @@ export function toPostgres(diagram) {
const enumStatements = diagram.enums
.map(
(e) =>
- `CREATE TYPE "${e.name}" AS ENUM (\n${e.values.map((v) => `\t'${v}'`).join("\n")}\n);`,
+ `CREATE TYPE "${e.name}" AS ENUM (\n${e.values.map((v) => `\t'${v}'`).join(",\n")}\n);`,
)
- .join("\n");
+ .join("\n\n");
- return `${enumStatements}\n${diagram.types.map((type) => {
- const typeStatements = type.fields
- .filter((f) => f.type === "ENUM" || f.type === "SET")
- .map(
- (f) =>
- `CREATE TYPE "${f.name}_t" AS ENUM (${f.values
- .map((v) => `'${v}'`)
- .join(", ")});\n`,
- );
- if (typeStatements.length > 0) {
- return (
- typeStatements.join("") +
- `${
- type.comment === "" ? "" : `/**\n${type.comment}\n*/\n`
- }CREATE TYPE ${type.name} AS (\n${type.fields
+ const typeStatements = diagram.types
+ .map(
+ (type) =>
+ `CREATE TYPE ${type.name} AS (\n${type.fields
.map((f) => `\t${f.name} ${f.type}`)
- .join("\n")}\n);`
- );
- } else {
- return `${
- type.comment === "" ? "" : `/**\n${type.comment}\n*/\n`
- }CREATE TYPE ${type.name} AS (\n${type.fields
- .map((f) => `\t${f.name} ${f.type}`)
- .join("\n")}\n);`;
- }
- })}\n${diagram.tables
+ .join("\n")}\n);\n${
+ type.comment.trim() !== ""
+ ? `\nCOMMENT ON TYPE "${type.name}" IS '${type.comment}';\n`
+ : ""
+ }`,
+ )
+ .join("\n");
+
+ return `${enumStatements}\n\n${typeStatements}\n${diagram.tables
.map(
(table) =>
- `${table.comment === "" ? "" : `/**\n${table.comment}\n*/\n`}${
- table.fields.filter((f) => f.type === "ENUM" || f.type === "SET")
- .length > 0
- ? `${table.fields
- .filter((f) => f.type === "ENUM" || f.type === "SET")
- .map(
- (f) =>
- `CREATE TYPE "${f.name}_t" AS ENUM (${f.values
- .map((v) => `'${v}'`)
- .join(", ")});\n\n`,
- )}`
- : ""
- }CREATE TABLE "${table.name}" (\n${table.fields
+ `CREATE TABLE "${table.name}" (\n${table.fields
.map(
(field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t"${
field.name
- }" ${field.type}${field.notNull ? " NOT NULL" : ""}${
- field.default !== ""
+ }" ${field.type}${field.notNull ? " NOT NULL" : ""}${field.unique ? " UNIQUE" : ""}${
+ field.default.trim() !== ""
? ` DEFAULT ${parseDefault(field, diagram.database)}`
: ""
}${
@@ -73,17 +49,19 @@ export function toPostgres(diagram) {
.join(", ")})`
: ""
}\n);\n${
- table.indices.length > 0
- ? `${table.indices.map(
- (i) =>
- `\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX "${
- i.name
- }"\nON "${table.name}" (${i.fields
- .map((f) => `"${f}"`)
- .join(", ")});`,
- )}`
+ table.comment.trim() !== ""
+ ? `\nCOMMENT ON TABLE "${table.name}" IS '${table.comment}';\n`
: ""
- }`,
+ }${table.indices
+ .map(
+ (i) =>
+ `\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX "${
+ i.name
+ }"\nON "${table.name}" (${i.fields
+ .map((f) => `"${f}"`)
+ .join(", ")});`,
+ )
+ .join("\n")}\n`,
)
.join("\n")}\n${diagram.references
.map(