Skip to content

Commit

Permalink
Turn off strict mode on import (#3784)
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder authored May 24, 2022
1 parent 7168e58 commit 1eac8dc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ exclude_patterns:
- "**/*.module"
- ".circleci/"
- ".gitignore"

4 changes: 2 additions & 2 deletions modules/common/src/Storage/AbstractDatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ private function sanitizedErrorMessage(string $unsanitizedMessage) {
}

/**
* Private.
* Create the table in the db if it does not yet exist.
*/
private function setTable() {
protected function setTable() {
if (!$this->tableExist($this->getTableName())) {
if ($this->schema) {
$this->tableCreate($this->getTableName(), $this->schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ protected function runIt() {
$spec = $this->generateTableSpec($columns);
$this->dataStorage->setSchema(['fields' => $spec]);

// Call `count` on database table in order to ensure a database table has
// been created for the datastore.
// @todo Find a better way to ensure creation of datastore tables.
$this->dataStorage->innodbStrictMode(FALSE);
$this->dataStorage->count();
$this->dataStorage->innodbStrictMode(TRUE);
// Construct and execute a SQL import statement using the information
// gathered from the CSV file being imported.
$this->getDatabaseConnectionCapableOfDataLoad()->query(
Expand Down
15 changes: 15 additions & 0 deletions modules/datastore/src/Storage/DatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Dkan\Datastore\Resource;
use Drupal\common\LoggerTrait;
use Drupal\common\Storage\AbstractDatabaseTable;
use Drupal\Core\Database\Driver\mysql\Connection as MysqlConnection;

/**
* Database storage object.
Expand Down Expand Up @@ -158,6 +159,20 @@ public function setSchema($schema) {
parent::setSchema($schema);
}

/**
* Disable/enable InnoDB strict mode for the given database connection.
*
* @param bool $on
* Whether strict mode should be "ON" or "OFF".
*/
public function innodbStrictMode(bool $on) {
$value = $on ? "ON" : "OFF";
// Only if we're using MySQL.
if ($this->connection instanceof MysqlConnection) {
$this->connection->query("SET SESSION innodb_strict_mode=:value", [':value' => $value]);
}
}

/**
* Get table schema.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Drupal\Tests\datastore\Functional\Storage;

use Dkan\Datastore\Resource;
use Drupal\datastore\Storage\DatabaseTable;
use Drupal\Tests\common\Traits\CleanUp;
use Drupal\Tests\common\Traits\GetDataTrait;
use weitzman\DrupalTestTraits\ExistingSiteBase;

/**
* Test ResourcePurger service.
*
* @package Drupal\Tests\datastore\Functional
* @group datastore
*/
class DatabaseTableTest extends ExistingSiteBase {
use GetDataTrait;
use CleanUp;

/**
* Test that setInnodbMode() turns strict mode off for datastore.
*/
public function testSetInnodbMode() {
$connection = \Drupal::service('dkan.datastore.database');
$result = $connection->query("SHOW SESSION VARIABLES LIKE 'innodb_strict_mode'")->fetchObject();
$this->assertEquals($result->Value, "ON");

$resource = new Resource('123', '/tmp', 'text/csv');

$databaseTable = new DatabaseTable($connection, $resource);
$databaseTable->innodbStrictMode(FALSE);

$result = $connection->query("SHOW SESSION VARIABLES LIKE 'innodb_strict_mode'")->fetchObject();
$this->assertEquals($result->Value, "OFF");

// Other database connection not affected.
$connection2 = \Drupal::service('database');
$result = $connection2->query("SHOW SESSION VARIABLES LIKE 'innodb_strict_mode'")->fetchObject();
$this->assertEquals($result->Value, "ON");

// Safe mode can be turned back on.
$databaseTable->innodbStrictMode(TRUE);
$result = $connection->query("SHOW SESSION VARIABLES LIKE 'innodb_strict_mode'")->fetchObject();
$this->assertEquals($result->Value, "ON");
}

}

0 comments on commit 1eac8dc

Please sign in to comment.