diff --git a/src/utils/exportSQL/generic.js b/src/utils/exportSQL/generic.js index e9d576378..6ba3a74eb 100644 --- a/src/utils/exportSQL/generic.js +++ b/src/utils/exportSQL/generic.js @@ -184,7 +184,7 @@ export function getTypeString( } export function jsonToMySQL(obj) { - return `${obj.tables + return `${obj.tables.map((table) => `DROP TABLE \`${table.name}\`;`).join("\n")}\n\n${obj.tables .map( (table) => `CREATE TABLE \`${table.name}\` (\n${table.fields @@ -246,39 +246,42 @@ export function jsonToMySQL(obj) { } export function jsonToPostgreSQL(obj) { - return `${obj.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(", ")});`, - ) - .join("\n"); - if (typeStatements.length > 0) { - return ( - typeStatements.join("") + - `${ - type.comment === "" ? "" : `/**\n${type.comment}\n*/\n` - }CREATE TYPE ${type.name} AS (\n${type.fields + return `${obj.tables.map((table) => `DROP TABLE IF EXISTS \`${table.name}\`;`).join("\n")}\n${obj.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(", ")});`, + ) + .join("\n"); + if (typeStatements.length > 0) { + return ( + typeStatements.join("") + + `${ + type.comment === "" ? "" : `/**\n${type.comment}\n*/\n` + }CREATE TYPE ${type.name} AS (\n${type.fields + .map( + (f) => + `\t${f.name} ${getTypeString(f, obj.database, DB.POSTGRES)}`, + ) + .join("\n")}\n);` + ); + } else { + return `CREATE TYPE ${type.name} AS (\n${type.fields .map( (f) => `\t${f.name} ${getTypeString(f, obj.database, DB.POSTGRES)}`, ) - .join("\n")}\n);` - ); - } else { - return `CREATE TYPE ${type.name} AS (\n${type.fields - .map( - (f) => `\t${f.name} ${getTypeString(f, obj.database, DB.POSTGRES)}`, - ) - .join(",\n")}\n);\n${ - type.comment && type.comment.trim() != "" - ? `\nCOMMENT ON TYPE ${type.name} IS '${escapeQuotes(type.comment)}';\n` - : "" - }`; - } - })}\n${obj.tables + .join(",\n")}\n);\n${ + type.comment && type.comment.trim() != "" + ? `\nCOMMENT ON TYPE ${type.name} IS '${escapeQuotes(type.comment)}';\n` + : "" + }`; + } + }, + )}\n${obj.tables .map( (table) => `${ @@ -386,7 +389,7 @@ export function getSQLiteType(field) { } export function jsonToSQLite(obj) { - return obj.tables + return `${obj.tables.map((table) => `DROP TABLE IF EXISTS \`${table.name}\`;`).join("\n")}\n\n${obj.tables .map((table) => { const inlineFK = getInlineFK(table, obj); return `${ @@ -423,11 +426,11 @@ export function jsonToSQLite(obj) { ) .join("\n")}`; }) - .join("\n"); + .join("\n")}`; } export function jsonToMariaDB(obj) { - return `${obj.tables + return `${obj.tables.map((table) => `DROP TABLE IF EXISTS \`${table.name}\`;`).join("\n")}\n\n${obj.tables .map( (table) => `CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields @@ -491,7 +494,7 @@ export function jsonToMariaDB(obj) { } export function jsonToSQLServer(obj) { - return `${obj.types + return `${obj.tables.map((table) => `DROP TABLE IF EXISTS [${table.name}];`).join("\n")}\n${obj.types .map((type) => { return `${ type.comment === "" ? "" : `/**\n${type.comment}\n*/\n` diff --git a/src/utils/exportSQL/mariadb.js b/src/utils/exportSQL/mariadb.js index 8bab3bc63..856fe6e2b 100644 --- a/src/utils/exportSQL/mariadb.js +++ b/src/utils/exportSQL/mariadb.js @@ -18,7 +18,7 @@ function parseType(field) { } export function toMariaDB(diagram) { - return `${diagram.tables + return `${diagram.tables.map((table) => `DROP TABLE IF EXISTS \`${table.name}\`;`).join("\n")}\n\n${diagram.tables .map( (table) => `CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields diff --git a/src/utils/exportSQL/mssql.js b/src/utils/exportSQL/mssql.js index a2354d358..901c34d74 100644 --- a/src/utils/exportSQL/mssql.js +++ b/src/utils/exportSQL/mssql.js @@ -32,6 +32,7 @@ GO } export function toMSSQL(diagram) { + const dropTablesSql = `${diagram.tables.map((table) => `DROP TABLE IF EXISTS [${table.name}];`).join("\n")}\n\n`; const tablesSql = diagram.tables .map((table) => { const fieldsSql = table.fields @@ -88,7 +89,7 @@ export function toMSSQL(diagram) { ) .join(""); - return `${createTableSql}${tableCommentSql}${columnCommentsSql}${indicesSql}`; + return `${dropTablesSql}${createTableSql}${tableCommentSql}${columnCommentsSql}${indicesSql}`; }) .join("\n"); diff --git a/src/utils/exportSQL/mysql.js b/src/utils/exportSQL/mysql.js index 6d9d4fb01..dde6535b9 100644 --- a/src/utils/exportSQL/mysql.js +++ b/src/utils/exportSQL/mysql.js @@ -21,7 +21,7 @@ function parseType(field) { } export function toMySQL(diagram) { - return `${diagram.tables + return `${diagram.tables.map((table) => `DROP TABLE \`${table.name}\`;`).join("\n")}\n\n${diagram.tables .map( (table) => `CREATE TABLE \`${table.name}\` (\n${table.fields diff --git a/src/utils/exportSQL/postgres.js b/src/utils/exportSQL/postgres.js index c4b7b1289..f3e304d4e 100644 --- a/src/utils/exportSQL/postgres.js +++ b/src/utils/exportSQL/postgres.js @@ -23,7 +23,7 @@ export function toPostgres(diagram) { ) .join("\n"); - return `${enumStatements}${enumStatements.trim() !== "" ? `\n${typeStatements}` : typeStatements}${diagram.tables + return `${diagram.tables.map((table) => `DROP TABLE IF EXISTS \`${table.name}\`;`).join("\n")}\n\n${enumStatements}${enumStatements.trim() !== "" ? `\n${typeStatements}` : typeStatements}${diagram.tables .map( (table) => `CREATE TABLE "${table.name}" (\n${table.fields diff --git a/src/utils/exportSQL/sqlite.js b/src/utils/exportSQL/sqlite.js index 25ae186c7..c05133c4d 100644 --- a/src/utils/exportSQL/sqlite.js +++ b/src/utils/exportSQL/sqlite.js @@ -3,7 +3,7 @@ import { exportFieldComment, getInlineFK, parseDefault } from "./shared"; import { dbToTypes } from "../../data/datatypes"; export function toSqlite(diagram) { - return diagram.tables + return `${diagram.tables.map((table) => `DROP TABLE IF EXISTS \`${table.name}\`;`).join("\n")}\n\n${diagram.tables .map((table) => { const inlineFK = getInlineFK(table, diagram); return `${ @@ -40,5 +40,5 @@ export function toSqlite(diagram) { ) .join("\n")}`; }) - .join("\n"); -} \ No newline at end of file + .join("\n")}`; +}