Skip to content

Commit

Permalink
#6093 Make review form preflight migrations conditional on table exis…
Browse files Browse the repository at this point in the history
…tence (for OPS)
  • Loading branch information
asmecher committed Sep 22, 2022
1 parent c2bf31a commit d336e73
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions classes/migration/upgrade/v3_4_0/PreflightCheckMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use APP\core\Application;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Schema;
use PKP\db\DAORegistry;

abstract class PreflightCheckMigration extends \PKP\migration\Migration
Expand Down Expand Up @@ -254,32 +254,37 @@ public function up(): void
foreach ($orphanedIds as $navigationMenuItemAssignmentId) {
DB::table('navigation_menu_item_assignment_settings')->where('navigation_menu_item_assignment_id', '=', $navigationMenuItemAssignmentId)->delete();
}
// Clean orphaned review_form_settings entries
$orphanedIds = DB::table('review_form_settings AS rfs')->leftJoin('review_forms AS rf', 'rf.review_form_id', '=', 'rfs.review_form_id')->whereNull('rf.review_form_id')->distinct()->pluck('rfs.review_form_id');
foreach ($orphanedIds as $reviewFormId) {
DB::table('review_form_settings')->where('review_form_id', '=', $reviewFormId)->delete();
}
// Clean orphaned review_form_elements entries
$orphanedIds = DB::table('review_form_elements AS rfe')->leftJoin('review_forms AS rf', 'rf.review_form_id', '=', 'rfe.review_form_id')->whereNull('rf.review_form_id')->distinct()->pluck('rfe.review_form_id');
foreach ($orphanedIds as $reviewFormId) {
DB::table('review_form_elements')->where('review_form_id', '=', $reviewFormId)->delete();
}
// Clean orphaned review_form_element_settings entries
$orphanedIds = DB::table('review_form_element_settings AS rfes')->leftJoin('review_form_elements AS rfe', 'rfes.review_form_element_id', '=', 'rfe.review_form_element_id')->whereNull('rfe.review_form_element_id')->distinct()->pluck('rfes.review_form_element_id');
foreach ($orphanedIds as $reviewFormElementId) {
DB::table('review_form_element_settings')->where('review_form_element_id', '=', $reviewFormElementId)->delete();
}
// Clean orphaned review_form_responses entries by review_form_element_id
$orphanedIds = DB::table('review_form_responses AS rfr')->leftJoin('review_form_elements AS rfe', 'rfe.review_form_element_id', '=', 'rfr.review_form_element_id')->whereNull('rfe.review_form_element_id')->distinct()->pluck('rfr.review_form_element_id');
foreach ($orphanedIds as $reviewFormElementId) {
$this->_installer->log("Removing orphaned review_form_responses for missing review_form_element_id ${reviewFormElementId}");
DB::table('review_form_responses')->where('review_form_element_id', '=', $reviewFormElementId)->delete();
}
// Clean orphaned review_form_responses entries by review_id
$orphanedIds = DB::table('review_form_responses AS rfr')->leftJoin('review_assignments AS ra', 'rfr.review_id', '=', 'ra.review_id')->whereNull('ra.review_id')->distinct()->pluck('rfr.review_id');
foreach ($orphanedIds as $reviewId) {
$this->_installer->log("Removing orphaned review_form_responses for missing review_id ${reviewId}");
DB::table('review_form_responses')->where('review_id', '=', $reviewId)->delete();
if (Schema::hasTable('review_form_settings')) {
// Clean orphaned review_form_settings entries
$orphanedIds = DB::table('review_form_settings AS rfs')->leftJoin('review_forms AS rf', 'rf.review_form_id', '=', 'rfs.review_form_id')->whereNull('rf.review_form_id')->distinct()->pluck('rfs.review_form_id');
foreach ($orphanedIds as $reviewFormId) {
DB::table('review_form_settings')->where('review_form_id', '=', $reviewFormId)->delete();
}
}

if (Schema::hasTable('review_form_elements')) {
// Clean orphaned review_form_elements entries
$orphanedIds = DB::table('review_form_elements AS rfe')->leftJoin('review_forms AS rf', 'rf.review_form_id', '=', 'rfe.review_form_id')->whereNull('rf.review_form_id')->distinct()->pluck('rfe.review_form_id');
foreach ($orphanedIds as $reviewFormId) {
DB::table('review_form_elements')->where('review_form_id', '=', $reviewFormId)->delete();
}
// Clean orphaned review_form_element_settings entries
$orphanedIds = DB::table('review_form_element_settings AS rfes')->leftJoin('review_form_elements AS rfe', 'rfes.review_form_element_id', '=', 'rfe.review_form_element_id')->whereNull('rfe.review_form_element_id')->distinct()->pluck('rfes.review_form_element_id');
foreach ($orphanedIds as $reviewFormElementId) {
DB::table('review_form_element_settings')->where('review_form_element_id', '=', $reviewFormElementId)->delete();
}
// Clean orphaned review_form_responses entries by review_form_element_id
$orphanedIds = DB::table('review_form_responses AS rfr')->leftJoin('review_form_elements AS rfe', 'rfe.review_form_element_id', '=', 'rfr.review_form_element_id')->whereNull('rfe.review_form_element_id')->distinct()->pluck('rfr.review_form_element_id');
foreach ($orphanedIds as $reviewFormElementId) {
$this->_installer->log("Removing orphaned review_form_responses for missing review_form_element_id ${reviewFormElementId}");
DB::table('review_form_responses')->where('review_form_element_id', '=', $reviewFormElementId)->delete();
}
// Clean orphaned review_form_responses entries by review_id
$orphanedIds = DB::table('review_form_responses AS rfr')->leftJoin('review_assignments AS ra', 'rfr.review_id', '=', 'ra.review_id')->whereNull('ra.review_id')->distinct()->pluck('rfr.review_id');
foreach ($orphanedIds as $reviewId) {
$this->_installer->log("Removing orphaned review_form_responses for missing review_id ${reviewId}");
DB::table('review_form_responses')->where('review_id', '=', $reviewId)->delete();
}
}
// Clean orphaned submissions by context_id
$orphanedIds = DB::table('submissions AS s')->leftJoin($this->getContextTable() . ' AS c', 's.context_id', '=', 'c.' . $this->getContextKeyField())->whereNull('c.' . $this->getContextKeyField())->distinct()->pluck('s.submission_id', 's.context_id');
Expand Down

0 comments on commit d336e73

Please sign in to comment.