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

    Function migrateRecord

    • Moves a single record from a source database to a target 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
      4. Deletes the record from the source database

      A destination row that differs from the source stops the move with DESTINATION_CONFLICT unless onConflict is replace. A destination row equal to the source is a retried copy and the move continues.

      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. For a move that survives a crash, use reassignShard with a journalShard configured.

      Parameters

      • source: SQLDatabase

        Source database containing the record

      • target: SQLDatabase

        Target database to receive the record

      • primaryKey: string

        Primary key of the record to migrate

      • tableName: string

        Name of the table containing the record

      • options: MigrateRecordOptions = {}

        Primary-key column (default id) and conflict policy (default fail)

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