Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

add support in schemas for export + temp fixes for user-define types (#295)

Jonathan Fishnerbbced225b1bf
diff --git a/src/lib/data/export-metadata/export-sql-script.ts b/src/lib/data/export-metadata/export-sql-script.ts
index 257ff833..97cffea8 100644
--- a/src/lib/data/export-metadata/export-sql-script.ts
+++ b/src/lib/data/export-metadata/export-sql-script.ts
@@ -20,12 +20,63 @@ export const exportBaseSQL = (diagram: Diagram): string => {
// Initialize the SQL script string
let sqlScript = '';
+ // First create the CREATE SCHEMA statements for all the found schemas based on tables
+ const schemas = new Set<string>();
+ tables.forEach((table) => {
+ if (table.schema) {
+ schemas.add(table.schema);
+ }
+ });
+
+ // Add CREATE SCHEMA statements if any schemas exist
+ schemas.forEach((schema) => {
+ sqlScript += `CREATE SCHEMA IF NOT EXISTS ${schema};\n`;
+ });
+ sqlScript += '\n';
+
+ // Add CREATE SEQUENCE statements
+ const sequences = new Set<string>();
+
+ tables.forEach((table) => {
+ table.fields.forEach((field) => {
+ if (field.default) {
+ // Match nextval('schema.sequence_name') or nextval('sequence_name')
+ const match = field.default.match(
+ /nextval\('([^']+)'(?:::[^)]+)?\)/
+ );
+ if (match) {
+ sequences.add(match[1]);
+ }
+ }
+ });
+ });
+
+ sequences.forEach((sequence) => {
+ sqlScript += `CREATE SEQUENCE IF NOT EXISTS ${sequence};\n`;
+ });
+ sqlScript += '\n';
+
// Loop through each non-view table to generate the SQL statements
nonViewTables.forEach((table) => {
- sqlScript += `CREATE TABLE ${table.name} (\n`;
+ const tableName = table.schema
+ ? `${table.schema}.${table.name}`
+ : table.name;
+ sqlScript += `CREATE TABLE ${tableName} (\n`;
table.fields.forEach((field, index) => {
- sqlScript += ` ${field.name} ${field.type.name}`;
+ let typeName = field.type.name;
+
+ // Temp fix for 'array' to be text[]
+ if (typeName.toLowerCase() === 'array') {
+ typeName = 'text[]';
+ }
+
+ // Temp fix for 'user-defined' to be text
+ if (typeName.toLowerCase() === 'user-defined') {
+ typeName = 'text';
+ }
+
+ sqlScript += ` ${field.name} ${typeName}`;
// Add size for character types
if (field.characterMaximumLength) {
@@ -46,7 +97,14 @@ export const exportBaseSQL = (diagram: Diagram): string => {
// Handle DEFAULT value
if (field.default) {
- sqlScript += ` DEFAULT ${field.default}`;
+ // Temp remove default user-define value when it have it
+ let fieldDefault = field.default;
+
+ // Remove the type cast part after :: if it exists
+ if (fieldDefault.includes('::')) {
+ fieldDefault = fieldDefault.split('::')[0];
+ }
+ sqlScript += ` DEFAULT ${fieldDefault}`;
}
// Handle PRIMARY KEY constraint
@@ -64,13 +122,13 @@ export const exportBaseSQL = (diagram: Diagram): string => {
// Add table comment
if (table.comments) {
- sqlScript += `COMMENT ON TABLE ${table.name} IS '${table.comments}';\n`;
+ sqlScript += `COMMENT ON TABLE ${tableName} IS '${table.comments}';\n`;
}
table.fields.forEach((field) => {
// Add column comment
if (field.comments) {
- sqlScript += `COMMENT ON COLUMN ${table.name}.${field.name} IS '${field.comments}';\n`;
+ sqlScript += `COMMENT ON COLUMN ${tableName}.${field.name} IS '${field.comments}';\n`;
}
});
@@ -85,7 +143,10 @@ export const exportBaseSQL = (diagram: Diagram): string => {
.join(', ');
if (fieldNames) {
- sqlScript += `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${index.name} ON ${table.name} (${fieldNames});\n`;
+ const indexName = table.schema
+ ? `${table.schema}_${index.name}`
+ : index.name;
+ sqlScript += `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName} ON ${tableName} (${fieldNames});\n`;
}
});
@@ -114,7 +175,13 @@ export const exportBaseSQL = (diagram: Diagram): string => {
sourceTableField &&
targetTableField
) {
- sqlScript += `ALTER TABLE ${sourceTable.name} ADD CONSTRAINT ${relationship.name} FOREIGN KEY (${sourceTableField.name}) REFERENCES ${targetTable.name} (${targetTableField.name});\n`;
+ const sourceTableName = sourceTable.schema
+ ? `${sourceTable.schema}.${sourceTable.name}`
+ : sourceTable.name;
+ const targetTableName = targetTable.schema
+ ? `${targetTable.schema}.${targetTable.name}`
+ : targetTable.name;
+ sqlScript += `ALTER TABLE ${sourceTableName} ADD CONSTRAINT ${relationship.name} FOREIGN KEY (${sourceTableField.name}) REFERENCES ${targetTableName} (${targetTableField.name});\n`;
}
});