Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
fix(import-database): auto detect when user try to import ddl script (#698)
Jonathan Fishner5a5e64abef51
diff --git a/src/dialogs/common/import-database/import-database.tsx b/src/dialogs/common/import-database/import-database.tsxindex c2c50df6..d58d1a8f 100644--- a/src/dialogs/common/import-database/import-database.tsx+++ b/src/dialogs/common/import-database/import-database.tsx@@ -40,6 +40,49 @@ import type { editor } from 'monaco-editor';const errorScriptOutputMessage ='Invalid JSON. Please correct it or contact us at [email protected] for help.';+// Helper to detect if content is likely SQL DDL or JSON+const detectContentType = (content: string): 'query' | 'ddl' | null => {+ if (!content || content.trim().length === 0) return null;++ // Common SQL DDL keywords+ const ddlKeywords = [+ 'CREATE TABLE',+ 'ALTER TABLE',+ 'DROP TABLE',+ 'CREATE INDEX',+ 'CREATE VIEW',+ 'CREATE PROCEDURE',+ 'CREATE FUNCTION',+ 'CREATE SCHEMA',+ 'CREATE DATABASE',+ ];++ const upperContent = content.toUpperCase();++ // Check for SQL DDL patterns+ const hasDDLKeywords = ddlKeywords.some((keyword) =>+ upperContent.includes(keyword)+ );+ if (hasDDLKeywords) return 'ddl';++ // Check if it looks like JSON+ try {+ // Just check structure, don't need full parse for detection+ if (+ (content.trim().startsWith('{') && content.trim().endsWith('}')) ||+ (content.trim().startsWith('[') && content.trim().endsWith(']'))+ ) {+ return 'query';+ }+ } catch (error) {+ // Not valid JSON, might be partial+ console.error('Error detecting content type:', error);+ }++ // If we can't confidently detect, return null+ return null;+};+export interface ImportDatabaseProps {goBack?: () => void;onImport: () => void;@@ -184,14 +227,35 @@ export const ImportDatabase: React.FC<ImportDatabaseProps> = ({setIsCheckingJson(false);}, [scriptResult, setScriptResult, formatEditor]);- const handleEditorDidMount = useCallback(- (editor: editor.IStandaloneCodeEditor) => {- editorRef.current = editor;- editor.onDidPaste(() => {+ const detectAndSetImportMethod = useCallback(() => {+ const content = editorRef.current?.getValue();+ if (content && content.trim()) {+ const detectedType = detectContentType(content);+ if (detectedType && detectedType !== importMethod) {+ setImportMethod(detectedType);+ }+ }+ }, [setImportMethod, importMethod]);++ const [editorDidMount, setEditorDidMount] = useState(false);++ useEffect(() => {+ if (editorRef.current && editorDidMount) {+ editorRef.current.onDidPaste(() => {setTimeout(() => {- editor.getAction('editor.action.formatDocument')?.run();+ editorRef.current+ ?.getAction('editor.action.formatDocument')+ ?.run();}, 0);+ setTimeout(detectAndSetImportMethod, 0);});+ }+ }, [detectAndSetImportMethod, editorDidMount]);++ const handleEditorDidMount = useCallback(+ (editor: editor.IStandaloneCodeEditor) => {+ editorRef.current = editor;+ setEditorDidMount(true);},[]);