Possible to access source database in prepareRow() in Migrate module? I'm doing a D2D migration that requires a mapping table to map tids from the source site to the destination site. In prepareRow(), I need to check the tid for a certain vocabulary and get the corresponding tid for the destination site. For a couple reasons, this mapping table is kept in the source site. However, the problem is that the default db connection that is being used in prepareRow() is the destination db, not the source db any more, so running my query throws an error. How can I switch the connection to use the source db so I can access this table and cache it? The short term method would be to copy this table to the destination site, but this will be an ongoing migration, and I don't want to have to update the table manually in the destination site every time the table is updated.

Thanks.
2 simple approaches:

You call the other DB explicitly like you do any other place to talk to it from drupal.

// Method #1
$query = Database::getConnection('default', 'for_migration')-&gt;select('source_table', 'st');
// $query = ....

// Method #2
db_set_active('my_other_db');
// ... code here ...
db_set_active();  // switch back to normal DB connection in-use. This is important.


Method 1 is outlined here: https://www.drupal.org/node/1014558
Method 2 is outlined here: https://www.drupal.org/node/18429

Your other option is to not do it in every prepareRow() explicitly. Think outside the box :P. Why re-query this information within every prepareRow() -- that just slows down your migration. You can define this custom $tid_map lookup array in your Migration class's constructor as a data member to the migration class itself. You only query it's value a single time (on migration startup). prepareRow then just checks the stored array whenever needed. You may or may not have to use a static variable to assure the value is saved and preserved throughout the Migration lifetime ... I'm not sure.