mirror of
https://github.com/chartdb/chartdb.git
synced 2025-11-02 13:03:17 +00:00
fix(dbml-editor): dealing with dbml editor for non-generic db-type (#624)
* fix(dbml-editor): dealing with dbml editor for non-generic db-type * small change * Handle ENUM type when exporting as varchar --------- Co-authored-by: Guy Ben-Aharon <baguy3@gmail.com>
This commit is contained in:
@@ -6,14 +6,17 @@ import type { DataType } from '../data-types/data-types';
|
||||
import { generateCacheKey, getFromCache, setInCache } from './export-sql-cache';
|
||||
import { exportMSSQL } from './export-per-type/mssql';
|
||||
|
||||
export const exportBaseSQL = (diagram: Diagram): string => {
|
||||
export const exportBaseSQL = (
|
||||
diagram: Diagram,
|
||||
isDBMLFlow: boolean = false
|
||||
): string => {
|
||||
const { tables, relationships } = diagram;
|
||||
|
||||
if (!tables || tables.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (diagram.databaseType === DatabaseType.SQL_SERVER) {
|
||||
if (!isDBMLFlow && diagram.databaseType === DatabaseType.SQL_SERVER) {
|
||||
return exportMSSQL(diagram);
|
||||
}
|
||||
|
||||
@@ -72,6 +75,11 @@ export const exportBaseSQL = (diagram: Diagram): string => {
|
||||
table.fields.forEach((field, index) => {
|
||||
let typeName = field.type.name;
|
||||
|
||||
// Handle ENUM type
|
||||
if (typeName.toLowerCase() === 'enum') {
|
||||
typeName = 'varchar';
|
||||
}
|
||||
|
||||
// Temp fix for 'array' to be text[]
|
||||
if (typeName.toLowerCase() === 'array') {
|
||||
typeName = 'text[]';
|
||||
|
||||
@@ -9,6 +9,7 @@ import { exportBaseSQL } from '@/lib/data/export-metadata/export-sql-script';
|
||||
import type { Diagram } from '@/lib/domain/diagram';
|
||||
import { useToast } from '@/components/toast/use-toast';
|
||||
import { setupDBMLLanguage } from '@/components/code-snippet/languages/dbml-language';
|
||||
import { DatabaseType } from '@/lib/domain/database-type';
|
||||
|
||||
export interface TableDBMLProps {
|
||||
filteredTables: DBTable[];
|
||||
@@ -18,6 +19,24 @@ const getEditorTheme = (theme: EffectiveTheme) => {
|
||||
return theme === 'dark' ? 'dbml-dark' : 'dbml-light';
|
||||
};
|
||||
|
||||
const databaseTypeToImportFormat = (
|
||||
type: DatabaseType
|
||||
): 'mysql' | 'postgres' | 'mssql' => {
|
||||
switch (type) {
|
||||
case DatabaseType.SQL_SERVER:
|
||||
return 'mssql';
|
||||
case DatabaseType.MYSQL:
|
||||
case DatabaseType.MARIADB:
|
||||
return 'mysql';
|
||||
case DatabaseType.POSTGRESQL:
|
||||
case DatabaseType.COCKROACHDB:
|
||||
case DatabaseType.SQLITE:
|
||||
return 'postgres';
|
||||
default:
|
||||
return 'postgres';
|
||||
}
|
||||
};
|
||||
|
||||
export const TableDBML: React.FC<TableDBMLProps> = ({ filteredTables }) => {
|
||||
const { currentDiagram } = useChartDB();
|
||||
const { effectiveTheme } = useTheme();
|
||||
@@ -57,10 +76,13 @@ export const TableDBML: React.FC<TableDBMLProps> = ({ filteredTables }) => {
|
||||
})) ?? [],
|
||||
} satisfies Diagram;
|
||||
|
||||
const baseScript = exportBaseSQL(filteredDiagramWithoutSpaces);
|
||||
const baseScript = exportBaseSQL(filteredDiagramWithoutSpaces, true);
|
||||
|
||||
try {
|
||||
return importer.import(baseScript, 'postgres');
|
||||
const importFormat = databaseTypeToImportFormat(
|
||||
currentDiagram.databaseType
|
||||
);
|
||||
return importer.import(baseScript, importFormat);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user