Skip to content

Commit

Permalink
add support in schemas for export + temp fixes for user-define types (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyfish authored Oct 29, 2024
1 parent 86efeb6 commit bbced22
Showing 1 changed file with 74 additions and 7 deletions.
81 changes: 74 additions & 7 deletions src/lib/data/export-metadata/export-sql-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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`;
}
});

Expand All @@ -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`;
}
});

Expand Down Expand Up @@ -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`;
}
});

Expand Down

0 comments on commit bbced22

Please sign in to comment.