getIds en Change source id mapping of a Drupal 8 migration <span>Change source id mapping of a Drupal 8 migration</span> <span><span lang="" about="/en/user/8" typeof="schema:Person" property="schema:name" datatype="">patrick</span></span> <span><time datetime="2017-12-13T06:57:34+01:00" title="Wednesday, December 13, 2017 - 06:57">Wed, 12/13/2017 - 06:57</time> </span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>Drupal Core's Migrate API maintains an internal table mapping unique source ids to destination ids. This is usually done via a database table that holds the field values that together uniquely identify a source row and the matching destination id of the entity in Drupal. The field names and types that uniquely identify a source row are provided by the source plugin of a migration. In most cases, you just have some kind of an id column, but migrate supports any number of fields for the primary key of the source row.</p> <p>Recently I was dealing with a migration that was using the <span class="geshifilter"><code class="php geshifilter-php">Url</code></span> source plugin provided by <a href="">Migrate Plus</a> to import files from an <span class="geshifilter"><code class="php geshifilter-php">XML</code></span> source. The source plugins provided by Migrate Plus allow you to specify the ids returned by <a href="">MigrateSourceInterface::getIds()</a> in the <span class="geshifilter"><code class="php geshifilter-php">YML</code></span> file of your migrations. Originally I used a simple id map like this:</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;">source<span style="color: #339933;">:</span> ids<span style="color: #339933;">:</span> id<span style="color: #339933;">:</span> type<span style="color: #339933;">:</span> integer</pre></div> <p>Unfortunately, I later noticed that I needed to add the file name as a second key to the id map, so I changed the YML file to something like this:</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;"> ids<span style="color: #339933;">:</span> id<span style="color: #339933;">:</span> type<span style="color: #339933;">:</span> integer file_name<span style="color: #339933;">:</span> type<span style="color: #339933;">:</span> string</pre></div> <p>So far so good. When I wanted to update the migration, I was greeted by this error message:</p> <blockquote> <p>Migration failed with source plugin exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column "sourceid2" in<br /> "field list": INSERT INTO {migrate_map_example_file} (source_ids_hash, sourceid1, sourceid2, source_row_status,<br /> rollback_action, hash, last_imported) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2,<br /> :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array<br /> (<br />     [:db_insert_placeholder_0] =&gt; a8d0cbb8ca5f48c73993244fd275342d84b92a2eb33300b0852ef560f70aa55d<br />     [:db_insert_placeholder_1] =&gt; 10817<br />     [:db_insert_placeholder_2] =&gt;<br />     [:db_insert_placeholder_3] =&gt; 2<br />     [:db_insert_placeholder_4] =&gt; 0<br />     [:db_insert_placeholder_5] =&gt;<br />     [:db_insert_placeholder_6] =&gt; 1513082182<br /> )  </p> </blockquote> <p>Solving this is actually way easier than I thought. Just drop the migrate mapping table for your migration from the SQL database:</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;">DROP TABLE migrate_map_example_file<span style="color: #339933;">;</span></pre></div> <p>If you are less adventurous, you might just want to rename the table initially:</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;"><a href=""><span style="color: #990000;">RENAME</span></a> TABLE migrate_map_example_file TO migrate_map_example_file_backup<span style="color: #339933;">;</span></pre></div> <p>As soon as you rerun your migration, migrate will recreate the table for you, this time using the new id mapping. Voilá.</p> </div> <div class="field field--name-field-systems field--type-entity-reference field--label-inline"> <div class="field--label">Systems</div> <a href="/de/systems/drupal-8-9" class="label label-default" hreflang="de">Drupal 8/9</a> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <a href="/de/tags/migrate" class="label label-default" hreflang="de">migrate</a> <a href="/en/tags/migrateplus" class="label label-default" hreflang="en">migrate_plus</a> <a href="/en/tags/source" class="label label-default" hreflang="en">source</a> <a href="/en/tags/url" class="label label-default" hreflang="en">Url</a> <a href="/en/tags/migratesourceinterface" class="label label-default" hreflang="en">MigrateSourceInterface</a> <a href="/en/tags/getids" class="label label-default" hreflang="en">getIds</a> </div> Wed, 13 Dec 2017 05:57:34 +0000 patrick 128 at