Skip to content

Conversation

bozana
Copy link
Contributor

@bozana bozana commented Oct 6, 2025

s. #11758

@bozana
Copy link
Contributor Author

bozana commented Oct 6, 2025

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...

Copy link
Contributor

@jonasraoni jonasraoni left a 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.

Comment on lines +47 to +52
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();
});
Copy link
Contributor

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()) {

Copy link
Contributor

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.

Comment on lines +53 to +58
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();
});
Copy link
Contributor

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) {
Copy link
Contributor

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.

Comment on lines +6 to +7
* Copyright (c) 2022-2025 Simon Fraser University
* Copyright (c) 2022-2025 John Willinsky
Copy link
Contributor

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

Suggested change
* Copyright (c) 2022-2025 Simon Fraser University
* Copyright (c) 2022-2025 John Willinsky
* Copyright (c) 2025 Simon Fraser University
* Copyright (c) 2025 John Willinsky

Comment on lines +17 to +32
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();
}
}
Copy link
Contributor

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.

Comment on lines +53 to +57
DB::table('region_mapping_tmp')->insert([
'country' => $this->country,
'fips' => $fips,
'iso' => $iso
]);
Copy link
Contributor

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).

Comment on lines +25 to +37
/** 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;
}
Copy link
Contributor

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

Suggested change
/** 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') {
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants