[Pkg-owncloud-commits] [owncloud] 48/70: Add repair step to set MySQL collation to utf8_bin Set default collation of mysql connection to utf8_bin Set utf_bin as default collation for new tables

David Prévot taffit at moszumanska.debian.org
Mon Jul 14 17:38:08 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit 2f15ae988f4766219f75f4cdc64be912127e74e5
Author: Robin Appelman <icewind at owncloud.com>
Date:   Mon Jul 7 17:37:35 2014 +0200

    Add repair step to set MySQL collation to utf8_bin
    Set default collation of mysql connection to utf8_bin
    Set utf_bin as default collation for new tables
---
 lib/private/db/mdb2schemareader.php  |  1 +
 lib/private/repair.php               |  3 +-
 lib/private/setup/mysql.php          |  2 +-
 lib/repair/collation.php             | 75 ++++++++++++++++++++++++++++++++++++
 tests/lib/repair/repaircollation.php | 73 +++++++++++++++++++++++++++++++++++
 5 files changed, 152 insertions(+), 2 deletions(-)

diff --git a/lib/private/db/mdb2schemareader.php b/lib/private/db/mdb2schemareader.php
index 61f58a1..288eef5 100644
--- a/lib/private/db/mdb2schemareader.php
+++ b/lib/private/db/mdb2schemareader.php
@@ -82,6 +82,7 @@ class MDB2SchemaReader {
 					$name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name);
 					$name = $this->platform->quoteIdentifier($name);
 					$table = $schema->createTable($name);
+					$table->addOption('collate', 'utf8_bin');
 					break;
 				case 'create':
 				case 'overwrite':
diff --git a/lib/private/repair.php b/lib/private/repair.php
index 89886dd..e6943c5 100644
--- a/lib/private/repair.php
+++ b/lib/private/repair.php
@@ -81,7 +81,8 @@ class Repair extends BasicEmitter {
 	 */
 	public static function getBeforeUpgradeRepairSteps() {
 		return array(
-			new \OC\Repair\InnoDB()
+			new \OC\Repair\InnoDB(),
+			new \OC\Repair\Collation(\OC::$server->getConfig(), \OC_DB::getConnection())
 		);
 	}
 
diff --git a/lib/private/setup/mysql.php b/lib/private/setup/mysql.php
index b2c2817..3327965 100644
--- a/lib/private/setup/mysql.php
+++ b/lib/private/setup/mysql.php
@@ -61,7 +61,7 @@ class MySQL extends AbstractDatabase {
 		$name = $this->dbname;
 		$user = $this->dbuser;
 		//we cant use OC_BD functions here because we need to connect as the administrative user.
-		$query = "CREATE DATABASE IF NOT EXISTS `$name`";
+		$query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET utf8 COLLATE utf8_bin;";
 		$result = mysql_query($query, $connection);
 		if(!$result) {
 			$entry = $this->trans->t('DB Error: "%s"', array(mysql_error($connection))) . '<br />';
diff --git a/lib/repair/collation.php b/lib/repair/collation.php
new file mode 100644
index 0000000..2247cf8
--- /dev/null
+++ b/lib/repair/collation.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind at owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Repair;
+
+use Doctrine\DBAL\Platforms\MySqlPlatform;
+use OC\Hooks\BasicEmitter;
+
+class Collation extends BasicEmitter implements \OC\RepairStep {
+	/**
+	 * @var \OCP\IConfig
+	 */
+	protected $config;
+
+	/**
+	 * @var \OC\DB\Connection
+	 */
+	protected $connection;
+
+	/**
+	 * @param \OCP\IConfig $config
+	 * @param \OC\DB\Connection $connection
+	 */
+	public function __construct($config, $connection) {
+		$this->connection = $connection;
+		$this->config = $config;
+	}
+
+	public function getName() {
+		return 'Repair MySQL collation';
+	}
+
+	/**
+	 * Fix mime types
+	 */
+	public function run() {
+		if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
+			$this->emit('\OC\Repair', 'info', array('Not a mysql database -> nothing to no'));
+			return;
+		}
+
+		$tables = $this->getAllNonUTF8BinTables($this->connection);
+		foreach ($tables as $table) {
+			$query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;');
+			$query->execute();
+		}
+	}
+
+	/**
+	 * @param \Doctrine\DBAL\Connection $connection
+	 * @return string[]
+	 */
+	protected function getAllNonUTF8BinTables($connection) {
+		$dbName = $this->config->getSystemValue("dbname");
+		$rows = $connection->fetchAll(
+			"SELECT DISTINCT(TABLE_NAME) AS `table`" .
+			"	FROM INFORMATION_SCHEMA . COLUMNS" .
+			"	WHERE TABLE_SCHEMA = ?" .
+			"	AND (COLLATION_NAME <> 'utf8_bin' OR CHARACTER_SET_NAME <> 'utf8')" .
+			"	AND TABLE_NAME LIKE \"*PREFIX*%\"",
+			array($dbName)
+		);
+		$result = array();
+		foreach ($rows as $row) {
+			$result[] = $row['table'];
+		}
+		return $result;
+	}
+}
+
diff --git a/tests/lib/repair/repaircollation.php b/tests/lib/repair/repaircollation.php
new file mode 100644
index 0000000..362feb8
--- /dev/null
+++ b/tests/lib/repair/repaircollation.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Copyright (c) 2014 Thomas Müller <deepdiver at owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+class TestCollationRepair extends \OC\Repair\Collation {
+	/**
+	 * @param \Doctrine\DBAL\Connection $connection
+	 * @return string[]
+	 */
+	public function getAllNonUTF8BinTables($connection) {
+		return parent::getAllNonUTF8BinTables($connection);
+	}
+}
+
+/**
+ * Tests for the converting of MySQL tables to InnoDB engine
+ *
+ * @see \OC\Repair\RepairMimeTypes
+ */
+class TestRepairCollation extends PHPUnit_Framework_TestCase {
+
+	/**
+	 * @var TestCollationRepair
+	 */
+	private $repair;
+
+	/**
+	 * @var \Doctrine\DBAL\Connection
+	 */
+	private $connection;
+
+	/**
+	 * @var string
+	 */
+	private $tableName;
+
+	/**
+	 * @var \OCP\IConfig
+	 */
+	private $config;
+
+	public function setUp() {
+		$this->connection = \OC_DB::getConnection();
+		$this->config = \OC::$server->getConfig();
+		if (!$this->connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MySqlPlatform) {
+			$this->markTestSkipped("Test only relevant on MySql");
+		}
+
+		$dbPrefix = $this->config->getSystemValue("dbtableprefix");
+		$this->tableName = uniqid($dbPrefix . "_collation_test");
+		$this->connection->exec("CREATE TABLE $this->tableName(text VARCHAR(16)) COLLATE utf8_unicode_ci");
+
+		$this->repair = new TestCollationRepair($this->config, $this->connection);
+	}
+
+	public function tearDown() {
+		$this->connection->getSchemaManager()->dropTable($this->tableName);
+	}
+
+	public function testCollationConvert() {
+		$tables = $this->repair->getAllNonUTF8BinTables($this->connection);
+		$this->assertGreaterThanOrEqual(1, count($tables));
+
+		$this->repair->run();
+
+		$tables = $this->repair->getAllNonUTF8BinTables($this->connection);
+		$this->assertCount(0, $tables);
+	}
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list