@earth-app/collegedb
    Preparing search index...

    Function migrateRecord

    • 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:

      1. Retrieves the complete record from the source database
      2. Ensures the target database has the required schema
      3. Inserts the record into the target database (using REPLACE for safety)
      4. Deletes the record from the source database

      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.

      Parameters

      • source: D1Database

        Source D1 database containing the record

      • target: D1Database

        Target D1 database to receive the record

      • primaryKey: string

        Primary key of the record to migrate

      • tableName: string

        Name of the table containing the record

      Returns Promise<void>

      Promise that resolves when migration is complete

      If source record not found, schema creation fails, or database operations fail

      // 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');