If a database name contains a hyphen, which is allowable with MySQL, then joining to it as a foreign database table will crash.

This comes up when using Migrate, if the Drupal 8 site's database name contains hyphens, as SqlBase::initializeIterator() does a join back to the D8 site's database as a foreign database.

Sample code:

  $query = \Drupal::database()->select('node_field_data', 'nfd');
  $query->addField('nfd', 'title');
  $alias = $query->leftJoin('my-other-database.node', 'foreign_node', "nfd.nid = foreign_node.nid")
  $query->range(0, 1);
  $nid = $query->execute()->fetchField();

The problem is that Connection::escapeTable() is too strict. It needs to allow the hyphen, and also the backtick, so that the following can be done:

  $alias = $query->leftJoin('`my-other-database`.node', 'foreign_node', "nfd.nid = foreign_node.nid")

Comments

joachim created an issue. See original summary.

joachim’s picture

Issue summary: View changes
joachim’s picture

Issue summary: View changes

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

shashikant_chauhan’s picture

joachim’s picture

Status: Active » Needs work
StatusFileSize
new1.16 KB

I'll upload the quick and dirty hack I made, in case someone working on this at the sprint finds it a useful starting point. (Note -- won't apply as I rolled it from my project's repo... will need a -p2.)

It worked for my case, but MySQL actually allows a wide range of characters in DB and table names, so my approach is incorrect.

What needs to happen is this sort of flow, I think:

- does the DB or table name contain only 'normal' characters?
- If so, let it through
- if not, sanitize it and then surround it in ``

dhirendra.mishra’s picture

Assigned: Unassigned » dhirendra.mishra
dhirendra.mishra’s picture

StatusFileSize
new1.52 KB

Adding a patch to allow the hypen in database name

shashikant_chauhan’s picture

Status: Needs work » Needs review

updating status.

The last submitted patch, 6: 2845333.drupal.join-hyphen-db-name-wip.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 8: 2845333-8.patch, failed testing.

joachim’s picture

Status: Needs work » Closed (duplicate)