My legacy data has spaces in some of the columns but the query run has the spaces removed. Not certain whether this is to do with dbtng module in d6 or the migrate one.

Comments

chrisivens’s picture

Status: Active » Closed (won't fix)

Looking through the code, it seems that the whole db layer is not written to cope with any spaces in table or column names. It is way too much hassle to rewrite for this edge-case. I'm cleaning the data instead.

dalin’s picture

Seriously? There's no way to handle a column name with a space? This is going to be a lot of work to clean these legacy tables.

dalin’s picture

Version: 6.x-2.1 » 7.x-2.x-dev

Warning: here be dragons!

To those who come after me: rather than trying to hack things to work with spaces in column names you will be _much_ better off with some simple SQL to convert your legacy table:

    // Convert column names to something usable.
    $columns = db_query('SELECT COLUMN_NAME, DATA_TYPE FROM information_schema.COLUMNS WHERE TABLE_NAME = :name', array(':name' => 'legacy_foo'))->fetchAllKeyed();
    $alters = array();
    foreach ($columns as $column => $data_type) {
      $column_clean = strtolower(preg_replace('/[^A-Za-z0-9_]+/', '_', $column));
      switch ($data_type) {
        case 'varchar':
          $column_definition = 'VARCHAR(255)';
          break;
        case 'int':
          $column_definition = 'INT(11)';
          break;
        default:
          $column_definition = $data_type;
      }
      if ($column_clean != $column) {
        $alters[] = "CHANGE `$column` $column_clean $column_definition";
      }
    }
    if ($alters) {
      db_query('ALTER TABLE legacy_foo ' . implode(', ', $alters));
    }
13rac1’s picture

Same problem here. I've removed whitespace as a workaround, but Drupal should support access to valid MySQL column names. Yes, this is already a Drupal core issue: #1426084: Provide backtick escaping for MySQL in DB abstraction layer

13rac1’s picture

From https://drupal.org/node/1426084#comment-6467210, here is a solution for the problem of needing backticks:

$query = Database::getConnection('default', 'joomla_migrate')
->select('jos_content', 'jc')
->fields('jc', array('id', 'title', 'introtext', 'created', 'modified'));
$query->addExpression('`fulltext`', 'full');

Added to docs: https://drupal.org/node/2050819