From b886a5eb672d0d4084f35c9d420b83000ee3b04c Mon Sep 17 00:00:00 2001 From: Carson Oldson Date: Fri, 19 Jan 2024 16:31:15 -0600 Subject: [PATCH] [EPAD8-2237] Adding updated deploy hook that should ensure we don't run into the duplicate key constraint violation as we're seeing on staging-www. --- .../custom/epa_core/epa_core.deploy.php | 84 ++++++++++++------- 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/services/drupal/web/modules/custom/epa_core/epa_core.deploy.php b/services/drupal/web/modules/custom/epa_core/epa_core.deploy.php index aa44699496..588c23b937 100644 --- a/services/drupal/web/modules/custom/epa_core/epa_core.deploy.php +++ b/services/drupal/web/modules/custom/epa_core/epa_core.deploy.php @@ -337,47 +337,75 @@ function epa_core_deploy_0004_set_card_field_default_values(&$sandbox) { // Update all card_group paragraph revision and non-revision items to set field_image_style if a value is not already set. $field_table = $table_prefix . 'field_image_style'; - $missing_image_styles_paragraphs = $database->query("SELECT p.id, p.revision_id FROM $item_table AS p LEFT JOIN $field_table AS i ON p.revision_id = i.revision_id WHERE p.type = 'card_group' AND i.field_image_style_value IS NULL")->fetchAll(); + $missing_image_styles_paragraphs = $database->query("SELECT p.id, p.revision_id, i.revision_id AS field_revision_id FROM $item_table AS p LEFT JOIN $field_table AS i ON p.revision_id = i.revision_id WHERE p.type = 'card_group' AND i.field_image_style_value IS NULL")->fetchAll(); foreach ($missing_image_styles_paragraphs as $paragraph) { - $database->insert($field_table)->fields([ - 'bundle' => 'card_group', - 'deleted' => 0, - 'entity_id' => $paragraph->id, - 'revision_id' => $paragraph->revision_id, - 'langcode' => 'en', - 'delta' => 0, - 'field_image_style_value' => 'exdent' - ])->execute(); + if (empty($paragraph->field_revision_id)) { + // Record does not exist in field table. + $database->insert($field_table)->fields([ + 'bundle' => 'card_group', + 'deleted' => 0, + 'entity_id' => $paragraph->id, + 'revision_id' => $paragraph->revision_id, + 'langcode' => 'en', + 'delta' => 0, + 'field_image_style_value' => 'exdent' + ])->execute(); + } + else { + // Record does exist but is null, run update instead. + $database->update($field_table)->fields([ + 'field_image_style_value' => 'exdent' + ])->execute(); + } } // Update all card_group paragraph revision and non-revision items to set field_title_placement if a value is not already set. $field_table = $table_prefix . 'field_title_placement'; - $missing_title_placement_paragraphs = $database->query("SELECT p.id, p.revision_id FROM $item_table AS p LEFT JOIN $field_table AS i ON p.revision_id = i.revision_id WHERE p.type = 'card_group' AND i.field_title_placement_value IS NULL")->fetchAll(); + $missing_title_placement_paragraphs = $database->query("SELECT p.id, p.revision_id, i.revision_id AS field_revision_id FROM $item_table AS p LEFT JOIN $field_table AS i ON p.revision_id = i.revision_id WHERE p.type = 'card_group' AND i.field_title_placement_value IS NULL")->fetchAll(); foreach ($missing_title_placement_paragraphs as $paragraph) { - $database->insert($field_table)->fields([ - 'bundle' => 'card_group', - 'deleted' => 0, - 'entity_id' => $paragraph->id, - 'revision_id' => $paragraph->revision_id, - 'langcode' => 'en', - 'delta' => 0, - 'field_title_placement_value' => 'media-first' - ])->execute(); + if (empty($paragraph->field_revision_id)) { + // Record does not exist in field table. + $database->insert($field_table)->fields([ + 'bundle' => 'card_group', + 'deleted' => 0, + 'entity_id' => $paragraph->id, + 'revision_id' => $paragraph->revision_id, + 'langcode' => 'en', + 'delta' => 0, + 'field_title_placement_value' => 'media-first' + ])->execute(); + } + else { + // Record does exist but is null, run update. + $database->update($field_table)->fields([ + 'field_title_placement_value' => 'media-first' + ])->execute(); + } + } // Update all card paragraph revision and non-revision items to set field_flag_card_alignment if a value is not already set. $field_table = $table_prefix . 'field_flag_card_alignment'; - $missing_flag_alignment_paragraphs = $database->query("SELECT p.id, p.revision_id FROM $item_table AS p LEFT JOIN $field_table AS i ON p.revision_id = i.revision_id WHERE p.type = 'card' AND i.field_flag_card_alignment_value IS NULL")->fetchAll(); + $missing_flag_alignment_paragraphs = $database->query("SELECT p.id, p.revision_id, i.revision_id as field_revision_id FROM $item_table AS p LEFT JOIN $field_table AS i ON p.revision_id = i.revision_id WHERE p.type = 'card' AND i.field_flag_card_alignment_value IS NULL")->fetchAll(); foreach ($missing_flag_alignment_paragraphs as $paragraph) { - $database->insert($field_table)->fields([ - 'bundle' => 'card', - 'deleted' => 0, - 'entity_id' => $paragraph->id, - 'revision_id' => $paragraph->revision_id, - 'langcode' => 'en', - 'delta' => 0, + if (empty($paragraph->field_revision_id)) { + // Record does not exist in field table. + $database->insert($field_table)->fields([ + 'bundle' => 'card', + 'deleted' => 0, + 'entity_id' => $paragraph->id, + 'revision_id' => $paragraph->revision_id, + 'langcode' => 'en', + 'delta' => 0, + 'field_flag_card_alignment_value' => 'default' + ])->execute(); + } + else { + // Record does exist but is null, run update. + $database->update($field_table)->fields([ 'field_flag_card_alignment_value' => 'default' ])->execute(); + } } } }