Source D1 database containing the record
Target D1 database to receive the record
Primary key of the record to migrate
Name of the table containing the record
Promise that resolves when migration is complete
// Migrate a user from east to west shard
try {
await migrateRecord(env.DB_EAST, env.DB_WEST, 'user-123', 'users');
console.log('User migration completed successfully');
} catch (error) {
console.error('Migration failed:', error.message);
// May need manual cleanup depending on where it failed
}
// Migrate a post between shards
await migrateRecord(source, target, 'post-456', 'posts');
Moves a single record from a source D1 database to a target D1 database. This is typically used during shard rebalancing operations when data needs to be redistributed across shards for load balancing.
The migration process:
The operation is atomic from the perspective of each database, but not across databases. If the operation fails partway through, manual cleanup may be required.