forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c241da
commit a815486
Showing
6 changed files
with
123 additions
and
9 deletions.
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
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
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
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
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
93 changes: 93 additions & 0 deletions
93
classes/migration/upgrade/v3_4_0/I8333_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,93 @@ | ||
<?php | ||
|
||
/** | ||
* @file classes/migration/upgrade/v3_4_0/I8333_AddMissingForeignKeys.php | ||
* | ||
* Copyright (c) 2023 Simon Fraser University | ||
* Copyright (c) 2023 John Willinsky | ||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. | ||
* | ||
* @class I8333_AddMissingForeignKeys | ||
* | ||
* @brief Upgrade/downgrade operations for introducing foreign key definitions to existing database relationships. | ||
*/ | ||
|
||
namespace PKP\migration\upgrade\v3_4_0; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
use PKP\install\DowngradeNotSupportedException; | ||
|
||
abstract class I8333_AddMissingForeignKeys extends \PKP\migration\Migration | ||
{ | ||
abstract protected function getContextTable(): string; | ||
abstract protected function getContextSettingsTable(): string; | ||
abstract protected function getContextKeyField(): string; | ||
|
||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('filters', function (Blueprint $table) { | ||
// Only needed to fix the default value (0) | ||
$table->bigInteger('filter_group_id')->default(null)->change(); | ||
$table->bigInteger('parent_filter_id')->nullable()->default(null)->change(); | ||
}); | ||
DB::table('filters')->where('parent_filter_id', '=', 0)->update(['parent_filter_id' => null]); | ||
Schema::table('filters', function (Blueprint $table) { | ||
$table->foreign('parent_filter_id', 'filters_parent_filter_id')->references('filter_id')->on('filters')->onDelete('cascade'); | ||
$table->index(['parent_filter_id'], 'filters_parent_filter_id'); | ||
}); | ||
|
||
Schema::table( | ||
'navigation_menu_item_assignments', | ||
fn (Blueprint $table) => $table->bigInteger('parent_id')->nullable()->default(null)->change() | ||
); | ||
DB::table('navigation_menu_item_assignments')->where('parent_id', '=', 0)->update(['parent_id' => null]); | ||
Schema::table( | ||
'navigation_menu_item_assignments', | ||
function (Blueprint $table) { | ||
$table->foreign('parent_id', 'navigation_menu_item_assignments_parent_id') | ||
->references('navigation_menu_item_assignment_id')->on('navigation_menu_item_assignments') | ||
->onDelete('cascade'); | ||
$table->index(['parent_id'], 'navigation_menu_item_assignments_parent_id'); | ||
} | ||
); | ||
|
||
// Entities missing the context_id foreign key | ||
foreach (['navigation_menu_items', 'navigation_menus', 'plugin_settings', 'user_groups', 'filters'] as $tableName) { | ||
Schema::table($tableName, fn (Blueprint $table) => $table->bigInteger('context_id')->nullable()->default(null)->change()); | ||
DB::table($tableName)->where('context_id', '=', 0)->update(['context_id' => null]); | ||
Schema::table( | ||
$tableName, | ||
function (Blueprint $table) use ($tableName) { | ||
$table->foreign('context_id', "{$tableName}_context_id") | ||
->references($this->getContextKeyField()) | ||
->on($this->getContextTable())->onDelete('cascade'); | ||
$table->index(['context_id'], "{$tableName}_context_id"); | ||
} | ||
); | ||
} | ||
Schema::table('email_log', fn (Blueprint $table) => $table->bigInteger('sender_id')->nullable()->default(null)->change()); | ||
DB::table('email_log')->where('sender_id', '=', 0)->update(['sender_id' => null]); | ||
Schema::table( | ||
'email_log', | ||
function (Blueprint $table) { | ||
$table->foreign('sender_id', 'email_log_sender_id') | ||
->references('user_id')->on('users') | ||
->onDelete('cascade'); | ||
$table->index(['sender_id'], 'email_log_sender_id'); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
throw new DowngradeNotSupportedException(); | ||
} | ||
} |