diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 91c9a9168..46fa9cfd6 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -1,7 +1,6 @@ name: pull-request on: pull_request: - branches: [main, staging, release/**] jobs: lintTest: runs-on: ubuntu-latest diff --git a/app/Console/Commands/ImportTreeSpeciesAssociations.php b/app/Console/Commands/ImportTreeSpeciesAssociations.php new file mode 100644 index 000000000..55bed7da5 --- /dev/null +++ b/app/Console/Commands/ImportTreeSpeciesAssociations.php @@ -0,0 +1,80 @@ +executeAbortableScript(function () { + $process = new Process(['wc', '-l', $this->argument('file')]); + $process->run(); + $this->assert($process->isSuccessful(), "WC failed {$process->getErrorOutput()}"); + + $lines = ((int)explode(' ', $process->getOutput())[0]) - 1; + + $fileHandle = fopen($this->argument('file'), 'r'); + $this->parseHeaders(fgetcsv($fileHandle)); + + $this->withProgressBar($lines, function ($progressBar) use ($fileHandle) { + while ($csvRow = fgetcsv($fileHandle)) { + $treeSpeciesUuid = $csvRow[$this->treeSpeciesUuidColumn]; + $taxonId = $csvRow[$this->taxonIdColumn]; + + if ($taxonId != 'NA') { + TreeSpecies::isUuid($treeSpeciesUuid)->update(['taxon_id' => $taxonId]); + } + $progressBar->advance(); + } + + $progressBar->finish(); + }); + + fclose($fileHandle); + }); + } + + protected function parseHeaders(array $headerRow): void + { + foreach ($headerRow as $index => $header) { + if ($header == 'tree_species_uuid') { + $this->treeSpeciesUuidColumn = $index; + } elseif ($header == 'taxon_id') { + $this->taxonIdColumn = $index; + } + } + + $this->assert( + $this->treeSpeciesUuidColumn != null && $this->taxonIdColumn != null, + 'Not all required columns were found' + ); + } +} diff --git a/app/Models/V2/TreeSpecies/TreeSpecies.php b/app/Models/V2/TreeSpecies/TreeSpecies.php index c1995f255..ff4dc0729 100644 --- a/app/Models/V2/TreeSpecies/TreeSpecies.php +++ b/app/Models/V2/TreeSpecies/TreeSpecies.php @@ -38,9 +38,7 @@ class TreeSpecies extends Model implements EntityRelationModel 'speciesable_id', 'collection', 'hidden', - - 'old_id', - 'old_model', + 'taxon_id', ]; public const COLLECTION_DIRECT_SEEDING = 'direct-seeding'; @@ -82,6 +80,11 @@ public function speciesable() return $this->morphTo(); } + public function taxonomicSpecies() + { + return $this->belongsTo(TreeSpeciesResearch::class, 'taxon_id'); + } + public function getRouteKeyName() { return 'uuid';