Skip to content

Commit

Permalink
#8826 Add foreign key to email_log.sender_id; add blank preflight che…
Browse files Browse the repository at this point in the history
…ck for 3.5
  • Loading branch information
asmecher committed May 10, 2024
1 parent 7f79f5b commit eedc261
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
2 changes: 1 addition & 1 deletion classes/migration/install/LogMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function up(): void
$table->bigInteger('assoc_id');

$table->bigInteger('sender_id')->nullable();
$table->foreign('sender_id')->references('user_id')->on('users')->onDelete('cascade');
$table->foreign('sender_id')->references('user_id')->on('users')->onDelete('set null');
$table->index(['sender_id'], 'email_log_sender_id');

$table->datetime('date_sent');
Expand Down
50 changes: 50 additions & 0 deletions classes/migration/upgrade/v3_5_0/I8826_AddMissingForeignKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @file classes/migration/upgrade/v3_5_0/I8826_AddMissingForeignKeys.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class I8826_AddMissingForeignKeys
*
* @brief Add foreign keys where missing.
*/

namespace PKP\migration\upgrade\v3_5_0;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PKP\install\DowngradeNotSupportedException;
use PKP\migration\Migration;

class I8826_AddMissingForeignKeys extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Permit NULL sender_id entries (previously used 0)
Schema::table('email_log', function (Blueprint $table) {
$table->bigInteger('sender_id')->nullable()->change();
});

// Add a new foreign key constraint and index on sender_id.
Schema::table('email_log', function (Blueprint $table) {
DB::table('email_log AS el')->leftJoin('users AS u', 'el.sender_id', '=', 'u.user_id')->whereNull('u.user_id')->update(['el.sender_id' => null]);
$table->foreign('sender_id')->references('user_id')->on('users')->onDelete('set null');
$table->index(['sender_id'], 'email_log_sender_id');
});
}

/**
* Reverse the migration.
*/
public function down(): void
{
throw new DowngradeNotSupportedException();
}
}
62 changes: 62 additions & 0 deletions classes/migration/upgrade/v3_5_0/PreflightCheckMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* @file classes/migration/upgrade/v3_4_0/PreflightCheckMigration.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PreflightCheckMigration
*
* @brief Check for common problems early in the upgrade process.
*/

namespace PKP\migration\upgrade\v3_4_0;

use PKP\db\DAORegistry;
use Throwable;

class PreflightCheckMigration extends \PKP\migration\Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
try {
} catch (Throwable $e) {
if ($fallbackVersion = $this->setFallbackVersion()) {
$this->_installer->log("A pre-flight check failed. The software was successfully upgraded to {$fallbackVersion} but could not be upgraded further (to " . $this->_installer->newVersion->getVersionString() . '). Check and correct the error, then try again.');
}
throw $e;
}
}

/**
* Rollback the migrations.
*/
public function down(): void
{
if ($fallbackVersion = $this->setFallbackVersion()) {
$this->_installer->log("An upgrade step failed! Fallback set to {$fallbackVersion}. Check and correct the error and try the upgrade again. We recommend restoring from backup, though you may be able to continue without doing so.");
// Prevent further downgrade migrations from executing.
$this->_installer->migrations = [];
}
}

/**
* Store the fallback version in the database, permitting resumption of partial upgrades.
*
* @return ?string Fallback version, if one was identified
*/
protected function setFallbackVersion(): ?string
{
if ($fallbackVersion = $this->_attributes['fallback'] ?? null) {
$versionDao = DAORegistry::getDAO('VersionDAO'); /** @var \PKP\site\VersionDAO $versionDao */
$versionDao->insertVersion(\PKP\site\Version::fromString($fallbackVersion));
return $fallbackVersion;
}
return null;
}
}

0 comments on commit eedc261

Please sign in to comment.