You are here

protected function SmartSqlTest::saveMap in Smart SQL ID Map 1.0.x

Same name and namespace in other branches
  1. 1.1.x tests/src/Unit/Plugin/migrate/id_map/SmartSqlTest.php \Drupal\Tests\smart_sql_idmap\Unit\Plugin\migrate\id_map\SmartSqlTest::saveMap()

Saves a single ID mapping row in the database.

Parameters

array $map: The row to save.

Overrides MigrateSqlIdMapTest::saveMap

File

tests/src/Unit/Plugin/migrate/id_map/SmartSqlTest.php, line 55

Class

SmartSqlTest
Tests the Smart SQL ID map plugin.

Namespace

Drupal\Tests\smart_sql_idmap\Unit\Plugin\migrate\id_map

Code

protected function saveMap(array $map) {
  $table = $this
    ->getIdMap()
    ->mapTableName();
  $schema = $this->database
    ->schema();

  // If the table already exists, add any columns which are in the map array,
  // but don't yet exist in the table. Yay, flexibility!
  if ($schema
    ->tableExists($table)) {
    foreach (array_keys($map) as $field) {
      if (!$schema
        ->fieldExists($table, $field)) {
        $schema
          ->addField($table, $field, [
          'type' => 'text',
        ]);
      }
    }
  }
  else {
    $schema
      ->createTable($table, $this
      ->createSchemaFromRow($map));
  }
  $this->database
    ->insert($table)
    ->fields($map)
    ->execute();
}