Skip to content

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

feat(import-sql): import postgresql via SQL (DDL script) (#639)

Jonathan Fishnerf7a6e0cb5e49
diff --git a/src/dialogs/common/import-database/import-database.tsx b/src/dialogs/common/import-database/import-database.tsx
index fdb0ee0e..08d0777b 100644
--- a/src/dialogs/common/import-database/import-database.tsx
+++ b/src/dialogs/common/import-database/import-database.tsx
@@ -1,4 +1,4 @@
-import React, { useCallback, useEffect, useMemo, useState } from 'react';
+import React, { Suspense, useCallback, useEffect, useState } from 'react';
import { Button } from '@/components/button/button';
import {
DialogClose,
@@ -8,32 +8,10 @@ import {
DialogInternalContent,
DialogTitle,
} from '@/components/dialog/dialog';
-import { ToggleGroup, ToggleGroupItem } from '@/components/toggle/toggle-group';
-import { DatabaseType } from '@/lib/domain/database-type';
-import { databaseSecondaryLogoMap } from '@/lib/databases';
-import { CodeSnippet } from '@/components/code-snippet/code-snippet';
-import { Textarea } from '@/components/textarea/textarea';
+import type { DatabaseType } from '@/lib/domain/database-type';
+import { Editor } from '@/components/code-snippet/code-snippet';
import type { DatabaseEdition } from '@/lib/domain/database-edition';
-import {
- databaseEditionToImageMap,
- databaseEditionToLabelMap,
- databaseTypeToEditionMap,
-} from '@/lib/domain/database-edition';
-import {
- Avatar,
- AvatarFallback,
- AvatarImage,
-} from '@/components/avatar/avatar';
-import { SSMSInfo } from './ssms-info/ssms-info';
import { useTranslation } from 'react-i18next';
-import { Tabs, TabsList, TabsTrigger } from '@/components/tabs/tabs';
-import type { DatabaseClient } from '@/lib/domain/database-clients';
-import {
- databaseClientToLabelMap,
- databaseTypeToClientsMap,
- databaseEditionToClientsMap,
-} from '@/lib/domain/database-clients';
-import type { ImportMetadataScripts } from '@/lib/data/import-metadata/scripts/scripts';
import { ZoomableImage } from '@/components/zoomable-image/zoomable-image';
import { useBreakpoint } from '@/hooks/use-breakpoint';
import { Spinner } from '@/components/spinner/spinner';
@@ -41,6 +19,16 @@ import {
fixMetadataJson,
isStringMetadataJson,
} from '@/lib/data/import-metadata/utils';
+import {
+ ResizableHandle,
+ ResizablePanel,
+ ResizablePanelGroup,
+} from '@/components/resizable/resizable';
+import { useTheme } from '@/hooks/use-theme';
+import type { OnChange } from '@monaco-editor/react';
+import { useDebounce } from '@/hooks/use-debounce-v2';
+import { InstructionsSection } from './instructions-section/instructions-section';
+import { parseSQLError } from '@/lib/data/sql-import';
const errorScriptOutputMessage =
'Invalid JSON. Please correct it or contact us at [email protected] for help.';
@@ -58,6 +46,8 @@ export interface ImportDatabaseProps {
>;
keepDialogAfterImport?: boolean;
title: string;
+ importMethod: 'query' | 'ddl';
+ setImportMethod: (method: 'query' | 'ddl') => void;
}
export const ImportDatabase: React.FC<ImportDatabaseProps> = ({
@@ -71,42 +61,51 @@ export const ImportDatabase: React.FC<ImportDatabaseProps> = ({
setDatabaseEdition,
keepDialogAfterImport,
title,
+ importMethod,
+ setImportMethod,
}) => {
- const databaseClients = useMemo(
- () => [
- ...databaseTypeToClientsMap[databaseType],
- ...(databaseEdition
- ? databaseEditionToClientsMap[databaseEdition]
- : []),
- ],
- [databaseType, databaseEdition]
- );
+ const { effectiveTheme } = useTheme();
const [errorMessage, setErrorMessage] = useState('');
- const [databaseClient, setDatabaseClient] = useState<
- DatabaseClient | undefined
- >();
- const { t } = useTranslation();
- const [importMetadataScripts, setImportMetadataScripts] =
- useState<ImportMetadataScripts | null>(null);
+ const { t } = useTranslation();
const { isSm: isDesktop } = useBreakpoint('sm');
const [showCheckJsonButton, setShowCheckJsonButton] = useState(false);
const [isCheckingJson, setIsCheckingJson] = useState(false);
-
const [showSSMSInfoDialog, setShowSSMSInfoDialog] = useState(false);
useEffect(() => {
- const loadScripts = async () => {
- const { importMetadataScripts } = await import(
- '@/lib/data/import-metadata/scripts/scripts'
- );
- setImportMetadataScripts(importMetadataScripts);
- };
- loadScripts();
- }, []);
+ setScriptResult('');
+ setErrorMessage('');
+ setShowCheckJsonButton(false);
+ }, [importMethod, setScriptResult]);
+ // Check if the ddl is valid
useEffect(() => {
+ if (importMethod !== 'ddl') {
+ return;
+ }
+
+ if (!scriptResult.trim()) return;
+
+ const result = parseSQLError({
+ sqlContent: scriptResult,
+ sourceDatabaseType: databaseType,
+ });
+
+ if (result.success) {
+ setErrorMessage('');
+ } else if (!result.success && result.error) {
+ setErrorMessage(result.error);
+ }
+ }, [importMethod, scriptResult, databaseType]);
+
+ // Check if the script result is a valid JSON
+ useEffect(() => {
+ if (importMethod !== 'query') {
+ return;
+ }
+
if (scriptResult.trim().length === 0) {
setErrorMessage('');
setShowCheckJsonButton(false);
@@ -126,7 +125,7 @@ export const ImportDatabase: React.FC<ImportDatabaseProps> = ({
setErrorMessage(errorScriptOutputMessage);
setShowCheckJsonButton(false);
}
- }, [scriptResult]);
+ }, [scriptResult, importMethod]);
const handleImport = useCallback(() => {
if (errorMessage.length === 0 && scriptResult.trim().length !== 0) {
@@ -134,19 +133,20 @@ export const ImportDatabase: React.FC<ImportDatabaseProps> = ({
}
}, [errorMessage.length, onImport, scriptResult]);
- const handleInputChange = useCallback(
- (e: React.ChangeEvent<HTMLTextAreaElement>) => {
- const inputValue = e.target.value;
- setScriptResult(inputValue);
+ const handleInputChange: OnChange = useCallback(
+ (inputValue) => {
+ setScriptResult(inputValue ?? '');
// Automatically open SSMS info when input length is exactly 65535
- if (inputValue.length === 65535) {
+ if ((inputValue ?? '').length === 65535) {
setShowSSMSInfoDialog(true);
}
},
[setScriptResult]
);
+ const debouncedHandleInputChange = useDebounce(handleInputChange, 500);
+
const handleCheckJson = useCallback(async () => {
setIsCheckingJson(true);
@@ -176,225 +176,131 @@ export const ImportDatabase: React.FC<ImportDatabaseProps> = ({
const renderContent = useCallback(() => {
return (
<DialogInternalContent>
- <div className="flex w-full flex-1 flex-col gap-6">
- {databaseTypeToEditionMap[databaseType].length > 0 ? (
- <div className="flex flex-col gap-1 md:flex-row">
- <p className="text-sm leading-6 text-muted-foreground">
- {t(
- 'new_diagram_dialog.import_database.database_edition'
- )}
- </p>
- <ToggleGroup
- type="single"
- className="ml-1 flex-wrap gap-2"
- value={
- !databaseEdition
- ? 'regular'
- : databaseEdition
- }
- onValueChange={(value) => {
- setDatabaseEdition(
- value === 'regular'
- ? undefined
- : (value as DatabaseEdition)
- );
- }}
- >
- <ToggleGroupItem
- value="regular"
- variant="outline"
- className="h-6 gap-1 p-0 px-2 shadow-none"
- >
- <Avatar className="size-4 rounded-none">
- <AvatarImage
- src={
- databaseSecondaryLogoMap[
- databaseType
- ]
- }
- alt="Regular"
- />
- <AvatarFallback>Regular</AvatarFallback>
- </Avatar>
- Regular
- </ToggleGroupItem>
- {databaseTypeToEditionMap[databaseType].map(
- (edition) => (
- <ToggleGroupItem
- value={edition}
- key={edition}
- variant="outline"
- className="h-6 gap-1 p-0 px-2 shadow-none"
- >
- <Avatar className="size-4">
- <AvatarImage
- src={
- databaseEditionToImageMap[
- edition
- ]
- }
- alt={
- databaseEditionToLabelMap[
- edition
- ]
- }
- />
- <AvatarFallback>
- {
- databaseEditionToLabelMap[
- edition
- ]
- }
- </AvatarFallback>
- </Avatar>
- {databaseEditionToLabelMap[edition]}
- </ToggleGroupItem>
- )
- )}
- </ToggleGroup>
- </div>
- ) : null}
- <div className="flex flex-col gap-1">
- <div className="flex flex-col gap-1 text-sm text-muted-foreground md:flex-row md:justify-between">
- <div>
- 1.{' '}
- {t('new_diagram_dialog.import_database.step_1')}
+ <ResizablePanelGroup
+ direction={isDesktop ? 'horizontal' : 'vertical'}
+ className="min-h-[500px] md:min-h-fit"
+ >
+ <ResizablePanel
+ defaultSize={25}
+ minSize={25}
+ maxSize={99}
+ className="min-h-fit rounded-md bg-gradient-to-b from-slate-50 to-slate-100 p-2 dark:from-slate-900 dark:to-slate-800 md:min-h-fit md:min-w-[350px] md:rounded-l-md md:p-2"
+ >
+ <InstructionsSection
+ databaseType={databaseType}
+ importMethod={importMethod}
+ setDatabaseEdition={setDatabaseEdition}
+ setImportMethod={setImportMethod}
+ databaseEdition={databaseEdition}
+ setShowSSMSInfoDialog={setShowSSMSInfoDialog}
+ showSSMSInfoDialog={showSSMSInfoDialog}
+ />
+ </ResizablePanel>
+ {isDesktop ? <ResizableHandle withHandle /> : null}
+ <ResizablePanel className="min-h-40 py-2 md:px-2 md:py-0">
+ <div className="flex size-full flex-col gap-1 overflow-hidden rounded-md border p-1">
+ <div className="w-full text-center text-xs text-muted-foreground">
+ {importMethod === 'query'
+ ? 'Smart Query Output'
+ : 'SQL DDL'}
</div>
- {databaseType === DatabaseType.SQL_SERVER && (
- <SSMSInfo
- open={showSSMSInfoDialog}
- setOpen={setShowSSMSInfoDialog}
- />
- )}
- </div>
- {databaseClients.length > 0 ? (
- <Tabs
- value={
- !databaseClient
- ? 'dbclient'
- : databaseClient
- }
- onValueChange={(value) => {
- setDatabaseClient(
- value === 'dbclient'
- ? undefined
- : (value as DatabaseClient)
- );
- }}
- >
- <div className="flex flex-1">
- <TabsList className="h-8 justify-start rounded-none rounded-t-sm ">
- <TabsTrigger
- value="dbclient"
- className="h-6 w-20"
- >
- DB Client
- </TabsTrigger>
+ <div className="flex-1 overflow-hidden">
+ <Suspense fallback={<Spinner />}>
+ <Editor
+ value={scriptResult}
+ onChange={debouncedHandleInputChange}
+ language={
+ importMethod === 'query'
+ ? 'json'
+ : 'sql'
+ }
+ loading={<Spinner />}
+ theme={
+ effectiveTheme === 'dark'
+ ? 'dbml-dark'
+ : 'dbml-light'
+ }
+ options={{
+ minimap: { enabled: false },
+ scrollBeyondLastLine: false,
+ automaticLayout: true,
+ glyphMargin: false,
+ lineNumbers: 'on',
+ guides: {
+ indentation: false,
+ },
+ folding: true,
+ lineNumbersMinChars: 3,
+ renderValidationDecorations: 'off',
+ lineDecorationsWidth: 0,
+ overviewRulerBorder: false,
+ overviewRulerLanes: 0,
+ hideCursorInOverviewRuler: true,
+ contextmenu: false,
- {databaseClients?.map((client) => (
- <TabsTrigger
- key={client}
- value={client}
- className="h-6 !w-20"
- >
- {
- databaseClientToLabelMap[
- client
- ]
- }
- </TabsTrigger>
- )) ?? []}
- </TabsList>
- </div>
- <CodeSnippet
- className="h-40 w-full"
- loading={!importMetadataScripts}
- code={
- importMetadataScripts?.[databaseType]?.(
- {
- databaseEdition,
- databaseClient,
- }
- ) ?? ''
- }
- language={databaseClient ? 'shell' : 'sql'}
- />
- </Tabs>
- ) : (
- <CodeSnippet
- className="h-40 w-full flex-auto"
- loading={!importMetadataScripts}
- code={
- importMetadataScripts?.[databaseType]?.({
- databaseEdition,
- }) ?? ''
- }
- language="sql"
- />
- )}
- </div>
- <div className="flex h-48 flex-col gap-1">
- <p className="text-sm text-muted-foreground">
- 2. {t('new_diagram_dialog.import_database.step_2')}
- </p>
- <Textarea
- className="w-full flex-1 rounded-md bg-muted p-2 text-sm"
- placeholder={t(
- 'new_diagram_dialog.import_database.script_results_placeholder'
- )}
- value={scriptResult}
- onChange={handleInputChange}
- />
- {showCheckJsonButton || errorMessage ? (
- <div className="mt-2 flex items-center gap-2">
- {showCheckJsonButton ? (
- <Button
- type="button"
- variant="outline"
- size="sm"
- onClick={handleCheckJson}
- disabled={isCheckingJson}
- >
- {isCheckingJson ? (
- <Spinner size="small" />
- ) : (
- t(
- 'new_diagram_dialog.import_database.check_script_result'
- )
- )}
- </Button>
- ) : (
- <p className="text-sm text-red-700">
- {errorMessage}
- </p>
- )}
+ scrollbar: {
+ vertical: 'hidden',
+ horizontal: 'hidden',
+ alwaysConsumeMouseWheel: false,
+ },
+ }}
+ className="size-full"
+ />
+ </Suspense>
</div>
- ) : null}
- </div>
- </div>
+
+ {showCheckJsonButton || errorMessage ? (
+ <div className="mt-2 flex shrink-0 items-center gap-2">
+ {showCheckJsonButton ? (
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ onClick={handleCheckJson}
+ disabled={isCheckingJson}
+ className="h-7"
+ >
+ {isCheckingJson ? (
+ <Spinner size="small" />
+ ) : (
+ t(
+ 'new_diagram_dialog.import_database.check_script_result'
+ )
+ )}
+ </Button>
+ ) : (
+ <p className="text-xs text-red-700">
+ {errorMessage}
+ </p>
+ )}
+ </div>
+ ) : null}
+ </div>
+ </ResizablePanel>
+ </ResizablePanelGroup>
</DialogInternalContent>
);
}, [
+ showSSMSInfoDialog,
databaseEdition,
databaseType,
errorMessage,
- handleInputChange,
scriptResult,
setDatabaseEdition,
- databaseClients,
- databaseClient,
- importMetadataScripts,
t,
+ isDesktop,
showCheckJsonButton,
isCheckingJson,
handleCheckJson,
- showSSMSInfoDialog,
- setShowSSMSInfoDialog,
+ effectiveTheme,
+ debouncedHandleInputChange,
+ importMethod,
+ setImportMethod,
]);
const renderFooter = useCallback(() => {
return (
- <DialogFooter className="mt-4 flex !justify-between gap-2">
+ <DialogFooter className="flex !justify-between gap-2">
<div className="flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2">
{goBack && (
<Button
diff --git a/src/dialogs/common/import-database/instructions-section/instructions-section.tsx b/src/dialogs/common/import-database/instructions-section/instructions-section.tsx
new file mode 100644
index 00000000..a98d635b
--- /dev/null
+++ b/src/dialogs/common/import-database/instructions-section/instructions-section.tsx
@@ -0,0 +1,178 @@
+import React from 'react';
+import logo from '@/assets/logo-2.png';
+import { ToggleGroup, ToggleGroupItem } from '@/components/toggle/toggle-group';
+import { DatabaseType } from '@/lib/domain/database-type';
+import { databaseSecondaryLogoMap } from '@/lib/databases';
+import type { DatabaseEdition } from '@/lib/domain/database-edition';
+import {
+ databaseEditionToImageMap,
+ databaseEditionToLabelMap,
+ databaseTypeToEditionMap,
+} from '@/lib/domain/database-edition';
+import {
+ Avatar,
+ AvatarFallback,
+ AvatarImage,
+} from '@/components/avatar/avatar';
+import { useTranslation } from 'react-i18next';
+import { Code } from 'lucide-react';
+import { SmartQueryInstructions } from './instructions/smart-query-instructions';
+import { DDLInstructions } from './instructions/ddl-instructions';
+
+const DatabasesWithoutDDLInstructions: DatabaseType[] = [
+ DatabaseType.CLICKHOUSE,
+];
+
+export interface InstructionsSectionProps {
+ databaseType: DatabaseType;
+ databaseEdition?: DatabaseEdition;
+ setDatabaseEdition: React.Dispatch<
+ React.SetStateAction<DatabaseEdition | undefined>
+ >;
+ importMethod: 'query' | 'ddl';
+ setImportMethod: (method: 'query' | 'ddl') => void;
+ showSSMSInfoDialog: boolean;
+ setShowSSMSInfoDialog: (show: boolean) => void;
+}
+
+export const InstructionsSection: React.FC<InstructionsSectionProps> = ({
+ databaseType,
+ databaseEdition,
+ setDatabaseEdition,
+ importMethod,
+ setImportMethod,
+ setShowSSMSInfoDialog,
+ showSSMSInfoDialog,
+}) => {
+ const { t } = useTranslation();
+
+ return (
+ <div className="flex w-full flex-1 flex-col gap-4">
+ {databaseTypeToEditionMap[databaseType].length > 0 ? (
+ <div className="flex flex-col gap-1">
+ <p className="text-sm leading-6 text-primary">
+ {t(
+ 'new_diagram_dialog.import_database.database_edition'
+ )}
+ </p>
+ <ToggleGroup
+ type="single"
+ className="ml-1 flex-wrap justify-start gap-2"
+ value={!databaseEdition ? 'regular' : databaseEdition}
+ onValueChange={(value) => {
+ setDatabaseEdition(
+ value === 'regular'
+ ? undefined
+ : (value as DatabaseEdition)
+ );
+ }}
+ >
+ <ToggleGroupItem
+ value="regular"
+ variant="outline"
+ className="h-6 gap-1 p-0 px-2 shadow-none data-[state=on]:bg-slate-200 dark:data-[state=on]:bg-slate-700"
+ >
+ <Avatar className="size-4 rounded-none">
+ <AvatarImage
+ src={databaseSecondaryLogoMap[databaseType]}
+ alt="Regular"
+ />
+ <AvatarFallback>Regular</AvatarFallback>
+ </Avatar>
+ Regular
+ </ToggleGroupItem>
+ {databaseTypeToEditionMap[databaseType].map(
+ (edition) => (
+ <ToggleGroupItem
+ value={edition}
+ key={edition}
+ variant="outline"
+ className="h-6 gap-1 p-0 px-2 shadow-none data-[state=on]:bg-slate-200 dark:data-[state=on]:bg-slate-700"
+ >
+ <Avatar className="size-4">
+ <AvatarImage
+ src={
+ databaseEditionToImageMap[
+ edition
+ ]
+ }
+ alt={
+ databaseEditionToLabelMap[
+ edition
+ ]
+ }
+ />
+ <AvatarFallback>
+ {databaseEditionToLabelMap[edition]}
+ </AvatarFallback>
+ </Avatar>
+ {databaseEditionToLabelMap[edition]}
+ </ToggleGroupItem>
+ )
+ )}
+ </ToggleGroup>
+ </div>
+ ) : null}
+
+ {DatabasesWithoutDDLInstructions.includes(databaseType) ? null : (
+ <div className="flex flex-col gap-1">
+ <p className="text-sm leading-6 text-primary">
+ How would you like to import?
+ </p>
+ <ToggleGroup
+ type="single"
+ className="ml-1 flex-wrap justify-start gap-2"
+ value={importMethod}
+ onValueChange={(value) => {
+ let selectedImportMethod: 'query' | 'ddl' = 'query';
+ if (value) {
+ selectedImportMethod = value as 'query' | 'ddl';
+ }
+
+ setImportMethod(selectedImportMethod);
+ }}
+ >
+ <ToggleGroupItem
+ value="query"
+ variant="outline"
+ className="h-6 gap-1 p-0 px-2 shadow-none data-[state=on]:bg-slate-200 dark:data-[state=on]:bg-slate-700"
+ >
+ <Avatar className="h-3 w-4 rounded-none">
+ <AvatarImage src={logo} alt="query" />
+ <AvatarFallback>Query</AvatarFallback>
+ </Avatar>
+ Smart Query
+ </ToggleGroupItem>
+ <ToggleGroupItem
+ value="ddl"
+ variant="outline"
+ className="h-6 gap-1 p-0 px-2 shadow-none data-[state=on]:bg-slate-200 dark:data-[state=on]:bg-slate-700"
+ >
+ <Avatar className="size-4 rounded-none">
+ <Code size={16} />
+ </Avatar>
+ DDL
+ </ToggleGroupItem>
+ </ToggleGroup>
+ </div>
+ )}
+
+ <div className="flex flex-col gap-2">
+ <div className="text-sm font-semibold">Instructions:</div>
+ {importMethod === 'query' ? (
+ <SmartQueryInstructions
+ databaseType={databaseType}
+ databaseEdition={databaseEdition}
+ showSSMSInfoDialog={showSSMSInfoDialog}
+ setShowSSMSInfoDialog={setShowSSMSInfoDialog}
+ />
+ ) : (
+ <DDLInstructions
+ databaseType={databaseType}
+ databaseEdition={databaseEdition}
+ />
+ )}
+ </div>
+ </div>
+ );
+};
diff --git a/src/dialogs/common/import-database/instructions-section/instructions/ddl-instruction-step.tsx b/src/dialogs/common/import-database/instructions-section/instructions/ddl-instruction-step.tsx
new file mode 100644
index 00000000..2ba88bc2
--- /dev/null
+++ b/src/dialogs/common/import-database/instructions-section/instructions/ddl-instruction-step.tsx
@@ -0,0 +1,48 @@
+import React from 'react';
+import { CodeSnippet } from '@/components/code-snippet/code-snippet';
+
+export interface DDLInstructionStepProps {
+ index: number;
+ text: string;
+ code?: string;
+ example?: string;
+}
+
+export const DDLInstructionStep: React.FC<DDLInstructionStepProps> = ({
+ index,
+ text,
+ code,
+ example,
+}) => {
+ return (
+ <div className="flex flex-col gap-1">
+ <div className="flex flex-col gap-1 text-sm text-primary">
+ <div>
+ <span className="font-medium">{index}.</span> {text}
+ </div>
+
+ {code ? (
+ <div className="h-[60px]">
+ <CodeSnippet
+ className="h-full"
+ code={code}
+ language={'shell'}
+ />
+ </div>
+ ) : null}
+ {example ? (
+ <>
+ <div className="my-2">Example:</div>
+ <div className="h-[60px]">
+ <CodeSnippet
+ className="h-full"
+ code={example}
+ language={'shell'}
+ />
+ </div>
+ </>
+ ) : null}
+ </div>
+ </div>
+ );
+};
diff --git a/src/dialogs/common/import-database/instructions-section/instructions/ddl-instructions.tsx b/src/dialogs/common/import-database/instructions-section/instructions/ddl-instructions.tsx
new file mode 100644
index 00000000..05b6a05a
--- /dev/null
+++ b/src/dialogs/common/import-database/instructions-section/instructions/ddl-instructions.tsx
@@ -0,0 +1,117 @@
+import React from 'react';
+import { DatabaseType } from '@/lib/domain/database-type';
+import type { DatabaseEdition } from '@/lib/domain/database-edition';
+import { DDLInstructionStep } from './ddl-instruction-step';
+
+interface DDLInstruction {
+ text: string;
+ code?: string;
+ example?: string;
+}
+
+const DDLInstructionsMap: Record<DatabaseType, DDLInstruction[]> = {
+ [DatabaseType.GENERIC]: [],
+ [DatabaseType.MYSQL]: [
+ {
+ text: 'Install mysqldump.',
+ },
+ {
+ text: 'Execute the following command in your terminal (prefix with sudo on Linux if needed):',
+ code: `mysqldump -h <host> -u <username>\n-P <port> -p --no-data\n<database_name> > <output_path>`,
+ example: `mysqldump -h localhost -u root -P\n3306 -p --no-data my_db >\nschema_export.sql`,
+ },
+ {
+ text: 'Open the exported SQL file, copy its contents, and paste them here.',
+ },
+ ],
+ [DatabaseType.POSTGRESQL]: [
+ {
+ text: 'Install pg_dump.',
+ },
+ {
+ text: 'Execute the following command in your terminal (prefix with sudo on Linux if needed):',
+ code: `pg_dump -h <host> -p <port> -d <database_name> \n -U <username> -s -F p -E UTF-8 \n -f <output_file_path>`,
+ example: `pg_dump -h localhost -p 5432 -d my_db \n -U postgres -s -F p -E UTF-8 \n -f schema_export.sql`,
+ },
+ {
+ text: 'Open the exported SQL file, copy its contents, and paste them here.',
+ },
+ ],
+ [DatabaseType.SQLITE]: [
+ {
+ text: 'Install sqlite3.',
+ },
+ {
+ text: 'Execute the following command in your terminal:',
+ code: `sqlite3 <database_file_path>\n.dump > <output_file_path>`,
+ example: `sqlite3 my_db.db\n.dump > schema_export.sql`,
+ },
+ {
+ text: 'Open the exported SQL file, copy its contents, and paste them here.',
+ },
+ ],
+ [DatabaseType.SQL_SERVER]: [
+ {
+ text: 'Download and install SQL Server Management Studio (SSMS).',
+ },
+ {
+ text: 'Connect to your SQL Server instance using SSMS.',
+ },
+ {
+ text: 'Right-click on the database you want to export and select Script Database as > CREATE To > New Query Editor Window.',
+ },
+ {
+ text: 'Copy the generated script and paste it here.',
+ },
+ ],
+ [DatabaseType.CLICKHOUSE]: [],
+ [DatabaseType.COCKROACHDB]: [
+ {
+ text: 'Install pg_dump.',
+ },
+ {
+ text: 'Execute the following command in your terminal (prefix with sudo on Linux if needed):',
+ code: `pg_dump -h <host> -p <port> -d <database_name> \n -U <username> -s -F p -E UTF-8 \n -f <output_file_path>`,
+ example: `pg_dump -h localhost -p 5432 -d my_db \n -U postgres -s -F p -E UTF-8 \n -f schema_export.sql`,
+ },
+ {
+ text: 'Open the exported SQL file, copy its contents, and paste them here.',
+ },
+ ],
+ [DatabaseType.MARIADB]: [
+ {
+ text: 'Install mysqldump.',
+ },
+ {
+ text: 'Execute the following command in your terminal (prefix with sudo on Linux if needed):',
+ code: `mysqldump -h <host> -u <username>\n-P <port> -p --no-data\n<database_name> > <output_path>`,
+ example: `mysqldump -h localhost -u root -P\n3306 -p --no-data my_db >\nschema_export.sql`,
+ },
+ {
+ text: 'Open the exported SQL file, copy its contents, and paste them here.',
+ },
+ ],
+};
+
+export interface DDLInstructionsProps {
+ databaseType: DatabaseType;
+ databaseEdition?: DatabaseEdition;
+}
+
+export const DDLInstructions: React.FC<DDLInstructionsProps> = ({
+ databaseType,
+}) => {
+ return (
+ <>
+ {DDLInstructionsMap[databaseType].map((instruction, index) => (
+ <DDLInstructionStep
+ key={index}
+ index={index + 1}
+ text={instruction.text}
+ code={instruction.code}
+ example={instruction.example}
+ />
+ ))}
+ </>
+ );
+};
diff --git a/src/dialogs/common/import-database/instructions-section/instructions/smart-query-instructions.tsx b/src/dialogs/common/import-database/instructions-section/instructions/smart-query-instructions.tsx
new file mode 100644
index 00000000..8d55ded8
--- /dev/null
+++ b/src/dialogs/common/import-database/instructions-section/instructions/smart-query-instructions.tsx
@@ -0,0 +1,134 @@
+import React, { useEffect, useMemo, useState } from 'react';
+import { DatabaseType } from '@/lib/domain/database-type';
+import { CodeSnippet } from '@/components/code-snippet/code-snippet';
+import type { DatabaseEdition } from '@/lib/domain/database-edition';
+import { SSMSInfo } from './ssms-info/ssms-info';
+import { useTranslation } from 'react-i18next';
+import { Tabs, TabsList, TabsTrigger } from '@/components/tabs/tabs';
+import type { DatabaseClient } from '@/lib/domain/database-clients';
+import {
+ databaseClientToLabelMap,
+ databaseTypeToClientsMap,
+ databaseEditionToClientsMap,
+} from '@/lib/domain/database-clients';
+import type { ImportMetadataScripts } from '@/lib/data/import-metadata/scripts/scripts';
+
+export interface SmartQueryInstructionsProps {
+ databaseType: DatabaseType;
+ databaseEdition?: DatabaseEdition;
+ showSSMSInfoDialog: boolean;
+ setShowSSMSInfoDialog: (show: boolean) => void;
+}
+
+export const SmartQueryInstructions: React.FC<SmartQueryInstructionsProps> = ({
+ databaseType,
+ databaseEdition,
+ showSSMSInfoDialog,
+ setShowSSMSInfoDialog,
+}) => {
+ const databaseClients = useMemo(
+ () => [
+ ...databaseTypeToClientsMap[databaseType],
+ ...(databaseEdition
+ ? databaseEditionToClientsMap[databaseEdition]
+ : []),
+ ],
+ [databaseType, databaseEdition]
+ );
+ const [databaseClient, setDatabaseClient] = useState<
+ DatabaseClient | undefined
+ >();
+ const { t } = useTranslation();
+ const [importMetadataScripts, setImportMetadataScripts] =
+ useState<ImportMetadataScripts | null>(null);
+
+ useEffect(() => {
+ const loadScripts = async () => {
+ const { importMetadataScripts } = await import(
+ '@/lib/data/import-metadata/scripts/scripts'
+ );
+ setImportMetadataScripts(importMetadataScripts);
+ };
+ loadScripts();
+ }, []);
+
+ return (
+ <>
+ <div className="flex flex-col gap-1">
+ <div className="flex flex-col gap-1 text-sm text-primary">
+ <div>
+ <span className="font-medium">1.</span>{' '}
+ {t('new_diagram_dialog.import_database.step_1')}
+ </div>
+ {databaseType === DatabaseType.SQL_SERVER && (
+ <SSMSInfo
+ open={showSSMSInfoDialog}
+ setOpen={setShowSSMSInfoDialog}
+ />
+ )}
+ </div>
+ {databaseClients.length > 0 ? (
+ <Tabs
+ value={!databaseClient ? 'dbclient' : databaseClient}
+ onValueChange={(value) => {
+ setDatabaseClient(
+ value === 'dbclient'
+ ? undefined
+ : (value as DatabaseClient)
+ );
+ }}
+ >
+ <div className="flex flex-1">
+ <TabsList className="h-8 justify-start rounded-none rounded-t-sm ">
+ <TabsTrigger
+ value="dbclient"
+ className="h-6 w-20"
+ >
+ DB Client
+ </TabsTrigger>
+
+ {databaseClients?.map((client) => (
+ <TabsTrigger
+ key={client}
+ value={client}
+ className="h-6 !w-20"
+ >
+ {databaseClientToLabelMap[client]}
+ </TabsTrigger>
+ )) ?? []}
+ </TabsList>
+ </div>
+ <CodeSnippet
+ className="h-40 w-full md:h-[200px]"
+ loading={!importMetadataScripts}
+ code={
+ importMetadataScripts?.[databaseType]?.({
+ databaseEdition,
+ databaseClient,
+ }) ?? ''
+ }
+ language={databaseClient ? 'shell' : 'sql'}
+ />
+ </Tabs>
+ ) : (
+ <CodeSnippet
+ className="h-40 w-full flex-auto md:h-[200px]"
+ loading={!importMetadataScripts}
+ code={
+ importMetadataScripts?.[databaseType]?.({
+ databaseEdition,
+ }) ?? ''
+ }
+ language="sql"
+ />
+ )}
+ </div>
+ <div className="flex flex-col gap-1">
+ <p className="text-sm text-primary">
+ <span className="font-medium">2.</span>{' '}
+ {t('new_diagram_dialog.import_database.step_2')}
+ </p>
+ </div>
+ </>
+ );
+};
diff --git a/src/dialogs/common/import-database/ssms-info/ssms-info.tsx b/src/dialogs/common/import-database/instructions-section/instructions/ssms-info/ssms-info.tsx
similarity index 100%
rename from src/dialogs/common/import-database/ssms-info/ssms-info.tsx
rename to src/dialogs/common/import-database/instructions-section/instructions/ssms-info/ssms-info.tsx
diff --git a/src/dialogs/create-diagram-dialog/create-diagram-dialog.tsx b/src/dialogs/create-diagram-dialog/create-diagram-dialog.tsx
index 3d2e2eb3..00c2dfbe 100644
--- a/src/dialogs/create-diagram-dialog/create-diagram-dialog.tsx
+++ b/src/dialogs/create-diagram-dialog/create-diagram-dialog.tsx
@@ -17,6 +17,7 @@ import { CreateDiagramDialogStep } from './create-diagram-dialog-step';
import { ImportDatabase } from '../common/import-database/import-database';
import { useTranslation } from 'react-i18next';
import type { BaseDialogProps } from '../common/base-dialog-props';
+import { sqlImportToDiagram } from '@/lib/data/sql-import';
export interface CreateDiagramDialogProps extends BaseDialogProps {}
@@ -25,6 +26,7 @@ export const CreateDiagramDialog: React.FC<CreateDiagramDialogProps> = ({
}) => {
const { diagramId } = useChartDB();
const { t } = useTranslation();
+ const [importMethod, setImportMethod] = useState<'query' | 'ddl'>('query');
const [databaseType, setDatabaseType] = useState<DatabaseType>(
DatabaseType.GENERIC
);
@@ -43,6 +45,7 @@ export const CreateDiagramDialog: React.FC<CreateDiagramDialogProps> = ({
useEffect(() => {
setDatabaseEdition(undefined);
+ setImportMethod('query');
}, [databaseType]);
useEffect(() => {
@@ -58,29 +61,41 @@ export const CreateDiagramDialog: React.FC<CreateDiagramDialogProps> = ({
setDatabaseType(DatabaseType.GENERIC);
setDatabaseEdition(undefined);
setScriptResult('');
+ setImportMethod('query');
}, [dialog.open]);
const hasExistingDiagram = (diagramId ?? '').trim().length !== 0;
const importNewDiagram = useCallback(async () => {
- const databaseMetadata: DatabaseMetadata =
- loadDatabaseMetadata(scriptResult);
+ let diagram: Diagram | undefined;
- const diagram = await loadFromDatabaseMetadata({
- databaseType,
- databaseMetadata,
- diagramNumber,
- databaseEdition:
- databaseEdition?.trim().length === 0
- ? undefined
- : databaseEdition,
- });
+ if (importMethod === 'ddl') {
+ diagram = sqlImportToDiagram({
+ sqlContent: scriptResult,
+ sourceDatabaseType: databaseType,
+ targetDatabaseType: databaseType,
+ });
+ } else {
+ const databaseMetadata: DatabaseMetadata =
+ loadDatabaseMetadata(scriptResult);
+
+ diagram = await loadFromDatabaseMetadata({
+ databaseType,
+ databaseMetadata,
+ diagramNumber,
+ databaseEdition:
+ databaseEdition?.trim().length === 0
+ ? undefined
+ : databaseEdition,
+ });
+ }
await addDiagram({ diagram });
await updateConfig({ defaultDiagramId: diagram.id });
closeCreateDiagramDialog();
navigate(`/diagrams/${diagram.id}`);
}, [
+ importMethod,
databaseType,
addDiagram,
databaseEdition,
@@ -137,7 +152,7 @@ export const CreateDiagramDialog: React.FC<CreateDiagramDialogProps> = ({
}}
>
<DialogContent
- className="flex max-h-screen w-[90vw] max-w-[90vw] flex-col overflow-y-auto md:overflow-visible lg:max-w-[60vw] xl:lg:max-w-lg xl:min-w-[45vw]"
+ className="flex max-h-screen w-full flex-col md:max-w-[900px]"
showClose={hasExistingDiagram}
>
{step === CreateDiagramDialogStep.SELECT_DATABASE ? (
@@ -163,6 +178,8 @@ export const CreateDiagramDialog: React.FC<CreateDiagramDialogProps> = ({
}
setScriptResult={setScriptResult}
title={t('new_diagram_dialog.import_database.title')}
+ importMethod={importMethod}
+ setImportMethod={setImportMethod}
/>
)}
</DialogContent>
diff --git a/src/dialogs/import-database-dialog/import-database-dialog.tsx b/src/dialogs/import-database-dialog/import-database-dialog.tsx
index d0ecbf3c..606d14ef 100644
--- a/src/dialogs/import-database-dialog/import-database-dialog.tsx
+++ b/src/dialogs/import-database-dialog/import-database-dialog.tsx
@@ -6,6 +6,7 @@ import { ImportDatabase } from '../common/import-database/import-database';
import type { DatabaseEdition } from '@/lib/domain/database-edition';
import type { DatabaseMetadata } from '@/lib/data/import-metadata/metadata-types/database-metadata';
import { loadDatabaseMetadata } from '@/lib/data/import-metadata/metadata-types/database-metadata';
+import type { Diagram } from '@/lib/domain/diagram';
import { loadFromDatabaseMetadata } from '@/lib/domain/diagram';
import { useChartDB } from '@/hooks/use-chartdb';
import { useRedoUndoStack } from '@/hooks/use-redo-undo-stack';
@@ -13,6 +14,7 @@ import { Trans, useTranslation } from 'react-i18next';
import { useReactFlow } from '@xyflow/react';
import type { BaseDialogProps } from '../common/base-dialog-props';
import { useAlert } from '@/context/alert-context/alert-context';
+import { sqlImportToDiagram } from '@/lib/data/sql-import';
export interface ImportDatabaseDialogProps extends BaseDialogProps {
databaseType: DatabaseType;
@@ -22,6 +24,7 @@ export const ImportDatabaseDialog: React.FC<ImportDatabaseDialogProps> = ({
dialog,
databaseType,
}) => {
+ const [importMethod, setImportMethod] = useState<'query' | 'ddl'>('query');
const { closeImportDatabaseDialog } = useDialog();
const { showAlert } = useAlert();
const {
@@ -54,17 +57,27 @@ export const ImportDatabaseDialog: React.FC<ImportDatabaseDialogProps> = ({
}, [dialog.open]);
const importDatabase = useCallback(async () => {
- const databaseMetadata: DatabaseMetadata =
- loadDatabaseMetadata(scriptResult);
-
- const diagram = await loadFromDatabaseMetadata({
- databaseType,
- databaseMetadata,
- databaseEdition:
- databaseEdition?.trim().length === 0
- ? undefined
- : databaseEdition,
- });
+ let diagram: Diagram | undefined;
+
+ if (importMethod === 'ddl') {
+ diagram = sqlImportToDiagram({
+ sqlContent: scriptResult,
+ sourceDatabaseType: databaseType,
+ targetDatabaseType: databaseType,
+ });
+ } else {
+ const databaseMetadata: DatabaseMetadata =
+ loadDatabaseMetadata(scriptResult);
+
+ diagram = await loadFromDatabaseMetadata({
+ databaseType,
+ databaseMetadata,
+ databaseEdition:
+ databaseEdition?.trim().length === 0
+ ? undefined
+ : databaseEdition,
+ });
+ }
const tableIdsToRemove = tables
.filter((table) =>
@@ -308,6 +321,7 @@ export const ImportDatabaseDialog: React.FC<ImportDatabaseDialogProps> = ({
closeImportDatabaseDialog();
}, [
+ importMethod,
databaseEdition,
currentDatabaseType,
updateDatabaseType,
@@ -337,7 +351,7 @@ export const ImportDatabaseDialog: React.FC<ImportDatabaseDialogProps> = ({
}}
>
<DialogContent
- className="flex max-h-screen w-[90vw] flex-col overflow-y-auto md:overflow-visible xl:min-w-[45vw]"
+ className="flex max-h-screen w-full flex-col md:max-w-[900px]"
showClose
>
<ImportDatabase
@@ -349,6 +363,8 @@ export const ImportDatabaseDialog: React.FC<ImportDatabaseDialogProps> = ({
setScriptResult={setScriptResult}
keepDialogAfterImport
title={t('import_database_dialog.title', { diagramName })}
+ importMethod={importMethod}
+ setImportMethod={setImportMethod}
/>
</DialogContent>
</Dialog>
diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts
index e268f71f..8c7fc086 100644
--- a/src/i18n/locales/en.ts
+++ b/src/i18n/locales/en.ts
@@ -232,7 +232,7 @@ export const en = {
title: 'Import your Database',
database_edition: 'Database Edition:',
step_1: 'Run this script in your database:',
- step_2: 'Paste the script result here:',
+ step_2: 'Paste the script result into this modal.',
script_results_placeholder: 'Script results here...',
ssms_instructions: {
button_text: 'SSMS Instructions',
diff --git a/src/lib/data/sql-import/common.ts b/src/lib/data/sql-import/common.ts
new file mode 100644
index 00000000..168a561b
--- /dev/null
+++ b/src/lib/data/sql-import/common.ts
@@ -0,0 +1,504 @@
+import type { Diagram } from '@/lib/domain/diagram';
+import { generateDiagramId, generateId } from '@/lib/utils';
+import type { DBTable } from '@/lib/domain/db-table';
+import type { Cardinality, DBRelationship } from '@/lib/domain/db-relationship';
+import type { DBField } from '@/lib/domain/db-field';
+import type { DataType } from '@/lib/data/data-types/data-types';
+import { genericDataTypes } from '@/lib/data/data-types/generic-data-types';
+import { randomColor } from '@/lib/colors';
+import { DatabaseType } from '@/lib/domain/database-type';
+
+// Common interfaces for SQL entities
+export interface SQLColumn {
+ name: string;
+ type: string;
+ nullable: boolean;
+ primaryKey: boolean;
+ unique: boolean;
+ typeArgs?: {
+ length?: number;
+ precision?: number;
+ scale?: number;
+ };
+ comment?: string;
+ default?: string;
+ increment?: boolean;
+}
+
+export interface SQLTable {
+ id: string;
+ name: string;
+ schema?: string;
+ columns: SQLColumn[];
+ indexes: SQLIndex[];
+ comment?: string;
+ order: number;
+}
+
+export interface SQLIndex {
+ name: string;
+ columns: string[];
+ unique: boolean;
+}
+
+export interface SQLForeignKey {
+ name: string;
+ sourceTable: string;
+ sourceSchema?: string;
+ sourceColumn: string;
+ targetTable: string;
+ targetSchema?: string;
+ targetColumn: string;
+ sourceTableId: string;
+ targetTableId: string;
+ updateAction?: string;
+ deleteAction?: string;
+}
+
+export interface SQLParserResult {
+ tables: SQLTable[];
+ relationships: SQLForeignKey[];
+ types?: SQLCustomType[];
+ enums?: SQLEnumType[];
+}
+
+// Define more specific types for SQL AST nodes
+export interface SQLASTNode {
+ type: string;
+ [key: string]: unknown;
+}
+
+export interface SQLBinaryExpr extends SQLASTNode {
+ type: 'binary_expr';
+ left: SQLASTNode;
+ right: SQLASTNode;
+ operator: string;
+}
+
+export interface SQLFunctionNode extends SQLASTNode {
+ type: 'function';
+ name: string;
+ args?: {
+ value: SQLASTArg[];
+ };
+}
+
+export interface SQLColumnRef extends SQLASTNode {
+ type: 'column_ref';
+ column: string;
+ table?: string;
+}
+
+export interface SQLExprList extends SQLASTNode {
+ type: 'expr_list';
+ value: Array<{ value: string | number }>;
+}
+
+export interface SQLStringLiteral extends SQLASTNode {
+ type: 'single_quote_string' | 'double_quote_string';
+ value: string;
+}
+
+export type SQLASTArg =
+ | SQLColumnRef
+ | SQLStringLiteral
+ | { type: string; value: string | number };
+
+export interface SQLCustomType {
+ name: string;
+ [key: string]: unknown;
+}
+
+export interface SQLEnumType {
+ name: string;
+ values: string[];
+ [key: string]: unknown;
+}
+
+// Helper functions for SQL dialect handling
+export function quoteIdentifier(str: string, dbType: DatabaseType): string {
+ switch (dbType) {
+ case DatabaseType.MYSQL:
+ case DatabaseType.MARIADB:
+ return `\`${str}\``;
+ case DatabaseType.POSTGRESQL:
+ case DatabaseType.SQLITE:
+ return `"${str}"`;
+ case DatabaseType.SQL_SERVER:
+ return `[${str}]`;
+ default:
+ return str;
+ }
+}
+
+export function buildSQLFromAST(
+ ast: SQLASTNode | null | undefined,
+ dbType: DatabaseType = DatabaseType.GENERIC
+): string {
+ if (!ast) return '';
+
+ if (ast.type === 'binary_expr') {
+ const expr = ast as SQLBinaryExpr;
+ const leftSQL = buildSQLFromAST(expr.left, dbType);
+ const rightSQL = buildSQLFromAST(expr.right, dbType);
+ return `${leftSQL} ${expr.operator} ${rightSQL}`;
+ }
+
+ if (ast.type === 'function') {
+ const func = ast as SQLFunctionNode;
+ let expr = func.name;
+ if (func.args) {
+ expr +=
+ '(' +
+ func.args.value
+ .map((v: SQLASTArg) => {
+ if (v.type === 'column_ref')
+ return quoteIdentifier(
+ (v as SQLColumnRef).column,
+ dbType
+ );
+ if (
+ v.type === 'single_quote_string' ||
+ v.type === 'double_quote_string'
+ )
+ return "'" + (v as SQLStringLiteral).value + "'";
+ return v.value;
+ })
+ .join(', ') +
+ ')';
+ }
+ return expr;
+ } else if (ast.type === 'column_ref') {
+ return quoteIdentifier((ast as SQLColumnRef).column, dbType);
+ } else if (ast.type === 'expr_list') {
+ return (ast as SQLExprList).value.map((v) => v.value).join(' AND ');
+ } else {
+ const valueNode = ast as { type: string; value: string | number };
+ return typeof valueNode.value === 'string'
+ ? "'" + valueNode.value + "'"
+ : String(valueNode.value);
+ }
+}
+
+// Helper to determine cardinality of relationships
+export function determineCardinality(
+ isSourceUnique: boolean,
+ isTargetUnique: boolean
+): { sourceCardinality: Cardinality; targetCardinality: Cardinality } {
+ if (isSourceUnique && isTargetUnique) {
+ return {
+ sourceCardinality: 'one' as Cardinality,
+ targetCardinality: 'one' as Cardinality,
+ };
+ } else if (isSourceUnique) {
+ return {
+ sourceCardinality: 'one' as Cardinality,
+ targetCardinality: 'many' as Cardinality,
+ };
+ } else if (isTargetUnique) {
+ return {
+ sourceCardinality: 'many' as Cardinality,
+ targetCardinality: 'one' as Cardinality,
+ };
+ } else {
+ return {
+ sourceCardinality: 'many' as Cardinality,
+ targetCardinality: 'many' as Cardinality,
+ };
+ }
+}
+
+// Map SQL data type to generic data type in our system
+export function mapSQLTypeToGenericType(sqlType: string): DataType {
+ const normalizedType = sqlType.toLowerCase().replace(/\(.*\)/, '');
+ const matchedType = genericDataTypes.find((t) => t.id === normalizedType);
+ if (matchedType) return matchedType;
+
+ const typeMap: Record<string, string> = {
+ int: 'integer',
+ integer: 'integer',
+ smallint: 'smallint',
+ bigint: 'bigint',
+ decimal: 'decimal',
+ numeric: 'numeric',
+ float: 'float',
+ double: 'double',
+ varchar: 'varchar',
+ 'character varying': 'varchar',
+ char: 'char',
+ character: 'char',
+ text: 'text',
+ boolean: 'boolean',
+ bool: 'boolean',
+ timestamp: 'timestamp',
+ datetime: 'timestamp',
+ date: 'date',
+ time: 'time',
+ json: 'json',
+ jsonb: 'json',
+ };
+
+ const mappedType = typeMap[normalizedType];
+ if (mappedType) {
+ const foundType = genericDataTypes.find((t) => t.id === mappedType);
+ if (foundType) return foundType;
+ }
+
+ return genericDataTypes.find((t) => t.id === 'varchar')!;
+}
+
+// Type affinity definitions for different database dialects
+export const typeAffinity: Record<string, Record<string, string>> = {
+ [DatabaseType.POSTGRESQL]: {
+ INT: 'INTEGER',
+ INTEGER: 'INTEGER',
+ MEDIUMINT: 'INTEGER',
+ BIT: 'BOOLEAN',
+ },
+ [DatabaseType.MYSQL]: {
+ INT: 'INTEGER',
+ INTEGER: 'INTEGER',
+ BOOL: 'BOOLEAN',
+ BOOLEAN: 'TINYINT',
+ },
+ [DatabaseType.MARIADB]: {
+ INT: 'INTEGER',
+ INTEGER: 'INTEGER',
+ BOOL: 'BOOLEAN',
+ BOOLEAN: 'TINYINT',
+ },
+ [DatabaseType.SQL_SERVER]: {
+ INT: 'INTEGER',
+ INTEGER: 'INT',
+ BOOL: 'BIT',
+ BOOLEAN: 'BIT',
+ },
+ [DatabaseType.SQLITE]: {
+ INT: 'INTEGER',
+ BOOL: 'INTEGER',
+ BOOLEAN: 'INTEGER',
+ },
+ [DatabaseType.GENERIC]: {
+ INTEGER: 'integer',
+ INT: 'integer',
+ MEDIUMINT: 'integer',
+ BIT: 'boolean',
+ VARCHAR: 'varchar',
+ 'CHARACTER VARYING': 'varchar',
+ CHAR: 'char',
+ CHARACTER: 'char',
+ TEXT: 'text',
+ BOOLEAN: 'boolean',
+ BOOL: 'boolean',
+ TIMESTAMP: 'timestamp',
+ DATETIME: 'timestamp',
+ DATE: 'date',
+ TIME: 'time',
+ JSON: 'json',
+ JSONB: 'json',
+ DECIMAL: 'decimal',
+ NUMERIC: 'numeric',
+ FLOAT: 'float',
+ DOUBLE: 'double',
+ BIGINT: 'bigint',
+ SMALLINT: 'smallint',
+ },
+};
+
+// For safe type conversions
+export function getTypeAffinity(
+ databaseType: DatabaseType,
+ sqlType: string
+): string {
+ if (!sqlType) return 'varchar';
+
+ const normalizedType = sqlType.toUpperCase();
+ const dialectAffinity =
+ typeAffinity[databaseType] || typeAffinity[DatabaseType.GENERIC];
+ return dialectAffinity[normalizedType] || sqlType.toLowerCase();
+}
+
+// Convert SQLParserResult to ChartDB Diagram structure
+export function convertToChartDBDiagram(
+ parserResult: SQLParserResult,
+ sourceDatabaseType: DatabaseType,
+ targetDatabaseType: DatabaseType
+): Diagram {
+ // Create a mapping of old table IDs to new ones
+ const tableIdMapping = new Map<string, string>();
+
+ // Convert SQL tables to ChartDB tables
+ const tables: DBTable[] = parserResult.tables.map((table, index) => {
+ const row = Math.floor(index / 4);
+ const col = index % 4;
+ const tableSpacing = 300;
+ const newId = generateId();
+ tableIdMapping.set(table.id, newId);
+
+ // Create fields from columns
+ const fields: DBField[] = table.columns.map((column) => {
+ const field: DBField = {
+ id: generateId(),
+ name: column.name,
+ type: mapSQLTypeToGenericType(column.type),
+ nullable: column.nullable,
+ primaryKey: column.primaryKey,
+ unique: column.unique,
+ default: column.default || '',
+ createdAt: Date.now(),
+ };
+
+ // Add type arguments if present
+ if (column.typeArgs) {
+ // Transfer length for varchar/char types
+ if (
+ column.typeArgs.length !== undefined &&
+ (field.type.id === 'varchar' || field.type.id === 'char')
+ ) {
+ field.characterMaximumLength =
+ column.typeArgs.length.toString();
+ }
+
+ // Transfer precision/scale for numeric types
+ if (
+ column.typeArgs.precision !== undefined &&
+ (field.type.id === 'numeric' || field.type.id === 'decimal')
+ ) {
+ field.precision = column.typeArgs.precision;
+ field.scale = column.typeArgs.scale;
+ }
+ }
+
+ return field;
+ });
+
+ // Create indexes
+ const indexes = table.indexes.map((sqlIndex) => {
+ const fieldIds = sqlIndex.columns.map((columnName) => {
+ const field = fields.find((f) => f.name === columnName);
+ if (!field) {
+ throw new Error(
+ `Index references non-existent column: ${columnName}`
+ );
+ }
+ return field.id;
+ });
+
+ return {
+ id: generateId(),
+ name: sqlIndex.name,
+ fieldIds,
+ unique: sqlIndex.unique,
+ createdAt: Date.now(),
+ };
+ });
+
+ return {
+ id: newId,
+ name: table.name,
+ schema: table.schema || '',
+ order: index,
+ fields,
+ indexes,
+ x: col * tableSpacing,
+ y: row * tableSpacing,
+ color: randomColor(),
+ isView: false,
+ createdAt: Date.now(),
+ };
+ });
+
+ // Process relationships
+ const relationships: DBRelationship[] = [];
+
+ parserResult.relationships.forEach((rel) => {
+ // First try to find the table with exact schema match
+ let sourceTable = tables.find(
+ (t) => t.name === rel.sourceTable && rel.sourceSchema === t.schema
+ );
+
+ // If not found, try without schema requirements
+ if (!sourceTable) {
+ sourceTable = tables.find((t) => t.name === rel.sourceTable);
+ }
+
+ // Similar approach for target table
+ let targetTable = tables.find(
+ (t) => t.name === rel.targetTable && rel.targetSchema === t.schema
+ );
+
+ // If not found, try without schema requirements
+ if (!targetTable) {
+ targetTable = tables.find((t) => t.name === rel.targetTable);
+ }
+
+ if (!sourceTable || !targetTable) {
+ console.warn('Relationship refers to non-existent table:', {
+ sourceTable: rel.sourceTable,
+ sourceSchema: rel.sourceSchema,
+ targetTable: rel.targetTable,
+ targetSchema: rel.targetSchema,
+ availableTables: tables.map(
+ (t) => `${t.schema || ''}.${t.name}`
+ ),
+ });
+ return;
+ }
+
+ const sourceTableId = tableIdMapping.get(rel.sourceTableId);
+ const targetTableId = tableIdMapping.get(rel.targetTableId);
+
+ if (!sourceTableId || !targetTableId) {
+ console.warn('Could not find mapped table IDs for relationship');
+ return;
+ }
+
+ const sourceField = sourceTable.fields.find(
+ (f) => f.name === rel.sourceColumn
+ );
+ const targetField = targetTable.fields.find(
+ (f) => f.name === rel.targetColumn
+ );
+
+ if (!sourceField || !targetField) {
+ console.warn('Relationship refers to non-existent field:', {
+ sourceTable: rel.sourceTable,
+ sourceField: rel.sourceColumn,
+ targetTable: rel.targetTable,
+ targetField: rel.targetColumn,
+ });
+ return;
+ }
+
+ const { sourceCardinality, targetCardinality } = determineCardinality(
+ sourceField.unique || sourceField.primaryKey,
+ targetField.unique || targetField.primaryKey
+ );
+
+ relationships.push({
+ id: generateId(),
+ name: rel.name,
+ sourceSchema: sourceTable.schema,
+ targetSchema: targetTable.schema,
+ sourceTableId: sourceTableId,
+ targetTableId: targetTableId,
+ sourceFieldId: sourceField.id,
+ targetFieldId: targetField.id,
+ sourceCardinality,
+ targetCardinality,
+ createdAt: Date.now(),
+ });
+ });
+
+ const diagram = {
+ id: generateDiagramId(),
+ name: `SQL Import (${sourceDatabaseType})`,
+ databaseType: targetDatabaseType,
+ tables,
+ relationships,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ };
+
+ return diagram;
+}
diff --git a/src/lib/data/sql-import/dialect-importers/mysql/mysql-common.ts b/src/lib/data/sql-import/dialect-importers/mysql/mysql-common.ts
new file mode 100644
index 00000000..9db5bed4
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/mysql/mysql-common.ts
@@ -0,0 +1,243 @@
+import { Parser } from 'node-sql-parser';
+
+// Common PostgreSQL parser instance
+export const parser = new Parser();
+export const parserOpts = {
+ database: 'MySQL', // Set dialect to MySQL
+};
+
+// Define interfaces for AST nodes - Fixed no-explicit-any issues
+export interface SQLAstNode {
+ type: string;
+ keyword?: string;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ [key: string]: any; // Need to keep any here for compatibility with Parser's AST
+}
+
+// Define a minimal interface for table objects used in helper functions
+export interface TableLike {
+ id: string;
+ name: string;
+ schema?: string;
+ columns: unknown[];
+ indexes: unknown[];
+}
+
+export interface TableReference {
+ table?: string;
+ schema?: string;
+ db?: string; // Support for PostgreSQL AST compatibility
+}
+
+export interface ColumnReference {
+ column?:
+ | string
+ | { value?: string; expr?: { value?: string; type?: string } };
+ expr?: { value?: string; type?: string };
+ value?: string;
+ type?: string;
+}
+
+export interface ColumnDefinition {
+ resource: string;
+ column: string | ColumnReference;
+ definition?: {
+ dataType?: string;
+ constraint?: string;
+ length?: number;
+ precision?: number;
+ scale?: number;
+ };
+ primary_key?: string;
+ nullable?: { type?: string };
+ unique?: string;
+ default_val?: SQLAstNode;
+ auto_increment?: string;
+}
+
+export interface ConstraintDefinition {
+ resource: string;
+ constraint_type: string;
+ constraint_name?: string;
+ definition?: Array<ColumnReference> | { columns?: string[] };
+ columns?: string[];
+ reference_definition?: ReferenceDefinition;
+ reference?: ReferenceDefinition;
+}
+
+export interface ReferenceDefinition {
+ table?: string | TableReference | TableReference[];
+ columns?: Array<ColumnReference | string> | string[];
+ definition?: Array<ColumnReference>;
+ on_update?: string;
+ on_delete?: string;
+}
+
+export interface CreateTableStatement extends SQLAstNode {
+ table: TableReference | TableReference[];
+ create_definitions?: Array<ColumnDefinition | ConstraintDefinition>;
+ comment?: string;
+}
+
+export interface CreateIndexStatement extends SQLAstNode {
+ table: TableReference | TableReference[] | string;
+ index?: string;
+ index_name?: string;
+ index_type?: string;
+ unique?: boolean;
+ columns?: Array<ColumnReference>;
+ index_columns?: Array<{ column?: ColumnReference } | ColumnReference>;
+}
+
+export interface AlterTableConstraintDefinition extends ConstraintDefinition {
+ constraint?: string;
+}
+
+export interface AlterTableExprItem {
+ action: string;
+ resource?: string;
+ type?: string;
+ constraint?: { constraint_type?: string };
+ create_definitions?:
+ | AlterTableConstraintDefinition
+ | {
+ constraint_type?: string;
+ definition?: Array<ColumnReference>;
+ constraint?: string;
+ reference_definition?: ReferenceDefinition;
+ resource?: string;
+ };
+}
+
+export interface AlterTableStatement extends SQLAstNode {
+ table: TableReference[] | TableReference | string;
+ expr: AlterTableExprItem[];
+}
+
+// Define type for column type arguments
+export interface TypeArgs {
+ length?: number;
+ precision?: number;
+ scale?: number;
+}
+
+// Helper to extract column name from different AST formats
+export function extractColumnName(
+ columnObj: string | ColumnReference | undefined
+): string {
+ if (!columnObj) return '';
+
+ // Handle different formats based on actual AST structure
+ if (typeof columnObj === 'string') return columnObj;
+
+ if (typeof columnObj === 'object') {
+ // Direct column property
+ if (columnObj.column) {
+ if (typeof columnObj.column === 'string') return columnObj.column;
+ if (typeof columnObj.column === 'object') {
+ // Handle nested value property
+ if (columnObj.column.value) return columnObj.column.value;
+ // Handle expression property with value
+ if (columnObj.column.expr?.value)
+ return columnObj.column.expr.value;
+ // Handle double_quote_string type which is common in PostgreSQL
+ if (columnObj.column.expr?.type === 'double_quote_string')
+ return columnObj.column.expr.value || '';
+ // Direct access to expr
+ if (columnObj.column.expr?.type === 'default')
+ return columnObj.column.expr.value || '';
+ }
+ }
+
+ // Direct expr property
+ if (columnObj.expr) {
+ if (columnObj.expr.type === 'default')
+ return columnObj.expr.value || '';
+ if (columnObj.expr.type === 'double_quote_string')
+ return columnObj.expr.value || '';
+ if (columnObj.expr.value) return columnObj.expr.value;
+ }
+
+ // Direct value property
+ if (columnObj.value) return columnObj.value;
+ }
+
+ console.warn('Could not extract column name from:', columnObj);
+ return '';
+}
+
+// Helper function to extract type arguments from column definition
+export function getTypeArgs(
+ definition: ColumnDefinition['definition'] | undefined
+): TypeArgs {
+ const typeArgs: TypeArgs = {};
+
+ if (!definition) return typeArgs;
+
+ if (definition.length !== undefined) {
+ typeArgs.length = definition.length;
+ }
+
+ if (definition.scale !== undefined && definition.precision !== undefined) {
+ typeArgs.precision = definition.precision;
+ typeArgs.scale = definition.scale;
+ }
+
+ return typeArgs;
+}
+
+// Helper function to find a table with consistent schema handling
+export function findTableWithSchemaSupport(
+ tables: TableLike[],
+ tableName: string,
+ schemaName?: string
+): TableLike | undefined {
+ // Default to public schema if none provided
+ const effectiveSchema = schemaName || 'public';
+
+ // First try with exact schema match
+ let table = tables.find(
+ (t) => t.name === tableName && t.schema === effectiveSchema
+ );
+
+ // If not found with schema, try with the legacy schema match
+ if (!table && schemaName) {
+ table = tables.find(
+ (t) => t.name === tableName && t.schema === schemaName
+ );
+ }
+
+ // If still not found with schema, try any match on the table name
+ if (!table) {
+ table = tables.find((t) => t.name === tableName);
+ }
+
+ return table;
+}
+
+// Helper function to find table ID with schema support
+export function getTableIdWithSchemaSupport(
+ tableMap: Record<string, string>,
+ tableName: string,
+ schemaName?: string
+): string | undefined {
+ // Default to public schema if none provided
+ const effectiveSchema = schemaName || 'public';
+
+ // First try with schema
+ const tableKey = `${effectiveSchema}.${tableName}`;
+ let tableId = tableMap[tableKey];
+
+ // If not found with the effective schema, try with the original schema if different
+ if (!tableId && schemaName && schemaName !== effectiveSchema) {
+ const originalSchemaKey = `${schemaName}.${tableName}`;
+ tableId = tableMap[originalSchemaKey];
+ }
+
+ // If still not found with schema, try without schema
+ if (!tableId) {
+ tableId = tableMap[tableName];
+ }
+
+ return tableId;
+}
diff --git a/src/lib/data/sql-import/dialect-importers/mysql/mysql.ts b/src/lib/data/sql-import/dialect-importers/mysql/mysql.ts
new file mode 100644
index 00000000..78b099db
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/mysql/mysql.ts
@@ -0,0 +1,895 @@
+import { generateId } from '@/lib/utils';
+import type {
+ SQLParserResult,
+ SQLTable,
+ SQLColumn,
+ SQLIndex,
+ SQLForeignKey,
+} from '../../common';
+import { buildSQLFromAST } from '../../common';
+import type {
+ ColumnDefinition,
+ ConstraintDefinition,
+ CreateTableStatement,
+ TableReference,
+} from './mysql-common';
+import {
+ parser,
+ parserOpts,
+ extractColumnName,
+ getTypeArgs,
+} from './mysql-common';
+
+// Interface for pending foreign keys that need to be processed later
+interface PendingForeignKey {
+ name: string;
+ sourceTable: string;
+ sourceTableId: string;
+ sourceColumns: string[];
+ targetTable: string;
+ targetColumns: string[];
+ updateAction?: string;
+ deleteAction?: string;
+}
+
+// Helper to extract statements from PostgreSQL dump
+function extractStatements(sqlContent: string): string[] {
+ const statements: string[] = [];
+ let currentStatement = '';
+ const lines = sqlContent.split('\n');
+
+ for (let i = 0; i < lines.length; i++) {
+ const line = lines[i].trim();
+
+ // Skip comments and empty lines
+ if (line.startsWith('--') || line === '') {
+ continue;
+ }
+
+ // Add line to current statement
+ currentStatement += line + ' ';
+
+ // If line ends with semicolon, consider statement complete
+ if (line.endsWith(';')) {
+ statements.push(currentStatement.trim());
+ currentStatement = '';
+ }
+ }
+
+ // Handle any remaining statement
+ if (currentStatement.trim()) {
+ statements.push(currentStatement.trim());
+ }
+
+ return statements;
+}
+
+// Function to extract columns from a CREATE TABLE statement using regex
+function extractColumnsFromCreateTable(statement: string): SQLColumn[] {
+ const columns: SQLColumn[] = [];
+
+ // Extract everything between the first opening and last closing parenthesis
+ const columnMatch = statement.match(/CREATE\s+TABLE.*?\((.*)\)[^)]*;$/s);
+ if (!columnMatch || !columnMatch[1]) {
+ return columns;
+ }
+
+ const columnDefs = columnMatch[1].trim();
+ // Split by commas, but not those within parentheses (for nested type definitions)
+ const columnLines = columnDefs.split(/,(?![^(]*\))/);
+
+ for (const columnLine of columnLines) {
+ const line = columnLine.trim();
+ // Skip constraints at the table level
+ if (
+ line.toUpperCase().startsWith('CONSTRAINT') ||
+ line.toUpperCase().startsWith('PRIMARY KEY') ||
+ line.toUpperCase().startsWith('FOREIGN KEY') ||
+ line.toUpperCase().startsWith('UNIQUE')
+ ) {
+ continue;
+ }
+
+ // Extract column name and definition
+ const columnNameMatch = line.match(/^"?([^"\s]+)"?\s+(.+)$/);
+ if (columnNameMatch) {
+ const columnName = columnNameMatch[1];
+ const definition = columnNameMatch[2];
+
+ // Determine if column is nullable
+ const nullable = !definition.toUpperCase().includes('NOT NULL');
+
+ // Determine if column is primary key
+ const primaryKey = definition.toUpperCase().includes('PRIMARY KEY');
+
+ // Extract data type
+ const typeMatch = definition.match(/^([^\s(]+)(?:\(([^)]+)\))?/);
+ const dataType = typeMatch ? typeMatch[1] : '';
+
+ columns.push({
+ name: columnName,
+ type: dataType,
+ nullable,
+ primaryKey,
+ unique: definition.toUpperCase().includes('UNIQUE'),
+ });
+ }
+ }
+
+ return columns;
+}
+
+// Process PostgreSQL pg_dump CREATE INDEX statements
+function processCreateIndexStatement(
+ statement: string,
+ tableMap: Record<string, string>,
+ tables: SQLTable[]
+): void {
+ if (
+ !statement.startsWith('CREATE INDEX') &&
+ !statement.startsWith('CREATE UNIQUE INDEX')
+ ) {
+ return;
+ }
+
+ try {
+ // Determine if the index is unique
+ const isUnique = statement.startsWith('CREATE UNIQUE INDEX');
+
+ // Extract index name
+ const indexNameRegex = /CREATE (?:UNIQUE )?INDEX\s+"?([^"\s]+)"?/i;
+ const indexNameMatch = statement.match(indexNameRegex);
+ const indexName = indexNameMatch ? indexNameMatch[1] : '';
+
+ if (!indexName) {
+ return;
+ }
+
+ // Extract table name and schema
+ const tableRegex = /ON\s+(?:"?([^"\s.]+)"?\.)?(?:"?([^"\s.(]+)"?)/i;
+ const tableMatch = statement.match(tableRegex);
+
+ if (!tableMatch) {
+ return;
+ }
+
+ const tableSchema = tableMatch[1] || '';
+ const tableName = tableMatch[2];
+
+ // Extract index columns
+ const columnsRegex = /\(\s*([^)]+)\)/i;
+ const columnsMatch = statement.match(columnsRegex);
+
+ if (!columnsMatch) {
+ return;
+ }
+
+ // Parse columns (handle function-based indexes, etc.)
+ const columnsStr = columnsMatch[1];
+ // This is a simplified approach - advanced indexes may need more complex parsing
+ const indexColumns = columnsStr.split(',').map((col) => {
+ // Extract basic column name, handling possible expressions
+ const colName = col
+ .trim()
+ .replace(/^"(.*)"$/, '$1')
+ .replace(/^\s*"?([^"\s(]+)"?\s*.*$/, '$1'); // Get just the column name part
+ return colName;
+ });
+
+ if (indexColumns.length === 0) {
+ return;
+ }
+
+ // Find the table
+ const tableKey = `${tableSchema ? tableSchema + '.' : ''}${tableName}`;
+ const tableId = tableMap[tableKey];
+
+ if (!tableId) {
+ return;
+ }
+
+ const table = tables.find((t) => t.id === tableId);
+ if (!table) {
+ return;
+ }
+
+ // Check if a similar index already exists (to avoid duplicates)
+ const existingIndex = table.indexes.find(
+ (idx) =>
+ idx.name === indexName ||
+ (idx.columns.length === indexColumns.length &&
+ idx.columns.every((col, i) => col === indexColumns[i]))
+ );
+
+ if (!existingIndex) {
+ table.indexes.push({
+ name: indexName,
+ columns: indexColumns,
+ unique: isUnique,
+ });
+ }
+ } catch (error) {
+ console.error('Error processing CREATE INDEX statement:', error);
+ }
+}
+
+export function fromMySQL(sqlContent: string): SQLParserResult {
+ const tables: SQLTable[] = [];
+ const relationships: SQLForeignKey[] = [];
+ const tableMap: Record<string, string> = {}; // Maps table name to its ID
+ const pendingForeignKeys: PendingForeignKey[] = []; // Store FKs that reference tables not yet created
+
+ try {
+ // Extract SQL statements from the dump
+ const statements = extractStatements(sqlContent);
+
+ // First pass: process CREATE TABLE statements
+ for (const statement of statements) {
+ const trimmedStmt = statement.trim();
+ // Process only CREATE TABLE statements
+ if (trimmedStmt.toUpperCase().startsWith('CREATE TABLE')) {
+ try {
+ // Parse with SQL parser
+ const ast = parser.astify(trimmedStmt, parserOpts);
+ if (
+ Array.isArray(ast) &&
+ ast.length > 0 &&
+ ast[0].type === 'create' &&
+ ast[0].keyword === 'table'
+ ) {
+ const createTableStmt = ast[0] as CreateTableStatement;
+
+ // Extract table name
+ let tableName = '';
+ if (typeof createTableStmt.table === 'object') {
+ if (
+ Array.isArray(createTableStmt.table) &&
+ createTableStmt.table.length > 0
+ ) {
+ tableName =
+ createTableStmt.table[0].table || '';
+ } else {
+ const tableObj =
+ createTableStmt.table as TableReference;
+ tableName = tableObj.table || '';
+ }
+ } else if (typeof createTableStmt.table === 'string') {
+ tableName = createTableStmt.table;
+ }
+
+ // Remove backticks from table name
+ tableName = tableName.replace(/`/g, '');
+
+ if (tableName) {
+ // Generate table ID
+ const tableId = generateId();
+ tableMap[tableName] = tableId;
+
+ // Process columns
+ const columns: SQLColumn[] = [];
+ const indexes: SQLIndex[] = [];
+
+ if (
+ createTableStmt.create_definitions &&
+ Array.isArray(
+ createTableStmt.create_definitions
+ )
+ ) {
+ createTableStmt.create_definitions.forEach(
+ (
+ def:
+ | ColumnDefinition
+ | ConstraintDefinition
+ ) => {
+ if (def.resource === 'column') {
+ const columnDef =
+ def as ColumnDefinition;
+ let columnName = extractColumnName(
+ columnDef.column
+ );
+
+ // Remove backticks
+ columnName = columnName.replace(
+ /`/g,
+ ''
+ );
+ const dataType =
+ columnDef.definition
+ ?.dataType || '';
+
+ // Check column constraints
+ const isPrimaryKey =
+ columnDef.primary_key ===
+ 'primary key' ||
+ columnDef.definition
+ ?.constraint ===
+ 'primary key';
+
+ const isAutoIncrement =
+ columnDef.auto_increment ===
+ 'auto_increment';
+
+ columns.push({
+ name: columnName,
+ type: dataType,
+ nullable:
+ columnDef.nullable?.type !==
+ 'not null',
+ primaryKey: isPrimaryKey,
+ unique:
+ columnDef.unique ===
+ 'unique' ||
+ columnDef.definition
+ ?.constraint ===
+ 'unique',
+ typeArgs: getTypeArgs(
+ columnDef.definition
+ ),
+ default: columnDef.default_val
+ ? buildSQLFromAST(
+ columnDef.default_val
+ )
+ : undefined,
+ increment: isAutoIncrement,
+ });
+ } else if (
+ def.resource === 'constraint'
+ ) {
+ const constraintDef =
+ def as ConstraintDefinition;
+
+ // Handle PRIMARY KEY constraint
+ if (
+ constraintDef.constraint_type ===
+ 'primary key'
+ ) {
+ if (
+ Array.isArray(
+ constraintDef.definition
+ )
+ ) {
+ const pkColumns =
+ constraintDef.definition
+ .filter(
+ (colDef) =>
+ typeof colDef ===
+ 'object' &&
+ 'type' in
+ colDef &&
+ colDef.type ===
+ 'column_ref'
+ )
+ .map((colDef) =>
+ extractColumnName(
+ colDef
+ ).replace(
+ /`/g,
+ ''
+ )
+ );
+
+ // Mark columns as PK
+ for (const colName of pkColumns) {
+ const col =
+ columns.find(
+ (c) =>
+ c.name ===
+ colName
+ );
+ if (col) {
+ col.primaryKey =
+ true;
+ }
+ }
+
+ // Add PK index
+ if (pkColumns.length > 0) {
+ indexes.push({
+ name: `pk_${tableName}`,
+ columns: pkColumns,
+ unique: true,
+ });
+ }
+ }
+ }
+ // Handle UNIQUE constraint
+ else if (
+ constraintDef.constraint_type ===
+ 'unique'
+ ) {
+ const uniqueColumns =
+ Array.isArray(
+ constraintDef.definition
+ )
+ ? constraintDef.definition.map(
+ (colDef) =>
+ extractColumnName(
+ colDef
+ ).replace(
+ /`/g,
+ ''
+ )
+ )
+ : (
+ constraintDef
+ .definition
+ ?.columns ||
+ []
+ ).map((col) =>
+ typeof col ===
+ 'string'
+ ? col.replace(
+ /`/g,
+ ''
+ )
+ : extractColumnName(
+ col
+ ).replace(
+ /`/g,
+ ''
+ )
+ );
+
+ if (uniqueColumns.length > 0) {
+ indexes.push({
+ name: constraintDef.constraint_name
+ ? constraintDef.constraint_name.replace(
+ /`/g,
+ ''
+ )
+ : `${tableName}_${uniqueColumns[0]}_key`,
+ columns: uniqueColumns,
+ unique: true,
+ });
+ }
+ }
+ // Handle FOREIGN KEY constraints
+ else if (
+ constraintDef.constraint_type ===
+ 'foreign key' ||
+ constraintDef.constraint_type ===
+ 'FOREIGN KEY'
+ ) {
+ // Extract source columns
+ let sourceColumns: string[] =
+ [];
+ if (
+ Array.isArray(
+ constraintDef.definition
+ )
+ ) {
+ sourceColumns =
+ constraintDef.definition.map(
+ (col) => {
+ const colName =
+ extractColumnName(
+ col
+ ).replace(
+ /`/g,
+ ''
+ );
+ return colName;
+ }
+ );
+ }
+
+ // Process reference info (target table/columns)
+ const reference =
+ constraintDef.reference_definition ||
+ constraintDef.reference;
+
+ if (
+ reference &&
+ sourceColumns.length > 0
+ ) {
+ // Extract target table
+ let targetTable = '';
+ if (reference.table) {
+ if (
+ typeof reference.table ===
+ 'object'
+ ) {
+ if (
+ Array.isArray(
+ reference.table
+ ) &&
+ reference.table
+ .length > 0
+ ) {
+ targetTable =
+ reference
+ .table[0]
+ .table ||
+ '';
+ } else {
+ const tableRef =
+ reference.table as TableReference;
+ targetTable =
+ tableRef.table ||
+ '';
+ }
+ } else {
+ targetTable =
+ reference.table as string;
+ }
+
+ // Remove backticks
+ targetTable =
+ targetTable.replace(
+ /`/g,
+ ''
+ );
+ }
+
+ // Extract target columns
+ let targetColumns: string[] =
+ [];
+ if (
+ reference.columns &&
+ Array.isArray(
+ reference.columns
+ )
+ ) {
+ targetColumns =
+ reference.columns.map(
+ (col) => {
+ const colName =
+ typeof col ===
+ 'string'
+ ? col.replace(
+ /`/g,
+ ''
+ )
+ : extractColumnName(
+ col
+ ).replace(
+ /`/g,
+ ''
+ );
+ return colName;
+ }
+ );
+ } else if (
+ reference.definition &&
+ Array.isArray(
+ reference.definition
+ )
+ ) {
+ targetColumns =
+ reference.definition.map(
+ (col) => {
+ const colName =
+ extractColumnName(
+ col
+ ).replace(
+ /`/g,
+ ''
+ );
+ return colName;
+ }
+ );
+ }
+
+ // Add relationships for matching columns
+ if (
+ targetTable &&
+ targetColumns.length > 0
+ ) {
+ const targetTableId =
+ tableMap[
+ targetTable
+ ];
+
+ if (!targetTableId) {
+ // Store for later processing (after all tables are created)
+ const pendingFk: PendingForeignKey =
+ {
+ name: constraintDef.constraint_name
+ ? constraintDef.constraint_name.replace(
+ /`/g,
+ ''
+ )
+ : `${tableName}_${sourceColumns[0]}_fkey`,
+ sourceTable:
+ tableName,
+ sourceTableId:
+ tableId,
+ sourceColumns,
+ targetTable,
+ targetColumns,
+ updateAction:
+ reference.on_update,
+ deleteAction:
+ reference.on_delete,
+ };
+ pendingForeignKeys.push(
+ pendingFk
+ );
+ } else {
+ // Create foreign key relationships
+ for (
+ let i = 0;
+ i <
+ Math.min(
+ sourceColumns.length,
+ targetColumns.length
+ );
+ i++
+ ) {
+ const fk: SQLForeignKey =
+ {
+ name: constraintDef.constraint_name
+ ? constraintDef.constraint_name.replace(
+ /`/g,
+ ''
+ )
+ : `${tableName}_${sourceColumns[i]}_fkey`,
+ sourceTable:
+ tableName,
+ sourceColumn:
+ sourceColumns[
+ i
+ ],
+ targetTable,
+ targetColumn:
+ targetColumns[
+ i
+ ],
+ sourceTableId:
+ tableId,
+ targetTableId,
+ updateAction:
+ reference.on_update,
+ deleteAction:
+ reference.on_delete,
+ };
+
+ relationships.push(
+ fk
+ );
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ );
+ } else {
+ // If parser fails, try regex-based extraction as fallback
+ const extractedColumns =
+ extractColumnsFromCreateTable(trimmedStmt);
+ if (extractedColumns.length > 0) {
+ columns.push(...extractedColumns);
+ }
+ }
+
+ // Create and store the table
+ tables.push({
+ id: tableId,
+ name: tableName,
+ columns,
+ indexes,
+ order: tables.length,
+ });
+ }
+ }
+ } catch (parseError) {
+ console.error(
+ 'Error parsing CREATE TABLE statement:',
+ parseError
+ );
+
+ // Error handling without logging
+ }
+ }
+ }
+
+ // Second pass: process CREATE INDEX statements
+ for (const statement of statements) {
+ const trimmedStmt = statement.trim();
+ if (
+ trimmedStmt.toUpperCase().startsWith('CREATE INDEX') ||
+ trimmedStmt.toUpperCase().startsWith('CREATE UNIQUE INDEX')
+ ) {
+ processCreateIndexStatement(trimmedStmt, tableMap, tables);
+ }
+ }
+
+ // Third pass: process ALTER TABLE statements for foreign keys
+ for (const statement of statements) {
+ const trimmedStmt = statement.trim();
+ if (
+ trimmedStmt.toUpperCase().startsWith('ALTER TABLE') &&
+ trimmedStmt.toUpperCase().includes('FOREIGN KEY')
+ ) {
+ try {
+ // Look for ALTER TABLE `table` ADD CONSTRAINT pattern
+ const tableMatch = trimmedStmt.match(
+ /ALTER TABLE\s+`?([^`\s(]+)`?\s+/i
+ );
+ if (!tableMatch) continue;
+
+ const sourceTable = tableMatch[1].replace(/`/g, '');
+ const sourceTableId = tableMap[sourceTable];
+
+ if (!sourceTableId) {
+ continue;
+ }
+
+ // Extract constraint name if it exists
+ let constraintName = '';
+ const constraintMatch = trimmedStmt.match(
+ /ADD CONSTRAINT\s+`?([^`\s(]+)`?\s+/i
+ );
+ if (constraintMatch) {
+ constraintName = constraintMatch[1].replace(/`/g, '');
+ }
+
+ // Extract source columns
+ const sourceColMatch = trimmedStmt.match(
+ /FOREIGN KEY\s*\(([^)]+)\)/i
+ );
+ if (!sourceColMatch) continue;
+
+ const sourceColumns = sourceColMatch[1]
+ .split(',')
+ .map((col) => col.trim().replace(/`/g, ''));
+
+ // Extract target table and columns
+ const targetMatch = trimmedStmt.match(
+ /REFERENCES\s+`?([^`\s(]+)`?\s*\(([^)]+)\)/i
+ );
+ if (!targetMatch) continue;
+
+ const targetTable = targetMatch[1].replace(/`/g, '');
+ const targetColumns = targetMatch[2]
+ .split(',')
+ .map((col) => col.trim().replace(/`/g, ''));
+
+ const targetTableId = tableMap[targetTable];
+
+ if (!targetTableId) {
+ continue;
+ }
+
+ // Extract ON DELETE and ON UPDATE actions
+ let updateAction: string | undefined;
+ let deleteAction: string | undefined;
+
+ const onDeleteMatch = trimmedStmt.match(
+ /ON DELETE\s+([A-Z\s]+?)(?=\s+ON|\s*$)/i
+ );
+ if (onDeleteMatch) {
+ deleteAction = onDeleteMatch[1].trim();
+ }
+
+ const onUpdateMatch = trimmedStmt.match(
+ /ON UPDATE\s+([A-Z\s]+?)(?=\s+ON|\s*$)/i
+ );
+ if (onUpdateMatch) {
+ updateAction = onUpdateMatch[1].trim();
+ }
+
+ // Create the foreign key relationships
+ for (
+ let i = 0;
+ i <
+ Math.min(sourceColumns.length, targetColumns.length);
+ i++
+ ) {
+ const fk: SQLForeignKey = {
+ name:
+ constraintName ||
+ `${sourceTable}_${sourceColumns[i]}_fkey`,
+ sourceTable,
+ sourceColumn: sourceColumns[i],
+ targetTable,
+ targetColumn: targetColumns[i],
+ sourceTableId,
+ targetTableId,
+ updateAction,
+ deleteAction,
+ };
+
+ relationships.push(fk);
+ }
+ } catch (fkError) {
+ console.error(
+ 'Error processing foreign key in ALTER TABLE:',
+ fkError
+ );
+
+ // Error handling without logging
+ }
+ }
+ }
+
+ // After processing all tables, process pending foreign keys:
+ if (pendingForeignKeys.length > 0) {
+ for (const pendingFk of pendingForeignKeys) {
+ const targetTableId = tableMap[pendingFk.targetTable];
+
+ if (!targetTableId) {
+ continue;
+ }
+
+ // Create foreign key relationships
+ for (
+ let i = 0;
+ i <
+ Math.min(
+ pendingFk.sourceColumns.length,
+ pendingFk.targetColumns.length
+ );
+ i++
+ ) {
+ const fk: SQLForeignKey = {
+ name:
+ pendingFk.name ||
+ `${pendingFk.sourceTable}_${pendingFk.sourceColumns[i]}_fkey`,
+ sourceTable: pendingFk.sourceTable,
+ sourceColumn: pendingFk.sourceColumns[i],
+ targetTable: pendingFk.targetTable,
+ targetColumn: pendingFk.targetColumns[i],
+ sourceTableId: pendingFk.sourceTableId,
+ targetTableId,
+ updateAction: pendingFk.updateAction,
+ deleteAction: pendingFk.deleteAction,
+ };
+
+ relationships.push(fk);
+ }
+ }
+ }
+
+ return { tables, relationships };
+ } catch (error) {
+ console.error('Error in MySQL dump parser:', error);
+
+ throw new Error(
+ `Error parsing MySQL dump: ${(error as Error).message}`
+ );
+ }
+}
+
+export function isMySQLFormat(sqlContent: string): boolean {
+ // Common patterns in MySQL dumps
+ const mysqlDumpPatterns = [
+ /START TRANSACTION/i,
+ /CREATE TABLE.*IF NOT EXISTS/i,
+ /ENGINE\s*=\s*(?:InnoDB|MyISAM|MEMORY|ARCHIVE)/i,
+ /DEFAULT CHARSET\s*=\s*(?:utf8|latin1)/i,
+ /COLLATE\s*=\s*(?:utf8_general_ci|latin1_swedish_ci)/i,
+ /AUTO_INCREMENT\s*=\s*\d+/i,
+ /ALTER TABLE.*ADD CONSTRAINT.*FOREIGN KEY/i,
+ /-- (MySQL|MariaDB) dump/i,
+ ];
+
+ // Look for backticks around identifiers (common in MySQL)
+ const hasBackticks = /`[^`]+`/.test(sqlContent);
+
+ // Check for MySQL specific comments
+ const hasMysqlComments =
+ /-- MySQL dump|-- Host:|-- Server version:|-- Dump completed on/.test(
+ sqlContent
+ );
+
+ // If there are MySQL specific comments, it's likely a MySQL dump
+ if (hasMysqlComments) {
+ return true;
+ }
+
+ // Count how many MySQL patterns are found
+ let patternCount = 0;
+ for (const pattern of mysqlDumpPatterns) {
+ if (pattern.test(sqlContent)) {
+ patternCount++;
+ }
+ }
+
+ // If the SQL has backticks and at least a few MySQL patterns, it's likely MySQL
+ const isLikelyMysql = hasBackticks && patternCount >= 2;
+
+ return isLikelyMysql;
+}
diff --git a/src/lib/data/sql-import/dialect-importers/postgresql/postgresql-common.ts b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql-common.ts
new file mode 100644
index 00000000..66b62591
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql-common.ts
@@ -0,0 +1,254 @@
+import { Parser } from 'node-sql-parser';
+
+// Common PostgreSQL parser instance
+export const parser = new Parser();
+export const parserOpts = { database: 'postgresql' };
+
+// Define interfaces for AST nodes - Fixed no-explicit-any issues
+export interface SQLAstNode {
+ type: string;
+ keyword?: string;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ [key: string]: any; // Need to keep any here for compatibility with Parser's AST
+}
+
+// Define a minimal interface for table objects used in helper functions
+export interface TableLike {
+ id: string;
+ name: string;
+ schema?: string;
+ columns: unknown[];
+ indexes: unknown[];
+}
+
+export interface TableReference {
+ table?: string;
+ schema?: string;
+ db?: string; // Support for PostgreSQL AST compatibility
+}
+
+export interface ColumnReference {
+ column?:
+ | string
+ | { value?: string; expr?: { value?: string; type?: string } };
+ expr?: { value?: string; type?: string };
+ value?: string;
+ type?: string;
+}
+
+export interface ColumnDefinition {
+ resource: string;
+ column: string | ColumnReference;
+ definition?: {
+ dataType?: string;
+ constraint?: string;
+ length?: number;
+ precision?: number;
+ scale?: number;
+ };
+ primary_key?: string;
+ nullable?: { type?: string };
+ unique?: string;
+ default_val?: SQLAstNode;
+ auto_increment?: string;
+}
+
+export interface ConstraintDefinition {
+ resource: string;
+ constraint_type: string;
+ constraint_name?: string;
+ definition?: Array<ColumnReference> | { columns?: string[] };
+ columns?: string[];
+ reference_definition?: ReferenceDefinition;
+ reference?: ReferenceDefinition;
+}
+
+export interface ReferenceDefinition {
+ table?: string | TableReference | TableReference[];
+ columns?: Array<ColumnReference | string> | string[];
+ definition?: Array<ColumnReference>;
+ on_update?: string;
+ on_delete?: string;
+}
+
+export interface CreateTableStatement extends SQLAstNode {
+ table: TableReference | TableReference[];
+ create_definitions?: Array<ColumnDefinition | ConstraintDefinition>;
+ comment?: string;
+}
+
+export interface CreateIndexStatement extends SQLAstNode {
+ table: TableReference | TableReference[] | string;
+ index?: string;
+ index_name?: string;
+ index_type?: string;
+ unique?: boolean;
+ columns?: Array<ColumnReference>;
+ index_columns?: Array<{ column?: ColumnReference } | ColumnReference>;
+}
+
+export interface AlterTableConstraintDefinition extends ConstraintDefinition {
+ constraint?: string;
+}
+
+export interface AlterTableExprItem {
+ action: string;
+ resource?: string;
+ type?: string;
+ constraint?: { constraint_type?: string };
+ create_definitions?:
+ | AlterTableConstraintDefinition
+ | {
+ constraint_type?: string;
+ definition?: Array<ColumnReference>;
+ constraint?: string;
+ reference_definition?: ReferenceDefinition;
+ resource?: string;
+ };
+}
+
+export interface AlterTableStatement extends SQLAstNode {
+ table: TableReference[] | TableReference | string;
+ expr: AlterTableExprItem[];
+}
+
+// Define type for column type arguments
+export interface TypeArgs {
+ length?: number;
+ precision?: number;
+ scale?: number;
+}
+
+// Helper to extract column name from different AST formats
+export function extractColumnName(
+ columnObj: string | ColumnReference | undefined
+): string {
+ if (!columnObj) return '';
+
+ // Handle different formats based on actual AST structure
+ if (typeof columnObj === 'string') return columnObj;
+
+ if (typeof columnObj === 'object') {
+ // Direct column property
+ if (columnObj.column) {
+ if (typeof columnObj.column === 'string') return columnObj.column;
+ if (typeof columnObj.column === 'object') {
+ // Handle nested value property
+ if (columnObj.column.value) return columnObj.column.value;
+ // Handle expression property with value
+ if (columnObj.column.expr?.value)
+ return columnObj.column.expr.value;
+ // Handle double_quote_string type which is common in PostgreSQL
+ if (columnObj.column.expr?.type === 'double_quote_string')
+ return columnObj.column.expr.value || '';
+ // Direct access to expr
+ if (columnObj.column.expr?.type === 'default')
+ return columnObj.column.expr.value || '';
+ }
+ }
+
+ // Direct expr property
+ if (columnObj.expr) {
+ if (columnObj.expr.type === 'default')
+ return columnObj.expr.value || '';
+ if (columnObj.expr.type === 'double_quote_string')
+ return columnObj.expr.value || '';
+ if (columnObj.expr.value) return columnObj.expr.value;
+ }
+
+ // Direct value property
+ if (columnObj.value) return columnObj.value;
+ }
+
+ return '';
+}
+
+// Helper function to extract type arguments from column definition
+export function getTypeArgs(
+ definition: ColumnDefinition['definition'] | undefined
+): TypeArgs {
+ const typeArgs: TypeArgs = {};
+
+ if (!definition) return typeArgs;
+
+ if (definition.length !== undefined) {
+ typeArgs.length = definition.length;
+ }
+
+ if (definition.scale !== undefined && definition.precision !== undefined) {
+ typeArgs.precision = definition.precision;
+ typeArgs.scale = definition.scale;
+ }
+
+ return typeArgs;
+}
+
+// Helper function to find a table with consistent schema handling
+export function findTableWithSchemaSupport(
+ tables: TableLike[],
+ tableName: string,
+ schemaName?: string
+): TableLike | undefined {
+ // Default to public schema if none provided
+ const effectiveSchema = schemaName || 'public';
+
+ // First try with exact schema match
+ let table = tables.find(
+ (t) => t.name === tableName && t.schema === effectiveSchema
+ );
+
+ // If not found with schema, try with the legacy schema match
+ if (!table && schemaName) {
+ table = tables.find(
+ (t) => t.name === tableName && t.schema === schemaName
+ );
+ }
+
+ // If still not found with schema, try any match on the table name
+ if (!table) {
+ table = tables.find((t) => t.name === tableName);
+ if (table) {
+ console.log(
+ `Found table ${tableName} without schema match, source schema: ${effectiveSchema}, table schema: ${table.schema}`
+ );
+ }
+ }
+
+ return table;
+}
+
+// Helper function to find table ID with schema support
+export function getTableIdWithSchemaSupport(
+ tableMap: Record<string, string>,
+ tableName: string,
+ schemaName?: string
+): string | undefined {
+ // Default to public schema if none provided
+ const effectiveSchema = schemaName || 'public';
+
+ // First try with schema
+ const tableKey = `${effectiveSchema}.${tableName}`;
+ let tableId = tableMap[tableKey];
+
+ // If not found with the effective schema, try with the original schema if different
+ if (!tableId && schemaName && schemaName !== effectiveSchema) {
+ const originalSchemaKey = `${schemaName}.${tableName}`;
+ tableId = tableMap[originalSchemaKey];
+ }
+
+ // If still not found with schema, try without schema
+ if (!tableId) {
+ tableId = tableMap[tableName];
+ if (tableId) {
+ console.log(
+ `Found table ID for ${tableName} without schema match, source schema: ${effectiveSchema}`
+ );
+ } else {
+ console.warn(
+ `No table ID found for ${tableName} with schema ${effectiveSchema}`
+ );
+ }
+ }
+
+ return tableId;
+}
diff --git a/src/lib/data/sql-import/dialect-importers/postgresql/postgresql-dump.ts b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql-dump.ts
new file mode 100644
index 00000000..80b54f74
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql-dump.ts
@@ -0,0 +1,778 @@
+import { generateId } from '@/lib/utils';
+import type {
+ SQLParserResult,
+ SQLTable,
+ SQLColumn,
+ SQLIndex,
+ SQLForeignKey,
+} from '../../common';
+import { buildSQLFromAST } from '../../common';
+import type {
+ ColumnDefinition,
+ ConstraintDefinition,
+ CreateTableStatement,
+ TableReference,
+} from './postgresql-common';
+import {
+ parser,
+ parserOpts,
+ extractColumnName,
+ getTypeArgs,
+} from './postgresql-common';
+
+// Helper to extract statements from PostgreSQL dump
+function extractStatements(sqlContent: string): string[] {
+ const statements: string[] = [];
+ let currentStatement = '';
+ const lines = sqlContent.split('\n');
+
+ for (let i = 0; i < lines.length; i++) {
+ const line = lines[i].trim();
+
+ // Skip comments and empty lines
+ if (line.startsWith('--') || line === '') {
+ continue;
+ }
+
+ // Add line to current statement
+ currentStatement += line + ' ';
+
+ // If line ends with semicolon, consider statement complete
+ if (line.endsWith(';')) {
+ statements.push(currentStatement.trim());
+ currentStatement = '';
+ }
+ }
+
+ // Handle any remaining statement
+ if (currentStatement.trim()) {
+ statements.push(currentStatement.trim());
+ }
+
+ return statements;
+}
+
+// Process PostgreSQL pg_dump foreign key constraints
+function processForeignKeyConstraint(
+ statement: string,
+ tableMap: Record<string, string>,
+ relationships: SQLForeignKey[]
+): void {
+ // Only process statements that look like foreign key constraints
+ if (
+ !statement.includes('ADD CONSTRAINT') ||
+ !statement.includes('FOREIGN KEY') ||
+ !statement.includes('REFERENCES')
+ ) {
+ return;
+ }
+
+ try {
+ // Extract source table info - find between ALTER TABLE and ADD CONSTRAINT
+ // This regex handles:
+ // - ALTER TABLE ONLY schema.table
+ // - ALTER TABLE ONLY "schema"."table"
+ // - ALTER TABLE schema.table
+ // - ALTER TABLE "schema"."table"
+ const tableRegex =
+ /ALTER TABLE(?:\s+ONLY)?\s+(?:"?([^"\s.]+)"?\.)?(?:"?([^"\s.(]+)"?)/i;
+ const tableMatch = statement.match(tableRegex);
+
+ if (!tableMatch) {
+ return;
+ }
+
+ // Extract source schema and table name
+ const sourceSchema = tableMatch[1] || '';
+ const sourceTable = tableMatch[2];
+
+ // Find constraint name
+ const constraintRegex = /ADD CONSTRAINT\s+"?([^"\s]+)"?\s+FOREIGN KEY/i;
+ const constraintMatch = statement.match(constraintRegex);
+ const constraintName = constraintMatch ? constraintMatch[1] : '';
+
+ // Extract source columns - handles either quoted or unquoted column names
+ // This regex captures columns in format: FOREIGN KEY (col1, col2, ...)
+ const sourceColRegex = /FOREIGN KEY\s+\(\s*([^)]+)\)/i;
+ const sourceColMatch = statement.match(sourceColRegex);
+
+ if (!sourceColMatch) {
+ return;
+ }
+
+ // Parse the captured group to extract all columns
+ const sourceColumnsPart = sourceColMatch[1];
+ const sourceColumns = sourceColumnsPart.split(',').map((col) =>
+ col
+ .trim()
+ .replace(/^"(.*)"$/, '$1')
+ .replace(/^\s*"?([^"\s]+)"?\s*$/, '$1')
+ );
+
+ // Extract target table and columns
+ // This regex handles: REFERENCES schema.table (col1, col2, ...)
+ const targetRegex =
+ /REFERENCES\s+(?:"?([^"\s.]+)"?\.)?(?:"?([^"\s.(]+)"?)\s*\(\s*([^)]+)\)/i;
+ const targetMatch = statement.match(targetRegex);
+
+ if (!targetMatch) {
+ return;
+ }
+
+ // Extract target schema, table and columns
+ const targetSchema = targetMatch[1] || '';
+ const targetTable = targetMatch[2];
+
+ // Parse the captured group to extract all target columns
+ const targetColumnsPart = targetMatch[3];
+ const targetColumns = targetColumnsPart.split(',').map((col) =>
+ col
+ .trim()
+ .replace(/^"(.*)"$/, '$1')
+ .replace(/^\s*"?([^"\s]+)"?\s*$/, '$1')
+ );
+
+ // Extract ON DELETE and ON UPDATE actions
+ const deleteActionRegex = /ON DELETE\s+([A-Z\s]+?)(?:\s+ON|;|\s*$)/i;
+ const deleteActionMatch = statement.match(deleteActionRegex);
+ const deleteAction = deleteActionMatch
+ ? deleteActionMatch[1].trim()
+ : undefined;
+
+ const updateActionRegex = /ON UPDATE\s+([A-Z\s]+?)(?:\s+ON|;|\s*$)/i;
+ const updateActionMatch = statement.match(updateActionRegex);
+ const updateAction = updateActionMatch
+ ? updateActionMatch[1].trim()
+ : undefined;
+
+ // Look up table IDs
+ const sourceTableKey = `${sourceSchema ? sourceSchema + '.' : ''}${sourceTable}`;
+ const sourceTableId = tableMap[sourceTableKey];
+
+ const targetTableKey = `${targetSchema ? targetSchema + '.' : ''}${targetTable}`;
+ const targetTableId = tableMap[targetTableKey];
+
+ if (!sourceTableId || !targetTableId) {
+ return;
+ }
+
+ // Create relationships for each column pair
+ for (
+ let i = 0;
+ i < Math.min(sourceColumns.length, targetColumns.length);
+ i++
+ ) {
+ const relationship: SQLForeignKey = {
+ name:
+ constraintName || `${sourceTable}_${sourceColumns[i]}_fkey`,
+ sourceTable,
+ sourceSchema,
+ sourceColumn: sourceColumns[i],
+ targetTable,
+ targetSchema,
+ targetColumn: targetColumns[i],
+ sourceTableId,
+ targetTableId,
+ updateAction,
+ deleteAction,
+ };
+
+ relationships.push(relationship);
+ }
+ } catch (error) {
+ console.error('Error processing foreign key constraint:', error);
+ }
+}
+
+// Function to extract columns from a CREATE TABLE statement using regex
+function extractColumnsFromCreateTable(statement: string): SQLColumn[] {
+ const columns: SQLColumn[] = [];
+
+ // Extract everything between the first opening and last closing parenthesis
+ const columnMatch = statement.match(/CREATE\s+TABLE.*?\((.*)\)[^)]*;$/s);
+ if (!columnMatch || !columnMatch[1]) {
+ return columns;
+ }
+
+ const columnDefs = columnMatch[1].trim();
+ // Split by commas, but not those within parentheses (for nested type definitions)
+ const columnLines = columnDefs.split(/,(?![^(]*\))/);
+
+ for (const columnLine of columnLines) {
+ const line = columnLine.trim();
+ // Skip constraints at the table level
+ if (
+ line.toUpperCase().startsWith('CONSTRAINT') ||
+ line.toUpperCase().startsWith('PRIMARY KEY') ||
+ line.toUpperCase().startsWith('FOREIGN KEY') ||
+ line.toUpperCase().startsWith('UNIQUE')
+ ) {
+ continue;
+ }
+
+ // Extract column name and definition
+ const columnNameMatch = line.match(/^"?([^"\s]+)"?\s+(.+)$/);
+ if (columnNameMatch) {
+ const columnName = columnNameMatch[1];
+ const definition = columnNameMatch[2];
+
+ // Determine if column is nullable
+ const nullable = !definition.toUpperCase().includes('NOT NULL');
+
+ // Determine if column is primary key
+ const primaryKey = definition.toUpperCase().includes('PRIMARY KEY');
+
+ // Extract data type
+ const typeMatch = definition.match(/^([^\s(]+)(?:\(([^)]+)\))?/);
+ const dataType = typeMatch ? typeMatch[1] : '';
+
+ columns.push({
+ name: columnName,
+ type: dataType,
+ nullable,
+ primaryKey,
+ unique: definition.toUpperCase().includes('UNIQUE'),
+ });
+ }
+ }
+
+ return columns;
+}
+
+// Process PostgreSQL pg_dump primary key constraints
+function processPrimaryKeyConstraint(
+ statement: string,
+ tableMap: Record<string, string>,
+ tables: SQLTable[]
+): void {
+ // Only process statements that look like primary key constraints
+ if (
+ !statement.includes('ADD CONSTRAINT') ||
+ !statement.includes('PRIMARY KEY')
+ ) {
+ return;
+ }
+
+ try {
+ // Extract source table info - similar pattern as FK extraction
+ const tableRegex =
+ /ALTER TABLE(?:\s+ONLY)?\s+(?:"?([^"\s.]+)"?\.)?(?:"?([^"\s.(]+)"?)/i;
+ const tableMatch = statement.match(tableRegex);
+
+ if (!tableMatch) {
+ return;
+ }
+
+ // Extract source schema and table name
+ const sourceSchema = tableMatch[1] || '';
+ const sourceTable = tableMatch[2];
+
+ // Extract primary key columns
+ const pkColRegex = /PRIMARY KEY\s+\(\s*([^)]+)\)/i;
+ const pkColMatch = statement.match(pkColRegex);
+
+ if (!pkColMatch) {
+ return;
+ }
+
+ // Parse the captured group to extract all columns
+ const pkColumnsPart = pkColMatch[1];
+ const pkColumns = pkColumnsPart.split(',').map((col) =>
+ col
+ .trim()
+ .replace(/^"(.*)"$/, '$1')
+ .replace(/^\s*"?([^"\s]+)"?\s*$/, '$1')
+ );
+
+ // Find the table in our collection
+ const tableKey = `${sourceSchema ? sourceSchema + '.' : ''}${sourceTable}`;
+ const tableId = tableMap[tableKey];
+
+ if (!tableId) {
+ return;
+ }
+
+ // Find the table in our tables array
+ const table = tables.find((t) => t.id === tableId);
+ if (!table) {
+ return;
+ }
+
+ // Mark columns as primary key
+ pkColumns.forEach((colName) => {
+ const column = table.columns.find((c) => c.name === colName);
+ if (column) {
+ column.primaryKey = true;
+ }
+ });
+
+ // Add a primary key index if it doesn't exist
+ if (pkColumns.length > 0) {
+ const existingPkIndex = table.indexes.find(
+ (idx) =>
+ idx.unique &&
+ idx.columns.length === pkColumns.length &&
+ idx.columns.every((col, i) => col === pkColumns[i])
+ );
+
+ if (!existingPkIndex) {
+ const pkIndexName =
+ statement.match(
+ /ADD CONSTRAINT\s+"?([^"\s]+)"?\s+PRIMARY KEY/i
+ )?.[1] || `pk_${sourceTable}`;
+
+ table.indexes.push({
+ name: pkIndexName,
+ columns: pkColumns,
+ unique: true,
+ });
+ }
+ }
+ } catch (error) {
+ console.error('Error processing primary key constraint:', error);
+ }
+}
+
+// Process PostgreSQL pg_dump unique constraints
+function processUniqueConstraint(
+ statement: string,
+ tableMap: Record<string, string>,
+ tables: SQLTable[]
+): void {
+ // Only process statements that look like unique constraints
+ if (
+ !statement.includes('ADD CONSTRAINT') ||
+ !statement.includes('UNIQUE')
+ ) {
+ return;
+ }
+
+ try {
+ // Extract source table info - similar pattern as other constraints
+ const tableRegex =
+ /ALTER TABLE(?:\s+ONLY)?\s+(?:"?([^"\s.]+)"?\.)?(?:"?([^"\s.(]+)"?)/i;
+ const tableMatch = statement.match(tableRegex);
+
+ if (!tableMatch) {
+ return;
+ }
+
+ // Extract source schema and table name
+ const sourceSchema = tableMatch[1] || '';
+ const sourceTable = tableMatch[2];
+
+ // Extract constraint name
+ const constraintNameRegex = /ADD CONSTRAINT\s+"?([^"\s]+)"?\s+UNIQUE/i;
+ const constraintNameMatch = statement.match(constraintNameRegex);
+ const constraintName = constraintNameMatch
+ ? constraintNameMatch[1]
+ : `unique_${sourceTable}`;
+
+ // Extract unique columns
+ const uniqueColRegex = /UNIQUE\s+\(\s*([^)]+)\)/i;
+ const uniqueColMatch = statement.match(uniqueColRegex);
+
+ if (!uniqueColMatch) {
+ return;
+ }
+
+ // Parse the captured group to extract all columns
+ const uniqueColumnsPart = uniqueColMatch[1];
+ const uniqueColumns = uniqueColumnsPart.split(',').map((col) =>
+ col
+ .trim()
+ .replace(/^"(.*)"$/, '$1')
+ .replace(/^\s*"?([^"\s]+)"?\s*$/, '$1')
+ );
+
+ // Find the table in our collection
+ const tableKey = `${sourceSchema ? sourceSchema + '.' : ''}${sourceTable}`;
+ const tableId = tableMap[tableKey];
+
+ if (!tableId) {
+ return;
+ }
+
+ // Find the table in our tables array
+ const table = tables.find((t) => t.id === tableId);
+ if (!table) {
+ return;
+ }
+
+ // Mark columns as unique if it's a single column constraint
+ if (uniqueColumns.length === 1) {
+ const column = table.columns.find(
+ (c) => c.name === uniqueColumns[0]
+ );
+ if (column) {
+ column.unique = true;
+ }
+ }
+
+ // Add a unique index if it doesn't exist
+ if (uniqueColumns.length > 0) {
+ const existingUniqueIndex = table.indexes.find(
+ (idx) =>
+ idx.unique &&
+ idx.columns.length === uniqueColumns.length &&
+ idx.columns.every((col, i) => col === uniqueColumns[i])
+ );
+
+ if (!existingUniqueIndex) {
+ table.indexes.push({
+ name: constraintName,
+ columns: uniqueColumns,
+ unique: true,
+ });
+ }
+ }
+ } catch (error) {
+ console.error('Error processing unique constraint:', error);
+ }
+}
+
+// Process PostgreSQL pg_dump CREATE INDEX statements
+function processCreateIndexStatement(
+ statement: string,
+ tableMap: Record<string, string>,
+ tables: SQLTable[]
+): void {
+ if (
+ !statement.startsWith('CREATE INDEX') &&
+ !statement.startsWith('CREATE UNIQUE INDEX')
+ ) {
+ return;
+ }
+
+ try {
+ // Determine if the index is unique
+ const isUnique = statement.startsWith('CREATE UNIQUE INDEX');
+
+ // Extract index name
+ const indexNameRegex = /CREATE (?:UNIQUE )?INDEX\s+"?([^"\s]+)"?/i;
+ const indexNameMatch = statement.match(indexNameRegex);
+ const indexName = indexNameMatch ? indexNameMatch[1] : '';
+
+ if (!indexName) {
+ return;
+ }
+
+ // Extract table name and schema
+ const tableRegex = /ON\s+(?:"?([^"\s.]+)"?\.)?(?:"?([^"\s.(]+)"?)/i;
+ const tableMatch = statement.match(tableRegex);
+
+ if (!tableMatch) {
+ return;
+ }
+
+ const tableSchema = tableMatch[1] || '';
+ const tableName = tableMatch[2];
+
+ // Extract index columns
+ const columnsRegex = /\(\s*([^)]+)\)/i;
+ const columnsMatch = statement.match(columnsRegex);
+
+ if (!columnsMatch) {
+ return;
+ }
+
+ // Parse columns (handle function-based indexes, etc.)
+ const columnsStr = columnsMatch[1];
+ // This is a simplified approach - advanced indexes may need more complex parsing
+ const indexColumns = columnsStr.split(',').map((col) => {
+ // Extract basic column name, handling possible expressions
+ const colName = col
+ .trim()
+ .replace(/^"(.*)"$/, '$1')
+ .replace(/^\s*"?([^"\s(]+)"?\s*.*$/, '$1'); // Get just the column name part
+ return colName;
+ });
+
+ if (indexColumns.length === 0) {
+ return;
+ }
+
+ // Find the table
+ const tableKey = `${tableSchema ? tableSchema + '.' : ''}${tableName}`;
+ const tableId = tableMap[tableKey];
+
+ if (!tableId) {
+ return;
+ }
+
+ const table = tables.find((t) => t.id === tableId);
+ if (!table) {
+ return;
+ }
+
+ // Check if a similar index already exists (to avoid duplicates)
+ const existingIndex = table.indexes.find(
+ (idx) =>
+ idx.name === indexName ||
+ (idx.columns.length === indexColumns.length &&
+ idx.columns.every((col, i) => col === indexColumns[i]))
+ );
+
+ if (!existingIndex) {
+ table.indexes.push({
+ name: indexName,
+ columns: indexColumns,
+ unique: isUnique,
+ });
+ }
+ } catch (error) {
+ console.error('Error processing CREATE INDEX statement:', error);
+ }
+}
+
+// PostgreSQL dump-specific parsing logic - optimized for pg_dump output format
+export function fromPostgresDump(sqlContent: string): SQLParserResult {
+ const tables: SQLTable[] = [];
+ const relationships: SQLForeignKey[] = [];
+ const tableMap: Record<string, string> = {}; // Maps table name to its ID
+
+ // Extract statements for different types to process in the correct order
+ const alterTableStatements: string[] = [];
+ const createTableStatements: string[] = [];
+ const createIndexStatements: string[] = [];
+
+ // Split SQL dump into statements
+ const statements = extractStatements(sqlContent);
+
+ for (const statement of statements) {
+ if (statement.trim().startsWith('CREATE TABLE')) {
+ createTableStatements.push(statement);
+ } else if (statement.trim().startsWith('CREATE INDEX')) {
+ createIndexStatements.push(statement);
+ } else if (statement.trim().startsWith('ALTER TABLE')) {
+ alterTableStatements.push(statement);
+ }
+ }
+
+ try {
+ // Phase 1: Process CREATE TABLE statements individually
+ for (const statement of createTableStatements) {
+ try {
+ // Parse just this statement with the SQL parser
+ const ast = parser.astify(statement, parserOpts);
+ if (Array.isArray(ast) && ast.length > 0) {
+ const createTableStmt = ast[0] as CreateTableStatement;
+
+ // Extract table name and schema
+ let tableName = '';
+ let schemaName = '';
+
+ if (
+ createTableStmt.table &&
+ typeof createTableStmt.table === 'object'
+ ) {
+ // Handle array of tables if needed
+ if (
+ Array.isArray(createTableStmt.table) &&
+ createTableStmt.table.length > 0
+ ) {
+ const tableObj = createTableStmt.table[0];
+ tableName = tableObj.table || '';
+ schemaName = tableObj.schema || '';
+ } else {
+ // Direct object reference
+ const tableObj =
+ createTableStmt.table as TableReference;
+ tableName = tableObj.table || '';
+ schemaName = tableObj.schema || '';
+ }
+ }
+
+ if (!tableName) {
+ // Try to extract table name using regex for cases where the parser might fail
+ const tableNameMatch = statement.match(
+ /CREATE\s+TABLE\s+(?:ONLY\s+)?(?:(?:"?([^"\s.]+)"?\.)?"?([^"\s.(]+)"?)/i
+ );
+ if (tableNameMatch) {
+ schemaName = tableNameMatch[1] || '';
+ tableName = tableNameMatch[2];
+ } else {
+ continue;
+ }
+ }
+
+ // If schema is not in the AST, try to extract it from the SQL
+ if (!schemaName) {
+ // Look for schema in CREATE TABLE statement: CREATE TABLE schema.table (
+ const schemaMatch = statement.match(
+ /CREATE\s+TABLE\s+(?:ONLY\s+)?(?:"?([^"\s.]+)"?\.)/i
+ );
+ if (schemaMatch && schemaMatch[1]) {
+ schemaName = schemaMatch[1];
+ }
+ }
+
+ // Generate a unique ID for the table
+ const tableId = generateId();
+ const tableKey = `${schemaName ? schemaName + '.' : ''}${tableName}`;
+ tableMap[tableKey] = tableId;
+
+ // Process table columns
+ let columns: SQLColumn[] = [];
+ const indexes: SQLIndex[] = [];
+
+ // Try to extract columns from AST first
+ let columnsFromAst = false;
+ if (
+ createTableStmt.create_definitions &&
+ Array.isArray(createTableStmt.create_definitions)
+ ) {
+ createTableStmt.create_definitions.forEach(
+ (def: ColumnDefinition | ConstraintDefinition) => {
+ if (def.resource === 'column') {
+ const columnDef = def as ColumnDefinition;
+ const columnName = extractColumnName(
+ columnDef.column
+ );
+ const dataType =
+ columnDef.definition?.dataType || '';
+
+ if (columnName) {
+ const isPrimaryKey =
+ columnDef.primary_key ===
+ 'primary key' ||
+ columnDef.definition?.constraint ===
+ 'primary key';
+
+ columns.push({
+ name: columnName,
+ type: dataType,
+ nullable:
+ columnDef.nullable?.type !==
+ 'not null',
+ primaryKey: isPrimaryKey,
+ unique:
+ columnDef.unique === 'unique',
+ typeArgs: getTypeArgs(
+ columnDef.definition
+ ),
+ default: columnDef.default_val
+ ? buildSQLFromAST(
+ columnDef.default_val
+ )
+ : undefined,
+ increment:
+ columnDef.auto_increment ===
+ 'auto_increment',
+ });
+ columnsFromAst = true;
+ }
+ }
+ }
+ );
+ }
+
+ // If we couldn't extract columns from AST, try regex approach
+ if (!columnsFromAst || columns.length === 0) {
+ columns = extractColumnsFromCreateTable(statement);
+ }
+
+ // Create and add the table object
+ const table: SQLTable = {
+ id: tableId,
+ name: tableName,
+ schema: schemaName,
+ columns,
+ indexes,
+ order: tables.length,
+ };
+
+ // Set comment if available
+ if (
+ 'comment' in createTableStmt &&
+ typeof createTableStmt.comment === 'string'
+ ) {
+ table.comment = createTableStmt.comment;
+ }
+
+ tables.push(table);
+ }
+ } catch (error) {
+ console.error('Error parsing CREATE TABLE statement:', error);
+
+ // Fallback: extract table and columns using regex
+ try {
+ const tableNameMatch = statement.match(
+ /CREATE\s+TABLE\s+(?:ONLY\s+)?(?:(?:"?([^"\s.]+)"?\.)?"?([^"\s.(]+)"?)/i
+ );
+ if (tableNameMatch) {
+ const schemaName = tableNameMatch[1] || '';
+ const tableName = tableNameMatch[2];
+
+ // Generate a unique ID for the table
+ const tableId = generateId();
+ const tableKey = `${schemaName ? schemaName + '.' : ''}${tableName}`;
+ tableMap[tableKey] = tableId;
+
+ // Extract columns using regex
+ const columns =
+ extractColumnsFromCreateTable(statement);
+
+ // Create and add the table object
+ const table: SQLTable = {
+ id: tableId,
+ name: tableName,
+ schema: schemaName,
+ columns,
+ indexes: [],
+ order: tables.length,
+ };
+
+ tables.push(table);
+ }
+ } catch (fallbackError) {
+ console.error(
+ 'Fallback extraction also failed:',
+ fallbackError
+ );
+ }
+ }
+ }
+
+ // Phase 2: Process CREATE INDEX statements
+ for (const statement of createIndexStatements) {
+ processCreateIndexStatement(statement, tableMap, tables);
+ }
+
+ // Phase 3: First process PRIMARY KEY constraints
+ for (const statement of alterTableStatements) {
+ if (statement.includes('PRIMARY KEY')) {
+ processPrimaryKeyConstraint(statement, tableMap, tables);
+ }
+ }
+
+ // Phase 3.5: Then process UNIQUE constraints
+ for (const statement of alterTableStatements) {
+ if (
+ statement.includes('UNIQUE') &&
+ !statement.includes('PRIMARY KEY')
+ ) {
+ processUniqueConstraint(statement, tableMap, tables);
+ }
+ }
+
+ // Phase 4: Then process FOREIGN KEY constraints
+ for (const statement of alterTableStatements) {
+ if (statement.includes('FOREIGN KEY')) {
+ processForeignKeyConstraint(statement, tableMap, relationships);
+ }
+ }
+
+ // Filter out relationships with missing IDs
+ const validRelationships = relationships.filter(
+ (rel) => rel.sourceTableId && rel.targetTableId
+ );
+
+ return { tables, relationships: validRelationships };
+ } catch (error: unknown) {
+ console.error('Error in PostgreSQL dump parser:', error);
+ throw new Error(
+ `Error parsing PostgreSQL dump: ${(error as Error).message}`
+ );
+ }
+}
diff --git a/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts
new file mode 100644
index 00000000..70c44aa7
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts
@@ -0,0 +1,878 @@
+import { generateId } from '@/lib/utils';
+import type {
+ SQLParserResult,
+ SQLTable,
+ SQLColumn,
+ SQLIndex,
+ SQLForeignKey,
+} from '../../common';
+import { buildSQLFromAST } from '../../common';
+import type {
+ SQLAstNode,
+ TableReference,
+ ColumnReference,
+ ColumnDefinition,
+ ConstraintDefinition,
+ CreateTableStatement,
+ CreateIndexStatement,
+ AlterTableExprItem,
+ AlterTableStatement,
+} from './postgresql-common';
+import {
+ parser,
+ parserOpts,
+ extractColumnName,
+ getTypeArgs,
+ findTableWithSchemaSupport,
+ getTableIdWithSchemaSupport,
+} from './postgresql-common';
+
+// PostgreSQL-specific parsing logic
+export function fromPostgres(sqlContent: string): SQLParserResult {
+ const tables: SQLTable[] = [];
+ const relationships: SQLForeignKey[] = [];
+ const tableMap: Record<string, string> = {}; // Maps table name to its ID
+
+ try {
+ // Parse the SQL DDL statements
+ const ast = parser.astify(sqlContent, parserOpts);
+
+ if (!Array.isArray(ast)) {
+ throw new Error('Failed to parse SQL DDL - AST is not an array');
+ }
+
+ // Process each CREATE TABLE statement
+ ast.forEach((stmt: SQLAstNode) => {
+ if (stmt.type === 'create' && stmt.keyword === 'table') {
+ // Extract table name and schema
+ let tableName = '';
+ let schemaName = '';
+
+ const createTableStmt = stmt as CreateTableStatement;
+
+ if (
+ createTableStmt.table &&
+ typeof createTableStmt.table === 'object'
+ ) {
+ // Handle array of tables if needed
+ if (
+ Array.isArray(createTableStmt.table) &&
+ createTableStmt.table.length > 0
+ ) {
+ const tableObj = createTableStmt.table[0];
+ tableName = tableObj.table || '';
+ // Check for schema in both 'schema' and 'db' fields
+ schemaName = tableObj.schema || tableObj.db || '';
+ } else {
+ // Direct object reference
+ const tableObj =
+ createTableStmt.table as TableReference;
+ tableName = tableObj.table || '';
+ // Check for schema in both 'schema' and 'db' fields
+ schemaName = tableObj.schema || tableObj.db || '';
+ }
+ }
+
+ if (!tableName) {
+ return;
+ }
+
+ // Check if tableName contains a schema prefix (schema.table)
+ if (!schemaName && tableName.includes('.')) {
+ const parts = tableName.split('.');
+ schemaName = parts[0].replace(/"/g, '');
+ tableName = parts[1].replace(/"/g, '');
+ }
+
+ // If still no schema, ensure default schema is set to public
+ if (!schemaName) {
+ schemaName = 'public';
+ }
+
+ // Generate a unique ID for the table
+ const tableId = generateId();
+ const tableKey = `${schemaName ? schemaName + '.' : ''}${tableName}`;
+ tableMap[tableKey] = tableId;
+
+ // Process table columns
+ const columns: SQLColumn[] = [];
+ const indexes: SQLIndex[] = [];
+
+ // Debugged from actual parse output - handle different structure formats
+ if (
+ createTableStmt.create_definitions &&
+ Array.isArray(createTableStmt.create_definitions)
+ ) {
+ createTableStmt.create_definitions.forEach(
+ (def: ColumnDefinition | ConstraintDefinition) => {
+ // Process column definition
+ if (def.resource === 'column') {
+ const columnDef = def as ColumnDefinition;
+ const columnName = extractColumnName(
+ columnDef.column
+ );
+ const dataType =
+ columnDef.definition?.dataType || '';
+
+ // Handle the column definition and add to columns array
+ if (columnName) {
+ // Check if the column has a PRIMARY KEY constraint inline
+ const isPrimaryKey =
+ columnDef.primary_key ===
+ 'primary key' ||
+ columnDef.definition?.constraint ===
+ 'primary key';
+
+ columns.push({
+ name: columnName,
+ type: dataType,
+ nullable:
+ columnDef.nullable?.type !==
+ 'not null',
+ primaryKey: isPrimaryKey,
+ unique: columnDef.unique === 'unique',
+ typeArgs: getTypeArgs(
+ columnDef.definition
+ ),
+ default: columnDef.default_val
+ ? buildSQLFromAST(
+ columnDef.default_val
+ )
+ : undefined,
+ increment:
+ columnDef.auto_increment ===
+ 'auto_increment',
+ });
+ }
+ } else if (def.resource === 'constraint') {
+ // Handle constraint definitions
+ const constraintDef =
+ def as ConstraintDefinition;
+ if (
+ constraintDef.constraint_type ===
+ 'primary key'
+ ) {
+ // Check if definition is an array (standalone PRIMARY KEY constraint)
+ if (
+ Array.isArray(constraintDef.definition)
+ ) {
+ // Extract column names from the constraint definition
+ for (const colDef of constraintDef.definition) {
+ if (
+ typeof colDef === 'object' &&
+ 'type' in colDef &&
+ colDef.type === 'column_ref' &&
+ 'column' in colDef &&
+ colDef.column
+ ) {
+ const pkColumnName =
+ extractColumnName(colDef);
+
+ // Find and mark the column as primary key
+ const column = columns.find(
+ (col) =>
+ col.name ===
+ pkColumnName
+ );
+ if (column) {
+ column.primaryKey = true;
+ }
+ }
+ }
+
+ // Add a primary key index
+ const pkColumnNames =
+ constraintDef.definition
+ .filter(
+ (colDef: ColumnReference) =>
+ typeof colDef ===
+ 'object' &&
+ 'type' in colDef &&
+ colDef.type ===
+ 'column_ref' &&
+ 'column' in colDef &&
+ colDef.column
+ )
+ .map(
+ (colDef: ColumnReference) =>
+ extractColumnName(
+ colDef
+ )
+ );
+
+ if (pkColumnNames.length > 0) {
+ indexes.push({
+ name: `pk_${tableName}`,
+ columns: pkColumnNames,
+ unique: true,
+ });
+ }
+ } else if (
+ constraintDef.definition &&
+ typeof constraintDef.definition ===
+ 'object' &&
+ !Array.isArray(
+ constraintDef.definition
+ ) &&
+ 'columns' in constraintDef.definition
+ ) {
+ // Handle different format where columns are in def.definition.columns
+ const colDefs =
+ constraintDef.definition.columns ||
+ [];
+ for (const colName of colDefs) {
+ // Find and mark the column as primary key
+ const column = columns.find(
+ (col) => col.name === colName
+ );
+ if (column) {
+ column.primaryKey = true;
+ }
+ }
+
+ // Add a primary key index
+ if (colDefs.length > 0) {
+ indexes.push({
+ name: `pk_${tableName}`,
+ columns: colDefs,
+ unique: true,
+ });
+ }
+ }
+ } else if (
+ constraintDef.constraint_type ===
+ 'unique' &&
+ constraintDef.definition &&
+ typeof constraintDef.definition ===
+ 'object' &&
+ !Array.isArray(constraintDef.definition) &&
+ 'columns' in constraintDef.definition
+ ) {
+ // Handle unique constraint
+ const columnDefs =
+ constraintDef.definition.columns || [];
+ columnDefs.forEach(
+ (
+ uniqueCol: string | ColumnReference
+ ) => {
+ const colName =
+ typeof uniqueCol === 'string'
+ ? uniqueCol
+ : extractColumnName(
+ uniqueCol
+ );
+ const col = columns.find(
+ (c) => c.name === colName
+ );
+ if (col) {
+ col.unique = true;
+ }
+ }
+ );
+
+ // Add as a unique index
+ if (columnDefs.length > 0) {
+ indexes.push({
+ name:
+ constraintDef.constraint_name ||
+ `${tableName}_${
+ typeof columnDefs[0] ===
+ 'string'
+ ? columnDefs[0]
+ : extractColumnName(
+ columnDefs[0] as ColumnReference
+ )
+ }_key`,
+ columns: columnDefs.map(
+ (
+ col:
+ | string
+ | ColumnReference
+ ) =>
+ typeof col === 'string'
+ ? col
+ : extractColumnName(col)
+ ),
+ unique: true,
+ });
+ }
+ } else if (
+ constraintDef.constraint_type ===
+ 'foreign key' ||
+ constraintDef.constraint_type ===
+ 'FOREIGN KEY'
+ ) {
+ // Handle foreign key directly at this level
+
+ // Extra code for this specific format
+ let sourceColumns: string[] = [];
+ if (
+ constraintDef.definition &&
+ Array.isArray(constraintDef.definition)
+ ) {
+ sourceColumns =
+ constraintDef.definition.map(
+ (col: ColumnReference) => {
+ const colName =
+ extractColumnName(col);
+ return colName;
+ }
+ );
+ } else if (
+ constraintDef.columns &&
+ Array.isArray(constraintDef.columns)
+ ) {
+ sourceColumns =
+ constraintDef.columns.map(
+ (
+ col:
+ | string
+ | ColumnReference
+ ) => {
+ const colName =
+ typeof col === 'string'
+ ? col
+ : extractColumnName(
+ col
+ );
+ return colName;
+ }
+ );
+ }
+
+ const reference =
+ constraintDef.reference_definition ||
+ constraintDef.reference;
+ if (reference && sourceColumns.length > 0) {
+ // Process similar to the constraint resource case
+ let targetTable = '';
+ let targetSchema = '';
+
+ if (reference.table) {
+ if (
+ typeof reference.table ===
+ 'object'
+ ) {
+ if (
+ Array.isArray(
+ reference.table
+ ) &&
+ reference.table.length > 0
+ ) {
+ targetTable =
+ reference.table[0]
+ .table || '';
+ targetSchema =
+ reference.table[0]
+ .schema ||
+ reference.table[0].db ||
+ '';
+ } else {
+ const tableRef =
+ reference.table as TableReference;
+ targetTable =
+ tableRef.table || '';
+ targetSchema =
+ tableRef.schema ||
+ tableRef.db ||
+ '';
+ }
+ } else {
+ targetTable =
+ reference.table as string;
+
+ // Check if targetTable contains a schema prefix (schema.table)
+ if (targetTable.includes('.')) {
+ const parts =
+ targetTable.split('.');
+ targetSchema =
+ parts[0].replace(
+ /"/g,
+ ''
+ );
+ targetTable =
+ parts[1].replace(
+ /"/g,
+ ''
+ );
+ }
+ }
+ }
+
+ // If no target schema was found, use default public schema
+ if (!targetSchema) {
+ targetSchema = 'public';
+ }
+
+ let targetColumns: string[] = [];
+ if (
+ reference.columns &&
+ Array.isArray(reference.columns)
+ ) {
+ targetColumns =
+ reference.columns.map(
+ (
+ col:
+ | string
+ | ColumnReference
+ ) => {
+ const colName =
+ typeof col ===
+ 'string'
+ ? col
+ : extractColumnName(
+ col
+ );
+ return colName;
+ }
+ );
+ } else if (
+ reference.definition &&
+ Array.isArray(reference.definition)
+ ) {
+ targetColumns =
+ reference.definition.map(
+ (col: ColumnReference) => {
+ const colName =
+ extractColumnName(
+ col
+ );
+ return colName;
+ }
+ );
+ }
+
+ // Create relationships
+ if (
+ targetColumns.length > 0 &&
+ targetTable
+ ) {
+ for (
+ let i = 0;
+ i <
+ Math.min(
+ sourceColumns.length,
+ targetColumns.length
+ );
+ i++
+ ) {
+ // Look up target table ID using the helper function
+ const targetTableId =
+ getTableIdWithSchemaSupport(
+ tableMap,
+ targetTable,
+ targetSchema
+ );
+
+ if (!targetTableId) {
+ continue; // Skip this relationship if target table not found
+ }
+
+ const fk: SQLForeignKey = {
+ name:
+ constraintDef.constraint_name ||
+ `${tableName}_${sourceColumns[i]}_fkey`,
+ sourceTable: tableName,
+ sourceSchema: schemaName,
+ sourceColumn:
+ sourceColumns[i],
+ targetTable,
+ targetSchema,
+ targetColumn:
+ targetColumns[i],
+ sourceTableId: tableId,
+ targetTableId,
+ updateAction:
+ reference.on_update,
+ deleteAction:
+ reference.on_delete,
+ };
+
+ relationships.push(fk);
+ }
+ }
+ }
+ }
+ }
+ }
+ );
+ }
+
+ // Create the table object
+ const table: SQLTable = {
+ id: tableId,
+ name: tableName,
+ schema: schemaName,
+ columns,
+ indexes,
+ order: tables.length,
+ };
+
+ // Set comment if available (if exists in the parser's output)
+ if (
+ 'comment' in createTableStmt &&
+ typeof createTableStmt.comment === 'string'
+ ) {
+ table.comment = createTableStmt.comment;
+ }
+
+ tables.push(table);
+ } else if (stmt.type === 'create' && stmt.keyword === 'index') {
+ // Handle CREATE INDEX statements
+ const createIndexStmt = stmt as CreateIndexStatement;
+ if (createIndexStmt.table) {
+ // Extract table name and schema
+ let tableName = '';
+ let schemaName = '';
+
+ if (typeof createIndexStmt.table === 'string') {
+ tableName = createIndexStmt.table;
+ } else if (Array.isArray(createIndexStmt.table)) {
+ if (createIndexStmt.table.length > 0) {
+ tableName = createIndexStmt.table[0].table || '';
+ schemaName = createIndexStmt.table[0].schema || '';
+ }
+ } else {
+ // Direct object reference
+ tableName = createIndexStmt.table.table || '';
+ schemaName = createIndexStmt.table.schema || '';
+ }
+
+ // Check if tableName contains a schema prefix (schema.table)
+ if (!schemaName && tableName.includes('.')) {
+ const parts = tableName.split('.');
+ schemaName = parts[0].replace(/"/g, '');
+ tableName = parts[1].replace(/"/g, '');
+ }
+
+ // If still no schema, use public
+ if (!schemaName) {
+ schemaName = 'public';
+ }
+
+ // Find the table in our collection using the helper function
+ const table = findTableWithSchemaSupport(
+ tables,
+ tableName,
+ schemaName
+ );
+
+ if (table) {
+ // Extract column names from index columns
+ let columns: string[] = [];
+
+ // Check different possible structures for index columns
+ if (
+ createIndexStmt.columns &&
+ Array.isArray(createIndexStmt.columns)
+ ) {
+ // Some PostgreSQL parsers use 'columns'
+ columns = createIndexStmt.columns
+ .map((col: ColumnReference) =>
+ extractColumnName(col)
+ )
+ .filter((col: string) => col !== '');
+ } else if (
+ createIndexStmt.index_columns &&
+ Array.isArray(createIndexStmt.index_columns)
+ ) {
+ // Other parsers use 'index_columns'
+ columns = createIndexStmt.index_columns
+ .map(
+ (
+ col:
+ | { column?: ColumnReference }
+ | ColumnReference
+ ) => {
+ const colRef =
+ 'column' in col ? col.column : col;
+ const colName = extractColumnName(
+ colRef || col
+ );
+ return colName;
+ }
+ )
+ .filter((col: string) => col !== '');
+ }
+
+ if (columns.length > 0) {
+ const indexName =
+ createIndexStmt.index ||
+ createIndexStmt.index_name ||
+ `idx_${tableName}_${columns.join('_')}`;
+
+ table.indexes.push({
+ name: indexName,
+ columns,
+ unique:
+ createIndexStmt.index_type === 'unique' ||
+ createIndexStmt.unique === true,
+ });
+ }
+ }
+ }
+ } else if (stmt.type === 'alter' && stmt.keyword === 'table') {
+ // Process ALTER TABLE statements for foreign keys
+ const alterTableStmt = stmt as AlterTableStatement;
+ if (
+ alterTableStmt.table &&
+ alterTableStmt.expr &&
+ alterTableStmt.expr.length > 0
+ ) {
+ // Fix the table name extraction - table is an array in ALTER TABLE statements
+ let tableName = '';
+ let schemaName = '';
+
+ if (
+ Array.isArray(alterTableStmt.table) &&
+ alterTableStmt.table.length > 0
+ ) {
+ const tableObj = alterTableStmt.table[0];
+ tableName = tableObj.table || '';
+ // Check for schema in both 'schema' and 'db' fields
+ schemaName = tableObj.schema || tableObj.db || '';
+ } else if (typeof alterTableStmt.table === 'object') {
+ const tableRef = alterTableStmt.table as TableReference;
+ tableName = tableRef.table || '';
+ // Check for schema in both 'schema' and 'db' fields
+ schemaName = tableRef.schema || tableRef.db || '';
+ } else {
+ tableName = alterTableStmt.table;
+ }
+
+ // Check if tableName contains a schema prefix (schema.table)
+ if (!schemaName && tableName.includes('.')) {
+ const parts = tableName.split('.');
+ schemaName = parts[0].replace(/"/g, '');
+ tableName = parts[1].replace(/"/g, '');
+ }
+
+ // If still no schema, use default
+ if (!schemaName) {
+ schemaName = 'public';
+ }
+
+ // Find this table in our collection using the helper function
+ const table = findTableWithSchemaSupport(
+ tables,
+ tableName,
+ schemaName
+ );
+
+ if (!table) {
+ return;
+ }
+
+ // Process each expression in the ALTER TABLE
+ alterTableStmt.expr.forEach((expr: AlterTableExprItem) => {
+ // Check multiple variations of constraint format
+ if (expr.action === 'add' && expr.create_definitions) {
+ // Check for foreign key constraint
+ if (
+ expr.create_definitions.constraint_type ===
+ 'FOREIGN KEY' ||
+ expr.create_definitions.constraint_type ===
+ 'foreign key'
+ ) {
+ const createDefs = expr.create_definitions;
+
+ // Extract source columns
+ let sourceColumns: string[] = [];
+ if (
+ createDefs.definition &&
+ Array.isArray(createDefs.definition)
+ ) {
+ sourceColumns = createDefs.definition.map(
+ (col: ColumnReference) => {
+ const colName =
+ extractColumnName(col);
+ return colName;
+ }
+ );
+ }
+
+ // Extract target table and schema
+ const reference =
+ createDefs.reference_definition;
+
+ // Declare target variables
+ let targetTable = '';
+ let targetSchema = '';
+ let targetColumns: string[] = [];
+
+ if (reference && reference.table) {
+ if (typeof reference.table === 'object') {
+ if (
+ Array.isArray(reference.table) &&
+ reference.table.length > 0
+ ) {
+ targetTable =
+ reference.table[0].table || '';
+ targetSchema =
+ reference.table[0].schema ||
+ reference.table[0].db ||
+ '';
+ } else {
+ const tableRef =
+ reference.table as TableReference;
+ targetTable = tableRef.table || '';
+ targetSchema =
+ tableRef.schema ||
+ tableRef.db ||
+ '';
+ }
+ } else {
+ targetTable = reference.table as string;
+
+ // Check if targetTable contains a schema prefix (schema.table)
+ if (targetTable.includes('.')) {
+ const parts =
+ targetTable.split('.');
+ targetSchema = parts[0].replace(
+ /"/g,
+ ''
+ );
+ targetTable = parts[1].replace(
+ /"/g,
+ ''
+ );
+ }
+ }
+ }
+
+ // If no target schema was found, use default schema
+ if (!targetSchema) {
+ targetSchema = 'public';
+ }
+
+ // Extract target columns
+ if (
+ reference &&
+ reference.definition &&
+ Array.isArray(reference.definition)
+ ) {
+ targetColumns = reference.definition.map(
+ (col: ColumnReference) => {
+ const colName =
+ extractColumnName(col);
+ return colName;
+ }
+ );
+ }
+
+ // Create relationships
+ if (
+ sourceColumns.length > 0 &&
+ targetTable &&
+ targetColumns.length > 0
+ ) {
+ for (
+ let i = 0;
+ i <
+ Math.min(
+ sourceColumns.length,
+ targetColumns.length
+ );
+ i++
+ ) {
+ // Look up source and target table IDs
+ const sourceTableId =
+ getTableIdWithSchemaSupport(
+ tableMap,
+ tableName,
+ schemaName
+ );
+ const targetTableId =
+ getTableIdWithSchemaSupport(
+ tableMap,
+ targetTable,
+ targetSchema
+ );
+
+ if (!sourceTableId) {
+ continue;
+ }
+
+ if (!targetTableId) {
+ continue;
+ }
+
+ // Safe to access properties with null check
+ const updateAction =
+ reference?.on_update;
+ const deleteAction =
+ reference?.on_delete;
+
+ const fk: SQLForeignKey = {
+ name:
+ 'constraint' in createDefs
+ ? createDefs.constraint ||
+ `${tableName}_${sourceColumns[i]}_fkey`
+ : `${tableName}_${sourceColumns[i]}_fkey`,
+ sourceTable: tableName,
+ sourceSchema: schemaName,
+ sourceColumn: sourceColumns[i],
+ targetTable,
+ targetSchema,
+ targetColumn: targetColumns[i],
+ sourceTableId,
+ targetTableId,
+ updateAction,
+ deleteAction,
+ };
+
+ relationships.push(fk);
+ }
+ }
+ } else if (
+ 'resource' in expr.create_definitions &&
+ expr.create_definitions.resource ===
+ 'constraint'
+ ) {
+ // For backward compatibility, keep the existing check
+ }
+ }
+ });
+ }
+ }
+ });
+
+ // Update table IDs in relationships and fix missing target table IDs
+ relationships.forEach((rel) => {
+ // Ensure schemas are set to 'public' if empty
+ if (!rel.sourceSchema) rel.sourceSchema = 'public';
+ if (!rel.targetSchema) rel.targetSchema = 'public';
+
+ // Only check/fix sourceTableId if not already set
+ if (!rel.sourceTableId) {
+ rel.sourceTableId =
+ getTableIdWithSchemaSupport(
+ tableMap,
+ rel.sourceTable,
+ rel.sourceSchema
+ ) || '';
+ }
+
+ // Check/fix targetTableId if not already set
+ if (!rel.targetTableId) {
+ rel.targetTableId =
+ getTableIdWithSchemaSupport(
+ tableMap,
+ rel.targetTable,
+ rel.targetSchema
+ ) || '';
+ }
+ });
+
+ // Filter out relationships with missing source table IDs or target table IDs
+ const validRelationships = relationships.filter(
+ (rel) => rel.sourceTableId && rel.targetTableId
+ );
+
+ return { tables, relationships: validRelationships };
+ } catch (error: unknown) {
+ throw new Error(
+ `Error parsing PostgreSQL SQL: ${(error as Error).message}`
+ );
+ }
+}
diff --git a/src/lib/data/sql-import/dialect-importers/sqlite/sqlite-common.ts b/src/lib/data/sql-import/dialect-importers/sqlite/sqlite-common.ts
new file mode 100644
index 00000000..e1889508
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/sqlite/sqlite-common.ts
@@ -0,0 +1,241 @@
+import { Parser } from 'node-sql-parser';
+import type { SQLASTNode } from '../../common';
+
+// Set up the SQL parser with SQLite dialect
+export const parser = new Parser();
+export const parserOpts = {
+ database: 'sqlite',
+};
+
+// Type definitions for SQLite AST nodes
+export interface TableReference {
+ db?: string;
+ schema?: string;
+ table: string;
+}
+
+export interface ColumnReference {
+ expr?: {
+ column?: string;
+ table?: string;
+ };
+ column?: string;
+ table?: string;
+}
+
+export interface ColumnDefinition {
+ column: ColumnReference;
+ dataType: {
+ dataType: string;
+ length?: number | number[];
+ suffix?: string[];
+ };
+ primary_key?: boolean;
+ autoIncrement?: boolean;
+ notNull?: boolean;
+ unique?: boolean;
+ default_val?: {
+ value: string | number;
+ type: string;
+ };
+ reference?: {
+ table: TableReference;
+ columns: ColumnReference[];
+ };
+}
+
+export interface ConstraintDefinition {
+ constraint_type: string;
+ columns?: ColumnReference[];
+ definition?: {
+ columns?: ColumnReference[];
+ table?: TableReference;
+ reference_columns?: ColumnReference[];
+ reference?: {
+ table: TableReference;
+ columns: ColumnReference[];
+ };
+ };
+ reference?: {
+ table: TableReference;
+ columns: ColumnReference[];
+ };
+ constraint_name?: string;
+}
+
+export interface CreateTableStatement extends SQLASTNode {
+ type: 'create';
+ keyword: 'table';
+ temporary?: boolean;
+ table: TableReference | TableReference[];
+ create_definitions?: (ColumnDefinition | ConstraintDefinition)[];
+ as?: unknown;
+ if_not_exists?: boolean;
+}
+
+export interface CreateIndexStatement extends SQLASTNode {
+ type: 'create';
+ keyword: 'index';
+ index: {
+ name: string;
+ type?: string;
+ };
+ table: TableReference | TableReference[];
+ columns?: ColumnReference[];
+ index_type?: string;
+ unique?: boolean;
+ concurrently?: boolean;
+ if_not_exists?: boolean;
+}
+
+export interface AlterTableExprItem {
+ action: string;
+ name?: ColumnReference;
+ dataType?: {
+ dataType: string;
+ length?: number | number[];
+ };
+ expr?: {
+ constraint_type?: string;
+ columns?: ColumnReference[];
+ reference?: {
+ table: TableReference;
+ columns: ColumnReference[];
+ };
+ };
+}
+
+export interface AlterTableStatement extends SQLASTNode {
+ type: 'alter';
+ table: TableReference;
+ expr?: AlterTableExprItem[];
+}
+
+/**
+ * Helper function to extract column name from column reference
+ */
+export function extractColumnName(column: ColumnReference): string {
+ if (typeof column === 'string') {
+ return column;
+ }
+
+ if (column.column) {
+ return column.column;
+ }
+
+ if (column.expr && column.expr.column) {
+ return column.expr.column;
+ }
+
+ return '';
+}
+
+/**
+ * Helper function to extract type arguments (e.g., size, precision, scale)
+ */
+export function getTypeArgs(dataType?: {
+ dataType: string;
+ length?: number | number[];
+}): {
+ size: number;
+ precision?: number | undefined;
+ scale?: number | undefined;
+} {
+ const result = {
+ size: 0,
+ precision: undefined as number | undefined,
+ scale: undefined as number | undefined,
+ };
+
+ if (!dataType || !dataType.length) {
+ return result;
+ }
+
+ if (typeof dataType.length === 'number') {
+ result.size = dataType.length;
+ } else if (Array.isArray(dataType.length)) {
+ if (dataType.length.length >= 1) {
+ result.size = dataType.length[0] as number;
+ }
+ if (dataType.length.length >= 2) {
+ result.precision = dataType.length[0] as number;
+ result.scale = dataType.length[1] as number;
+ }
+ }
+
+ return result;
+}
+
+/**
+ * Find a table in the collection that matches the table name and schema name
+ */
+export function findTableWithSchemaSupport(
+ tables: { id: string; name: string; schema?: string }[],
+ tableName: string,
+ schemaName?: string
+): { id: string; name: string; schema?: string } | undefined {
+ return tables.find(
+ (t) =>
+ t.name === tableName &&
+ (!schemaName || t.schema === schemaName || !t.schema)
+ );
+}
+
+/**
+ * Get a table ID with schema support
+ */
+export function getTableIdWithSchemaSupport(
+ tableName: string,
+ schemaName?: string
+): string {
+ return schemaName ? `${schemaName}.${tableName}` : tableName;
+}
+
+/**
+ * Validates a foreign key relationship to ensure it refers to valid tables and columns
+ */
+export function isValidForeignKeyRelationship(
+ relationship: {
+ sourceTable: string;
+ sourceColumn: string;
+ targetTable: string;
+ targetColumn: string;
+ },
+ tables: { id: string; name: string; schema?: string }[]
+): boolean {
+ // Check for empty values
+ if (
+ !relationship.sourceTable ||
+ !relationship.sourceColumn ||
+ !relationship.targetTable ||
+ !relationship.targetColumn
+ ) {
+ return false;
+ }
+
+ // Check for SQL keywords that might have been mistakenly captured
+ const invalidKeywords = [
+ 'CREATE',
+ 'TABLE',
+ 'FOREIGN',
+ 'KEY',
+ 'REFERENCES',
+ 'PRIMARY',
+ ];
+ if (
+ invalidKeywords.includes(relationship.sourceColumn.toUpperCase()) ||
+ invalidKeywords.includes(relationship.targetColumn.toUpperCase())
+ ) {
+ return false;
+ }
+
+ // Source table must exist in our schema
+ const sourceTableExists = tables.some(
+ (t) => t.name === relationship.sourceTable
+ );
+ if (!sourceTableExists) {
+ return false;
+ }
+
+ return true;
+}
diff --git a/src/lib/data/sql-import/dialect-importers/sqlite/sqlite.ts b/src/lib/data/sql-import/dialect-importers/sqlite/sqlite.ts
new file mode 100644
index 00000000..3decb301
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/sqlite/sqlite.ts
@@ -0,0 +1,583 @@
+import type {
+ SQLParserResult,
+ SQLTable,
+ SQLColumn,
+ SQLIndex,
+ SQLForeignKey,
+ SQLASTNode,
+} from '../../common';
+import type {
+ TableReference,
+ ColumnReference,
+ ColumnDefinition,
+ ConstraintDefinition,
+ CreateTableStatement,
+ CreateIndexStatement,
+ AlterTableStatement,
+} from './sqlite-common';
+import {
+ parser as sqlParser,
+ parserOpts,
+ extractColumnName,
+ getTypeArgs,
+ getTableIdWithSchemaSupport,
+ isValidForeignKeyRelationship,
+} from './sqlite-common';
+
+/**
+ * SQLite-specific parsing logic
+ */
+export function fromSQLite(sqlContent: string): SQLParserResult {
+ const tables: SQLTable[] = [];
+ const relationships: SQLForeignKey[] = [];
+ const tableMap: Record<string, string> = {}; // Maps table name to its ID
+
+ try {
+ // Parse the SQL DDL statements
+
+ const ast = sqlParser.astify(
+ sqlContent,
+ parserOpts
+ ) as unknown as SQLASTNode[];
+
+ if (!Array.isArray(ast)) {
+ throw new Error('Failed to parse SQL DDL - AST is not an array');
+ }
+
+ // Process each statement
+ ast.forEach((stmt: SQLASTNode) => {
+ // Process CREATE TABLE statements
+ if (stmt.type === 'create' && stmt.keyword === 'table') {
+ processCreateTableStatement(
+ stmt as CreateTableStatement,
+ tables,
+ relationships,
+ tableMap
+ );
+ }
+ // Process CREATE INDEX statements
+ else if (stmt.type === 'create' && stmt.keyword === 'index') {
+ processCreateIndexStatement(
+ stmt as CreateIndexStatement,
+ tables
+ );
+ }
+ // Process ALTER TABLE statements
+ else if (stmt.type === 'alter' && stmt.table) {
+ processAlterTableStatement(stmt as AlterTableStatement, tables);
+ }
+ });
+
+ // Use regex to find foreign keys that the parser might have missed
+ findForeignKeysUsingRegex(sqlContent, tableMap, relationships);
+
+ // Create placeholder tables for any missing referenced tables
+ addPlaceholderTablesForFKReferences(tables, relationships, tableMap);
+
+ // Filter out any invalid relationships
+ const validRelationships = relationships.filter((rel) => {
+ return isValidForeignKeyRelationship(rel, tables);
+ });
+
+ return { tables, relationships: validRelationships };
+ } catch (error) {
+ console.error('Error parsing SQLite SQL:', error);
+ throw error;
+ }
+}
+
+/**
+ * Process a CREATE TABLE statement to extract table and column information
+ */
+function processCreateTableStatement(
+ createTableStmt: CreateTableStatement,
+ tables: SQLTable[],
+ _: SQLForeignKey[],
+ tableMap: Record<string, string>
+): void {
+ // Extract table name and schema
+ let tableName = '';
+ let schemaName = '';
+
+ if (createTableStmt.table && typeof createTableStmt.table === 'object') {
+ // Handle array of tables if needed
+ if (
+ Array.isArray(createTableStmt.table) &&
+ createTableStmt.table.length > 0
+ ) {
+ const tableObj = createTableStmt.table[0];
+ tableName = tableObj.table || '';
+ schemaName = tableObj.schema || '';
+ } else {
+ // Direct object reference
+ const tableObj = createTableStmt.table as TableReference;
+ tableName = tableObj.table || '';
+ schemaName = tableObj.schema || '';
+ }
+ }
+
+ // Skip if table name is empty
+ if (!tableName) {
+ return;
+ }
+
+ // Generate a unique ID for the table
+ const tableId = getTableIdWithSchemaSupport(tableName, schemaName);
+
+ // Store the table ID in the map for later reference
+ tableMap[`${schemaName ? schemaName + '.' : ''}${tableName}`] = tableId;
+ tableMap[tableName] = tableId; // Also store without schema for easier lookup
+
+ // Initialize column and index arrays
+ const columns: SQLColumn[] = [];
+ const indexes: SQLIndex[] = [];
+ let primaryKeyColumns: string[] = [];
+
+ // Process column definitions and constraints
+ if (
+ createTableStmt.create_definitions &&
+ Array.isArray(createTableStmt.create_definitions)
+ ) {
+ createTableStmt.create_definitions.forEach((def) => {
+ if ('column' in def) {
+ // Process column definition
+ const columnDef = def as ColumnDefinition;
+ const columnName = extractColumnName(columnDef.column);
+
+ if (!columnName) {
+ return;
+ }
+
+ // Extract type information - handle nullable dataType field
+ let typeName = 'text'; // Default to text if no type specified
+ const typeArgs = {
+ length: undefined as number | undefined,
+ precision: undefined as number | undefined,
+ scale: undefined as number | undefined,
+ };
+
+ if (columnDef.dataType) {
+ typeName = columnDef.dataType.dataType || 'text';
+ const args = getTypeArgs(columnDef.dataType);
+ typeArgs.length = args.size > 0 ? args.size : undefined;
+ typeArgs.precision = args.precision;
+ typeArgs.scale = args.scale;
+ }
+
+ // Check if this column is part of the primary key
+ const isPrimaryKey = !!columnDef.primary_key;
+ if (isPrimaryKey) {
+ primaryKeyColumns.push(columnName);
+ }
+
+ // Process default value if present
+ let defaultValue = '';
+ if (columnDef.default_val) {
+ defaultValue = String(columnDef.default_val.value);
+ }
+
+ // Add the column to our collection
+ columns.push({
+ name: columnName,
+ type: typeName,
+ nullable: !columnDef.notNull,
+ primaryKey: isPrimaryKey,
+ unique: !!columnDef.unique,
+ default: defaultValue,
+ increment:
+ isPrimaryKey && typeName.toLowerCase() === 'integer',
+ typeArgs:
+ typeArgs.length || typeArgs.precision || typeArgs.scale
+ ? typeArgs
+ : undefined,
+ });
+ } else if ('constraint_type' in def) {
+ // Process constraint definition
+ const constraintDef = def as ConstraintDefinition;
+
+ // Process PRIMARY KEY constraint
+ if (
+ constraintDef.constraint_type === 'primary key' &&
+ constraintDef.columns
+ ) {
+ primaryKeyColumns = constraintDef.columns
+ .map(extractColumnName)
+ .filter(Boolean);
+ }
+
+ // Process UNIQUE constraint
+ if (
+ constraintDef.constraint_type === 'unique' &&
+ constraintDef.columns
+ ) {
+ const uniqueColumns = constraintDef.columns
+ .map(extractColumnName)
+ .filter(Boolean);
+
+ // Create a unique index for this constraint
+ if (uniqueColumns.length > 0) {
+ const uniqueIndexName =
+ constraintDef.constraint_name ||
+ `uk_${tableName}_${uniqueColumns.join('_')}`;
+ indexes.push({
+ name: uniqueIndexName,
+ columns: uniqueColumns,
+ unique: true,
+ });
+ }
+ }
+ }
+ });
+ }
+
+ // Update primary key flags in columns
+ if (primaryKeyColumns.length > 0) {
+ columns.forEach((column) => {
+ if (primaryKeyColumns.includes(column.name)) {
+ column.primaryKey = true;
+
+ // In SQLite, INTEGER PRIMARY KEY is automatically an alias for ROWID (auto-incrementing)
+ if (column.type.toLowerCase() === 'integer') {
+ column.increment = true;
+ }
+ }
+ });
+ }
+
+ // Create the table object
+ tables.push({
+ id: tableId,
+ name: tableName,
+ schema: schemaName || undefined,
+ columns,
+ indexes,
+ order: tables.length,
+ });
+}
+
+/**
+ * Process a CREATE INDEX statement to extract index information
+ */
+function processCreateIndexStatement(
+ createIndexStmt: CreateIndexStatement,
+ tables: SQLTable[]
+): void {
+ if (!createIndexStmt.index || !createIndexStmt.table) {
+ return;
+ }
+
+ // Extract table and index information
+ const indexName = createIndexStmt.index.name;
+ let tableName = '';
+ let schemaName = '';
+
+ if (typeof createIndexStmt.table === 'object') {
+ if (
+ Array.isArray(createIndexStmt.table) &&
+ createIndexStmt.table.length > 0
+ ) {
+ tableName = createIndexStmt.table[0].table || '';
+ schemaName = createIndexStmt.table[0].schema || '';
+ } else {
+ const tableRef = createIndexStmt.table as TableReference;
+ tableName = tableRef.table || '';
+ schemaName = tableRef.schema || '';
+ }
+ }
+
+ // Find the table in our collection
+ const table = tables.find(
+ (t) => t.name === tableName && (!schemaName || t.schema === schemaName)
+ );
+
+ if (table) {
+ // Extract column names from index columns
+ let columns: string[] = [];
+
+ if (createIndexStmt.columns && Array.isArray(createIndexStmt.columns)) {
+ columns = createIndexStmt.columns
+ .map((col: ColumnReference) => extractColumnName(col))
+ .filter((col: string) => col !== '');
+ }
+
+ if (columns.length > 0) {
+ // Create the index
+ table.indexes.push({
+ name: indexName,
+ columns: columns,
+ unique: !!createIndexStmt.unique,
+ });
+ }
+ }
+}
+
+/**
+ * Process an ALTER TABLE statement to extract changes to table structure
+ */
+function processAlterTableStatement(
+ alterTableStmt: AlterTableStatement,
+ tables: SQLTable[]
+): void {
+ if (!alterTableStmt.table || !alterTableStmt.expr) {
+ return;
+ }
+
+ const tableName = alterTableStmt.table.table;
+ const schemaName = alterTableStmt.table.schema || '';
+
+ // Find the target table
+ const table = tables.find(
+ (t) => t.name === tableName && (!schemaName || t.schema === schemaName)
+ );
+
+ if (!table) {
+ return;
+ }
+
+ // Note: We're relying on the regex approach to find foreign keys from ALTER TABLE statements
+}
+
+/**
+ * Uses regular expressions to find foreign key relationships in the SQL content
+ */
+function findForeignKeysUsingRegex(
+ sqlContent: string,
+ tableMap: Record<string, string>,
+ relationships: SQLForeignKey[]
+): void {
+ // Define patterns to find foreign keys
+ const foreignKeyPatterns = [
+ // Pattern for inline column references - REFERENCES table_name(column_name)
+ /\b(\w+)\b\s+\w+(?:\([^)]*\))?\s+(?:NOT\s+NULL\s+)?(?:REFERENCES)\s+["'`]?(\w+)["'`]?\s*\(\s*["'`]?(\w+)["'`]?\s*\)/gi,
+
+ // Pattern: FOREIGN KEY (column_name) REFERENCES table_name(column_name)
+ /FOREIGN\s+KEY\s*\(\s*["'`]?(\w+)["'`]?\s*\)\s+REFERENCES\s+["'`]?(\w+)["'`]?\s*\(\s*["'`]?(\w+)["'`]?\s*\)/gi,
+
+ // Pattern for quoted column names with optional ON DELETE/UPDATE clauses
+ /["'`](\w+)["'`]\s+\w+(?:\([^)]*\))?\s+(?:NOT\s+NULL\s+)?REFERENCES\s+["'`]?(\w+)["'`]?\s*\(\s*["'`]?(\w+)["'`]?\s*\)(?:\s+ON\s+(?:DELETE|UPDATE)\s+[^,)]+)?/gi,
+ ];
+
+ // First pass: identify all tables
+ const tableNamePattern =
+ /CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?["'`]?(\w+)["'`]?/gi;
+ let match;
+
+ tableNamePattern.lastIndex = 0;
+ while ((match = tableNamePattern.exec(sqlContent)) !== null) {
+ const tableName = match[1];
+
+ // Skip invalid table names
+ if (!tableName || tableName === 'CREATE') continue;
+
+ // Ensure the table is in our tableMap
+ if (!tableMap[tableName]) {
+ const tableId = getTableIdWithSchemaSupport(tableName);
+ tableMap[tableName] = tableId;
+ }
+ }
+
+ // Track already added relationships to avoid duplicates
+ const addedRelationships = new Set<string>();
+
+ // Second pass: find foreign keys using regex
+ for (const pattern of foreignKeyPatterns) {
+ pattern.lastIndex = 0;
+ while ((match = pattern.exec(sqlContent)) !== null) {
+ const sourceColumn = match[1];
+ const targetTable = match[2];
+ const targetColumn = match[3];
+
+ // Skip if any required component is missing
+ if (!sourceColumn || !targetTable || !targetColumn) continue;
+
+ // Skip invalid column names that might be SQL keywords
+ if (
+ sourceColumn.toUpperCase() === 'CREATE' ||
+ sourceColumn.toUpperCase() === 'FOREIGN' ||
+ sourceColumn.toUpperCase() === 'KEY'
+ )
+ continue;
+
+ // Find the source table by examining the CREATE TABLE statement
+ const tableSection = sqlContent.substring(0, match.index);
+ const lastCreateTablePos = tableSection.lastIndexOf('CREATE TABLE');
+
+ if (lastCreateTablePos === -1) continue; // Skip if not in a CREATE TABLE
+
+ const tableSubstring = tableSection.substring(lastCreateTablePos);
+ const tableMatch =
+ /CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?["'`]?(\w+)["'`]?/i.exec(
+ tableSubstring
+ );
+
+ if (!tableMatch || !tableMatch[1]) continue; // Skip if we can't determine the table
+
+ const sourceTable = tableMatch[1];
+
+ // Create a unique key to track this relationship
+ const relationshipKey = `${sourceTable}.${sourceColumn}-${targetTable}.${targetColumn}`;
+
+ // Skip if we've already added this relationship
+ if (addedRelationships.has(relationshipKey)) continue;
+ addedRelationships.add(relationshipKey);
+
+ // Get table IDs
+ const sourceTableId =
+ tableMap[sourceTable] ||
+ getTableIdWithSchemaSupport(sourceTable);
+ const targetTableId =
+ tableMap[targetTable] ||
+ getTableIdWithSchemaSupport(targetTable);
+
+ // Add the relationship
+ relationships.push({
+ name: `FK_${sourceTable}_${sourceColumn}_${targetTable}`,
+ sourceTable,
+ sourceSchema: '',
+ sourceColumn,
+ targetTable,
+ targetSchema: '',
+ targetColumn,
+ sourceTableId,
+ targetTableId,
+ });
+ }
+ }
+
+ // Look for additional foreign keys using a more specific pattern for multi-line declarations
+ const multiLineFkPattern =
+ /CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?["'`]?(\w+)["'`]?[^;]+?FOREIGN\s+KEY\s*\(\s*["'`]?(\w+)["'`]?\s*\)\s+REFERENCES\s+["'`]?(\w+)["'`]?\s*\(\s*["'`]?(\w+)["'`]?\s*\)/gi;
+
+ multiLineFkPattern.lastIndex = 0;
+ while ((match = multiLineFkPattern.exec(sqlContent)) !== null) {
+ const sourceTable = match[1];
+ const sourceColumn = match[2];
+ const targetTable = match[3];
+ const targetColumn = match[4];
+
+ // Skip if any part is invalid
+ if (!sourceTable || !sourceColumn || !targetTable || !targetColumn)
+ continue;
+
+ // Create a unique key to track this relationship
+ const relationshipKey = `${sourceTable}.${sourceColumn}-${targetTable}.${targetColumn}`;
+
+ // Skip if we've already added this relationship
+ if (addedRelationships.has(relationshipKey)) continue;
+ addedRelationships.add(relationshipKey);
+
+ // Get table IDs
+ const sourceTableId =
+ tableMap[sourceTable] || getTableIdWithSchemaSupport(sourceTable);
+ const targetTableId =
+ tableMap[targetTable] || getTableIdWithSchemaSupport(targetTable);
+
+ // Add the relationship
+ relationships.push({
+ name: `FK_${sourceTable}_${sourceColumn}_${targetTable}`,
+ sourceTable,
+ sourceSchema: '',
+ sourceColumn,
+ targetTable,
+ targetSchema: '',
+ targetColumn,
+ sourceTableId,
+ targetTableId,
+ });
+ }
+
+ // Filter out relationships to non-existent tables
+ const validRelationships = relationships.filter((rel) => {
+ // Ensure source table exists
+ if (!tableMap[rel.sourceTable]) {
+ return false;
+ }
+
+ // Don't filter out if the column name is suspicious
+ if (
+ rel.sourceColumn.toUpperCase() === 'CREATE' ||
+ rel.sourceColumn.toUpperCase() === 'FOREIGN' ||
+ rel.sourceColumn.toUpperCase() === 'KEY'
+ ) {
+ return false;
+ }
+
+ return true;
+ });
+
+ // Replace the relationships array with the filtered list
+ relationships.length = 0;
+ validRelationships.forEach((rel) => relationships.push(rel));
+}
+
+/**
+ * Adds placeholder tables for tables referenced in foreign keys that don't exist in the schema
+ */
+function addPlaceholderTablesForFKReferences(
+ tables: SQLTable[],
+ relationships: SQLForeignKey[],
+ tableMap: Record<string, string>
+): void {
+ // Get all existing table names
+ const existingTableNames = new Set(tables.map((t) => t.name));
+
+ // Find all target tables mentioned in relationships that don't exist
+ const missingTableNames = new Set<string>();
+
+ relationships.forEach((rel) => {
+ if (rel.targetTable && !existingTableNames.has(rel.targetTable)) {
+ missingTableNames.add(rel.targetTable);
+ }
+ });
+
+ // Add placeholder tables for missing tables
+ missingTableNames.forEach((tableName) => {
+ // Generate a table ID
+ const tableId = getTableIdWithSchemaSupport(tableName);
+
+ // Add to table map
+ tableMap[tableName] = tableId;
+
+ // Create minimal placeholder table with the target column as PK
+ const targetColumns = new Set<string>();
+
+ // Collect all referenced columns for this table
+ relationships.forEach((rel) => {
+ if (rel.targetTable === tableName) {
+ targetColumns.add(rel.targetColumn);
+ }
+ });
+
+ // Create columns for the placeholder table
+ const columns: SQLColumn[] = Array.from(targetColumns).map(
+ (colName) => ({
+ name: colName,
+ type: 'unknown',
+ primaryKey: true, // Assume it's a primary key since it's referenced
+ unique: true,
+ nullable: false,
+ })
+ );
+
+ // Add a generic ID column if no columns were found
+ if (columns.length === 0) {
+ columns.push({
+ name: 'id',
+ type: 'unknown',
+ primaryKey: true,
+ unique: true,
+ nullable: false,
+ });
+ }
+
+ // Add the placeholder table
+ tables.push({
+ id: getTableIdWithSchemaSupport(tableName),
+ name: tableName,
+ columns,
+ indexes: [],
+ order: tables.length,
+ // This is a placeholder table for a missing referenced table
+ });
+ });
+}
diff --git a/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver-common.ts b/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver-common.ts
new file mode 100644
index 00000000..69b96c92
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver-common.ts
@@ -0,0 +1,190 @@
+import { Parser } from 'node-sql-parser';
+import { generateId } from '@/lib/utils';
+import type { SQLASTNode } from '../../common';
+
+// Set up the SQL parser with SQL Server dialect
+export const parser = new Parser();
+export const parserOpts = {
+ database: 'transactsql',
+};
+
+// Type definitions for SQL Server AST
+export interface TableReference {
+ db?: string;
+ schema?: string;
+ table: string;
+ as?: string;
+}
+
+export interface ColumnReference {
+ type: 'column_ref';
+ table?: string;
+ column: string;
+}
+
+export interface CreateTableStatement extends SQLASTNode {
+ type: 'create';
+ keyword: 'table';
+ table: TableReference | TableReference[];
+ create_definitions: (ColumnDefinition | ConstraintDefinition)[];
+ table_options?: Record<string, unknown>[];
+ if_not_exists?: boolean;
+}
+
+export interface CreateIndexStatement extends SQLASTNode {
+ type: 'create';
+ keyword: 'index';
+ index: string;
+ table: TableReference | TableReference[];
+ columns: ColumnReference[];
+ constraint?: string;
+ index_using?: string;
+ index_options?: Record<string, unknown>[];
+}
+
+export interface AlterTableStatement extends SQLASTNode {
+ type: 'alter';
+ keyword: 'table';
+ table: TableReference | TableReference[];
+ expr: AlterTableExprItem[];
+}
+
+export interface AlterTableExprItem {
+ action: string;
+ column?: ColumnReference | string;
+ definition?: Record<string, unknown>;
+ resource?: string;
+ [key: string]: unknown;
+}
+
+export interface ColumnDefinition {
+ column: ColumnReference | string;
+ definition?: {
+ dataType: string;
+ length?: number | string;
+ width?: number | string;
+ scale?: number;
+ precision?: number;
+ parentheses?: boolean;
+ suffix?: string[];
+ constraint?: string;
+ [key: string]: unknown;
+ };
+ nullable?: { type: string };
+ primary_key?: string;
+ unique?: string;
+ default_val?: unknown;
+ auto_increment?: string;
+ comment?: string;
+ reference?: Record<string, unknown>;
+ resource: string;
+ [key: string]: unknown;
+}
+
+export interface ConstraintDefinition {
+ constraint_type: string;
+ constraint?: string;
+ definition?: Array<unknown> | Record<string, unknown>;
+ resource: string;
+ reference?: {
+ table: TableReference;
+ columns: ColumnReference[];
+ on_delete?: string;
+ on_update?: string;
+ };
+ [key: string]: unknown;
+}
+
+/**
+ * Extract column name from a column reference
+ */
+export function extractColumnName(columnRef: ColumnReference | string): string {
+ if (typeof columnRef === 'string') {
+ return columnRef;
+ }
+
+ if (columnRef.type === 'column_ref') {
+ return columnRef.column;
+ }
+
+ return '';
+}
+
+/**
+ * Extract type arguments such as length, precision, scale
+ */
+export function getTypeArgs(
+ definition?: ColumnDefinition['definition']
+): { length?: number; precision?: number; scale?: number } | undefined {
+ if (!definition) return undefined;
+
+ const result: { length?: number; precision?: number; scale?: number } = {};
+
+ // Check if length/width is present
+ if (definition.length !== undefined) {
+ result.length = Number(definition.length);
+ } else if (definition.width !== undefined) {
+ result.length = Number(definition.width);
+ }
+
+ // Check if precision is present
+ if (definition.precision !== undefined) {
+ result.precision = Number(definition.precision);
+ }
+
+ // Check if scale is present
+ if (definition.scale !== undefined) {
+ result.scale = Number(definition.scale);
+ }
+
+ return Object.keys(result).length > 0 ? result : undefined;
+}
+
+/**
+ * Find a table in the tables array with schema support
+ */
+export function findTableWithSchemaSupport(
+ tables: Array<{ id: string; name: string; schema?: string }>,
+ tableName: string,
+ schemaName?: string
+): { id: string; name: string; schema?: string } | undefined {
+ // If schema is provided, search for exact match
+ if (schemaName) {
+ return tables.find(
+ (t) => t.name === tableName && t.schema === schemaName
+ );
+ }
+
+ // No schema provided, first try to find exact match without schema
+ const exactMatch = tables.find(
+ (t) => t.name === tableName && (!t.schema || t.schema === 'dbo')
+ );
+ if (exactMatch) return exactMatch;
+
+ // Finally, look for any table with matching name regardless of schema
+ return tables.find((t) => t.name === tableName);
+}
+
+/**
+ * Get the ID of a table with schema support, or generate a new ID if not found
+ */
+export function getTableIdWithSchemaSupport(
+ tables: Array<{ id: string; name: string; schema?: string }>,
+ tableMap: Record<string, string>,
+ tableName: string,
+ schemaName?: string
+): string {
+ const table = findTableWithSchemaSupport(tables, tableName, schemaName);
+ if (table) return table.id;
+
+ // If not found, check if we have an entry in tableMap
+ const tableKey = `${schemaName ? schemaName + '.' : ''}${tableName}`;
+ if (tableMap[tableKey]) {
+ return tableMap[tableKey];
+ }
+
+ // Generate a new ID
+ const newId = generateId();
+ tableMap[tableKey] = newId;
+ return newId;
+}
diff --git a/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver.ts b/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver.ts
new file mode 100644
index 00000000..10ef779c
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver.ts
@@ -0,0 +1,860 @@
+import { generateId } from '@/lib/utils';
+import type {
+ SQLParserResult,
+ SQLTable,
+ SQLColumn,
+ SQLIndex,
+ SQLForeignKey,
+ SQLASTNode,
+} from '../../common';
+import { buildSQLFromAST } from '../../common';
+import { DatabaseType } from '@/lib/domain/database-type';
+import type {
+ TableReference,
+ ColumnReference,
+ ColumnDefinition,
+ ConstraintDefinition,
+ CreateTableStatement,
+ CreateIndexStatement,
+ AlterTableStatement,
+} from './sqlserver-common';
+import {
+ parser,
+ parserOpts,
+ extractColumnName,
+ getTypeArgs,
+ findTableWithSchemaSupport,
+} from './sqlserver-common';
+
+/**
+ * Helper function to safely build SQL from AST nodes, handling null/undefined/invalid cases
+ */
+function safelyBuildSQLFromAST(ast: unknown): string | undefined {
+ if (!ast) return undefined;
+
+ // Make sure it's a valid AST node with a 'type' property
+ if (typeof ast === 'object' && ast !== null && 'type' in ast) {
+ return buildSQLFromAST(ast as SQLASTNode, DatabaseType.SQL_SERVER);
+ }
+
+ // Return string representation for non-AST objects
+ if (ast !== null && (typeof ast === 'string' || typeof ast === 'number')) {
+ return String(ast);
+ }
+
+ return undefined;
+}
+
+/**
+ * Preprocess SQL Server script to remove or modify parts that the parser can't handle
+ */
+function preprocessSQLServerScript(sqlContent: string): string {
+ // 1. Remove IF NOT EXISTS ... BEGIN ... END blocks (typically used for schema creation)
+ sqlContent = sqlContent.replace(
+ /IF\s+NOT\s+EXISTS\s*\([^)]+\)\s*BEGIN\s+[^;]+;\s*END;?/gi,
+ ''
+ );
+
+ // 2. Remove any GO statements (batch separators)
+ sqlContent = sqlContent.replace(/\bGO\b/gi, ';');
+
+ // 3. Remove any EXEC statements
+ sqlContent = sqlContent.replace(/EXEC\s*\([^)]+\)\s*;?/gi, '');
+ sqlContent = sqlContent.replace(/EXEC\s+[^;]+;/gi, '');
+
+ // 4. Replace any remaining procedural code blocks that might cause issues
+ sqlContent = sqlContent.replace(
+ /BEGIN\s+TRANSACTION|COMMIT\s+TRANSACTION|ROLLBACK\s+TRANSACTION/gi,
+ '-- $&'
+ );
+
+ // 5. Special handling for CREATE TABLE with reserved keywords as column names
+ // Find CREATE TABLE statements
+ const createTablePattern =
+ /CREATE\s+TABLE\s+\[?([^\]]*)\]?\.?\[?([^\]]*)\]?\s*\(([^;]*)\)/gi;
+
+ sqlContent = sqlContent.replace(
+ createTablePattern,
+ (_, schema, tableName, columnDefs) => {
+ // Process column definitions to rename problematic columns
+ let processedColumnDefs = columnDefs;
+
+ // Replace any column named "column" with "column_name"
+ processedColumnDefs = processedColumnDefs.replace(
+ /\[column\]/gi,
+ '[column_name]'
+ );
+
+ // Replace any column named "int" with "int_col"
+ processedColumnDefs = processedColumnDefs.replace(
+ /\[int\]/gi,
+ '[int_col]'
+ );
+
+ // Replace any column named "time" with "time_col"
+ processedColumnDefs = processedColumnDefs.replace(
+ /\[time\]/gi,
+ '[time_col]'
+ );
+
+ // Replace any column named "order" with "order_column"
+ processedColumnDefs = processedColumnDefs.replace(
+ /\[order\]/gi,
+ '[order_column]'
+ );
+
+ // Rebuild the CREATE TABLE statement
+ return `CREATE TABLE [${schema || 'dbo'}].[${tableName}] (${processedColumnDefs})`;
+ }
+ );
+
+ // 6. Handle default value expressions with functions - replace with simpler defaults
+ sqlContent = sqlContent.replace(/DEFAULT\s+'\([^)]+\)'/gi, "DEFAULT '0'");
+ sqlContent = sqlContent.replace(/DEFAULT\s+\([^)]+\)/gi, 'DEFAULT 0');
+
+ // 7. Split into individual statements to handle them separately
+ const statements = sqlContent
+ .split(';')
+ .filter((stmt) => stmt.trim().length > 0);
+
+ // Filter to keep only CREATE TABLE, CREATE INDEX, and ALTER TABLE statements
+ const filteredStatements = statements.filter((stmt) => {
+ const trimmedStmt = stmt.trim().toUpperCase();
+ return (
+ trimmedStmt.startsWith('CREATE TABLE') ||
+ trimmedStmt.startsWith('CREATE UNIQUE INDEX') ||
+ trimmedStmt.startsWith('CREATE INDEX') ||
+ trimmedStmt.startsWith('ALTER TABLE')
+ );
+ });
+
+ return filteredStatements.join(';') + ';';
+}
+
+/**
+ * Manual parsing of ALTER TABLE ADD CONSTRAINT statements
+ * This is a fallback for when the node-sql-parser fails to properly parse the constraints
+ */
+function parseAlterTableAddConstraint(statements: string[]): {
+ fkData: SQLForeignKey[];
+ tableMap: Record<string, string>;
+} {
+ const fkData: SQLForeignKey[] = [];
+ const tableMap: Record<string, string> = {};
+
+ // Regular expressions to extract information from ALTER TABLE statements
+ const alterTableRegex =
+ /ALTER\s+TABLE\s+\[?([^\]]*)\]?\.?\[?([^\]]*)\]?\s+ADD\s+CONSTRAINT\s+\[?([^\]]*)\]?\s+FOREIGN\s+KEY\s*\(\[?([^\]]*)\]?\)\s+REFERENCES\s+\[?([^\]]*)\]?\.?\[?([^\]]*)\]?\s*\(\[?([^\]]*)\]?\)/i;
+
+ for (const stmt of statements) {
+ const match = stmt.match(alterTableRegex);
+ if (match) {
+ const [
+ ,
+ sourceSchema = 'dbo',
+ sourceTable,
+ constraintName,
+ sourceColumn,
+ targetSchema = 'dbo',
+ targetTable,
+ targetColumn,
+ ] = match;
+
+ // Generate IDs for tables if they don't already exist
+ const sourceTableKey = `${sourceSchema}.${sourceTable}`;
+ const targetTableKey = `${targetSchema}.${targetTable}`;
+
+ if (!tableMap[sourceTableKey]) {
+ tableMap[sourceTableKey] = generateId();
+ }
+
+ if (!tableMap[targetTableKey]) {
+ tableMap[targetTableKey] = generateId();
+ }
+
+ fkData.push({
+ name: constraintName,
+ sourceTable: sourceTable,
+ sourceSchema: sourceSchema,
+ sourceColumn: sourceColumn,
+ targetTable: targetTable,
+ targetSchema: targetSchema,
+ targetColumn: targetColumn,
+ sourceTableId: tableMap[sourceTableKey],
+ targetTableId: tableMap[targetTableKey],
+ });
+ }
+ }
+
+ return { fkData, tableMap };
+}
+
+/**
+ * Map SQL Server data type strings to normalized types
+ * This ensures consistent type handling across the application
+ */
+function normalizeSQLServerDataType(dataType: string): string {
+ // Convert to lowercase for consistent comparison
+ const lowerType = dataType.toLowerCase().trim();
+
+ // Handle SQL Server specific types
+ switch (lowerType) {
+ // Exact numeric types
+ case 'tinyint':
+ return 'tinyint';
+ case 'smallint':
+ return 'smallint';
+ case 'int':
+ return 'int';
+ case 'bigint':
+ return 'bigint';
+ case 'decimal':
+ case 'numeric':
+ return lowerType;
+ case 'money':
+ case 'smallmoney':
+ return lowerType;
+
+ // Approximate numeric types
+ case 'float':
+ case 'real':
+ return lowerType;
+
+ // Date and time types
+ case 'date':
+ return 'date';
+ case 'datetime':
+ return 'datetime';
+ case 'datetime2':
+ return 'datetime2';
+ case 'datetimeoffset':
+ return 'datetimeoffset';
+ case 'smalldatetime':
+ return 'smalldatetime';
+ case 'time':
+ return 'time';
+
+ // Character strings
+ case 'char':
+ case 'varchar':
+ case 'text':
+ return lowerType;
+
+ // Unicode character strings
+ case 'nchar':
+ case 'nvarchar':
+ case 'ntext':
+ return lowerType;
+
+ // Binary strings
+ case 'binary':
+ case 'varbinary':
+ case 'image':
+ return lowerType;
+
+ // Other data types
+ case 'bit':
+ return 'bit';
+ case 'uniqueidentifier':
+ return 'uniqueidentifier';
+ case 'xml':
+ return 'xml';
+ case 'json':
+ return 'json';
+
+ // Default fallback
+ default:
+ return dataType;
+ }
+}
+
+/**
+ * Parse SQL Server DDL scripts and extract database structure
+ * @param sqlContent SQL Server DDL content as string
+ * @returns Parsed structure including tables, columns, and relationships
+ */
+export function fromSQLServer(sqlContent: string): SQLParserResult {
+ const tables: SQLTable[] = [];
+ const relationships: SQLForeignKey[] = [];
+ const tableMap: Record<string, string> = {}; // Maps table name to its ID
+
+ try {
+ // Preprocess the SQL content to handle T-SQL specific syntax
+ const preprocessedSQL = preprocessSQLServerScript(sqlContent);
+
+ const statements = sqlContent
+ .split(';')
+ .filter((stmt) => stmt.trim().length > 0);
+ const alterTableStatements = statements.filter(
+ (stmt) =>
+ stmt.trim().toUpperCase().startsWith('ALTER TABLE') &&
+ stmt.includes('FOREIGN KEY')
+ );
+
+ if (alterTableStatements.length > 0) {
+ const { fkData, tableMap: fkTableMap } =
+ parseAlterTableAddConstraint(alterTableStatements);
+
+ // Store table IDs from alter statements
+ Object.assign(tableMap, fkTableMap);
+
+ // Store foreign key relationships for later processing
+ relationships.push(...fkData);
+ }
+
+ let ast;
+ try {
+ ast = parser.astify(preprocessedSQL, parserOpts);
+ } catch {
+ // Fallback: Try to parse each statement individually
+ const statements = preprocessedSQL
+ .split(';')
+ .filter((stmt) => stmt.trim().length > 0);
+ ast = [];
+
+ for (const stmt of statements) {
+ try {
+ const stmtAst = parser.astify(stmt + ';', parserOpts);
+ if (Array.isArray(stmtAst)) {
+ ast.push(...stmtAst);
+ } else if (stmtAst) {
+ ast.push(stmtAst);
+ }
+ } catch {
+ // Skip statements that can't be parsed
+ }
+ }
+ }
+
+ if (!Array.isArray(ast) || ast.length === 0) {
+ throw new Error('Failed to parse SQL DDL - Empty or invalid AST');
+ }
+
+ // Process each statement
+ (ast as unknown as SQLASTNode[]).forEach((stmt) => {
+ // Process CREATE TABLE statements
+ if (stmt.type === 'create' && stmt.keyword === 'table') {
+ processCreateTable(
+ stmt as CreateTableStatement,
+ tables,
+ tableMap,
+ relationships
+ );
+ }
+ // Process CREATE INDEX statements
+ else if (stmt.type === 'create' && stmt.keyword === 'index') {
+ processCreateIndex(stmt as CreateIndexStatement, tables);
+ }
+ // Process ALTER TABLE statements
+ else if (stmt.type === 'alter' && stmt.keyword === 'table') {
+ processAlterTable(
+ stmt as AlterTableStatement,
+ tables,
+ relationships
+ );
+ }
+ });
+
+ // Link relationships to ensure all targetTableId and sourceTableId fields are filled
+ const validRelationships = linkRelationships(
+ tables,
+ relationships,
+ tableMap
+ );
+
+ // Sort tables by dependency (for better visualization)
+ const sortedTables = [...tables];
+ sortedTables.sort((a, b) => a.order - b.order);
+
+ return {
+ tables: sortedTables,
+ relationships: validRelationships,
+ };
+ } catch (error) {
+ console.error('Error parsing SQL Server DDL:', error);
+ throw new Error(`Error parsing SQL Server DDL: ${error}`);
+ }
+}
+
+/**
+ * Process a CREATE TABLE statement
+ */
+function processCreateTable(
+ stmt: CreateTableStatement,
+ tables: SQLTable[],
+ tableMap: Record<string, string>,
+ relationships: SQLForeignKey[]
+): void {
+ let tableName = '';
+ let schemaName = '';
+
+ // Extract table name and schema
+ if (stmt.table && typeof stmt.table === 'object') {
+ // Handle array of tables if needed
+ if (Array.isArray(stmt.table) && stmt.table.length > 0) {
+ const tableObj = stmt.table[0];
+ tableName = tableObj.table || '';
+ // SQL Server uses 'schema' or 'db' field
+ schemaName = tableObj.schema || tableObj.db || '';
+ } else {
+ // Direct object reference
+ const tableObj = stmt.table as TableReference;
+ tableName = tableObj.table || '';
+ schemaName = tableObj.schema || tableObj.db || '';
+ }
+ }
+
+ if (!tableName) {
+ return;
+ }
+
+ // If no schema specified, use default 'dbo' schema for SQL Server
+ if (!schemaName) {
+ schemaName = 'dbo';
+ }
+
+ // Generate a unique ID for the table
+ const tableId = generateId();
+ const tableKey = `${schemaName ? schemaName + '.' : ''}${tableName}`;
+ tableMap[tableKey] = tableId;
+
+ // Process table columns
+ const columns: SQLColumn[] = [];
+ const indexes: SQLIndex[] = [];
+
+ if (stmt.create_definitions && Array.isArray(stmt.create_definitions)) {
+ stmt.create_definitions.forEach(
+ (def: ColumnDefinition | ConstraintDefinition) => {
+ if (def.resource === 'column') {
+ // Process column definition
+ const columnDef = def as ColumnDefinition;
+ const columnName = extractColumnName(columnDef.column);
+ const rawDataType = columnDef.definition?.dataType || '';
+ const normalizedDataType =
+ normalizeSQLServerDataType(rawDataType);
+
+ if (columnName) {
+ // Check for SQL Server specific column properties
+ const isPrimaryKey =
+ columnDef.primary_key === 'primary key';
+
+ // For SQL Server, check for IDENTITY property in suffixes
+ const hasIdentity = columnDef.definition?.suffix?.some(
+ (suffix) =>
+ suffix.toLowerCase().includes('identity')
+ );
+
+ columns.push({
+ name: columnName,
+ type: normalizedDataType,
+ nullable: columnDef.nullable?.type !== 'not null',
+ primaryKey: isPrimaryKey,
+ unique: columnDef.unique === 'unique',
+ typeArgs: getTypeArgs(columnDef.definition),
+ default: columnDef.default_val
+ ? safelyBuildSQLFromAST(columnDef.default_val)
+ : undefined,
+ increment: hasIdentity,
+ });
+ }
+ } else if (def.resource === 'constraint') {
+ // Handle constraint definitions
+ const constraintDef = def as ConstraintDefinition;
+
+ // Handle PRIMARY KEY constraints
+ if (constraintDef.constraint_type === 'primary key') {
+ if (Array.isArray(constraintDef.definition)) {
+ // Extract column names from primary key constraint
+ for (const colDef of constraintDef.definition) {
+ if (
+ colDef &&
+ typeof colDef === 'object' &&
+ 'type' in colDef &&
+ colDef.type === 'column_ref' &&
+ 'column' in colDef
+ ) {
+ const pkColumnName = extractColumnName(
+ colDef as ColumnReference
+ );
+ // Find and mark the column as primary key
+ const column = columns.find(
+ (col) => col.name === pkColumnName
+ );
+ if (column) {
+ column.primaryKey = true;
+ }
+ }
+ }
+ }
+ }
+ // Handle UNIQUE constraints
+ else if (constraintDef.constraint_type === 'unique') {
+ if (Array.isArray(constraintDef.definition)) {
+ const uniqueColumns: string[] = [];
+ // Extract column names from unique constraint
+ for (const colDef of constraintDef.definition) {
+ if (
+ colDef &&
+ typeof colDef === 'object' &&
+ 'type' in colDef &&
+ colDef.type === 'column_ref' &&
+ 'column' in colDef
+ ) {
+ const uniqueColumnName = extractColumnName(
+ colDef as ColumnReference
+ );
+ uniqueColumns.push(uniqueColumnName);
+ }
+ }
+
+ // Add as an index
+ if (uniqueColumns.length > 0) {
+ indexes.push({
+ name:
+ constraintDef.constraint ||
+ `unique_${tableName}_${uniqueColumns.join('_')}`,
+ columns: uniqueColumns,
+ unique: true,
+ });
+ }
+ }
+ }
+ // Handle FOREIGN KEY constraints
+ else if (
+ constraintDef.constraint_type === 'foreign key' &&
+ constraintDef.reference
+ ) {
+ const reference = constraintDef.reference;
+ if (
+ reference &&
+ reference.table &&
+ reference.columns &&
+ reference.columns.length > 0
+ ) {
+ // Extract target table info
+ const targetTable =
+ reference.table as TableReference;
+ const targetTableName = targetTable.table;
+ const targetSchemaName =
+ targetTable.schema || targetTable.db || 'dbo';
+
+ // Extract source column
+ let sourceColumnName = '';
+ if (
+ Array.isArray(constraintDef.definition) &&
+ constraintDef.definition.length > 0
+ ) {
+ const sourceColDef =
+ constraintDef.definition[0];
+ if (
+ sourceColDef &&
+ typeof sourceColDef === 'object' &&
+ 'type' in sourceColDef &&
+ sourceColDef.type === 'column_ref'
+ ) {
+ sourceColumnName = extractColumnName(
+ sourceColDef as ColumnReference
+ );
+ }
+ }
+
+ // Extract target column
+ const targetColumnName = extractColumnName(
+ reference.columns[0]
+ );
+
+ if (
+ sourceColumnName &&
+ targetTableName &&
+ targetColumnName
+ ) {
+ // Create a foreign key relationship
+ relationships.push({
+ name:
+ constraintDef.constraint ||
+ `fk_${tableName}_${sourceColumnName}`,
+ sourceTable: tableName,
+ sourceSchema: schemaName,
+ sourceColumn: sourceColumnName,
+ targetTable: targetTableName,
+ targetSchema: targetSchemaName,
+ targetColumn: targetColumnName,
+ sourceTableId: tableId,
+ targetTableId: '', // Will be filled later
+ updateAction: reference.on_update,
+ deleteAction: reference.on_delete,
+ });
+ }
+ }
+ }
+ }
+ }
+ );
+ }
+
+ // Create the table object
+ tables.push({
+ id: tableId,
+ name: tableName,
+ schema: schemaName,
+ columns,
+ indexes,
+ order: tables.length,
+ });
+}
+
+/**
+ * Process a CREATE INDEX statement
+ */
+function processCreateIndex(
+ stmt: CreateIndexStatement,
+ tables: SQLTable[]
+): void {
+ if (!stmt.table || !stmt.columns || stmt.columns.length === 0) {
+ return;
+ }
+
+ // Extract table name and schema
+ let tableName = '';
+ let schemaName = '';
+
+ if (typeof stmt.table === 'object') {
+ // Handle array of tables if needed
+ if (Array.isArray(stmt.table) && stmt.table.length > 0) {
+ const tableObj = stmt.table[0];
+ tableName = tableObj.table || '';
+ schemaName = tableObj.schema || tableObj.db || '';
+ } else {
+ // Direct object reference
+ const tableObj = stmt.table as TableReference;
+ tableName = tableObj.table || '';
+ schemaName = tableObj.schema || tableObj.db || '';
+ }
+ }
+
+ if (!tableName) {
+ return;
+ }
+
+ // If no schema specified, use default 'dbo' schema for SQL Server
+ if (!schemaName) {
+ schemaName = 'dbo';
+ }
+
+ // Find the table
+ const table = findTableWithSchemaSupport(tables, tableName, schemaName);
+ if (!table) {
+ return;
+ }
+
+ // Extract column names from the index definition
+ const indexColumns = stmt.columns.map((col) => extractColumnName(col));
+ if (indexColumns.length === 0) {
+ return;
+ }
+
+ // Create the index
+ const indexName =
+ stmt.index || `idx_${tableName}_${indexColumns.join('_')}`;
+ const isUnique = stmt.constraint === 'unique';
+
+ // Add index to the table
+ const tableObj = tables.find((t) => t.id === table.id);
+ if (tableObj) {
+ tableObj.indexes.push({
+ name: indexName,
+ columns: indexColumns,
+ unique: isUnique,
+ });
+ }
+}
+
+/**
+ * Process an ALTER TABLE statement
+ */
+function processAlterTable(
+ stmt: AlterTableStatement,
+ tables: SQLTable[],
+ relationships: SQLForeignKey[]
+): void {
+ if (!stmt.table || !stmt.expr || !Array.isArray(stmt.expr)) {
+ return;
+ }
+
+ // Extract table name and schema
+ let tableName = '';
+ let schemaName = '';
+
+ if (typeof stmt.table === 'object') {
+ // Handle array of tables if needed
+ if (Array.isArray(stmt.table) && stmt.table.length > 0) {
+ const tableObj = stmt.table[0];
+ tableName = tableObj.table || '';
+ schemaName = tableObj.schema || tableObj.db || '';
+ } else {
+ // Direct object reference
+ const tableObj = stmt.table as TableReference;
+ tableName = tableObj.table || '';
+ schemaName = tableObj.schema || tableObj.db || '';
+ }
+ }
+
+ if (!tableName) {
+ return;
+ }
+
+ // If no schema specified, use default 'dbo' schema for SQL Server
+ if (!schemaName) {
+ schemaName = 'dbo';
+ }
+
+ // Find the table
+ const table = findTableWithSchemaSupport(tables, tableName, schemaName);
+ if (!table) {
+ return;
+ }
+
+ // Process each expression in the ALTER TABLE statement
+ for (const expr of stmt.expr) {
+ const action = expr.action;
+
+ // Handle ADD CONSTRAINT for foreign keys
+ if (action === 'add' && expr.resource === 'constraint') {
+ const constraintDef = expr as unknown as ConstraintDefinition;
+
+ if (
+ constraintDef.constraint_type === 'foreign key' &&
+ constraintDef.reference
+ ) {
+ const reference = constraintDef.reference;
+ if (
+ reference &&
+ reference.table &&
+ reference.columns &&
+ reference.columns.length > 0
+ ) {
+ // Extract target table info
+ const targetTable = reference.table as TableReference;
+ const targetTableName = targetTable.table;
+ const targetSchemaName =
+ targetTable.schema || targetTable.db || 'dbo';
+
+ // Extract source column
+ let sourceColumnName = '';
+ if (
+ Array.isArray(constraintDef.definition) &&
+ constraintDef.definition.length > 0
+ ) {
+ const sourceColDef = constraintDef.definition[0];
+ if (
+ sourceColDef &&
+ typeof sourceColDef === 'object' &&
+ 'type' in sourceColDef &&
+ sourceColDef.type === 'column_ref'
+ ) {
+ sourceColumnName = extractColumnName(
+ sourceColDef as ColumnReference
+ );
+ }
+ }
+
+ // Extract target column
+ const targetColumnName = extractColumnName(
+ reference.columns[0]
+ );
+
+ if (
+ sourceColumnName &&
+ targetTableName &&
+ targetColumnName
+ ) {
+ // Create a foreign key relationship
+ relationships.push({
+ name:
+ constraintDef.constraint ||
+ `fk_${tableName}_${sourceColumnName}`,
+ sourceTable: tableName,
+ sourceSchema: schemaName,
+ sourceColumn: sourceColumnName,
+ targetTable: targetTableName,
+ targetSchema: targetSchemaName,
+ targetColumn: targetColumnName,
+ sourceTableId: table.id,
+ targetTableId: '', // Will be filled later
+ updateAction: reference.on_update,
+ deleteAction: reference.on_delete,
+ });
+ }
+ }
+ }
+ }
+ }
+}
+
+/**
+ * Post-process the tables and relationships to ensure all targetTableId and sourceTableId fields are filled
+ */
+function linkRelationships(
+ tables: SQLTable[],
+ relationships: SQLForeignKey[],
+ tableMap: Record<string, string>
+): SQLForeignKey[] {
+ // First, ensure all table keys are normalized
+ const normalizedTableMap: Record<string, string> = {};
+ for (const [key, id] of Object.entries(tableMap)) {
+ // Normalize key format to ensure consistent lookups
+ let normalizedKey = key;
+ if (!key.includes('.')) {
+ normalizedKey = `dbo.${key}`;
+ }
+ normalizedTableMap[normalizedKey.toLowerCase()] = id;
+
+ // Also add without schema for fallback
+ const tableName = key.includes('.') ? key.split('.')[1] : key;
+ normalizedTableMap[tableName.toLowerCase()] = id;
+ }
+
+ // Add all tables to the normalized map
+ for (const table of tables) {
+ const tableKey = `${table.schema || 'dbo'}.${table.name}`;
+ normalizedTableMap[tableKey.toLowerCase()] = table.id;
+ normalizedTableMap[table.name.toLowerCase()] = table.id;
+ }
+
+ // Process all relationships
+ const validRelationships = relationships.filter((rel) => {
+ // Normalize keys for lookup
+ const sourceTableKey = `${rel.sourceSchema || 'dbo'}.${rel.sourceTable}`;
+ const targetTableKey = `${rel.targetSchema || 'dbo'}.${rel.targetTable}`;
+
+ // Get the source table ID if it's not already set
+ if (!rel.sourceTableId || rel.sourceTableId === '') {
+ const sourceId =
+ normalizedTableMap[sourceTableKey.toLowerCase()] ||
+ normalizedTableMap[rel.sourceTable.toLowerCase()];
+
+ if (sourceId) {
+ rel.sourceTableId = sourceId;
+ } else {
+ return false;
+ }
+ }
+
+ // Get the target table ID
+ if (!rel.targetTableId || rel.targetTableId === '') {
+ const targetId =
+ normalizedTableMap[targetTableKey.toLowerCase()] ||
+ normalizedTableMap[rel.targetTable.toLowerCase()];
+
+ if (targetId) {
+ rel.targetTableId = targetId;
+ } else {
+ return false;
+ }
+ }
+
+ return true;
+ });
+
+ return validRelationships;
+}
diff --git a/src/lib/data/sql-import/index.ts b/src/lib/data/sql-import/index.ts
new file mode 100644
index 00000000..25074a49
--- /dev/null
+++ b/src/lib/data/sql-import/index.ts
@@ -0,0 +1,328 @@
+import { DatabaseType } from '@/lib/domain/database-type';
+import type { Diagram } from '@/lib/domain/diagram';
+import { fromPostgres } from './dialect-importers/postgresql/postgresql';
+import { fromPostgresDump } from './dialect-importers/postgresql/postgresql-dump';
+
+import { fromSQLServer } from './dialect-importers/sqlserver/sqlserver';
+import { fromSQLite } from './dialect-importers/sqlite/sqlite';
+import type { SQLParserResult } from './common';
+import { convertToChartDBDiagram } from './common';
+import { adjustTablePositions } from '@/lib/domain/db-table';
+import { fromMySQL, isMySQLFormat } from './dialect-importers/mysql/mysql';
+
+/**
+ * Detect if SQL content is from pg_dump format
+ * @param sqlContent SQL content as string
+ * @returns boolean indicating if the SQL is likely from pg_dump
+ */
+function isPgDumpFormat(sqlContent: string): boolean {
+ // pg_dump output often contains specific markers
+ const pgDumpMarkers = [
+ 'SET statement_timeout',
+ 'SET lock_timeout',
+ 'SET client_encoding',
+ 'SET standard_conforming_strings',
+ 'SELECT pg_catalog.set_config',
+ 'ALTER TABLE ONLY',
+ 'COMMENT ON EXTENSION',
+ ];
+
+ // Check for specific pg_dump patterns
+ for (const marker of pgDumpMarkers) {
+ if (sqlContent.includes(marker)) {
+ return true;
+ }
+ }
+
+ // Check for other pg_dump patterns like COPY statements or specific comments
+ if (
+ (sqlContent.includes('COPY') && sqlContent.includes('FROM stdin')) ||
+ sqlContent.match(/--\s+Name:.*Type:/i)
+ ) {
+ return true;
+ }
+
+ return false;
+}
+
+/**
+ * Detect if SQL content is from SQL Server DDL format
+ * @param sqlContent SQL content as string
+ * @returns boolean indicating if the SQL is likely from SQL Server
+ */
+function isSQLServerFormat(sqlContent: string): boolean {
+ // SQL Server output often contains specific markers
+ const sqlServerMarkers = [
+ 'SET ANSI_NULLS ON',
+ 'SET QUOTED_IDENTIFIER ON',
+ 'SET ANSI_PADDING ON',
+ 'CREATE PROCEDURE',
+ 'EXEC sys.sp_',
+ 'EXECUTE sys.sp_',
+ '[dbo].',
+ 'IDENTITY(',
+ 'NVARCHAR',
+ 'UNIQUEIDENTIFIER',
+ 'ALTER TABLE [',
+ 'CREATE TABLE [dbo]',
+ 'CREATE INDEX [dbo_',
+ 'datetime2',
+ ];
+
+ // Check for specific SQL Server patterns
+ for (const marker of sqlServerMarkers) {
+ if (sqlContent.includes(marker)) {
+ return true;
+ }
+ }
+
+ // Also check for brackets used in SQL Server syntax - [dbo].[TableName]
+ if (sqlContent.match(/\[[^\]]+\]\.\[[^\]]+\]/)) {
+ return true;
+ }
+
+ return false;
+}
+
+/**
+ * Detect if SQL content is from SQLite format
+ * @param sqlContent SQL content as string
+ * @returns boolean indicating if the SQL is likely from SQLite
+ */
+function isSQLiteFormat(sqlContent: string): boolean {
+ // SQLite output often contains specific markers
+ const sqliteMarkers = [
+ 'PRAGMA',
+ 'INTEGER PRIMARY KEY AUTOINCREMENT',
+ 'DEFAULT (datetime(',
+ 'sqlite_sequence',
+ 'CREATE TRIGGER',
+ 'BEGIN',
+ 'END;',
+ ];
+
+ // Check for specific SQLite patterns
+ for (const marker of sqliteMarkers) {
+ if (sqlContent.includes(marker)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/**
+ * Auto-detect database type from SQL content
+ * @param sqlContent SQL content as string
+ * @returns Detected database type or null if can't determine
+ */
+export function detectDatabaseType(sqlContent: string): DatabaseType | null {
+ // First check for PostgreSQL dump format
+ if (isPgDumpFormat(sqlContent)) {
+ return DatabaseType.POSTGRESQL;
+ }
+
+ // Check for SQL Server format
+ if (isSQLServerFormat(sqlContent)) {
+ return DatabaseType.SQL_SERVER;
+ }
+
+ // Check for MySQL dump format
+ if (isMySQLFormat(sqlContent)) {
+ return DatabaseType.MYSQL;
+ }
+
+ // Check for SQLite format
+ if (isSQLiteFormat(sqlContent)) {
+ return DatabaseType.SQLITE;
+ }
+
+ // Look for database-specific keywords
+ if (
+ sqlContent.includes('SERIAL PRIMARY KEY') ||
+ sqlContent.includes('CREATE EXTENSION') ||
+ sqlContent.includes('WITH (OIDS') ||
+ sqlContent.includes('RETURNS SETOF')
+ ) {
+ return DatabaseType.POSTGRESQL;
+ }
+
+ if (
+ sqlContent.includes('AUTO_INCREMENT') ||
+ sqlContent.includes('ENGINE=InnoDB') ||
+ sqlContent.includes('DEFINER=')
+ ) {
+ return DatabaseType.MYSQL;
+ }
+
+ // Could not determine the database type
+ return null;
+}
+
+/**
+ * Parse SQL statements and convert to a Diagram object
+ * @param sqlContent SQL content as string
+ * @param sourceDatabaseType Source database type
+ * @param targetDatabaseType Target database type for the diagram
+ * @returns Diagram object
+ */
+export function sqlImportToDiagram({
+ sqlContent,
+ sourceDatabaseType,
+ targetDatabaseType = DatabaseType.GENERIC,
+}: {
+ sqlContent: string;
+ sourceDatabaseType: DatabaseType;
+ targetDatabaseType: DatabaseType;
+}): Diagram {
+ // If source database type is GENERIC, try to auto-detect the type
+ if (sourceDatabaseType === DatabaseType.GENERIC) {
+ const detectedType = detectDatabaseType(sqlContent);
+ if (detectedType) {
+ sourceDatabaseType = detectedType;
+ } else {
+ sourceDatabaseType = DatabaseType.POSTGRESQL;
+ }
+ }
+
+ let parserResult: SQLParserResult;
+
+ // Select the appropriate parser based on database type
+ switch (sourceDatabaseType) {
+ case DatabaseType.POSTGRESQL:
+ // Check if the SQL is from pg_dump and use the appropriate parser
+ if (isPgDumpFormat(sqlContent)) {
+ parserResult = fromPostgresDump(sqlContent);
+ } else {
+ parserResult = fromPostgres(sqlContent);
+ }
+ break;
+ case DatabaseType.MYSQL:
+ // Check if the SQL is from MySQL dump and use the appropriate parser
+ parserResult = fromMySQL(sqlContent);
+
+ break;
+ case DatabaseType.SQL_SERVER:
+ parserResult = fromSQLServer(sqlContent);
+ break;
+ case DatabaseType.SQLITE:
+ parserResult = fromSQLite(sqlContent);
+ break;
+ default:
+ throw new Error(`Unsupported database type: ${sourceDatabaseType}`);
+ }
+
+ // Convert the parsed SQL to a diagram
+ const diagram = convertToChartDBDiagram(
+ parserResult,
+ sourceDatabaseType,
+ targetDatabaseType
+ );
+
+ const adjustedTables = adjustTablePositions({
+ tables: diagram.tables ?? [],
+ relationships: diagram.relationships ?? [],
+ mode: 'perSchema',
+ });
+
+ const sortedTables = adjustedTables.sort((a, b) => {
+ if (a.isView === b.isView) {
+ // Both are either tables or views, so sort alphabetically by name
+ return a.name.localeCompare(b.name);
+ }
+ // If one is a view and the other is not, put tables first
+ return a.isView ? 1 : -1;
+ });
+
+ return {
+ ...diagram,
+ tables: sortedTables,
+ };
+}
+
+/**
+ * Parse SQL and identify any errors
+ * @param sqlContent SQL content as string
+ * @param sourceDatabaseType Source database type
+ * @returns Object with success status and error information
+ */
+export function parseSQLError({
+ sqlContent,
+ sourceDatabaseType,
+}: {
+ sqlContent: string;
+ sourceDatabaseType: DatabaseType;
+}): { success: boolean; error?: string; line?: number; column?: number } {
+ try {
+ // Validate SQL based on the database type
+ switch (sourceDatabaseType) {
+ case DatabaseType.POSTGRESQL:
+ // PostgreSQL validation - check format and use appropriate parser
+ if (isPgDumpFormat(sqlContent)) {
+ fromPostgresDump(sqlContent);
+ } else {
+ fromPostgres(sqlContent);
+ }
+ break;
+ case DatabaseType.MYSQL:
+ fromMySQL(sqlContent);
+
+ break;
+ case DatabaseType.SQL_SERVER:
+ // SQL Server validation
+ fromSQLServer(sqlContent);
+ break;
+ case DatabaseType.SQLITE:
+ // SQLite validation
+ fromSQLite(sqlContent);
+ break;
+ // Add more database types here
+ default:
+ throw new Error(
+ `Unsupported database type: ${sourceDatabaseType}`
+ );
+ }
+
+ return { success: true };
+ } catch (error: unknown) {
+ // Extract line and column information from the error message
+ let line: number | undefined;
+ let column: number | undefined;
+ let errorMessage: string;
+
+ // Type guard to check if error is an object with a message property
+ if (error instanceof Error) {
+ errorMessage = error.message;
+
+ // Parse error location if available
+ const lineMatch = error.message.match(/line\s*(\d+)/i);
+ if (lineMatch && lineMatch[1]) {
+ line = parseInt(lineMatch[1], 10);
+ }
+
+ const columnMatch = error.message.match(/column\s*(\d+)/i);
+ if (columnMatch && columnMatch[1]) {
+ column = parseInt(columnMatch[1], 10);
+ }
+
+ // Clean up error message if needed
+ if (error.message.includes('Error parsing')) {
+ // Extract everything after the colon using regex
+ const match = error.message.match(/Error parsing[^:]*:(.*)/);
+ if (match && match[1]) {
+ errorMessage = match[1].trim();
+ }
+ }
+ } else {
+ // Fallback for non-Error objects
+ errorMessage = String(error);
+ }
+
+ return {
+ success: false,
+ error: errorMessage,
+ line,
+ column,
+ };
+ }
+}