Skip to content

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

fix: add support for ALTER TABLE ADD COLUMN in PostgreSQL importer (#892)

Guy Ben-Aharonec6e46fe81ea
diff --git a/src/lib/data/sql-import/dialect-importers/postgresql/__tests__/postgresql-alter-add-column.test.ts b/src/lib/data/sql-import/dialect-importers/postgresql/__tests__/postgresql-alter-add-column.test.ts
new file mode 100644
index 00000000..183e938b
--- /dev/null
+++ b/src/lib/data/sql-import/dialect-importers/postgresql/__tests__/postgresql-alter-add-column.test.ts
@@ -0,0 +1,215 @@
+import { describe, it, expect } from 'vitest';
+import { fromPostgres } from '../postgresql';
+
+describe('PostgreSQL ALTER TABLE ADD COLUMN Tests', () => {
+ it('should handle ALTER TABLE ADD COLUMN statements', async () => {
+ const sql = `
+ CREATE SCHEMA IF NOT EXISTS "public";
+
+ CREATE TABLE "public"."location" (
+ "id" bigint NOT NULL,
+ CONSTRAINT "pk_table_7_id" PRIMARY KEY ("id")
+ );
+
+ -- Add new fields to existing location table
+ ALTER TABLE location ADD COLUMN country_id INT;
+ ALTER TABLE location ADD COLUMN state_id INT;
+ ALTER TABLE location ADD COLUMN location_type_id INT;
+ ALTER TABLE location ADD COLUMN city_id INT;
+ ALTER TABLE location ADD COLUMN street TEXT;
+ ALTER TABLE location ADD COLUMN block TEXT;
+ ALTER TABLE location ADD COLUMN building TEXT;
+ ALTER TABLE location ADD COLUMN floor TEXT;
+ ALTER TABLE location ADD COLUMN apartment TEXT;
+ ALTER TABLE location ADD COLUMN lat INT;
+ ALTER TABLE location ADD COLUMN long INT;
+ ALTER TABLE location ADD COLUMN elevation INT;
+ ALTER TABLE location ADD COLUMN erp_site_id INT;
+ ALTER TABLE location ADD COLUMN is_active TEXT;
+ ALTER TABLE location ADD COLUMN remarks TEXT;
+ `;
+
+ const result = await fromPostgres(sql);
+
+ expect(result.tables).toHaveLength(1);
+ const locationTable = result.tables[0];
+
+ expect(locationTable.name).toBe('location');
+ expect(locationTable.schema).toBe('public');
+
+ // Should have the original id column plus all the added columns
+ expect(locationTable.columns).toHaveLength(16);
+
+ // Check that the id column is present
+ const idColumn = locationTable.columns.find((col) => col.name === 'id');
+ expect(idColumn).toBeDefined();
+ expect(idColumn?.type).toBe('BIGINT');
+ expect(idColumn?.primaryKey).toBe(true);
+
+ // Check some of the added columns
+ const countryIdColumn = locationTable.columns.find(
+ (col) => col.name === 'country_id'
+ );
+ expect(countryIdColumn).toBeDefined();
+ expect(countryIdColumn?.type).toBe('INTEGER');
+
+ const streetColumn = locationTable.columns.find(
+ (col) => col.name === 'street'
+ );
+ expect(streetColumn).toBeDefined();
+ expect(streetColumn?.type).toBe('TEXT');
+
+ const remarksColumn = locationTable.columns.find(
+ (col) => col.name === 'remarks'
+ );
+ expect(remarksColumn).toBeDefined();
+ expect(remarksColumn?.type).toBe('TEXT');
+ });
+
+ it('should handle ALTER TABLE ADD COLUMN with schema qualification', async () => {
+ const sql = `
+ CREATE TABLE public.users (
+ id INTEGER PRIMARY KEY
+ );
+
+ ALTER TABLE public.users ADD COLUMN email VARCHAR(255);
+ ALTER TABLE public.users ADD COLUMN created_at TIMESTAMP;
+ `;
+
+ const result = await fromPostgres(sql);
+
+ expect(result.tables).toHaveLength(1);
+ const usersTable = result.tables[0];
+
+ expect(usersTable.columns).toHaveLength(3);
+
+ const emailColumn = usersTable.columns.find(
+ (col) => col.name === 'email'
+ );
+ expect(emailColumn).toBeDefined();
+ expect(emailColumn?.type).toBe('VARCHAR(255)');
+
+ const createdAtColumn = usersTable.columns.find(
+ (col) => col.name === 'created_at'
+ );
+ expect(createdAtColumn).toBeDefined();
+ expect(createdAtColumn?.type).toBe('TIMESTAMP');
+ });
+
+ it('should handle ALTER TABLE ADD COLUMN with constraints', async () => {
+ const sql = `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY
+ );
+
+ ALTER TABLE products ADD COLUMN name VARCHAR(100) NOT NULL;
+ ALTER TABLE products ADD COLUMN sku VARCHAR(50) UNIQUE;
+ ALTER TABLE products ADD COLUMN price DECIMAL(10,2) DEFAULT 0.00;
+ `;
+
+ const result = await fromPostgres(sql);
+
+ expect(result.tables).toHaveLength(1);
+ const productsTable = result.tables[0];
+
+ expect(productsTable.columns).toHaveLength(4);
+
+ const nameColumn = productsTable.columns.find(
+ (col) => col.name === 'name'
+ );
+ expect(nameColumn).toBeDefined();
+ expect(nameColumn?.nullable).toBe(false);
+
+ const skuColumn = productsTable.columns.find(
+ (col) => col.name === 'sku'
+ );
+ expect(skuColumn).toBeDefined();
+ expect(skuColumn?.unique).toBe(true);
+
+ const priceColumn = productsTable.columns.find(
+ (col) => col.name === 'price'
+ );
+ expect(priceColumn).toBeDefined();
+ expect(priceColumn?.default).toBe('has default');
+ });
+
+ it('should not add duplicate columns', async () => {
+ const sql = `
+ CREATE TABLE items (
+ id INTEGER PRIMARY KEY,
+ name VARCHAR(100)
+ );
+
+ ALTER TABLE items ADD COLUMN description TEXT;
+ ALTER TABLE items ADD COLUMN name VARCHAR(200); -- Should not be added as duplicate
+ `;
+
+ const result = await fromPostgres(sql);
+
+ expect(result.tables).toHaveLength(1);
+ const itemsTable = result.tables[0];
+
+ // Should only have 3 columns: id, name (original), and description
+ expect(itemsTable.columns).toHaveLength(3);
+
+ const nameColumns = itemsTable.columns.filter(
+ (col) => col.name === 'name'
+ );
+ expect(nameColumns).toHaveLength(1);
+ expect(nameColumns[0].type).toBe('VARCHAR(100)'); // Should keep original type
+ });
+
+ it('should use default schema when not specified', async () => {
+ const sql = `
+ CREATE TABLE test_table (
+ id INTEGER PRIMARY KEY
+ );
+
+ ALTER TABLE test_table ADD COLUMN value TEXT;
+ `;
+
+ const result = await fromPostgres(sql);
+
+ expect(result.tables).toHaveLength(1);
+ const testTable = result.tables[0];
+
+ expect(testTable.schema).toBe('public');
+ expect(testTable.columns).toHaveLength(2);
+
+ const valueColumn = testTable.columns.find(
+ (col) => col.name === 'value'
+ );
+ expect(valueColumn).toBeDefined();
+ });
+
+ it('should handle quoted identifiers in ALTER TABLE ADD COLUMN', async () => {
+ const sql = `
+ CREATE TABLE "my-table" (
+ "id" INTEGER PRIMARY KEY
+ );
+
+ ALTER TABLE "my-table" ADD COLUMN "my-column" VARCHAR(50);
+ ALTER TABLE "my-table" ADD COLUMN "another-column" INTEGER;
+ `;
+
+ const result = await fromPostgres(sql);
+
+ expect(result.tables).toHaveLength(1);
+ const myTable = result.tables[0];
+
+ expect(myTable.name).toBe('my-table');
+ expect(myTable.columns).toHaveLength(3);
+
+ const myColumn = myTable.columns.find(
+ (col) => col.name === 'my-column'
+ );
+ expect(myColumn).toBeDefined();
+ expect(myColumn?.type).toBe('VARCHAR(50)');
+
+ const anotherColumn = myTable.columns.find(
+ (col) => col.name === 'another-column'
+ );
+ expect(anotherColumn).toBeDefined();
+ expect(anotherColumn?.type).toBe('INTEGER');
+ });
+});
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
index 51654941..32d55d19 100644
--- a/src/lib/data/sql-import/dialect-importers/postgresql/postgresql-common.ts
+++ b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql-common.ts
@@ -92,6 +92,36 @@ export interface AlterTableExprItem {
resource?: string;
type?: string;
constraint?: { constraint_type?: string };
+ // Properties for ADD COLUMN
+ column?:
+ | {
+ column?:
+ | {
+ expr?: {
+ value?: string;
+ };
+ }
+ | string;
+ }
+ | string
+ | ColumnReference;
+ definition?: {
+ dataType?: string;
+ length?: number;
+ precision?: number;
+ scale?: number;
+ suffix?: unknown[];
+ nullable?: { type: string };
+ unique?: string;
+ primary_key?: string;
+ constraint?: string;
+ default_val?: unknown;
+ auto_increment?: string;
+ };
+ nullable?: { type: string; value?: string };
+ unique?: string;
+ default_val?: unknown;
+ // Properties for constraints
create_definitions?:
| AlterTableConstraintDefinition
| {
diff --git a/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts
index 75a44b2a..45ebb61a 100644
--- a/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts
+++ b/src/lib/data/sql-import/dialect-importers/postgresql/postgresql.ts
@@ -996,7 +996,7 @@ export async function fromPostgres(
}
}
- // Fourth pass: process ALTER TABLE statements for foreign keys
+ // Fourth pass: process ALTER TABLE statements for foreign keys and ADD COLUMN
for (const stmt of statements) {
if (stmt.type === 'alter' && stmt.parsed) {
const alterTableStmt = stmt.parsed as AlterTableStatement;
@@ -1026,13 +1026,326 @@ export async function fromPostgres(
);
if (!table) continue;
- // Process foreign key constraints in ALTER TABLE
+ // Process ALTER TABLE expressions
if (alterTableStmt.expr && Array.isArray(alterTableStmt.expr)) {
alterTableStmt.expr.forEach((expr: AlterTableExprItem) => {
- if (expr.action === 'add' && expr.create_definitions) {
+ // Handle both 'add' action and 'add column' type
+ if (expr.action === 'add' && expr.resource === 'column') {
+ // Handle ADD COLUMN directly from expr structure
+ // Extract column name from the nested structure
+ let columnName: string | undefined;
+ if (
+ typeof expr.column === 'object' &&
+ 'column' in expr.column
+ ) {
+ const innerColumn = expr.column.column;
+ if (
+ typeof innerColumn === 'object' &&
+ 'expr' in innerColumn &&
+ innerColumn.expr?.value
+ ) {
+ columnName = innerColumn.expr.value;
+ } else if (typeof innerColumn === 'string') {
+ columnName = innerColumn;
+ }
+ } else if (typeof expr.column === 'string') {
+ columnName = expr.column;
+ }
+
+ if (columnName && typeof columnName === 'string') {
+ const definition = expr.definition || {};
+ const rawDataType = String(
+ definition?.dataType || 'TEXT'
+ );
+ // console.log('expr:', JSON.stringify(expr, null, 2));
+
+ // Normalize the type
+ let normalizedBaseType =
+ normalizePostgreSQLType(rawDataType);
+
+ // Check if it's a serial type
+ const upperType = rawDataType.toUpperCase();
+ const isSerialType = [
+ 'SERIAL',
+ 'SERIAL2',
+ 'SERIAL4',
+ 'SERIAL8',
+ 'BIGSERIAL',
+ 'SMALLSERIAL',
+ ].includes(upperType.split('(')[0]);
+
+ if (isSerialType) {
+ const typeLength = definition?.length as
+ | number
+ | undefined;
+ if (upperType === 'SERIAL') {
+ if (typeLength === 2) {
+ normalizedBaseType = 'SMALLINT';
+ } else if (typeLength === 8) {
+ normalizedBaseType = 'BIGINT';
+ } else {
+ normalizedBaseType = 'INTEGER';
+ }
+ }
+ }
+
+ // Handle type parameters
+ let finalDataType = normalizedBaseType;
+ const isNormalizedIntegerType =
+ ['INTEGER', 'BIGINT', 'SMALLINT'].includes(
+ normalizedBaseType
+ ) &&
+ (upperType === 'INT' || upperType === 'SERIAL');
+
+ if (!isSerialType && !isNormalizedIntegerType) {
+ const precision = definition?.precision;
+ const scale = definition?.scale;
+ const length = definition?.length;
+ const suffix =
+ (definition?.suffix as unknown[]) || [];
+
+ if (suffix.length > 0) {
+ const params = suffix
+ .map((s: unknown) => {
+ if (
+ typeof s === 'object' &&
+ s !== null &&
+ 'value' in s
+ ) {
+ return String(
+ (s as { value: unknown })
+ .value
+ );
+ }
+ return String(s);
+ })
+ .join(',');
+ finalDataType = `${normalizedBaseType}(${params})`;
+ } else if (precision !== undefined) {
+ if (scale !== undefined) {
+ finalDataType = `${normalizedBaseType}(${precision},${scale})`;
+ } else {
+ finalDataType = `${normalizedBaseType}(${precision})`;
+ }
+ } else if (
+ length !== undefined &&
+ length !== null
+ ) {
+ finalDataType = `${normalizedBaseType}(${length})`;
+ }
+ }
+
+ // Check for nullable constraint
+ let nullable = true;
+ if (isSerialType) {
+ nullable = false;
+ } else if (
+ expr.nullable &&
+ expr.nullable.type === 'not null'
+ ) {
+ nullable = false;
+ } else if (
+ definition?.nullable &&
+ definition.nullable.type === 'not null'
+ ) {
+ nullable = false;
+ }
+
+ // Check for unique constraint
+ const isUnique =
+ expr.unique === 'unique' ||
+ definition?.unique === 'unique';
+
+ // Check for default value
+ const hasDefault =
+ expr.default_val || definition?.default_val;
+
+ // Create the new column object
+ const newColumn: SQLColumn = {
+ name: columnName,
+ type: finalDataType,
+ nullable: nullable,
+ primaryKey:
+ definition?.primary_key === 'primary key' ||
+ definition?.constraint === 'primary key' ||
+ isSerialType,
+ unique: isUnique,
+ default: isSerialType
+ ? undefined
+ : hasDefault
+ ? 'has default'
+ : undefined,
+ increment:
+ isSerialType ||
+ definition?.auto_increment ===
+ 'auto_increment' ||
+ (stmt.sql
+ .toUpperCase()
+ .includes('GENERATED') &&
+ stmt.sql
+ .toUpperCase()
+ .includes('IDENTITY')),
+ };
+
+ // Add the column to the table if it doesn't already exist
+ const tableColumns = table.columns as SQLColumn[];
+ if (
+ !tableColumns.some(
+ (col) => col.name === columnName
+ )
+ ) {
+ tableColumns.push(newColumn);
+ }
+ }
+ } else if (
+ expr.action === 'add' &&
+ expr.create_definitions
+ ) {
const createDefs = expr.create_definitions;
- if (
+ // Check if it's adding a column (legacy structure)
+ if (createDefs.resource === 'column') {
+ const columnDef =
+ createDefs as unknown as ColumnDefinition;
+ const columnName = extractColumnName(
+ columnDef.column
+ );
+
+ if (columnName) {
+ // Extract the column type and properties
+ const definition =
+ columnDef.definition as Record<
+ string,
+ unknown
+ >;
+ const rawDataType = String(
+ definition?.dataType || 'TEXT'
+ );
+
+ // Normalize the type
+ let normalizedBaseType =
+ normalizePostgreSQLType(rawDataType);
+
+ // Check if it's a serial type
+ const upperType = rawDataType.toUpperCase();
+ const isSerialType = [
+ 'SERIAL',
+ 'SERIAL2',
+ 'SERIAL4',
+ 'SERIAL8',
+ 'BIGSERIAL',
+ 'SMALLSERIAL',
+ ].includes(upperType.split('(')[0]);
+
+ if (isSerialType) {
+ const typeLength = definition?.length as
+ | number
+ | undefined;
+ if (upperType === 'SERIAL') {
+ if (typeLength === 2) {
+ normalizedBaseType = 'SMALLINT';
+ } else if (typeLength === 8) {
+ normalizedBaseType = 'BIGINT';
+ } else {
+ normalizedBaseType = 'INTEGER';
+ }
+ }
+ }
+
+ // Handle type parameters
+ let finalDataType = normalizedBaseType;
+ const isNormalizedIntegerType =
+ ['INTEGER', 'BIGINT', 'SMALLINT'].includes(
+ normalizedBaseType
+ ) &&
+ (upperType === 'INT' ||
+ upperType === 'SERIAL');
+
+ if (!isSerialType && !isNormalizedIntegerType) {
+ const precision =
+ columnDef.definition?.precision;
+ const scale = columnDef.definition?.scale;
+ const length = columnDef.definition?.length;
+ const suffix =
+ (definition?.suffix as unknown[]) || [];
+
+ if (suffix.length > 0) {
+ const params = suffix
+ .map((s: unknown) => {
+ if (
+ typeof s === 'object' &&
+ s !== null &&
+ 'value' in s
+ ) {
+ return String(
+ (
+ s as {
+ value: unknown;
+ }
+ ).value
+ );
+ }
+ return String(s);
+ })
+ .join(',');
+ finalDataType = `${normalizedBaseType}(${params})`;
+ } else if (precision !== undefined) {
+ if (scale !== undefined) {
+ finalDataType = `${normalizedBaseType}(${precision},${scale})`;
+ } else {
+ finalDataType = `${normalizedBaseType}(${precision})`;
+ }
+ } else if (
+ length !== undefined &&
+ length !== null
+ ) {
+ finalDataType = `${normalizedBaseType}(${length})`;
+ }
+ }
+
+ // Create the new column object
+ const newColumn: SQLColumn = {
+ name: columnName,
+ type: finalDataType,
+ nullable: isSerialType
+ ? false
+ : columnDef.nullable?.type !==
+ 'not null',
+ primaryKey:
+ columnDef.primary_key ===
+ 'primary key' ||
+ columnDef.definition?.constraint ===
+ 'primary key' ||
+ isSerialType,
+ unique: columnDef.unique === 'unique',
+ typeArgs: getTypeArgs(columnDef.definition),
+ default: isSerialType
+ ? undefined
+ : getDefaultValueString(columnDef),
+ increment:
+ isSerialType ||
+ columnDef.auto_increment ===
+ 'auto_increment' ||
+ (stmt.sql
+ .toUpperCase()
+ .includes('GENERATED') &&
+ stmt.sql
+ .toUpperCase()
+ .includes('IDENTITY')),
+ };
+
+ // Add the column to the table if it doesn't already exist
+ const tableColumns2 =
+ table.columns as SQLColumn[];
+ if (
+ !tableColumns2.some(
+ (col) => col.name === columnName
+ )
+ ) {
+ tableColumns2.push(newColumn);
+ }
+ }
+ } else if (
createDefs.constraint_type === 'FOREIGN KEY' ||
createDefs.constraint_type === 'foreign key'
) {
@@ -1143,6 +1456,56 @@ export async function fromPostgres(
}
} else if (stmt.type === 'alter' && !stmt.parsed) {
// Handle ALTER TABLE statements that failed to parse
+
+ // First try to extract ADD COLUMN statements
+ const alterColumnMatch = stmt.sql.match(
+ /ALTER\s+TABLE\s+(?:ONLY\s+)?(?:(?:"([^"]+)"|([^"\s.]+))\.)?(?:"([^"]+)"|([^"\s.(]+))\s+ADD\s+COLUMN\s+(?:"([^"]+)"|([^"\s]+))\s+([\w_]+(?:\([^)]*\))?(?:\[\])?)/i
+ );
+
+ if (alterColumnMatch) {
+ const schemaName =
+ alterColumnMatch[1] || alterColumnMatch[2] || 'public';
+ const tableName = alterColumnMatch[3] || alterColumnMatch[4];
+ const columnName = alterColumnMatch[5] || alterColumnMatch[6];
+ let columnType = alterColumnMatch[7];
+
+ const table = findTableWithSchemaSupport(
+ tables,
+ tableName,
+ schemaName
+ );
+ if (table && columnName) {
+ const tableColumns = table.columns as SQLColumn[];
+ if (!tableColumns.some((col) => col.name === columnName)) {
+ // Normalize the type
+ columnType = normalizePostgreSQLType(columnType);
+
+ // Check for constraints in the statement
+ const columnDefPart = stmt.sql.substring(
+ stmt.sql.indexOf(columnName)
+ );
+ const isPrimary =
+ columnDefPart.match(/PRIMARY\s+KEY/i) !== null;
+ const isNotNull =
+ columnDefPart.match(/NOT\s+NULL/i) !== null;
+ const isUnique =
+ columnDefPart.match(/\bUNIQUE\b/i) !== null;
+ const hasDefault =
+ columnDefPart.match(/DEFAULT\s+/i) !== null;
+
+ tableColumns.push({
+ name: columnName,
+ type: columnType,
+ nullable: !isNotNull && !isPrimary,
+ primaryKey: isPrimary,
+ unique: isUnique || isPrimary,
+ default: hasDefault ? 'has default' : undefined,
+ increment: false,
+ });
+ }
+ }
+ }
+
// Extract foreign keys using regex as fallback
// Updated regex to handle quoted identifiers properly
const alterFKMatch = stmt.sql.match(