From d2f3a05b315bf8318d5584dd3415f781c432cd82 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sat, 23 Dec 2017 00:32:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BA=D0=BE=D0=B4=D0=B8=D1=80=D0=BE=D0=B2=D0=BA?= =?UTF-8?q?=D1=83=20utf8md4=5Funicode=5Fci?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 2 +- console/db/Migration.php | 2 +- ...2_200114_migrate_to_utf8md4_unicode_ci.php | 37 +++++++++++++++++++ docker/mariadb/Dockerfile | 2 +- docker/mariadb/custom.cnf | 8 ++-- 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 console/migrations/m171222_200114_migrate_to_utf8md4_unicode_ci.php diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index aa07dcc..6b6b653 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,7 +9,7 @@ variables: test:backend: image: docker:latest services: - - mariadb:10.0 + - mariadb:10.2.11 - redis:3.0-alpine variables: # mariadb config diff --git a/console/db/Migration.php b/console/db/Migration.php index dd1cc3c..9633643 100644 --- a/console/db/Migration.php +++ b/console/db/Migration.php @@ -11,7 +11,7 @@ class Migration extends YiiMigration { public function getTableOptions($engine = 'InnoDB') { $tableOptions = null; if ($this->db->driverName === 'mysql') { - $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=' . $engine; + $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=' . $engine; } return $tableOptions; diff --git a/console/migrations/m171222_200114_migrate_to_utf8md4_unicode_ci.php b/console/migrations/m171222_200114_migrate_to_utf8md4_unicode_ci.php new file mode 100644 index 0000000..0fef2b7 --- /dev/null +++ b/console/migrations/m171222_200114_migrate_to_utf8md4_unicode_ci.php @@ -0,0 +1,37 @@ +execute('SET FOREIGN_KEY_CHECKS=0'); + + $dbName = $this->db->createCommand('SELECT DATABASE()')->queryScalar(); + $this->execute("ALTER DATABASE {{%$dbName}} CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci"); + $tables = $this->db->createCommand('SHOW TABLES')->queryColumn(); + foreach ($tables as $table) { + $this->execute("ALTER TABLE {{%$table}} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); + } + + $this->execute('ALTER TABLE {{%usernames_history}} MODIFY username VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL'); + + $this->execute('SET FOREIGN_KEY_CHECKS=1'); + } + + public function safeDown() { + $this->execute('SET FOREIGN_KEY_CHECKS=0'); + + $dbName = $this->db->createCommand('SELECT DATABASE()')->queryScalar(); + $this->execute("ALTER DATABASE {{%$dbName}} CHARACTER SET = utf8 COLLATE = utf8_general_ci"); + $tables = $this->db->createCommand('SHOW TABLES')->queryColumn(); + foreach ($tables as $table) { + $this->execute("ALTER TABLE {{%$table}} CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci"); + } + + $this->execute('ALTER TABLE {{%usernames_history}} MODIFY username VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL'); + + $this->execute('SET FOREIGN_KEY_CHECKS=1'); + } + +} diff --git a/docker/mariadb/Dockerfile b/docker/mariadb/Dockerfile index 68358b6..2dabddb 100644 --- a/docker/mariadb/Dockerfile +++ b/docker/mariadb/Dockerfile @@ -1,4 +1,4 @@ -FROM mariadb:10.0 +FROM mariadb:10.2.11 COPY custom.cnf /etc/mysql/conf.d/ diff --git a/docker/mariadb/custom.cnf b/docker/mariadb/custom.cnf index 0cf5418..66a1832 100644 --- a/docker/mariadb/custom.cnf +++ b/docker/mariadb/custom.cnf @@ -1,9 +1,9 @@ [mysql] -default-character-set = utf8 +default-character-set = utf8mb4 [mysqld] -character-set-server = utf8 -collation-server = utf8_general_ci +character-set-server = utf8mb4 +collation-server = utf8mb4_unicode_ci [client] -default-character-set = utf8 +default-character-set = utf8mb4