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

convert() — drizzle-orm Function Reference

Architecture documentation for the convert() function in sqlgenerator.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  e1057ffc_e69b_1858_cc5c_7d65f1ff7a5b["convert()"]
  9c3270db_8c9d_38fd_184f_511ea1d8a518["LibSQLRecreateTableConvertor"]
  e1057ffc_e69b_1858_cc5c_7d65f1ff7a5b -->|defined in| 9c3270db_8c9d_38fd_184f_511ea1d8a518
  style e1057ffc_e69b_1858_cc5c_7d65f1ff7a5b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-kit/src/sqlgenerator.ts lines 3838–3893

	convert(statement: JsonRecreateTableStatement): string[] {
		const { tableName, columns, compositePKs, referenceData, checkConstraints } = statement;

		const columnNames = columns.map((it) => `"${it.name}"`).join(', ');
		const newTableName = `__new_${tableName}`;

		const sqlStatements: string[] = [];

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

		sqlStatements.push(`PRAGMA foreign_keys=OFF;`);

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

		// migrate data
		sqlStatements.push(
			`INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`,
		);

		// drop table
		sqlStatements.push(
			new SQLiteDropTableConvertor().convert({
				type: 'drop_table',
				tableName: tableName,
				schema: '',
			}),
		);

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

		sqlStatements.push(`PRAGMA foreign_keys=ON;`);

		return sqlStatements;
	}

Domain

Subdomains

Frequently Asked Questions

What does convert() do?
convert() is a function in the drizzle-orm codebase, defined in drizzle-kit/src/sqlgenerator.ts.
Where is convert() defined?
convert() is defined in drizzle-kit/src/sqlgenerator.ts at line 3838.

Analyze Your Own Codebase

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

Try Supermodel Free