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

prepareLibSQLDropReferencesJson() — drizzle-orm Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  a8694cd4_7e24_0011_ea04_a41215131f49["prepareLibSQLDropReferencesJson()"]
  6139f734_8ada_c641_9fec_9a55cfdf376f["jsonStatements.ts"]
  a8694cd4_7e24_0011_ea04_a41215131f49 -->|defined in| 6139f734_8ada_c641_9fec_9a55cfdf376f
  55569cde_7638_9cfc_9dcd_34636d975ec3["applyLibSQLSnapshotsDiff()"]
  55569cde_7638_9cfc_9dcd_34636d975ec3 -->|calls| a8694cd4_7e24_0011_ea04_a41215131f49
  bf324bc7_f5cf_5525_01b1_a5b9a1a0cd80["getNewTableName()"]
  a8694cd4_7e24_0011_ea04_a41215131f49 -->|calls| bf324bc7_f5cf_5525_01b1_a5b9a1a0cd80
  style a8694cd4_7e24_0011_ea04_a41215131f49 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-kit/src/jsonStatements.ts lines 3002–3072

export const prepareLibSQLDropReferencesJson = (
	tableName: string,
	schema: string,
	foreignKeys: Record<string, string>,
	json2: SQLiteSchemaSquashed,
	meta: SQLiteSchemaInternal['_meta'],
	action?: 'push',
): JsonDeleteReferenceStatement[] => {
	const statements = Object.values(foreignKeys).map((fkData) => {
		const { columnsFrom, tableFrom, columnsTo, name, tableTo, onDelete, onUpdate } = action === 'push'
			? SQLiteSquasher.unsquashPushFK(fkData)
			: SQLiteSquasher.unsquashFK(fkData);

		// If all columns from where were references were deleted -> skip this logic
		// Drop columns will cover this scenario
		const keys = Object.keys(json2.tables[tableName].columns);
		const filtered = columnsFrom.filter((it) => keys.includes(it));
		const fullDrop = filtered.length === 0;
		if (fullDrop) return;

		// When trying to alter table in lib sql it is necessary to pass all config for column like "NOT NULL", "DEFAULT", etc.
		// If it is multicolumn reference it is not possible to pass this data for all columns
		// Pass multicolumn flag for sql statements to not generate migration
		let isMulticolumn = false;

		if (columnsFrom.length > 1 || columnsTo.length > 1) {
			isMulticolumn = true;

			return {
				type: 'delete_reference',
				tableName,
				data: fkData,
				schema,
				isMulticolumn,
			};
		}

		const columnFrom = columnsFrom[0];
		const newTableName = getNewTableName(tableFrom, meta);

		const {
			notNull: columnNotNull,
			default: columnDefault,
			type: columnType,
		} = json2.tables[newTableName].columns[columnFrom];

		const fkToSquash = {
			columnsFrom,
			columnsTo,
			name,
			tableFrom: newTableName,
			tableTo,
			onDelete,
			onUpdate,
		};
		const foreignKey = action === 'push'
			? SQLiteSquasher.squashPushFK(fkToSquash)
			: SQLiteSquasher.squashFK(fkToSquash);
		return {
			type: 'delete_reference',
			tableName,
			data: foreignKey,
			schema,
			columnNotNull,
			columnDefault,
			columnType,
		};
	});

	return statements.filter((it) => it) as JsonDeleteReferenceStatement[];
};

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free