Problem/Motivation

MigrateIdMapInterface::getRowByDestination() should always return an array based on the documentation, but the actual implementation of \Drupal\migrate\Plugin\migrate\id_map\Sql::getRowByDestination() might also return FALSE.

Proposed resolution

Ensure \Drupal\migrate\Plugin\migrate\id_map\Sql::getRowByDestination() follows the documentation and always returns an array.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

huzooka created an issue. See original summary.

huzooka’s picture

Assigned: huzooka » Unassigned
Status: Active » Needs review
StatusFileSize
new2.96 KB
new3.84 KB

Status: Needs review » Needs work
huzooka’s picture

Assigned: Unassigned » huzooka

Hmmm... so core has a test coverage for one of its documentation violation...

huzooka’s picture

Assigned: huzooka » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.43 KB
new2.31 KB
wim leers’s picture

🤯

Might I also suggest something like

diff --git a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
index b55fda4b43..8459315299 100644
--- a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
+++ b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
@@ -542,7 +542,9 @@ public function getRowByDestination(array $destination_id_values) {
       $query->condition("map.$destination_id", $destination_id_values[$field_name], '=');
     }
     $result = $query->execute();
-    return $result->fetchAssoc();
+    $return = $result->fetchAssoc();
+    assert(is_array($return));
+    return $return;
   }
 
   /**

to not make any change that could break anything (i.e. adding a return type would be better, but might be seen as an API change), but would have helped pinpoint this ages ago?

huzooka’s picture

Re #7:
We obviously cannot do that, because \Drupal\Core\Database\StatementInterface::fetchAssoc() can return FALSE as well.

wim leers’s picture

#7: I just mean "add that assert for the return value".

+++ b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
@@ -539,10 +539,13 @@ public function getRowByDestination(array $destination_id_values) {
+    return $result ? $result : [];

Because this could theoretically still return NULL for example.

huzooka’s picture

Re #10:
That is impossible.
But if the actual statement's ::fetchAssoc() method returns anything else than an array or FALSE, then we have to fix the actual DB statement (because its interface declares that it has to return with an array or FALSE).

huzooka’s picture

wim leers’s picture

Status: Needs review » Reviewed & tested by the community

(because its interface declares that it has to return

Aha — all good then! I have learned to not trust the database layer 🤓, but in this case that is then unwarranted!

Sorry for derailing this over a nitpick.


This patch has explicit test coverage. It is updating code to actually do what its docs say. I can't find anything else to nitpick here.

huzooka’s picture

Title: Sql id map plugin's getRowByDestination might return FALSE » Sql id map plugin's getRowByDestination shouldn't return FALSE

  • catch committed 8dea1fb on 9.3.x
    Issue #3227549 by huzooka, Wim Leers: Sql id map plugin's...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 8dea1fb and pushed to 9.3.x. Thanks!

huzooka’s picture

Wow, that was fast!
Thank you, @catch!

huzooka’s picture

Issue summary: View changes

Fixing the IS.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.