-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#8826 Add foreign key to email_log.sender_id; add blank preflight che…
…ck for 3.5
- Loading branch information
Showing
3 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
classes/migration/upgrade/v3_5_0/I8826_AddMissingForeignKeys.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
classes/migration/upgrade/v3_5_0/PreflightCheckMigration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |