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

_moveDataStatements() — drizzle-orm Function Reference

Architecture documentation for the _moveDataStatements() function in sqlitePushUtils.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  dd67d960_17b4_aac7_41cc_e07e2f072289["_moveDataStatements()"]
  934183d7_9412_6b03_702c_de0c2903ced3["sqlitePushUtils.ts"]
  dd67d960_17b4_aac7_41cc_e07e2f072289 -->|defined in| 934183d7_9412_6b03_702c_de0c2903ced3
  5e0b1139_8050_e207_b519_7736291ee6af["logSuggestionsAndReturn()"]
  5e0b1139_8050_e207_b519_7736291ee6af -->|calls| dd67d960_17b4_aac7_41cc_e07e2f072289
  style dd67d960_17b4_aac7_41cc_e07e2f072289 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-kit/src/cli/commands/sqlitePushUtils.ts lines 15–99

export const _moveDataStatements = (
	tableName: string,
	json: SQLiteSchemaSquashed,
	dataLoss: boolean = false,
) => {
	const statements: string[] = [];

	const newTableName = `__new_${tableName}`;

	// create table statement from a new json2 with proper name
	const tableColumns = Object.values(json.tables[tableName].columns);
	const referenceData = Object.values(json.tables[tableName].foreignKeys);
	const compositePKs = Object.values(
		json.tables[tableName].compositePrimaryKeys,
	).map((it) => SQLiteSquasher.unsquashPK(it));
	const checkConstraints = Object.values(json.tables[tableName].checkConstraints);

	const mappedCheckConstraints: string[] = checkConstraints.map((it) =>
		it.replaceAll(`"${tableName}".`, `"${newTableName}".`)
			.replaceAll(`\`${tableName}\`.`, `\`${newTableName}\`.`)
			.replaceAll(`${tableName}.`, `${newTableName}.`)
			.replaceAll(`'${tableName}'.`, `\`${newTableName}\`.`)
	);

	const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));

	// create new table
	statements.push(
		new SQLiteCreateTableConvertor().convert({
			type: 'sqlite_create_table',
			tableName: newTableName,
			columns: tableColumns,
			referenceData: fks,
			compositePKs,
			checkConstraints: mappedCheckConstraints,
		}),
	);

	// move data
	if (!dataLoss) {
		const columns = Object.keys(json.tables[tableName].columns).map(
			(c) => `"${c}"`,
		);

		statements.push(
			`INSERT INTO \`${newTableName}\`(${
				columns.join(
					', ',
				)
			}) SELECT ${columns.join(', ')} FROM \`${tableName}\`;`,
		);
	}

	statements.push(
		new SQLiteDropTableConvertor().convert({
			type: 'drop_table',
			tableName: tableName,
			schema: '',
		}),
	);

	// rename table
	statements.push(
		new SqliteRenameTableConvertor().convert({
			fromSchema: '',
			tableNameFrom: newTableName,
			tableNameTo: tableName,
			toSchema: '',
			type: 'rename_table',
		}),
	);

	for (const idx of Object.values(json.tables[tableName].indexes)) {
		statements.push(
			new CreateSqliteIndexConvertor().convert({
				type: 'create_index',
				tableName: tableName,
				schema: '',
				data: idx,
			}),
		);

Domain

Subdomains

Frequently Asked Questions

What does _moveDataStatements() do?
_moveDataStatements() is a function in the drizzle-orm codebase, defined in drizzle-kit/src/cli/commands/sqlitePushUtils.ts.
Where is _moveDataStatements() defined?
_moveDataStatements() is defined in drizzle-kit/src/cli/commands/sqlitePushUtils.ts at line 15.
What calls _moveDataStatements()?
_moveDataStatements() is called by 1 function(s): logSuggestionsAndReturn.

Analyze Your Own Codebase

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

Try Supermodel Free