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