Home / Function/ preparePgAlterColumns() — drizzle-orm Function Reference

preparePgAlterColumns() — drizzle-orm Function Reference

Architecture documentation for the preparePgAlterColumns() function in jsonStatements.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  a9a30054_27e3_8351_5d74_1c8afbf09503["preparePgAlterColumns()"]
  6139f734_8ada_c641_9fec_9a55cfdf376f["jsonStatements.ts"]
  a9a30054_27e3_8351_5d74_1c8afbf09503 -->|defined in| 6139f734_8ada_c641_9fec_9a55cfdf376f
  c7a398db_43c9_7771_09b7_73bc09e703e8["applyPgSnapshotsDiff()"]
  c7a398db_43c9_7771_09b7_73bc09e703e8 -->|calls| a9a30054_27e3_8351_5d74_1c8afbf09503
  style a9a30054_27e3_8351_5d74_1c8afbf09503 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-kit/src/jsonStatements.ts lines 2074–2433

export const preparePgAlterColumns = (
	_tableName: string,
	schema: string,
	columns: AlteredColumn[],
	// TODO: remove?
	json2: PgSchemaSquashed,
	json1: PgSchemaSquashed,
	action?: 'push' | undefined,
): JsonAlterColumnStatement[] => {
	const tableKey = `${schema || 'public'}.${_tableName}`;
	let statements: JsonAlterColumnStatement[] = [];
	let dropPkStatements: JsonAlterColumnDropPrimaryKeyStatement[] = [];
	let setPkStatements: JsonAlterColumnSetPrimaryKeyStatement[] = [];

	for (const column of columns) {
		const columnName = typeof column.name !== 'string' ? column.name.new : column.name;

		const tableName = json2.tables[tableKey].name;

		// I used any, because those fields are available only for mysql dialect
		// For other dialects it will become undefined, that is fine for json statements
		const columnType = json2.tables[tableKey].columns[columnName].type;
		const columnDefault = json2.tables[tableKey].columns[columnName].default;
		const columnGenerated = json2.tables[tableKey].columns[columnName].generated;
		const columnOnUpdate = (json2.tables[tableKey].columns[columnName] as any)
			.onUpdate;
		const columnNotNull = json2.tables[tableKey].columns[columnName].notNull;
		const columnAutoIncrement = (
			json2.tables[tableKey].columns[columnName] as any
		).autoincrement;
		const columnPk = (json2.tables[tableKey].columns[columnName] as any)
			.primaryKey;
		const typeSchema = json2.tables[tableKey].columns[columnName].typeSchema;
		const json1ColumnTypeSchema = json1.tables[tableKey].columns[columnName].typeSchema;

		const compositePk = json2.tables[tableKey].compositePrimaryKeys[`${tableName}_${columnName}`];

		if (typeof column.name !== 'string') {
			statements.push({
				type: 'alter_table_rename_column',
				tableName,
				oldColumnName: column.name.old,
				newColumnName: column.name.new,
				schema,
			});
		}

		if (column.type?.type === 'changed') {
			const arrayDefinitionRegex = /\[\d*(?:\[\d*\])*\]/g;
			const parsedNewColumnType = column.type.new.replace(arrayDefinitionRegex, '');
			const parsedOldColumnType = column.type.old.replace(arrayDefinitionRegex, '');

			const isNewTypeIsEnum = json2.enums[`${typeSchema}.${parsedNewColumnType}`];
			const isOldTypeIsEnum = json1.enums[`${json1ColumnTypeSchema}.${parsedOldColumnType}`];

			statements.push({
				type: 'pg_alter_table_alter_column_set_type',
				tableName,
				columnName,
				typeSchema: typeSchema,
				newDataType: {
					name: column.type.new,
					isEnum: isNewTypeIsEnum ? true : false,
				},
				oldDataType: {
					name: column.type.old,
					isEnum: isOldTypeIsEnum ? true : false,
				},
				schema,
				columnDefault,
				columnOnUpdate,
				columnNotNull,
				columnAutoIncrement,
				columnPk,
			});
		}

		if (
			column.primaryKey?.type === 'deleted'
			|| (column.primaryKey?.type === 'changed'
				&& !column.primaryKey.new

Domain

Subdomains

Frequently Asked Questions

What does preparePgAlterColumns() do?
preparePgAlterColumns() is a function in the drizzle-orm codebase, defined in drizzle-kit/src/jsonStatements.ts.
Where is preparePgAlterColumns() defined?
preparePgAlterColumns() is defined in drizzle-kit/src/jsonStatements.ts at line 2074.
What calls preparePgAlterColumns()?
preparePgAlterColumns() is called by 1 function(s): applyPgSnapshotsDiff.

Analyze Your Own Codebase

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

Try Supermodel Free