Home / Class/ PgAlterTableAddColumnConvertor Class — drizzle-orm Architecture

PgAlterTableAddColumnConvertor Class — drizzle-orm Architecture

Architecture documentation for the PgAlterTableAddColumnConvertor class in sqlgenerator.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  f46a3695_7524_9fcb_de7c_cba5c96f4c7f["PgAlterTableAddColumnConvertor"]
  fe4174c7_3b9c_5b26_98a0_b2395ca21939["sqlgenerator.ts"]
  f46a3695_7524_9fcb_de7c_cba5c96f4c7f -->|defined in| fe4174c7_3b9c_5b26_98a0_b2395ca21939
  f40e3f1c_87ba_0b2b_72b7_b69afcb62d09["can()"]
  f46a3695_7524_9fcb_de7c_cba5c96f4c7f -->|method| f40e3f1c_87ba_0b2b_72b7_b69afcb62d09
  6b7041f7_5104_2b55_7314_0bd494d2c545["convert()"]
  f46a3695_7524_9fcb_de7c_cba5c96f4c7f -->|method| 6b7041f7_5104_2b55_7314_0bd494d2c545

Relationship Graph

Source Code

drizzle-kit/src/sqlgenerator.ts lines 1737–1800

class PgAlterTableAddColumnConvertor extends Convertor {
	can(statement: JsonStatement, dialect: Dialect): boolean {
		return (
			statement.type === 'alter_table_add_column' && dialect === 'postgresql'
		);
	}

	convert(statement: JsonAddColumnStatement) {
		const { tableName, column, schema } = statement;
		const { name, type, notNull, generated, primaryKey, identity } = column;

		const primaryKeyStatement = primaryKey ? ' PRIMARY KEY' : '';

		const tableNameWithSchema = schema
			? `"${schema}"."${tableName}"`
			: `"${tableName}"`;

		const defaultStatement = `${column.default !== undefined ? ` DEFAULT ${column.default}` : ''}`;

		const schemaPrefix = column.typeSchema && column.typeSchema !== 'public'
			? `"${column.typeSchema}".`
			: '';

		const fixedType = parseType(schemaPrefix, column.type);

		const notNullStatement = `${notNull ? ' NOT NULL' : ''}`;

		const unsquashedIdentity = identity
			? PgSquasher.unsquashIdentity(identity)
			: undefined;

		const identityWithSchema = schema
			? `"${schema}"."${unsquashedIdentity?.name}"`
			: `"${unsquashedIdentity?.name}"`;

		const identityStatement = unsquashedIdentity
			? ` GENERATED ${
				unsquashedIdentity.type === 'always' ? 'ALWAYS' : 'BY DEFAULT'
			} AS IDENTITY (sequence name ${identityWithSchema}${
				unsquashedIdentity.increment
					? ` INCREMENT BY ${unsquashedIdentity.increment}`
					: ''
			}${
				unsquashedIdentity.minValue
					? ` MINVALUE ${unsquashedIdentity.minValue}`
					: ''
			}${
				unsquashedIdentity.maxValue
					? ` MAXVALUE ${unsquashedIdentity.maxValue}`
					: ''
			}${
				unsquashedIdentity.startWith
					? ` START WITH ${unsquashedIdentity.startWith}`
					: ''
			}${unsquashedIdentity.cache ? ` CACHE ${unsquashedIdentity.cache}` : ''}${
				unsquashedIdentity.cycle ? ` CYCLE` : ''
			})`
			: '';

		const generatedStatement = generated ? ` GENERATED ALWAYS AS (${generated?.as}) STORED` : '';

		return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN "${name}" ${fixedType}${primaryKeyStatement}${defaultStatement}${generatedStatement}${notNullStatement}${identityStatement};`;
	}
}

Domain

Frequently Asked Questions

What is the PgAlterTableAddColumnConvertor class?
PgAlterTableAddColumnConvertor is a class in the drizzle-orm codebase, defined in drizzle-kit/src/sqlgenerator.ts.
Where is PgAlterTableAddColumnConvertor defined?
PgAlterTableAddColumnConvertor is defined in drizzle-kit/src/sqlgenerator.ts at line 1737.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free