From 20fbc74d57528148d85cf368d2ee0cd06e9489ef Mon Sep 17 00:00:00 2001 From: Pranay Pandey Date: Mon, 6 Oct 2025 23:49:34 +0530 Subject: [PATCH] Add "IF NOT EXISTS" clause in MySQL and PostgreSQL export --- src/utils/exportSQL/generic.js | 4 ++-- src/utils/exportSQL/mysql.js | 2 +- src/utils/exportSQL/postgres.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/exportSQL/generic.js b/src/utils/exportSQL/generic.js index e9d576378..04d220839 100644 --- a/src/utils/exportSQL/generic.js +++ b/src/utils/exportSQL/generic.js @@ -187,7 +187,7 @@ export function jsonToMySQL(obj) { return `${obj.tables .map( (table) => - `CREATE TABLE \`${table.name}\` (\n${table.fields + `CREATE TABLE IF NOT EXISTS \`${table.name}\` (\n${table.fields .map( (field) => `\t\`${ @@ -294,7 +294,7 @@ export function jsonToPostgreSQL(obj) { ) .join("\n")}\n` : "" - }CREATE TABLE "${table.name}" (\n${table.fields + }CREATE TABLE IF NOT EXISTS "${table.name}" (\n${table.fields .map( (field) => `${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t"${ diff --git a/src/utils/exportSQL/mysql.js b/src/utils/exportSQL/mysql.js index a835778e3..f35dbda3f 100644 --- a/src/utils/exportSQL/mysql.js +++ b/src/utils/exportSQL/mysql.js @@ -24,7 +24,7 @@ export function toMySQL(diagram) { return `${diagram.tables .map( (table) => - `CREATE TABLE \`${table.name}\` (\n${table.fields + `CREATE TABLE IF NOT EXISTS \`${table.name}\` (\n${table.fields .map( (field) => `\t\`${field.name}\` ${parseType(field)}${ diff --git a/src/utils/exportSQL/postgres.js b/src/utils/exportSQL/postgres.js index 49b468345..3dc6c0e13 100644 --- a/src/utils/exportSQL/postgres.js +++ b/src/utils/exportSQL/postgres.js @@ -81,7 +81,7 @@ export function toPostgres(diagram) { ) .join("\n"); - return `CREATE TABLE "${table.name}" (\n${fieldDefinitions}${primaryKeyClause}${inheritsClause};\n\n${commentStatements}\n${indexStatements}`; + return `CREATE TABLE IF NOT EXISTS "${table.name}" (\n${fieldDefinitions}${primaryKeyClause}${inheritsClause};\n\n${commentStatements}\n${indexStatements}`; }) .join("\n\n");