Problem/Motivation

I have the following snippet in migration yml file:

foo:
    plugin: migration_lookup
    migration:
      - migration_1
      - migration_2
      - migration_3
    source: bar
    source_ids:
      migration_1:
        - foo_bar
        - bar_baz

My expectations are the following:

  • For migration_1 the source values should be: foo_bar + bar_baz
  • For other migrations (migration_2 and migration_3) the source value should be bar

Unfortunately, it's not happening, because the original value is overwritten in the migrations loop and there is no way to go back.

Steps to reproduce

You can either try the snippet above or just take a look into plugin code: https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/modules/migr...

Proposed resolution

Simplify the yml file and specific source_ids only if it differs from the original source by refreshing the original source value before the processing another migration lookup.

Remaining tasks

Review, add tests and commit.

Issue fork drupal-3196583

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Matroskeen created an issue. See original summary.

Matroskeen’s picture

Issue summary: View changes
Matroskeen’s picture

Issue summary: View changes
Status: Active » Needs review
Issue tags: +Needs tests
JoCowood’s picture

Hey Matroskeen,

have you tried this solution?

foo:
    plugin: migration_lookup
    migration:
      - migration_1
      - migration_2
      - migration_3
    source_ids:
      migration_1:
        - foo_bar
        - bar_baz
      migration_2:
        - bar
      migration_3:
        - bar
Matroskeen’s picture

Yep, it works. According to this example, I can also move migration_1 to the end of the migrations list and it'll work as well.
However, I consider it as a workaround and I still think that this is not correct behavior.
If I specify:

source_ids:
  migration_1:
    - foo_bar
    - bar_baz

I don't expect that it'll be applicable to every migration in the list after migration_1.

Considering my particular case, I have many (more than 10) migrations to lookup. I would like to simplify the yml file and specific source_ids only if it differs from the original source.

quietone’s picture

Issue summary: View changes
Status: Needs review » Needs work

As a review swap Matroskeen asked me to, well, review this issue.

After reading the issue I understand the desire to make the yml simpler and I have added that bit to the proposed resolution. Because migration_lookup is ubiquitous we need to be very careful about changes and ultimately consult with mikelutz because he drove the work on converting it to a service.

I want to think through this slowly, If we have a process like this it will work well.

plugin: migration_lookup
  source: user
  migration: users

Then we add a lookup to another migration, it is reasonable that someone would do this and it would work.

plugin: migration_lookup
  source: user
  migration:
    - users
    - members
  source_ids:
    members:
      - id

But lets say the did this, have the members migration first. This would not work. The lookup would be done on 'members' first and the source values set to ['id']. If that fails a lookup on 'users' is done and the source values are not changes so ['id'] is used when it should be 'user'.

plugin: migration_lookup
  source: user
  migration:
    - members
    - users
  source_ids:
    members:
      - id

For this to work 'users' must be added to the source_ids, as is done in the example in MigrationLookup.

Is it a bug that this does not work? Possibly. At the very least it is a bug in the documentation that it is not explicit that when using multiple migrations with different source_ids they all must be listed. The suggested change in this patch will also require documentation changes so it seems fine to leave the status of this as a Bug.

I don't see any BC issues with this change, so that is good. I have not looked at the tests, but I do think a fail test is needed.

Changing to NW for tests and documentation.

Matroskeen’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
1.64 KB
4.36 KB

Added test and one more example to the documentation block.
Also fixed the indentation in another plugin example. Hopefully, it can be fixed within this issue.

The last submitted patch, 8: 3196583-test-only-8.patch, failed testing. View results

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
1.09 KB
4.38 KB
  1. +++ b/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php
    @@ -58,14 +58,31 @@
    + * source identifier for migration is not specified, default source value will
    + * be used. In the example below, 'author' source property will be used to
    + * do a lookup in 'users' migration, and 'uid' property in 'members' migration.
    

    This could use some minor language tightening.

  2. +++ b/core/modules/migrate/tests/src/Unit/process/MigrationLookupTest.php
    @@ -251,4 +251,37 @@ public function testMultipleSourceIds() {
    +    $this->migrateLookup->lookup('foobaz', [1])->willReturn([[2]]);
    +    $this->migrateLookup->lookup('foobaz', [2])->willReturn([]);
    +    $this->migrateLookup->lookup('foobar', [1, 2])->willReturn([]);
    +    $this->migrateLookup->lookup('foobar', [3, 4])->willReturn([[5]]);
    

    Nit: $this->migrateLookup is not defined as a class property.

    I looked at the wrong file 🙈

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 10: 3196583-10.patch, failed testing. View results

catch’s picture

Status: Needs work » Reviewed & tested by the community

Restoring status after HEAD was broken.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 10: 3196583-10.patch, failed testing. View results

Wim Leers’s picture

Re-testing — random fail in CKEditor tests.

Matroskeen’s picture

Status: Needs work » Reviewed & tested by the community

Back to RTBC per #10.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
  1. +++ b/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php
    @@ -58,14 +58,32 @@
    + * It's not required to describe source identifiers for each migration. If the
    + * source identifier for migration is not specified, the default source value
    

    Supernit🧐: I think this should read 'If the source identifier for a migration'

  2. +++ b/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php
    @@ -178,22 +196,23 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
    -      if (!is_array($value)) {
    -        $value = [$value];
    +      if (!is_array($lookup_value)) {
    +        $lookup_value = [$lookup_value];
    

    could this just be $lookup_value = (array) $lookup_value if we're touching these lines?

Matroskeen’s picture

Status: Needs review » Reviewed & tested by the community

Yep, that works. Thanks for the review!

There is a mix of merge request and patches, so let's move back to merge request flow.
And moving back to RTBC after addressing #17.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Matroskeen’s picture

Status: Needs work » Needs review

Ahh, got it. Thanks!

quietone’s picture

Status: Needs review » Reviewed & tested by the community

I applied the MR and read the patch and code. The improvements suggested in #10 and #17 have been done. The added documentation in MigrationLookup reads well and is easy to understand. The test is testing what it needs to.

Back to RTBC.

  • catch committed eaf2958 on 9.3.x
    Issue #3196583 by Matroskeen, Wim Leers, quietone, larowlan:...

  • catch committed 667275f on 9.2.x
    Issue #3196583 by Matroskeen, Wim Leers, quietone, larowlan:...
catch’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.3.x and cherry-picked to 9.2.x, thanks!

Status: Fixed » Closed (fixed)

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