Skip to content

Add "DROP TABLE" before "CREATE TABLE" in SQL export (#486) #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 38 additions & 35 deletions src/utils/exportSQL/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) =>
`${
Expand Down Expand Up @@ -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 `${
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion src/utils/exportSQL/mariadb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/utils/exportSQL/mssql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,7 +89,7 @@ export function toMSSQL(diagram) {
)
.join("");

return `${createTableSql}${tableCommentSql}${columnCommentsSql}${indicesSql}`;
return `${dropTablesSql}${createTableSql}${tableCommentSql}${columnCommentsSql}${indicesSql}`;
})
.join("\n");

Expand Down
2 changes: 1 addition & 1 deletion src/utils/exportSQL/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/utils/exportSQL/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/utils/exportSQL/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `${
Expand Down Expand Up @@ -40,5 +40,5 @@ export function toSqlite(diagram) {
)
.join("\n")}`;
})
.join("\n");
}
.join("\n")}`;
}