-
Notifications
You must be signed in to change notification settings - Fork 466
pkp/pkp-lib11758 fix region codes #11899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: stable-3_4_0
Are you sure you want to change the base?
Conversation
…ns that needs to be fixed, ensure the jobs can be re-tried
The job RegionMappingTmpClear is maybe not needed, because the table is cleared before insertion in RegionMappingTmpInsert, but I left it for now. Please let me know what you think, if I should remove it... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion we shouldn't defer upgrade code that deals with the database structure or data integrity, I think it's better to code this upgrade without jobs.
Schema::table('metrics_submission_geo_daily', function (Blueprint $table) { | ||
$table->string('region', 3)->change(); | ||
}); | ||
Schema::table('metrics_submission_geo_monthly', function (Blueprint $table) { | ||
$table->string('region', 3)->change(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think jobs shouldn't touch the structure of the database.
DB::table('metrics_submission_geo_daily')->whereNotNull('region')->exists()) { | ||
if (DB::table('metrics_submission_geo_monthly')->where('region', '<>', '')->exists() || | ||
DB::table('metrics_submission_geo_daily')->where('region', '<>', '')->exists()) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub doesn't allow me to comment on the lines below, but adding an index to the temporary table's columns might be helpful to performance.
Schema::table('metrics_submission_geo_daily', function (Blueprint $table) { | ||
$table->string('region', 7)->change(); | ||
}); | ||
Schema::table('metrics_submission_geo_monthly', function (Blueprint $table) { | ||
$table->string('region', 7)->change(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds risky, if the job never gets completed, the database will be left with an unexpected structure.
The same for the indexes below.
} | ||
$jobs[] = new CleanTmpChangesForRegionCodesFixes(); | ||
Bus::chain($jobs) | ||
->catch(function (Throwable $e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be some error logging here, the Throwable
covers even syntax errors at included files.
* Copyright (c) 2022-2025 Simon Fraser University | ||
* Copyright (c) 2022-2025 John Willinsky |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For new files we're keeping just the initial year
* Copyright (c) 2022-2025 Simon Fraser University | |
* Copyright (c) 2022-2025 John Willinsky | |
* Copyright (c) 2025 Simon Fraser University | |
* Copyright (c) 2025 John Willinsky |
namespace PKP\migration\upgrade\v3_4_0\jobs; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use PKP\jobs\BaseJob; | ||
|
||
class RegionMappingTmpClear extends BaseJob | ||
{ | ||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
// clear region_mapping_tmp table | ||
DB::table('region_mapping_tmp')->delete(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cleanup is covered by the RegionMappingTmpInsert.php
, so this whole file can be removed.
DB::table('region_mapping_tmp')->insert([ | ||
'country' => $this->country, | ||
'fips' => $fips, | ||
'iso' => $iso | ||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can store the records in an array and do a batch insert (chunks of 1000).
/** The range of metrics_submission_geo_daily_ids to consider for this update */ | ||
protected int $startId; | ||
protected int $endId; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct(int $startId, int $endId) | ||
{ | ||
parent::__construct(); | ||
$this->startId = $startId; | ||
$this->endId = $endId; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this isn't going to be extended, it can be private
and it's possible to simplify the initialization
/** The range of metrics_submission_geo_daily_ids to consider for this update */ | |
protected int $startId; | |
protected int $endId; | |
/** | |
* Create a new job instance. | |
*/ | |
public function __construct(int $startId, int $endId) | |
{ | |
parent::__construct(); | |
$this->startId = $startId; | |
$this->endId = $endId; | |
} | |
/** | |
* Create a new job instance. | |
* $startId and $endId provide the range of metrics_submission_geo_daily_ids to consider for this update | |
*/ | |
public function __construct(private int $startId, private int $endId) | |
{ | |
parent::__construct(); | |
} |
// update region code from FIPS to ISP, according to the entries in the table region_mapping_tmp | ||
// Laravel join+update does not work well with PostgreSQL, so use the direct SQLs | ||
// daily | ||
if (substr(Config::getVar('database', 'driver'), 0, strlen('postgres')) === 'postgres') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be possible to build this update and the others using the Laravel query builder.
s. #11758