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
Comment #2
joachim commentedComment #3
joachim commentedComment #5
shashikant_chauhan commentedComment #6
joachim commentedI'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 ``
Comment #7
dhirendra.mishra commentedComment #8
dhirendra.mishra commentedAdding a patch to allow the hypen in database name
Comment #9
shashikant_chauhan commentedupdating status.
Comment #12
joachim commentedMarking as a duplicate of #1426084: Provide backtick escaping for MySQL in DB abstraction layer.