-
-
Save Braunson/a6d647d24423a037fa628fa76bd24fe7 to your computer and use it in GitHub Desktop.
Transaction Migration Job - This migrates transactions from an old table to a new table by dispatching the job StartTransactionMigration while running in chunks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Jobs; | |
| use Illuminate\Bus\Queueable; | |
| use Illuminate\Contracts\Queue\ShouldQueue; | |
| use Illuminate\Foundation\Bus\Dispatchable; | |
| use Illuminate\Queue\InteractsWithQueue; | |
| use Illuminate\Queue\SerializesModels; | |
| use Illuminate\Support\Facades\Bus; | |
| use Illuminate\Support\Facades\DB; | |
| class MigrateTransactions implements ShouldQueue | |
| { | |
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | |
| /** | |
| * Execute the job. | |
| * | |
| * @return void | |
| */ | |
| public function handle() | |
| { | |
| $chain = []; | |
| $plans = DB::connection('mysql_reader') | |
| ->table('plans') | |
| ->select(['id', 'name']) | |
| ->get(); | |
| // Get the last migrated transaction (and move over the newest ones only) | |
| $last_migrated_transaction_id = DB::table('transactions') | |
| ->latest() | |
| ->value('id'); | |
| DB::connection('mysql_reader') | |
| ->table('transaction_log') | |
| ->when(! is_null($last_migrated_transaction_id), fn($query) => $query->where('id', '>', $last_migrated_transaction_id)) | |
| ->select(['id']) | |
| ->orderBy('id') | |
| ->chunk(1200, function ($transactions) use (&$chain, $plans) { | |
| $chain[] = new StartTransactionMigration( | |
| $transactions->pluck('id')->toArray(), | |
| $plans | |
| ); | |
| }); | |
| Bus::chain($chain)->dispatch(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment