Skip to content

Commit

Permalink
Merge pull request #620 from wri/feat/TM-1579-exact-match-tree-cleaning
Browse files Browse the repository at this point in the history
[TM-1579] A script to update v2_tree_species with a taxon_id when there is an exact name match.
  • Loading branch information
roguenet authored Dec 18, 2024
2 parents e3d0e8c + a5ee992 commit a5b3be3
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/Console/Commands/OneOff/AssociateExactMatchTrees.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Console\Commands\OneOff;

use App\Models\V2\TreeSpecies\TreeSpecies;
use Illuminate\Console\Command;

class AssociateExactMatchTrees extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'one-off:associate-exact-match-trees';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update tree species rows without a taxon_id but that do have an exact match in the backbone.';

/**
* Execute the console command.
*/
public function handle()
{
TreeSpecies::withoutTimestamps(function () {
$query = TreeSpecies::withTrashed()
->join('tree_species_research', 'v2_tree_species.name', '=', 'tree_species_research.scientific_name')
->where('v2_tree_species.taxon_id', null);
$this->withProgressBar((clone $query)->count(), function ($progressBar) use ($query) {
$query->chunkById(100, function ($trees) use ($progressBar) {
foreach ($trees as $tree) {
TreeSpecies::where('id', $tree->id)->update(['taxon_id' => $tree->taxon_id]);
$progressBar->advance();
}
});
});
});
}
}

0 comments on commit a5b3be3

Please sign in to comment.