migrate() — drizzle-orm Function Reference
Architecture documentation for the migrate() function in migrator.ts from the drizzle-orm codebase.
Entity Profile
Dependency Diagram
graph TD 7e8e9761_55a5_b39b_567c_a3333e92713d["migrate()"] 2a844237_8d8e_95f7_04ad_b2253da28464["migrator.ts"] 7e8e9761_55a5_b39b_567c_a3333e92713d -->|defined in| 2a844237_8d8e_95f7_04ad_b2253da28464 406e3a87_50a6_8ba8_a8f5_7d8e0f572e95["batch()"] 7e8e9761_55a5_b39b_567c_a3333e92713d -->|calls| 406e3a87_50a6_8ba8_a8f5_7d8e0f572e95 style 7e8e9761_55a5_b39b_567c_a3333e92713d fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
drizzle-orm/src/d1/migrator.ts lines 6–49
export async function migrate<TSchema extends Record<string, unknown>>(
db: DrizzleD1Database<TSchema>,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
const migrationsTable = config.migrationsTable ?? '__drizzle_migrations';
const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
id SERIAL PRIMARY KEY,
hash text NOT NULL,
created_at numeric
)
`;
await db.session.run(migrationTableCreate);
const dbMigrations = await db.values<[number, string, string]>(
sql`SELECT id, hash, created_at FROM ${sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`,
);
const lastDbMigration = dbMigrations[0] ?? undefined;
const statementToBatch = [];
for (const migration of migrations) {
if (!lastDbMigration || Number(lastDbMigration[2])! < migration.folderMillis) {
for (const stmt of migration.sql) {
statementToBatch.push(db.run(sql.raw(stmt)));
}
statementToBatch.push(
db.run(
sql`INSERT INTO ${sql.identifier(migrationsTable)} ("hash", "created_at") VALUES(${
sql.raw(`'${migration.hash}'`)
}, ${sql.raw(`${migration.folderMillis}`)})`,
),
);
}
}
if (statementToBatch.length > 0) {
await db.session.batch(statementToBatch);
}
}
Domain
Subdomains
Defined In
Calls
Source
Frequently Asked Questions
What does migrate() do?
migrate() is a function in the drizzle-orm codebase, defined in drizzle-orm/src/d1/migrator.ts.
Where is migrate() defined?
migrate() is defined in drizzle-orm/src/d1/migrator.ts at line 6.
What does migrate() call?
migrate() calls 1 function(s): batch.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free