Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/main/9069 migration issues #540

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 81 additions & 45 deletions classes/migration/upgrade/v3_4_0/PreflightCheckMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

namespace APP\migration\upgrade\v3_4_0;

use Exception;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\PostgresConnection;
use Illuminate\Support\Facades\DB;

class PreflightCheckMigration extends \PKP\migration\upgrade\v3_4_0\PreflightCheckMigration
{
protected function getContextTable(): string
Expand All @@ -36,51 +31,92 @@ protected function getContextSettingsTable(): string
return 'journal_settings';
}

public function up(): void
protected function buildOrphanedEntityProcessor(): void
{
parent::up();
try {
// Clean orphaned sections entries by journal_id
$orphanedIds = DB::table('sections AS s')->leftJoin('journals AS j', 's.journal_id', '=', 'j.journal_id')->whereNull('j.journal_id')->distinct()->pluck('s.journal_id');
foreach ($orphanedIds as $journalId) {
DB::table('sections')->where('journal_id', '=', $journalId)->delete();
}
parent::buildOrphanedEntityProcessor();

$this->addTableProcessor('publications', function (): int {
$affectedRows = 0;
$affectedRows += $this->cleanOptionalReference('publications', 'section_id', 'sections', 'section_id');
return $affectedRows;
});

// Clean orphaned section_settings entries
$orphanedIds = DB::table('section_settings AS ss')->leftJoin('sections AS s', 'ss.section_id', '=', 's.section_id')->whereNull('s.section_id')->distinct()->pluck('ss.section_id');
foreach ($orphanedIds as $sectionId) {
DB::table('section_settings')->where('section_id', '=', $sectionId)->delete();
}
$this->addTableProcessor('publication_galleys', function (): int {
$affectedRows = 0;
// Depends directly on ~3 entities: doi_id->dois.doi_id(not found in previous version) publication_id->publications.publication_id submission_file_id->submission_files.submission_file_id
$affectedRows += $this->deleteRequiredReference('publication_galleys', 'publication_id', 'publications', 'publication_id');
$affectedRows += $this->deleteOptionalReference('publication_galleys', 'submission_file_id', 'submission_files', 'submission_file_id');
return $affectedRows;
});

// Clean orphaned publications entries by primary_contact_id
switch (true) {
case DB::connection() instanceof MySqlConnection:
DB::statement('UPDATE publications p LEFT JOIN authors a ON (p.primary_contact_id = a.author_id) SET p.primary_contact_id = NULL WHERE a.author_id IS NULL');
break;
case DB::connection() instanceof PostgresConnection:
DB::statement('UPDATE publications SET primary_contact_id = NULL WHERE publication_id IN (SELECT p.publication_id FROM publications p LEFT JOIN authors a ON (p.primary_contact_id = a.author_id) WHERE a.author_id IS NULL AND p.primary_contact_id IS NOT NULL)');
break;
default: throw new Exception('Unknown database connection type!');
}
$this->addTableProcessor('sections', function (): int {
$affectedRows = 0;
// Depends directly on ~2 entities: review_form_id->review_forms.review_form_id server_id->servers.server_id(not found in previous version)
$affectedRows += $this->deleteRequiredReference('sections', $this->getContextKeyField(), $this->getContextTable(), $this->getContextKeyField());
$affectedRows += $this->cleanOptionalReference('sections', 'review_form_id', 'review_forms', 'review_form_id');
return $affectedRows;
});

// Clean orphaned publication_galleys entries by publication_id
$orphanedIds = DB::table('publication_galleys AS pg')->leftJoin('publications AS p', 'pg.publication_id', '=', 'p.publication_id')->whereNull('p.publication_id')->distinct()->pluck('pg.publication_id');
foreach ($orphanedIds as $publicationId) {
DB::table('publication_galleys')->where('publication_id', '=', $publicationId)->delete();
}
$this->addTableProcessor('publication_galley_settings', function (): int {
$affectedRows = 0;
// Depends directly on ~1 entities: galley_id->publication_galleys.galley_id
$affectedRows += $this->deleteRequiredReference('publication_galley_settings', 'galley_id', 'publication_galleys', 'galley_id');
return $affectedRows;
});

// Clean orphaned publication_galley_settings entries
$orphanedIds = DB::table('publication_galley_settings AS pgs')->leftJoin('publication_galleys AS pg', 'pgs.galley_id', '=', 'pg.galley_id')->whereNull('pg.galley_id')->distinct()->pluck('pgs.galley_id');
foreach ($orphanedIds as $galleyId) {
DB::table('publication_galley_settings')->where('galley_id', '=', $galleyId)->delete();
}
$this->addTableProcessor('section_settings', function (): int {
$affectedRows = 0;
// Depends directly on ~1 entities: section_id->sections.section_id
$affectedRows += $this->deleteRequiredReference('section_settings', 'section_id', 'sections', 'section_id');
return $affectedRows;
});

DB::table('sections')->where('review_form_id', '=', 0)->update(['review_form_id' => null]);
} catch (Exception $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;
}
$this->addTableProcessor('server_settings', function (): int {
$affectedRows = 0;
// Depends directly on ~1 entities: server_id->servers.server_id(not found in previous version)
$affectedRows += $this->deleteRequiredReference($this->getContextSettingsTable(), $this->getContextKeyField(), $this->getContextTable(), $this->getContextKeyField());
return $affectedRows;
});
}

protected function getEntityRelationships(): array
{
return [
$this->getContextTable() => ['submissions', 'user_groups', 'categories', 'sections', 'navigation_menu_items', 'genres', 'filters', 'announcement_types', 'notifications', 'navigation_menus', 'library_files', 'email_templates', 'user_group_stage', 'subeditor_submission_group', 'plugin_settings', 'notification_subscription_settings', $this->getContextSettingsTable()],
'submissions' => ['submission_files', 'publications', 'review_rounds', 'review_assignments', 'submission_search_objects', 'library_files', 'submission_settings', 'submission_comments', 'stage_assignments', 'review_round_files', 'edit_decisions'],
'users' => ['submission_files', 'review_assignments', 'notifications', 'event_log', 'email_log', 'user_user_groups', 'user_settings', 'user_interests', 'temporary_files', 'submission_comments', 'subeditor_submission_group', 'stage_assignments', 'sessions', 'query_participants', 'notification_subscription_settings', 'notes', 'email_log_users', 'edit_decisions', 'access_keys'],
'submission_files' => ['submission_files', 'publication_galleys', 'submission_file_settings', 'submission_file_revisions', 'review_round_files', 'review_files'],
'user_groups' => ['authors', 'user_user_groups', 'user_group_stage', 'user_group_settings', 'subeditor_submission_group', 'stage_assignments'],
'publications' => ['submissions', 'publication_galleys', 'authors', 'citations', 'publication_settings', 'publication_categories'],
'publication_galleys' => ['publication_galley_settings'],
'review_forms' => ['sections', 'review_form_elements', 'review_assignments', 'review_form_settings'],
'categories' => ['categories', 'publication_categories', 'category_settings'],
'review_rounds' => ['review_assignments', 'review_round_files', 'edit_decisions'],
'authors' => ['publications', 'author_settings'],
'controlled_vocab_entries' => ['user_interests', 'controlled_vocab_entry_settings'],
'data_object_tombstones' => ['data_object_tombstone_settings', 'data_object_tombstone_oai_set_objects'],
'files' => ['submission_files', 'submission_file_revisions'],
'filters' => ['filters', 'filter_settings'],
'genres' => ['submission_files', 'genre_settings'],
'navigation_menu_item_assignments' => ['navigation_menu_item_assignments', 'navigation_menu_item_assignment_settings'],
'announcement_types' => ['announcements', 'announcement_type_settings'],
'review_assignments' => ['review_form_responses', 'review_files'],
'review_form_elements' => ['review_form_responses', 'review_form_element_settings'],
'sections' => ['publications', 'section_settings'],
'navigation_menu_items' => ['navigation_menu_item_assignments', 'navigation_menu_item_settings'],
'queries' => ['query_participants'],
'navigation_menus' => ['navigation_menu_item_assignments'],
'notifications' => ['notification_settings'],
'event_log' => ['event_log_settings'],
'email_templates' => ['email_templates_settings'],
'library_files' => ['library_file_settings'],
'email_log' => ['email_log_users'],
'controlled_vocabs' => ['controlled_vocab_entries'],
'submission_search_keyword_list' => ['submission_search_object_keywords'],
'submission_search_objects' => ['submission_search_object_keywords'],
'citations' => ['citation_settings'],
'announcements' => ['announcement_settings'],
'filter_groups' => ['filters']
];
}
}
2 changes: 1 addition & 1 deletion dbscripts/xml/upgrade.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
</upgrade>

<upgrade minversion="3.1.0.0" maxversion="3.3.9.9">
<migration class="APP\migration\upgrade\v3_4_0\PreflightCheckMigration" fallback="3.3.9.9" />
<migration class="APP\migration\upgrade\v3_4_0\ReviewFormsMigration" />
<migration class="APP\migration\upgrade\v3_4_0\PreflightCheckMigration" fallback="3.3.9.9" />
<migration class="PKP\migration\upgrade\v3_4_0\I8060_UpdateUserLocalesDefaultToEmptyArrayFromNull" />
<migration class="PKP\migration\upgrade\v3_4_0\I7245_UpdateUserLocaleStringToParsableJsonString" />
<migration class="APP\migration\upgrade\v3_4_0\I3573_AddPrimaryKeys" />
Expand Down