Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
fix: add SQL viewes support for import via ddl (#1044)
Jonathan Fishner0502efdc211f
diff --git a/src/lib/data/sql-import/common.ts b/src/lib/data/sql-import/common.tsindex b0b03703..f80241ff 100644--- a/src/lib/data/sql-import/common.ts+++ b/src/lib/data/sql-import/common.ts@@ -9,7 +9,7 @@ import {type DataType,} from '@/lib/data/data-types/data-types';import { genericDataTypes } from '@/lib/data/data-types/generic-data-types';-import { defaultTableColor } from '@/lib/colors';+import { defaultTableColor, viewColor } from '@/lib/colors';import { DatabaseType } from '@/lib/domain/database-type';import type { DBCustomType } from '@/lib/domain/db-custom-type';import { DBCustomTypeKind } from '@/lib/domain/db-custom-type';@@ -43,6 +43,7 @@ export interface SQLTable {indexes: SQLIndex[];comment?: string;order: number;+ isView?: boolean;}export interface SQLIndex {@@ -911,8 +912,8 @@ export function convertToChartDBDiagram(indexes,x: col * tableSpacing,y: row * tableSpacing,- color: defaultTableColor,- isView: false,+ color: table.isView ? viewColor : defaultTableColor,+ isView: table.isView ?? false,createdAt: Date.now(),} satisfies DBTable;});diff --git a/src/lib/data/sql-import/dialect-importers/mysql/__tests__/mysql-views.test.ts b/src/lib/data/sql-import/dialect-importers/mysql/__tests__/mysql-views.test.tsnew file mode 100644index 00000000..39b1f081--- /dev/null+++ b/src/lib/data/sql-import/dialect-importers/mysql/__tests__/mysql-views.test.ts@@ -0,0 +1,575 @@+import { describe, it, expect } from 'vitest';+import { fromMySQL } from '../mysql';++const SQL_WITH_VIEWS = `+-- MySQL 8+ example schema: 12 tables + 6 views+-- Domain: small SaaS app (orgs, users, projects, issues, billing, events)++CREATE DATABASE IF NOT EXISTS demo;+USE demo;++-- ----------------+-- Tables (12)+-- ----------------++CREATE TABLE organizations (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ name VARCHAR(200) NOT NULL,+ slug VARCHAR(80) NOT NULL,+ plan_tier VARCHAR(30) NOT NULL DEFAULT 'free',+ created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ PRIMARY KEY (id),+ UNIQUE KEY uq_organizations_slug (slug)+) ENGINE=InnoDB;++CREATE TABLE users (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ email VARCHAR(320) NOT NULL,+ full_name VARCHAR(200) NULL,+ is_active TINYINT(1) NOT NULL DEFAULT 1,+ created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ PRIMARY KEY (id),+ UNIQUE KEY uq_users_email (email)+) ENGINE=InnoDB;++CREATE TABLE org_memberships (+ org_id BIGINT UNSIGNED NOT NULL,+ user_id BIGINT UNSIGNED NOT NULL,+ role VARCHAR(30) NOT NULL DEFAULT 'member',+ joined_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ PRIMARY KEY (org_id, user_id),+ CONSTRAINT fk_org_memberships_org+ FOREIGN KEY (org_id) REFERENCES organizations(id) ON DELETE CASCADE,+ CONSTRAINT fk_org_memberships_user+ FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE+) ENGINE=InnoDB;++CREATE TABLE projects (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ org_id BIGINT UNSIGNED NOT NULL,+ name VARCHAR(200) NOT NULL,+ project_key VARCHAR(20) NOT NULL,+ is_archived TINYINT(1) NOT NULL DEFAULT 0,+ created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ PRIMARY KEY (id),+ UNIQUE KEY uq_projects_org_key (org_id, project_key),+ KEY ix_projects_org_id (org_id),+ CONSTRAINT fk_projects_org+ FOREIGN KEY (org_id) REFERENCES organizations(id) ON DELETE CASCADE+) ENGINE=InnoDB;++CREATE TABLE labels (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ org_id BIGINT UNSIGNED NOT NULL,+ name VARCHAR(80) NOT NULL,+ color VARCHAR(20) NOT NULL DEFAULT '#999999',+ PRIMARY KEY (id),+ UNIQUE KEY uq_labels_org_name (org_id, name),+ KEY ix_labels_org_id (org_id),+ CONSTRAINT fk_labels_org+ FOREIGN KEY (org_id) REFERENCES organizations(id) ON DELETE CASCADE+) ENGINE=InnoDB;++CREATE TABLE issues (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ project_id BIGINT UNSIGNED NOT NULL,+ title VARCHAR(300) NOT NULL,+ description TEXT NULL,+ status VARCHAR(30) NOT NULL DEFAULT 'open',+ priority INT NOT NULL DEFAULT 3,+ created_by BIGINT UNSIGNED NULL,+ created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ closed_at TIMESTAMP(3) NULL,+ PRIMARY KEY (id),+ KEY ix_issues_project_status (project_id, status),+ KEY ix_issues_created_by (created_by),+ CONSTRAINT fk_issues_project+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,+ CONSTRAINT fk_issues_created_by+ FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL,+ CONSTRAINT ck_issues_status CHECK (status IN ('open','in_progress','closed')),+ CONSTRAINT ck_issues_priority CHECK (priority BETWEEN 1 AND 5)+) ENGINE=InnoDB;++CREATE TABLE issue_labels (+ issue_id BIGINT UNSIGNED NOT NULL,+ label_id BIGINT UNSIGNED NOT NULL,+ PRIMARY KEY (issue_id, label_id),+ KEY ix_issue_labels_label (label_id),+ CONSTRAINT fk_issue_labels_issue+ FOREIGN KEY (issue_id) REFERENCES issues(id) ON DELETE CASCADE,+ CONSTRAINT fk_issue_labels_label+ FOREIGN KEY (label_id) REFERENCES labels(id) ON DELETE CASCADE+) ENGINE=InnoDB;++CREATE TABLE comments (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ issue_id BIGINT UNSIGNED NOT NULL,+ author_id BIGINT UNSIGNED NULL,+ body TEXT NOT NULL,+ created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ PRIMARY KEY (id),+ KEY ix_comments_issue_created_at (issue_id, created_at),+ KEY ix_comments_author_id (author_id),+ CONSTRAINT fk_comments_issue+ FOREIGN KEY (issue_id) REFERENCES issues(id) ON DELETE CASCADE,+ CONSTRAINT fk_comments_author+ FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE SET NULL+) ENGINE=InnoDB;++CREATE TABLE api_keys (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ org_id BIGINT UNSIGNED NOT NULL,+ name VARCHAR(120) NOT NULL,+ key_hash VARCHAR(200) NOT NULL,+ last_used_at TIMESTAMP(3) NULL,+ created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ PRIMARY KEY (id),+ UNIQUE KEY uq_api_keys_org_name (org_id, name),+ KEY ix_api_keys_org_id (org_id),+ CONSTRAINT fk_api_keys_org+ FOREIGN KEY (org_id) REFERENCES organizations(id) ON DELETE CASCADE+) ENGINE=InnoDB;++CREATE TABLE invoices (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ org_id BIGINT UNSIGNED NOT NULL,+ period_start DATE NOT NULL,+ period_end DATE NOT NULL,+ status VARCHAR(30) NOT NULL DEFAULT 'open',+ total_cents INT NOT NULL DEFAULT 0,+ created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ PRIMARY KEY (id),+ KEY ix_invoices_org_status (org_id, status),+ CONSTRAINT fk_invoices_org+ FOREIGN KEY (org_id) REFERENCES organizations(id) ON DELETE CASCADE,+ CONSTRAINT ck_invoices_period CHECK (period_end >= period_start),+ CONSTRAINT ck_invoices_status CHECK (status IN ('open','paid','void')),+ CONSTRAINT ck_invoices_total_cents CHECK (total_cents >= 0)+) ENGINE=InnoDB;++CREATE TABLE payments (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ invoice_id BIGINT UNSIGNED NOT NULL,+ provider VARCHAR(30) NOT NULL,+ amount_cents INT NOT NULL,+ paid_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ PRIMARY KEY (id),+ KEY ix_payments_invoice_id (invoice_id),+ CONSTRAINT fk_payments_invoice+ FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE CASCADE,+ CONSTRAINT ck_payments_amount CHECK (amount_cents > 0)+) ENGINE=InnoDB;++CREATE TABLE events (+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,+ org_id BIGINT UNSIGNED NOT NULL,+ user_id BIGINT UNSIGNED NULL,+ event_type VARCHAR(80) NOT NULL,+ metadata JSON NOT NULL,+ occurred_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),+ PRIMARY KEY (id),+ KEY ix_events_org_occurred_at (org_id, occurred_at),+ KEY ix_events_user_id (user_id),+ CONSTRAINT fk_events_org+ FOREIGN KEY (org_id) REFERENCES organizations(id) ON DELETE CASCADE,+ CONSTRAINT fk_events_user+ FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL+) ENGINE=InnoDB;++-- ----------------+-- Views (6)+-- ----------------++CREATE OR REPLACE VIEW v_active_org_members AS+SELECT+ o.id AS org_id,+ o.slug AS org_slug,+ u.id AS user_id,+ u.email,+ u.full_name,+ m.role,+ m.joined_at+FROM org_memberships m+JOIN organizations o ON o.id = m.org_id+JOIN users u ON u.id = m.user_id+WHERE u.is_active = 1;++CREATE OR REPLACE VIEW v_project_issue_summary AS+SELECT+ p.id AS project_id,+ p.org_id,+ p.name AS project_name,+ p.project_key,+ SUM(CASE WHEN i.status = 'open' THEN 1 ELSE 0 END) AS open_issues,+ SUM(CASE WHEN i.status = 'in_progress' THEN 1 ELSE 0 END) AS in_progress_issues,+ SUM(CASE WHEN i.status = 'closed' THEN 1 ELSE 0 END) AS closed_issues,+ COUNT(i.id) AS total_issues,+ MAX(i.created_at) AS last_issue_created_at+FROM projects p+LEFT JOIN issues i ON i.project_id = p.id+GROUP BY p.id, p.org_id, p.name, p.project_key;++CREATE OR REPLACE VIEW v_issue_details AS+SELECT+ i.id AS issue_id,+ i.project_id,+ p.org_id,+ i.title,+ i.status,+ i.priority,+ i.created_at,+ i.closed_at,+ i.created_by,+ u.email AS created_by_email,+ (SELECT COUNT(*) FROM comments c WHERE c.issue_id = i.id) AS comment_count+FROM issues i+JOIN projects p ON p.id = i.project_id+LEFT JOIN users u ON u.id = i.created_by;++CREATE OR REPLACE VIEW v_invoice_balances AS+SELECT+ inv.id AS invoice_id,+ inv.org_id,+ inv.period_start,+ inv.period_end,+ inv.status,+ inv.total_cents,+ COALESCE(SUM(pay.amount_cents), 0) AS paid_cents,+ GREATEST(inv.total_cents - COALESCE(SUM(pay.amount_cents), 0), 0) AS due_cents,+ MAX(pay.paid_at) AS last_payment_at+FROM invoices inv+LEFT JOIN payments pay ON pay.invoice_id = inv.id+GROUP BY inv.id, inv.org_id, inv.period_start, inv.period_end, inv.status, inv.total_cents;++CREATE OR REPLACE VIEW v_recent_events AS+SELECT+ e.id,+ e.org_id,+ o.slug AS org_slug,+ e.user_id,+ u.email AS user_email,+ e.event_type,+ e.metadata,+ e.occurred_at+FROM events e+JOIN organizations o ON o.id = e.org_id+LEFT JOIN users u ON u.id = e.user_id+WHERE e.occurred_at >= (UTC_TIMESTAMP(3) - INTERVAL 7 DAY);++CREATE OR REPLACE VIEW v_org_activity_daily AS+SELECT+ e.org_id,+ DATE(e.occurred_at) AS day,+ COUNT(*) AS events_count,+ SUM(CASE WHEN e.event_type = 'login' THEN 1 ELSE 0 END) AS logins+FROM events e+WHERE e.occurred_at >= (UTC_TIMESTAMP(3) - INTERVAL 30 DAY)+GROUP BY e.org_id, DATE(e.occurred_at);+`;++describe('MySQL View Import', () => {+ it('should import 12 tables and 6 views', async () => {+ const result = await fromMySQL(SQL_WITH_VIEWS);++ // Count tables and views+ const tables = result.tables.filter((t) => !t.isView);+ const views = result.tables.filter((t) => t.isView);++ expect(tables.length).toBe(12);+ expect(views.length).toBe(6);+ });++ it('should correctly parse view names', async () => {+ const result = await fromMySQL(SQL_WITH_VIEWS);++ const views = result.tables.filter((t) => t.isView);+ const viewNames = views.map((v) => v.name).sort();++ expect(viewNames).toEqual([+ 'v_active_org_members',+ 'v_invoice_balances',+ 'v_issue_details',+ 'v_org_activity_daily',+ 'v_project_issue_summary',+ 'v_recent_events',+ ]);+ });++ it('should correctly parse table names', async () => {+ const result = await fromMySQL(SQL_WITH_VIEWS);++ const tables = result.tables.filter((t) => !t.isView);+ const tableNames = tables.map((t) => t.name).sort();++ expect(tableNames).toEqual([+ 'api_keys',+ 'comments',+ 'events',+ 'invoices',+ 'issue_labels',+ 'issues',+ 'labels',+ 'org_memberships',+ 'organizations',+ 'payments',+ 'projects',+ 'users',+ ]);+ });++ it('should parse relationships correctly', async () => {+ const result = await fromMySQL(SQL_WITH_VIEWS);++ // Expected foreign key relationships (15 total):+ // 1. org_memberships.org_id -> organizations.id+ // 2. org_memberships.user_id -> users.id+ // 3. projects.org_id -> organizations.id+ // 4. labels.org_id -> organizations.id+ // 5. issues.project_id -> projects.id+ // 6. issues.created_by -> users.id+ // 7. issue_labels.issue_id -> issues.id+ // 8. issue_labels.label_id -> labels.id+ // 9. comments.issue_id -> issues.id+ // 10. comments.author_id -> users.id+ // 11. api_keys.org_id -> organizations.id+ // 12. invoices.org_id -> organizations.id+ // 13. payments.invoice_id -> invoices.id+ // 14. events.org_id -> organizations.id+ // 15. events.user_id -> users.id++ // Should have exactly 15 relationships (no duplicates)+ expect(result.relationships.length).toBe(15);++ // Create a map for easier lookup+ const relationshipMap = new Map<+ string,+ (typeof result.relationships)[0]+ >();+ for (const rel of result.relationships) {+ const key = `${rel.sourceTable}.${rel.sourceColumn}->${rel.targetTable}.${rel.targetColumn}`;+ relationshipMap.set(key, rel);+ }++ // Validate each relationship exists and is correctly defined+ const expectedRelationships = [+ {+ sourceTable: 'org_memberships',+ sourceColumn: 'org_id',+ targetTable: 'organizations',+ targetColumn: 'id',+ },+ {+ sourceTable: 'org_memberships',+ sourceColumn: 'user_id',+ targetTable: 'users',+ targetColumn: 'id',+ },+ {+ sourceTable: 'projects',+ sourceColumn: 'org_id',+ targetTable: 'organizations',+ targetColumn: 'id',+ },+ {+ sourceTable: 'labels',+ sourceColumn: 'org_id',+ targetTable: 'organizations',+ targetColumn: 'id',+ },+ {+ sourceTable: 'issues',+ sourceColumn: 'project_id',+ targetTable: 'projects',+ targetColumn: 'id',+ },+ {+ sourceTable: 'issues',+ sourceColumn: 'created_by',+ targetTable: 'users',+ targetColumn: 'id',+ },+ {+ sourceTable: 'issue_labels',+ sourceColumn: 'issue_id',+ targetTable: 'issues',+ targetColumn: 'id',+ },+ {+ sourceTable: 'issue_labels',+ sourceColumn: 'label_id',+ targetTable: 'labels',+ targetColumn: 'id',+ },+ {+ sourceTable: 'comments',+ sourceColumn: 'issue_id',+ targetTable: 'issues',+ targetColumn: 'id',+ },+ {+ sourceTable: 'comments',+ sourceColumn: 'author_id',+ targetTable: 'users',+ targetColumn: 'id',+ },+ {+ sourceTable: 'api_keys',+ sourceColumn: 'org_id',+ targetTable: 'organizations',+ targetColumn: 'id',+ },+ {+ sourceTable: 'invoices',+ sourceColumn: 'org_id',+ targetTable: 'organizations',+ targetColumn: 'id',+ },+ {+ sourceTable: 'payments',+ sourceColumn: 'invoice_id',+ targetTable: 'invoices',+ targetColumn: 'id',+ },+ {+ sourceTable: 'events',+ sourceColumn: 'org_id',+ targetTable: 'organizations',+ targetColumn: 'id',+ },+ {+ sourceTable: 'events',+ sourceColumn: 'user_id',+ targetTable: 'users',+ targetColumn: 'id',+ },+ ];++ for (const expected of expectedRelationships) {+ const key = `${expected.sourceTable}.${expected.sourceColumn}->${expected.targetTable}.${expected.targetColumn}`;+ expect(+ relationshipMap.has(key),+ `Missing relationship: ${key}`+ ).toBe(true);+ }++ // Verify all relationships have valid sourceTableId and targetTableId+ for (const rel of result.relationships) {+ expect(+ rel.sourceTableId,+ `Relationship ${rel.sourceTable}.${rel.sourceColumn} missing sourceTableId`+ ).toBeTruthy();+ expect(+ rel.targetTableId,+ `Relationship ${rel.sourceTable}.${rel.sourceColumn} missing targetTableId`+ ).toBeTruthy();+ }+ });++ it('should correctly parse the organizations table', async () => {+ const result = await fromMySQL(SQL_WITH_VIEWS);++ const orgsTable = result.tables.find((t) => t.name === 'organizations');++ expect(orgsTable).toBeDefined();+ expect(orgsTable?.isView).toBeFalsy();++ // Check columns+ const columnNames = orgsTable?.columns.map((c) => c.name);+ expect(columnNames).toContain('id');+ expect(columnNames).toContain('name');+ expect(columnNames).toContain('slug');+ expect(columnNames).toContain('plan_tier');+ expect(columnNames).toContain('created_at');++ // Check id column properties+ const idColumn = orgsTable?.columns.find((c) => c.name === 'id');+ expect(idColumn?.primaryKey).toBe(true);+ expect(idColumn?.increment).toBe(true);++ // Check name column properties+ const nameColumn = orgsTable?.columns.find((c) => c.name === 'name');+ expect(nameColumn?.nullable).toBe(false);+ expect(nameColumn?.type.toLowerCase()).toContain('varchar');+ });++ it('should correctly parse the issues table', async () => {+ const result = await fromMySQL(SQL_WITH_VIEWS);++ const issuesTable = result.tables.find((t) => t.name === 'issues');++ expect(issuesTable).toBeDefined();+ expect(issuesTable?.isView).toBeFalsy();++ // Check columns+ const columnNames = issuesTable?.columns.map((c) => c.name);+ expect(columnNames).toContain('id');+ expect(columnNames).toContain('project_id');+ expect(columnNames).toContain('title');+ expect(columnNames).toContain('description');+ expect(columnNames).toContain('status');+ expect(columnNames).toContain('priority');+ expect(columnNames).toContain('created_by');+ expect(columnNames).toContain('created_at');+ expect(columnNames).toContain('closed_at');++ // Check id column properties+ const idColumn = issuesTable?.columns.find((c) => c.name === 'id');+ expect(idColumn?.primaryKey).toBe(true);+ expect(idColumn?.increment).toBe(true);++ // Check nullable columns+ const descColumn = issuesTable?.columns.find(+ (c) => c.name === 'description'+ );+ expect(descColumn?.nullable).toBe(true);++ const createdByColumn = issuesTable?.columns.find(+ (c) => c.name === 'created_by'+ );+ expect(createdByColumn?.nullable).toBe(true);++ const closedAtColumn = issuesTable?.columns.find(+ (c) => c.name === 'closed_at'+ );+ expect(closedAtColumn?.nullable).toBe(true);+ });++ it('should extract columns from views', async () => {+ const result = await fromMySQL(SQL_WITH_VIEWS);++ const activeOrgMembersView = result.tables.find(+ (t) => t.name === 'v_active_org_members'+ );++ expect(activeOrgMembersView).toBeDefined();+ expect(activeOrgMembersView?.isView).toBe(true);++ // Check that columns were extracted from the SELECT clause+ const columnNames = activeOrgMembersView?.columns.map((c) => c.name);+ expect(columnNames).toContain('org_id');+ expect(columnNames).toContain('org_slug');+ expect(columnNames).toContain('user_id');+ expect(columnNames).toContain('email');+ expect(columnNames).toContain('full_name');+ expect(columnNames).toContain('role');+ expect(columnNames).toContain('joined_at');+ });++ it('should mark views with isView=true and tables with isView falsy', async () => {+ const result = await fromMySQL(SQL_WITH_VIEWS);++ const tables = result.tables.filter((t) => !t.isView);+ const views = result.tables.filter((t) => t.isView);++ // All tables should NOT have isView set+ tables.forEach((table) => {+ expect(table.isView).toBeFalsy();+ });++ // All views should have isView=true+ views.forEach((view) => {+ expect(view.isView).toBe(true);+ });+ });+});diff --git a/src/lib/data/sql-import/dialect-importers/mysql/mysql.ts b/src/lib/data/sql-import/dialect-importers/mysql/mysql.tsindex 7fef28fe..cbb79536 100644--- a/src/lib/data/sql-import/dialect-importers/mysql/mysql.ts+++ b/src/lib/data/sql-import/dialect-importers/mysql/mysql.ts@@ -59,6 +59,102 @@ function extractStatements(sqlContent: string): string[] {return statements;}+/**+ * Extract columns from a CREATE VIEW statement+ * Views can have explicit column names or derive them from the SELECT+ */+function extractColumnsFromView(sql: string): SQLColumn[] {+ const columns: SQLColumn[] = [];++ // First, try to extract explicit column list from CREATE VIEW viewname (col1, col2, ...) AS+ const explicitColumnsMatch = sql.match(+ /CREATE\s+(?:OR\s+REPLACE\s+)?(?:ALGORITHM\s*=\s*\w+\s+)?(?:DEFINER\s*=\s*[^\s]+\s+)?(?:SQL\s+SECURITY\s+\w+\s+)?VIEW\s+(?:`?[^`\s.]+`?\.)?`?[^`\s.(]+`?\s*\(([^)]+)\)\s*AS/i+ );++ if (explicitColumnsMatch) {+ // Parse explicit column list+ const columnList = explicitColumnsMatch[1];+ const columnNames = columnList+ .split(',')+ .map((col) => col.trim().replace(/^[`'"]+|[`'"]+$/g, ''));++ for (const colName of columnNames) {+ if (colName) {+ columns.push({+ name: colName,+ type: 'text', // Default type for views since we don't know the actual type+ nullable: true,+ primaryKey: false,+ unique: false,+ });+ }+ }++ return columns;+ }++ // If no explicit columns, try to extract from SELECT clause+ const selectMatch = sql.match(/\bAS\s+SELECT\s+([\s\S]+?)\s+FROM\s+/i);++ if (selectMatch) {+ const selectClause = selectMatch[1];++ // Handle SELECT * - we can't determine columns+ if (selectClause.trim() === '*') {+ return columns;+ }++ // Split by comma, but be careful of nested functions/expressions+ let depth = 0;+ let currentCol = '';+ const selectParts: string[] = [];++ for (const char of selectClause) {+ if (char === '(' || char === '[') depth++;+ else if (char === ')' || char === ']') depth--;+ else if (char === ',' && depth === 0) {+ selectParts.push(currentCol.trim());+ currentCol = '';+ continue;+ }+ currentCol += char;+ }+ if (currentCol.trim()) {+ selectParts.push(currentCol.trim());+ }++ for (const part of selectParts) {+ let columnName = '';++ // Check for alias: ... AS `name` or ... AS name+ const aliasMatch = part.match(/\s+AS\s+[`'"']?(\w+)[`'"']?\s*$/i);+ if (aliasMatch) {+ columnName = aliasMatch[1];+ } else {+ // Try to extract the column reference+ const colRefMatch = part.match(+ /(?:[\w`"]+\.)?[`'"']?(\w+)[`'"']?\s*$/+ );+ if (colRefMatch) {+ columnName = colRefMatch[1];+ }+ }++ if (columnName && columnName !== '*') {+ columns.push({+ name: columnName,+ type: 'text',+ nullable: true,+ primaryKey: false,+ unique: false,+ });+ }+ }+ }++ return columns;+}+// Function to extract columns from a CREATE TABLE statement using regexfunction extractColumnsFromCreateTable(statement: string): SQLColumn[] {const columns: SQLColumn[] = [];@@ -711,6 +807,10 @@ export async function fromMySQL(sqlContent: string): Promise<SQLParserResult> {relationships.push(fk);+ // Track this relationship to avoid duplicates from regex fallback+ addedRelationships.add(+ `${fk.sourceTable}.${fk.sourceColumn}-${fk.targetTable}.${fk.targetColumn}`+ );}}}@@ -771,7 +871,52 @@ export async function fromMySQL(sqlContent: string): Promise<SQLParserResult> {}}- // Second pass: process CREATE INDEX statements+ // Second pass: process CREATE VIEW statements+ for (const statement of statements) {+ const trimmedStmt = statement.trim();+ const upperStmt = trimmedStmt.toUpperCase();++ if (+ upperStmt.startsWith('CREATE VIEW') ||+ upperStmt.startsWith('CREATE OR REPLACE VIEW') ||+ upperStmt.includes('CREATE VIEW') ||+ upperStmt.includes('CREATE OR REPLACE VIEW')+ ) {+ // Extract view name - handle MySQL syntax with ALGORITHM, DEFINER, etc.+ const viewMatch = trimmedStmt.match(+ /CREATE\s+(?:OR\s+REPLACE\s+)?(?:ALGORITHM\s*=\s*\w+\s+)?(?:DEFINER\s*=\s*[^\s]+\s+)?(?:SQL\s+SECURITY\s+\w+\s+)?VIEW\s+(?:`?([^`\s.]+)`?\.)?`?([^`\s.(]+)`?/i+ );++ if (viewMatch) {+ const database = viewMatch[1] || '';+ const viewName = viewMatch[2].replace(/`/g, '');++ if (viewName) {+ const viewId = generateId();+ tableMap[viewName] = viewId;+ if (database) {+ tableMap[`${database}.${viewName}`] = viewId;+ }++ // Extract columns from the view definition+ const columns = extractColumnsFromView(trimmedStmt);++ // Create view object (as a table with isView: true)+ tables.push({+ id: viewId,+ name: viewName,+ schema: database || undefined,+ columns,+ indexes: [], // Views don't have indexes+ order: tables.length,+ isView: true,+ });+ }+ }+ }+ }++ // Third pass: process CREATE INDEX statementsfor (const statement of statements) {const trimmedStmt = statement.trim();if (@@ -782,7 +927,7 @@ export async function fromMySQL(sqlContent: string): Promise<SQLParserResult> {}}- // Third pass: process ALTER TABLE statements for foreign keys+ // Fourth pass: process ALTER TABLE statements for foreign keysfor (const statement of statements) {const trimmedStmt = statement.trim();if (@@ -892,6 +1037,10 @@ export async function fromMySQL(sqlContent: string): Promise<SQLParserResult> {};relationships.push(fk);+ // Track this relationship to avoid duplicates from regex fallback+ addedRelationships.add(+ `${fk.sourceTable}.${fk.sourceColumn}-${fk.targetTable}.${fk.targetColumn}`+ );}} catch (fkError) {console.error(@@ -946,6 +1095,10 @@ export async function fromMySQL(sqlContent: string): Promise<SQLParserResult> {};relationships.push(fk);+ // Track this relationship to avoid duplicates from regex fallback+ addedRelationships.add(+ `${fk.sourceTable}.${fk.sourceColumn}-${fk.targetTable}.${fk.targetColumn}`+ );}}}@@ -1109,12 +1262,14 @@ function findForeignKeysUsingRegex(continue;}- // Get table IDs- const sourceTableKey = `${sourceSchema}.${sourceTable}`;- const targetTableKey = `${sourceSchema}.${targetTable}`;-- const sourceTableId = tableMap[sourceTableKey];- const targetTableId = tableMap[targetTableKey];+ // Get table IDs - try multiple key formats+ // Tables might be stored as just "tableName" or "schema.tableName"+ const sourceTableId =+ tableMap[sourceTable] ||+ tableMap[`${sourceSchema}.${sourceTable}`];+ const targetTableId =+ tableMap[targetTable] ||+ tableMap[`${sourceSchema}.${targetTable}`];// Skip if either table ID is missingif (!sourceTableId || !targetTableId) {diff --git a/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.tsindex d2fc3564..edfb459d 100644--- a/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts+++ b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts@@ -30,6 +30,7 @@ import {interface ParsedStatement {type:| 'table'+ | 'view'| 'index'| 'alter'| 'function'@@ -142,6 +143,13 @@ function preprocessSQL(sqlContent: string): PreprocessResult {} else {statements.push({ type: 'alter', sql: trimmedStmt });}+ } else if (+ upperStmt.startsWith('CREATE VIEW') ||+ upperStmt.startsWith('CREATE OR REPLACE VIEW') ||+ upperStmt.includes('CREATE VIEW') ||+ upperStmt.includes('CREATE OR REPLACE VIEW')+ ) {+ statements.push({ type: 'view', sql: trimmedStmt });} else if (upperStmt.startsWith('CREATE FUNCTION') ||upperStmt.startsWith('CREATE OR REPLACE FUNCTION')@@ -568,6 +576,105 @@ function extractColumnsFromSQL(sql: string): SQLColumn[] {return columns;}+/**+ * Extract columns from a CREATE VIEW statement+ * Views can have explicit column names or derive them from the SELECT+ */+function extractColumnsFromView(sql: string): SQLColumn[] {+ const columns: SQLColumn[] = [];++ // First, try to extract explicit column list from CREATE VIEW viewname (col1, col2, ...) AS+ const explicitColumnsMatch = sql.match(+ /CREATE\s+(?:OR\s+REPLACE\s+)?VIEW\s+(?:(?:"[^"]+"|[^"\s.]+)\.)?(?:"[^"]+"|[^"\s.(]+)\s*\(([^)]+)\)\s*AS/i+ );++ if (explicitColumnsMatch) {+ // Parse explicit column list+ const columnList = explicitColumnsMatch[1];+ const columnNames = columnList+ .split(',')+ .map((col) => col.trim().replace(/^["']|["']$/g, ''));++ for (const colName of columnNames) {+ if (colName) {+ columns.push({+ name: colName,+ type: 'text', // Default type for views since we don't know the actual type+ nullable: true,+ primaryKey: false,+ unique: false,+ });+ }+ }++ return columns;+ }++ // If no explicit columns, try to extract from SELECT clause+ // Match: AS SELECT ... FROM (extracting the column references)+ const selectMatch = sql.match(/\bAS\s+SELECT\s+([\s\S]+?)\s+FROM\s+/i);++ if (selectMatch) {+ const selectClause = selectMatch[1];++ // Handle SELECT * - we can't determine columns+ if (selectClause.trim() === '*') {+ return columns;+ }++ // Split by comma, but be careful of nested functions/expressions+ let depth = 0;+ let currentCol = '';+ const selectParts: string[] = [];++ for (const char of selectClause) {+ if (char === '(' || char === '[') depth++;+ else if (char === ')' || char === ']') depth--;+ else if (char === ',' && depth === 0) {+ selectParts.push(currentCol.trim());+ currentCol = '';+ continue;+ }+ currentCol += char;+ }+ if (currentCol.trim()) {+ selectParts.push(currentCol.trim());+ }++ for (const part of selectParts) {+ // Extract column name - handle aliases (AS name), qualified names (table.col), and expressions+ let columnName = '';++ // Check for alias: ... AS "name" or ... AS name+ const aliasMatch = part.match(/\s+AS\s+["']?(\w+)["']?\s*$/i);+ if (aliasMatch) {+ columnName = aliasMatch[1];+ } else {+ // Try to extract the column reference+ // Handle: col, table.col, "col", table."col"+ const colRefMatch = part.match(+ /(?:[\w"]+\.)?["']?(\w+)["']?\s*$/+ );+ if (colRefMatch) {+ columnName = colRefMatch[1];+ }+ }++ if (columnName && columnName !== '*') {+ columns.push({+ name: columnName,+ type: 'text', // Default type for views+ nullable: true,+ primaryKey: false,+ unique: false,+ });+ }+ }+ }++ return columns;+}+/*** Extract enum type definition from CREATE TYPE statement*/@@ -745,7 +852,7 @@ export async function fromPostgres(const { Parser } = await import('node-sql-parser');const parser = new Parser();- // First pass: collect all table names and custom types+ // First pass: collect all table names, view names, and custom typesfor (const stmt of statements) {if (stmt.type === 'table') {// Extract just the CREATE TABLE part if there are comments@@ -770,6 +877,24 @@ export async function fromPostgres(const tableKey = `${schemaName}.${tableName}`;tableMap[tableKey] = generateId();}+ } else if (stmt.type === 'view') {+ // Extract view name similar to table+ const createViewIndex = stmt.sql.toUpperCase().indexOf('CREATE');+ const sqlFromCreate =+ createViewIndex >= 0+ ? stmt.sql.substring(createViewIndex)+ : stmt.sql;++ // Matches: CREATE [OR REPLACE] VIEW [schema.]viewname+ const viewMatch = sqlFromCreate.match(+ /CREATE\s+(?:OR\s+REPLACE\s+)?VIEW\s+(?:(?:"([^"]+)"|([^"\s.]+))\.)?(?:"([^"]+)"|([^"\s.(]+))/i+ );+ if (viewMatch) {+ const schemaName = viewMatch[1] || viewMatch[2] || 'public';+ const viewName = viewMatch[3] || viewMatch[4];+ const viewKey = `${schemaName}.${viewName}`;+ tableMap[viewKey] = generateId();+ }} else if (stmt.type === 'type') {// Extract enum type definitionconst enumType = extractEnumFromSQL(stmt.sql);@@ -783,6 +908,7 @@ export async function fromPostgres(for (const stmt of statements) {if (stmt.type === 'table' ||+ stmt.type === 'view' ||stmt.type === 'index' ||stmt.type === 'alter') {@@ -807,8 +933,8 @@ export async function fromPostgres();// Mark the statement as having parse errors but keep it for fallback processing- if (stmt.type === 'table') {- stmt.parsed = null; // Mark as failed but still a table+ if (stmt.type === 'table' || stmt.type === 'view') {+ stmt.parsed = null; // Mark as failed but still a table/view}}}@@ -1142,6 +1268,47 @@ export async function fromPostgres(}}+ // Third pass (continued): extract view definitions+ for (const stmt of statements) {+ if (stmt.type === 'view') {+ // Extract view name+ const createViewIndex = stmt.sql.toUpperCase().indexOf('CREATE');+ const sqlFromCreate =+ createViewIndex >= 0+ ? stmt.sql.substring(createViewIndex)+ : stmt.sql;++ const viewMatch = sqlFromCreate.match(+ /CREATE\s+(?:OR\s+REPLACE\s+)?VIEW\s+(?:(?:"([^"]+)"|([^"\s.]+))\.)?(?:"([^"]+)"|([^"\s.(]+))/i+ );++ if (viewMatch) {+ const schemaName = viewMatch[1] || viewMatch[2] || 'public';+ const viewName = viewMatch[3] || viewMatch[4];+ const viewKey = `${schemaName}.${viewName}`;+ const viewId = tableMap[viewKey];++ if (viewId) {+ // Extract columns from the view definition+ const columns = extractColumnsFromView(stmt.sql);++ // Create view object (as a table with isView: true)+ const view: SQLTable = {+ id: viewId,+ name: viewName,+ schema: schemaName,+ columns,+ indexes: [], // Views don't have indexes+ order: tables.length,+ isView: true,+ };++ tables.push(view);+ }+ }+ }+ }+// Fourth pass: process ALTER TABLE statements for foreign keys and ADD COLUMNfor (const stmt of statements) {if (stmt.type === 'alter' && stmt.parsed) {diff --git a/src/lib/data/sql-import/dialect-importers/sqlserver/__tests__/sqlserver-views.test.ts b/src/lib/data/sql-import/dialect-importers/sqlserver/__tests__/sqlserver-views.test.tsnew file mode 100644index 00000000..7622a8e5--- /dev/null+++ b/src/lib/data/sql-import/dialect-importers/sqlserver/__tests__/sqlserver-views.test.ts@@ -0,0 +1,353 @@+import { describe, it, expect } from 'vitest';+import { fromSQLServer } from '../sqlserver';++const SQL_WITH_VIEWS = `+/* SQL Server (T-SQL) simple, "flat" schema: 12 tables + 6 views+ No dynamic EXEC, just straightforward CREATE statements.+*/++CREATE SCHEMA demo;+GO++-- ----------------+-- Tables (12)+-- ----------------++CREATE TABLE demo.organizations (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_organizations PRIMARY KEY,+ name nvarchar(200) NOT NULL,+ slug nvarchar(80) NOT NULL,+ plan_tier nvarchar(30) NOT NULL CONSTRAINT DF_organizations_plan_tier DEFAULT ('free'),+ created_at datetime2(3) NOT NULL CONSTRAINT DF_organizations_created_at DEFAULT (SYSUTCDATETIME()),+ CONSTRAINT UQ_organizations_slug UNIQUE (slug)+);+GO++CREATE TABLE demo.users (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_users PRIMARY KEY,+ email nvarchar(320) NOT NULL,+ full_name nvarchar(200) NULL,+ is_active bit NOT NULL CONSTRAINT DF_users_is_active DEFAULT (1),+ created_at datetime2(3) NOT NULL CONSTRAINT DF_users_created_at DEFAULT (SYSUTCDATETIME())+);+GO++CREATE TABLE demo.org_memberships (+ org_id bigint NOT NULL,+ user_id bigint NOT NULL,+ role nvarchar(30) NOT NULL CONSTRAINT DF_org_memberships_role DEFAULT ('member'),+ joined_at datetime2(3) NOT NULL CONSTRAINT DF_org_memberships_joined_at DEFAULT (SYSUTCDATETIME()),+ CONSTRAINT PK_org_memberships PRIMARY KEY (org_id, user_id),+ CONSTRAINT FK_org_memberships_org FOREIGN KEY (org_id) REFERENCES demo.organizations(id) ON DELETE CASCADE,+ CONSTRAINT FK_org_memberships_user FOREIGN KEY (user_id) REFERENCES demo.users(id) ON DELETE CASCADE+);+GO++CREATE TABLE demo.projects (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_projects PRIMARY KEY,+ org_id bigint NOT NULL,+ name nvarchar(200) NOT NULL,+ [key] nvarchar(20) NOT NULL,+ is_archived bit NOT NULL CONSTRAINT DF_projects_is_archived DEFAULT (0),+ created_at datetime2(3) NOT NULL CONSTRAINT DF_projects_created_at DEFAULT (SYSUTCDATETIME()),+ CONSTRAINT FK_projects_org FOREIGN KEY (org_id) REFERENCES demo.organizations(id) ON DELETE CASCADE,+ CONSTRAINT UQ_projects_org_key UNIQUE (org_id, [key])+);+GO++CREATE TABLE demo.labels (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_labels PRIMARY KEY,+ org_id bigint NOT NULL,+ name nvarchar(80) NOT NULL,+ color nvarchar(20) NOT NULL CONSTRAINT DF_labels_color DEFAULT ('#999999'),+ CONSTRAINT FK_labels_org FOREIGN KEY (org_id) REFERENCES demo.organizations(id) ON DELETE CASCADE,+ CONSTRAINT UQ_labels_org_name UNIQUE (org_id, name)+);+GO++CREATE TABLE demo.issues (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_issues PRIMARY KEY,+ project_id bigint NOT NULL,+ title nvarchar(300) NOT NULL,+ [description] nvarchar(max) NULL,+ status nvarchar(30) NOT NULL CONSTRAINT DF_issues_status DEFAULT ('open'),+ priority int NOT NULL CONSTRAINT DF_issues_priority DEFAULT (3),+ created_by bigint NULL,+ created_at datetime2(3) NOT NULL CONSTRAINT DF_issues_created_at DEFAULT (SYSUTCDATETIME()),+ closed_at datetime2(3) NULL,+ CONSTRAINT FK_issues_project FOREIGN KEY (project_id) REFERENCES demo.projects(id) ON DELETE CASCADE,+ CONSTRAINT FK_issues_created_by FOREIGN KEY (created_by) REFERENCES demo.users(id) ON DELETE SET NULL,+ CONSTRAINT CK_issues_status CHECK (status IN ('open','in_progress','closed')),+ CONSTRAINT CK_issues_priority CHECK (priority BETWEEN 1 AND 5)+);+GO++CREATE TABLE demo.issue_labels (+ issue_id bigint NOT NULL,+ label_id bigint NOT NULL,+ CONSTRAINT PK_issue_labels PRIMARY KEY (issue_id, label_id),+ CONSTRAINT FK_issue_labels_issue FOREIGN KEY (issue_id) REFERENCES demo.issues(id) ON DELETE CASCADE,+ CONSTRAINT FK_issue_labels_label FOREIGN KEY (label_id) REFERENCES demo.labels(id) ON DELETE CASCADE+);+GO++CREATE TABLE demo.comments (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_comments PRIMARY KEY,+ issue_id bigint NOT NULL,+ author_id bigint NULL,+ body nvarchar(max) NOT NULL,+ created_at datetime2(3) NOT NULL CONSTRAINT DF_comments_created_at DEFAULT (SYSUTCDATETIME()),+ CONSTRAINT FK_comments_issue FOREIGN KEY (issue_id) REFERENCES demo.issues(id) ON DELETE CASCADE,+ CONSTRAINT FK_comments_author FOREIGN KEY (author_id) REFERENCES demo.users(id) ON DELETE SET NULL+);+GO++CREATE TABLE demo.api_keys (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_api_keys PRIMARY KEY,+ org_id bigint NOT NULL,+ name nvarchar(120) NOT NULL,+ key_hash nvarchar(200) NOT NULL,+ last_used_at datetime2(3) NULL,+ created_at datetime2(3) NOT NULL CONSTRAINT DF_api_keys_created_at DEFAULT (SYSUTCDATETIME()),+ CONSTRAINT FK_api_keys_org FOREIGN KEY (org_id) REFERENCES demo.organizations(id) ON DELETE CASCADE,+ CONSTRAINT UQ_api_keys_org_name UNIQUE (org_id, name)+);+GO++CREATE TABLE demo.invoices (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_invoices PRIMARY KEY,+ org_id bigint NOT NULL,+ period_start date NOT NULL,+ period_end date NOT NULL,+ status nvarchar(30) NOT NULL CONSTRAINT DF_invoices_status DEFAULT ('open'),+ total_cents int NOT NULL CONSTRAINT DF_invoices_total_cents DEFAULT (0),+ created_at datetime2(3) NOT NULL CONSTRAINT DF_invoices_created_at DEFAULT (SYSUTCDATETIME()),+ CONSTRAINT FK_invoices_org FOREIGN KEY (org_id) REFERENCES demo.organizations(id) ON DELETE CASCADE,+ CONSTRAINT CK_invoices_period CHECK (period_end >= period_start),+ CONSTRAINT CK_invoices_status CHECK (status IN ('open','paid','void')),+ CONSTRAINT CK_invoices_total_cents CHECK (total_cents >= 0)+);+GO++CREATE TABLE demo.payments (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_payments PRIMARY KEY,+ invoice_id bigint NOT NULL,+ provider nvarchar(30) NOT NULL,+ amount_cents int NOT NULL,+ paid_at datetime2(3) NOT NULL CONSTRAINT DF_payments_paid_at DEFAULT (SYSUTCDATETIME()),+ CONSTRAINT FK_payments_invoice FOREIGN KEY (invoice_id) REFERENCES demo.invoices(id) ON DELETE CASCADE,+ CONSTRAINT CK_payments_amount CHECK (amount_cents > 0)+);+GO++CREATE TABLE demo.events (+ id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_events PRIMARY KEY,+ org_id bigint NOT NULL,+ user_id bigint NULL,+ event_type nvarchar(80) NOT NULL,+ metadata nvarchar(max) NOT NULL CONSTRAINT DF_events_metadata DEFAULT (N'{}'),+ occurred_at datetime2(3) NOT NULL CONSTRAINT DF_events_occurred_at DEFAULT (SYSUTCDATETIME()),+ CONSTRAINT FK_events_org FOREIGN KEY (org_id) REFERENCES demo.organizations(id) ON DELETE CASCADE,+ CONSTRAINT FK_events_user FOREIGN KEY (user_id) REFERENCES demo.users(id) ON DELETE SET NULL,+ CONSTRAINT CK_events_metadata_isjson CHECK (ISJSON(metadata) = 1)+);+GO++-- ----------------+-- Views (6)+-- ----------------++CREATE VIEW demo.v_active_org_members AS+SELECT+ o.id AS org_id,+ o.slug AS org_slug,+ u.id AS user_id,+ u.email,+ u.full_name,+ m.role,+ m.joined_at+FROM demo.org_memberships m+JOIN demo.organizations o ON o.id = m.org_id+JOIN demo.users u ON u.id = m.user_id+WHERE u.is_active = 1;+GO++CREATE VIEW demo.v_project_issue_summary AS+SELECT+ p.id AS project_id,+ p.org_id,+ p.name AS project_name,+ p.[key] AS project_key,+ SUM(CASE WHEN i.status = 'open' THEN 1 ELSE 0 END) AS open_issues,+ SUM(CASE WHEN i.status = 'in_progress' THEN 1 ELSE 0 END) AS in_progress_issues,+ SUM(CASE WHEN i.status = 'closed' THEN 1 ELSE 0 END) AS closed_issues,+ COUNT(i.id) AS total_issues,+ MAX(i.created_at) AS last_issue_created_at+FROM demo.projects p+LEFT JOIN demo.issues i ON i.project_id = p.id+GROUP BY p.id, p.org_id, p.name, p.[key];+GO++CREATE VIEW demo.v_issue_details AS+SELECT+ i.id AS issue_id,+ i.project_id,+ p.org_id,+ i.title,+ i.status,+ i.priority,+ i.created_at,+ i.closed_at,+ i.created_by,+ u.email AS created_by_email,+ (SELECT COUNT(*) FROM demo.comments c WHERE c.issue_id = i.id) AS comment_count+FROM demo.issues i+JOIN demo.projects p ON p.id = i.project_id+LEFT JOIN demo.users u ON u.id = i.created_by;+GO++CREATE VIEW demo.v_invoice_balances AS+SELECT+ inv.id AS invoice_id,+ inv.org_id,+ inv.period_start,+ inv.period_end,+ inv.status,+ inv.total_cents,+ ISNULL(SUM(pay.amount_cents), 0) AS paid_cents,+ CASE+ WHEN inv.total_cents - ISNULL(SUM(pay.amount_cents), 0) < 0 THEN 0+ ELSE inv.total_cents - ISNULL(SUM(pay.amount_cents), 0)+ END AS due_cents,+ MAX(pay.paid_at) AS last_payment_at+FROM demo.invoices inv+LEFT JOIN demo.payments pay ON pay.invoice_id = inv.id+GROUP BY inv.id, inv.org_id, inv.period_start, inv.period_end, inv.status, inv.total_cents;+GO++CREATE VIEW demo.v_recent_events AS+SELECT+ e.id,+ e.org_id,+ o.slug AS org_slug,+ e.user_id,+ u.email AS user_email,+ e.event_type,+ e.metadata,+ e.occurred_at+FROM demo.events e+JOIN demo.organizations o ON o.id = e.org_id+LEFT JOIN demo.users u ON u.id = e.user_id+WHERE e.occurred_at >= DATEADD(day, -7, SYSUTCDATETIME());+GO++CREATE VIEW demo.v_org_activity_daily AS+SELECT+ e.org_id,+ CAST(e.occurred_at AS date) AS [day],+ COUNT(*) AS events_count,+ SUM(CASE WHEN e.event_type = 'login' THEN 1 ELSE 0 END) AS logins+FROM demo.events e+WHERE e.occurred_at >= DATEADD(day, -30, SYSUTCDATETIME())+GROUP BY e.org_id, CAST(e.occurred_at AS date);+GO+`;++describe('SQL Server View Import', () => {+ it('should import 12 tables and 6 views', async () => {+ const result = await fromSQLServer(SQL_WITH_VIEWS);++ // Count tables and views+ const tables = result.tables.filter((t) => !t.isView);+ const views = result.tables.filter((t) => t.isView);++ expect(tables.length).toBe(12);+ expect(views.length).toBe(6);+ });++ it('should correctly parse view names and schemas', async () => {+ const result = await fromSQLServer(SQL_WITH_VIEWS);++ const views = result.tables.filter((t) => t.isView);+ const viewNames = views.map((v) => v.name).sort();++ expect(viewNames).toEqual([+ 'v_active_org_members',+ 'v_invoice_balances',+ 'v_issue_details',+ 'v_org_activity_daily',+ 'v_project_issue_summary',+ 'v_recent_events',+ ]);++ // All views should be in the 'demo' schema+ views.forEach((view) => {+ expect(view.schema).toBe('demo');+ });+ });++ it('should correctly parse table names and schemas', async () => {+ const result = await fromSQLServer(SQL_WITH_VIEWS);++ const tables = result.tables.filter((t) => !t.isView);+ const tableNames = tables.map((t) => t.name).sort();++ expect(tableNames).toEqual([+ 'api_keys',+ 'comments',+ 'events',+ 'invoices',+ 'issue_labels',+ 'issues',+ 'labels',+ 'org_memberships',+ 'organizations',+ 'payments',+ 'projects',+ 'users',+ ]);++ // All tables should be in the 'demo' schema+ tables.forEach((table) => {+ expect(table.schema).toBe('demo');+ });+ });++ it('should extract columns from views', async () => {+ const result = await fromSQLServer(SQL_WITH_VIEWS);++ const activeOrgMembersView = result.tables.find(+ (t) => t.name === 'v_active_org_members'+ );++ expect(activeOrgMembersView).toBeDefined();+ expect(activeOrgMembersView?.isView).toBe(true);++ // Check that columns were extracted from the SELECT clause+ const columnNames = activeOrgMembersView?.columns.map((c) => c.name);+ expect(columnNames).toContain('org_id');+ expect(columnNames).toContain('org_slug');+ expect(columnNames).toContain('user_id');+ expect(columnNames).toContain('email');+ expect(columnNames).toContain('full_name');+ expect(columnNames).toContain('role');+ expect(columnNames).toContain('joined_at');+ });++ it('should parse relationships correctly', async () => {+ const result = await fromSQLServer(SQL_WITH_VIEWS);++ // Check that foreign key relationships are parsed+ expect(result.relationships.length).toBeGreaterThan(0);++ // Check for specific relationships+ const projectsOrgFk = result.relationships.find(+ (r) =>+ r.sourceTable === 'projects' &&+ r.targetTable === 'organizations'+ );+ expect(projectsOrgFk).toBeDefined();+ expect(projectsOrgFk?.sourceColumn).toBe('org_id');+ expect(projectsOrgFk?.targetColumn).toBe('id');+ });+});diff --git a/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver.ts b/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver.tsindex 7b49b5ad..013e098c 100644--- a/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver.ts+++ b/src/lib/data/sql-import/dialect-importers/sqlserver/sqlserver.ts@@ -20,6 +20,102 @@ import {findTableWithSchemaSupport,} from './sqlserver-common';+/**+ * Extract columns from a CREATE VIEW statement+ * Views can have explicit column names or derive them from the SELECT+ */+function extractColumnsFromView(sql: string): SQLColumn[] {+ const columns: SQLColumn[] = [];++ // First, try to extract explicit column list from CREATE VIEW viewname (col1, col2, ...) AS+ const explicitColumnsMatch = sql.match(+ /CREATE\s+(?:OR\s+ALTER\s+)?VIEW\s+(?:\[?[^\]]+\]?\.)?(?:\[?[^\]]+\]?)\s*\(([^)]+)\)\s*(?:WITH\s+[^)]+\s*)?AS/i+ );++ if (explicitColumnsMatch) {+ // Parse explicit column list+ const columnList = explicitColumnsMatch[1];+ const columnNames = columnList+ .split(',')+ .map((col) => col.trim().replace(/^\[|\]$/g, ''));++ for (const colName of columnNames) {+ if (colName) {+ columns.push({+ name: colName,+ type: 'nvarchar', // Default type for views+ nullable: true,+ primaryKey: false,+ unique: false,+ });+ }+ }++ return columns;+ }++ // If no explicit columns, try to extract from SELECT clause+ const selectMatch = sql.match(/\bAS\s+SELECT\s+([\s\S]+?)\s+FROM\s+/i);++ if (selectMatch) {+ const selectClause = selectMatch[1];++ // Handle SELECT * - we can't determine columns+ if (selectClause.trim() === '*') {+ return columns;+ }++ // Split by comma, but be careful of nested functions/expressions+ let depth = 0;+ let currentCol = '';+ const selectParts: string[] = [];++ for (const char of selectClause) {+ if (char === '(' || char === '[') depth++;+ else if (char === ')' || char === ']') depth--;+ else if (char === ',' && depth === 0) {+ selectParts.push(currentCol.trim());+ currentCol = '';+ continue;+ }+ currentCol += char;+ }+ if (currentCol.trim()) {+ selectParts.push(currentCol.trim());+ }++ for (const part of selectParts) {+ let columnName = '';++ // Check for alias: ... AS [name] or ... AS name+ const aliasMatch = part.match(/\s+AS\s+\[?(\w+)\]?\s*$/i);+ if (aliasMatch) {+ columnName = aliasMatch[1];+ } else {+ // Try to extract the column reference+ const colRefMatch = part.match(+ /(?:[\w[\]]+\.)?\[?(\w+)\]?\s*$/+ );+ if (colRefMatch) {+ columnName = colRefMatch[1];+ }+ }++ if (columnName && columnName !== '*') {+ columns.push({+ name: columnName,+ type: 'nvarchar',+ nullable: true,+ primaryKey: false,+ unique: false,+ });+ }+ }+ }++ return columns;+}+/*** Preprocess SQL Server script to remove or modify parts that the parser can't handle*/@@ -688,6 +784,50 @@ export async function fromSQLServer(parseCreateTableManually(stmt, tables, tableMap, relationships);}+ // Parse CREATE VIEW statements+ const createViewStatements = statements.filter((stmt) => {+ const upperStmt = stmt.trim().toUpperCase();+ return (+ upperStmt.includes('CREATE VIEW') ||+ upperStmt.includes('CREATE OR ALTER VIEW')+ );+ });++ for (const stmt of createViewStatements) {+ // Extract view name and schema+ // Handle: CREATE VIEW schema.viewname, CREATE VIEW [schema].[viewname], etc.+ const viewMatch = stmt.match(+ /CREATE\s+(?:OR\s+ALTER\s+)?VIEW\s+(?:\[?(\w+)\]?\.)?\[?(\w+)\]?/i+ );++ if (viewMatch) {+ // If there's a dot in the view reference, group 1 is schema, group 2 is name+ // Otherwise, group 1 is undefined and group 2 is the name+ const schema = viewMatch[1] || 'dbo';+ const viewName = viewMatch[2];++ if (viewName) {+ const viewId = generateId();+ const viewKey = `${schema}.${viewName}`;+ tableMap[viewKey] = viewId;++ // Extract columns from the view definition+ const columns = extractColumnsFromView(stmt);++ // Create view object (as a table with isView: true)+ tables.push({+ id: viewId,+ name: viewName,+ schema: schema,+ columns,+ indexes: [], // Views don't have indexes+ order: tables.length,+ isView: true,+ });+ }+ }+ }+// Preprocess the SQL content for node-sql-parserconst preprocessedSQL = preprocessSQLServerScript(sqlContent);diff --git a/src/lib/data/sql-import/validators/mysql-validator.ts b/src/lib/data/sql-import/validators/mysql-validator.tsindex 9e83341a..4b4b1f8b 100644--- a/src/lib/data/sql-import/validators/mysql-validator.ts+++ b/src/lib/data/sql-import/validators/mysql-validator.ts@@ -40,6 +40,7 @@ export function validateMySQLDialect(sql: string): ValidationResult {// Check for common MySQL syntax patternsconst lines = sql.split('\n');let tableCount = 0;+ let viewCount = 0;lines.forEach((line, index) => {const trimmedLine = line.trim();@@ -49,6 +50,11 @@ export function validateMySQLDialect(sql: string): ValidationResult {tableCount++;}+ // Count CREATE VIEW statements+ if (trimmedLine.match(/^\s*CREATE\s+(OR\s+REPLACE\s+)?VIEW/i)) {+ viewCount++;+ }+// Check for PostgreSQL-specific syntax that won't work in MySQLif (trimmedLine.includes('SERIAL')) {warnings.push({@@ -65,10 +71,26 @@ export function validateMySQLDialect(sql: string): ValidationResult {}});+ // Add import summary message+ if (tableCount > 0 || viewCount > 0) {+ const parts: string[] = [];+ if (tableCount > 0) {+ parts.push(`${tableCount} table${tableCount !== 1 ? 's' : ''}`);+ }+ if (viewCount > 0) {+ parts.push(`${viewCount} view${viewCount !== 1 ? 's' : ''}`);+ }+ warnings.unshift({+ message: `Found ${parts.join(' and ')} to import.`,+ type: 'compatibility',+ });+ }+return {isValid: errors.length === 0,errors,warnings,tableCount,+ viewCount,};}diff --git a/src/lib/data/sql-import/validators/postgresql-validator.ts b/src/lib/data/sql-import/validators/postgresql-validator.tsindex b0bf3eeb..4fad9b85 100644--- a/src/lib/data/sql-import/validators/postgresql-validator.ts+++ b/src/lib/data/sql-import/validators/postgresql-validator.ts@@ -9,6 +9,7 @@ export interface ValidationResult {warnings: ValidationWarning[];fixedSQL?: string;tableCount?: number;+ viewCount?: number;relationshipCount?: number;}@@ -151,13 +152,7 @@ export function validatePostgreSQLDialect(sql: string): ValidationResult {});}- // 5. Check for views- if (/CREATE\s+(OR\s+REPLACE\s+)?VIEW/i.test(sql)) {- warnings.push({- message: `View definitions found. These will not be imported.`,- type: 'compatibility',- });- }+ // 5. Views are now supported - we'll add a message with counts later// 6. Attempt to auto-fix common issueslet hasAutoFixes = false;@@ -217,9 +212,33 @@ export function validatePostgreSQLDialect(sql: string): ValidationResult {let tableCount = 0;const createTableRegex =/CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?(?:\s+ONLY)?\s+(?:"?[^"\s.]+?"?\.)?["'`]?[^"'`\s.(]+["'`]?/gi;- const matches = sql.match(createTableRegex);- if (matches) {- tableCount = matches.length;+ const tableMatches = sql.match(createTableRegex);+ if (tableMatches) {+ tableCount = tableMatches.length;+ }++ // 10. Count CREATE VIEW statements+ let viewCount = 0;+ const createViewRegex =+ /CREATE\s+(?:OR\s+REPLACE\s+)?VIEW\s+(?:"?[^"\s.]+?"?\.)?["'`]?[^"'`\s.(]+["'`]?/gi;+ const viewMatches = sql.match(createViewRegex);+ if (viewMatches) {+ viewCount = viewMatches.length;+ }++ // 11. Add import summary message+ if (tableCount > 0 || viewCount > 0) {+ const parts: string[] = [];+ if (tableCount > 0) {+ parts.push(`${tableCount} table${tableCount !== 1 ? 's' : ''}`);+ }+ if (viewCount > 0) {+ parts.push(`${viewCount} view${viewCount !== 1 ? 's' : ''}`);+ }+ warnings.unshift({+ message: `Found ${parts.join(' and ')} to import.`,+ type: 'compatibility',+ });}return {@@ -228,6 +247,7 @@ export function validatePostgreSQLDialect(sql: string): ValidationResult {warnings,fixedSQL: hasAutoFixes && fixedSQL !== sql ? fixedSQL : undefined,tableCount,+ viewCount,};}diff --git a/src/lib/data/sql-import/validators/sqlserver-validator.ts b/src/lib/data/sql-import/validators/sqlserver-validator.tsindex dc2f38dc..2ccf33ab 100644--- a/src/lib/data/sql-import/validators/sqlserver-validator.ts+++ b/src/lib/data/sql-import/validators/sqlserver-validator.ts@@ -40,6 +40,7 @@ export function validateSQLServerDialect(sql: string): ValidationResult {// Check for common SQL Server syntax patternsconst lines = sql.split('\n');let tableCount = 0;+ let viewCount = 0;lines.forEach((line, index) => {const trimmedLine = line.trim();@@ -49,6 +50,11 @@ export function validateSQLServerDialect(sql: string): ValidationResult {tableCount++;}+ // Count CREATE VIEW statements+ if (trimmedLine.match(/^\s*CREATE\s+(OR\s+ALTER\s+)?VIEW/i)) {+ viewCount++;+ }+// Check for syntax from other databases that won't work in SQL Serverif (trimmedLine.includes('AUTO_INCREMENT')) {warnings.push({@@ -65,10 +71,26 @@ export function validateSQLServerDialect(sql: string): ValidationResult {}});+ // Add import summary message+ if (tableCount > 0 || viewCount > 0) {+ const parts: string[] = [];+ if (tableCount > 0) {+ parts.push(`${tableCount} table${tableCount !== 1 ? 's' : ''}`);+ }+ if (viewCount > 0) {+ parts.push(`${viewCount} view${viewCount !== 1 ? 's' : ''}`);+ }+ warnings.unshift({+ message: `Found ${parts.join(' and ')} to import.`,+ type: 'compatibility',+ });+ }+return {isValid: errors.length === 0,errors,warnings,tableCount,+ viewCount,};}