Hi,

I'm trying to setup a migration between two different database (D5 -> D6) but am struggling to get it to work. I looked at #828288: Importing from a different database connection to try and get it going with no luck. In my migrate class right now I have:

    $this->map = new MigrateSQLMap(
      $this->machineName,
      array(
        'style' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'D5 Unique user ID',
        ),
      ),
      MigrateDestinationUser::getKeySchema(),
      'd5_db'
    );

The d5_db is the source database. And is configured in settings.php on the d6 site that I'm running the script from.

    $query = db_select('users', 'u')
      ->fields('u', array('name', 'pass', 'mail'));

    $this->source = new MigrateSourceSQL($query);
    $this->destination = new MigrateDestinationUser();

However, I get an error with this:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'd5_db.migrate_map_d5userobject' doesn't exist' in ...

Since I'm running the migrate code from within the destination site, I would have thought that it would only create the migrate_map tables in the destination site (which is what it seems to be doing). But for some reason it's looking for them in the source database.

Any ideas on what I can do to get around this?

Thanks!!
Scott

Comments

hadsie’s picture

Status: Active » Fixed

Ok, I think I've figured this one out...

The migrate map remains the same whether connecting to a table in this database versus a table in an external database.

    $this->map = new MigrateSQLMap(
      $this->machineName,
      array(
        'uid' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'D5 Unique user ID',
          'alias' => 'u',
        ),
      ),
      MigrateDestinationUser::getKeySchema()
    );

getConnection must have the d5_db set. Note that this must also be configured in settings.php, like so:

$db_url['default'] = 'mysql://drupal6:drupal6@localhost/drupal6';
$db_url['d5_db'] = 'mysql://drupal5:drupal5@localhost/d5_db';
    $query = Database::getConnection('default', 'd5_db')
      ->select('users', 'u')
      ->fields('u', array('uid', 'name', 'pass', 'mail', 'mode', 'sort', 'threshold', 'signature', 'created', 'access', 'login', 'status', 'language', 'init', 'data'));

I think we need a custom count query to access the external database.

    $count_query = Database::getConnection('default', 'd5_db')
      ->select('users', 'u');
    $count_query->addExpression('COUNT(uid)', 'cnt');

Finally, the source must have the map_joinable parameter set to FALSE so it doesn't try to join against the drupal database.

    $this->source = new MigrateSourceSQL($query, array(), $count_query, FALSE);
    $this->destination = new MigrateDestinationUser();
moshe weitzman’s picture

Component: Miscellaneous » Documentation
Category: support » task
Status: Fixed » Active

Any chance you can turn this into a patch for README.txt (Multiple DB connections)?

Ivo.Radulovski’s picture

Component: Documentation » Miscellaneous
Category: task » support

Hello,

struggling too here...

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mglegacy1.migrate_map_mgterm' doesn't exist' in...

mglegacy1 is the source database

followed the steps above... and additionally I've got this....

WD php: Argument 4 passed to MigrateSourceSQL::__construct() must be an array, boolean given, called in                                                                           [error]
/sites/all/modules/migrate_mg/mg.inc on line 47 and defined in
/sites/all/modules/migrate/plugins/sources/sql.inc on line 90.

comes from here...

$this->source = new MigrateSourceSQL($query, array(), $count_query, FALSE);

Any Ideas?

Thanks,
Ivo

fangel’s picture

I use the easy way out - a single database-connection that allows access to both databases, and then using database.table when constructing the db_select()

Ie.

$query = db_select('old_system.article', 'art')
			->fields('art', array('articleid', 'categoryid', 'publishdate', 'modifieddate', 'author'))
			->condition('status','online');
$this->source = new MigrateSourceSQL($query);

So you might want to check if that option is available to you too (that is, accessing the old database from your current Drupal database connection) instead of trying to use multiple database-connections..

Ivo.Radulovski’s picture

Thanks!

This works for me!

Additionally i resolved the problem with using multiple database-connections

I was looking into the other errors...but it is obvious that something was wrong here

    $this->source = new MigrateSourceSQL($query, array(), $count_query, FALSE);

So i changed the code according to the error message to

    $this->source = new MigrateSourceSQL($query, array(), $count_query, array('map_joinable' => FALSE));

It works! :=)

Thanks,
Ivo

moshe weitzman’s picture

FYI, I have been using this "period trick" a lot and it works nicely on mysql. I fought to preserve this feature in Drupal 7 as it was impossible for a little while #715132: Over aggressive escaping of table names. Note that the period technique is better than setting mapJoinable = FALSE since those map joins improve efficiency a lot.

We need some better docs here. Anyone up for a README.txt patch?

Ivo.Radulovski’s picture

Thanks!

When we write up some documentation for the client I will try to gather some info for the docs!

Regards,
Ivo Radulovski
www.drupal.bg/en

mikeryan’s picture

Component: Miscellaneous » Documentation

Rather than README.txt, let's work detailed documentation like this into the module documentation on d.o, which is finally taking shape. I've created a page for this subject: http://drupal.org/node/1014558

BTMash’s picture

I was going through this for a project I'm migrating from Drupal 6 to Drupal 7 and I found that using Database::getConnection requires for the target and key to be flipped - I've added a comment at http://drupal.org/node/1014558#comment-4105852

EDIT: nvm - I think I had my thing idea flipped..

Blackice2999’s picture

Hi,

the issues are a littlebit confusing me so i write down here my experience with two databases and hope it helps some.

first i defined the second database in drupal settings.php like this:

$databases['joomla15']['default'] = array(
  'database' => 'devJoomla15',
  'username' => 'xxxx',
  'password' => 'xxxx',
  'host' => 'localhost',
  'port' => '',
  'driver' => 'mysql',
  'prefix' => '',  
);

in my migration class (node migration) i used following SQLMap

    $this->map = new MigrateSQLMap($this->machineName,
        array('id' => array('type' => 'int',
                           'unsigned' => 1,
                           'length' => 11,
                           'not null' => TRUE,
                           'description' => 'Joomla content id',
                          )
        ),
        MigrateDestinationNode::getKeySchema()
      );
    // initiate connection to the joomla database and select the content table...
    $query = Database::getConnection('default', 'joomla15')
             ->select('jos_content', 'jc');
    
    // now add the fields for query based upon the description of Drupals Dynamic Database description
    // see here: drupal.org/node/310075#fields
    $query->addField('jc', 'id', 'id');
 
    // now we use a own count query and use again the joomla database for it
    // and select again the jos_content table...
    $count_query = Database::getConnection('default', 'joomla15')
      ->select('jos_content', 'jc');    
    $count_query->addExpression('COUNT(id)', 'cnt');

    // define the migration source and ensure that we dont use map_joinable because
    // we use two diffent databases and the map table is in the drupal databse...
    $this->source = new MigrateSourceSQL($query, array(), $count_query, array('map_joinable' => FALSE));

this way works perfekt for me. I can get the source from the Joomla Database and the mapping also works fine.

regards
Dennis

mikeryan’s picture

Status: Active » Needs review

OK, I've updated http://drupal.org/node/1014558 based on input from @drewish and @BTMash - how does that look?

mikeryan’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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

rfay’s picture

I still had quite a lot of trouble navigating this, so created an example, #1210064: Provide a cross-db migration example

ermannob’s picture

Hello Mike,
I'm sorry, but I can't see any improvement on that page...
I found a solution for my problem only looking at this issue. I'm working on two DBs on the same server. Drupal 6.
#4 saved my day.
Thanks.

-ermannob