Use utf8mb4 by default on every VARCHAR field on MySQL

Added instructions for users experiencing errorcode 1366

Affects issues:
- Fixed #1509
This commit is contained in:
Risto Lahtela 2020-08-08 17:49:15 +03:00
parent 01ffe8cbe1
commit 2b85c75b7f
3 changed files with 8 additions and 1 deletions

View File

@ -131,6 +131,10 @@ public class DBOpException extends IllegalStateException implements ExceptionWit
case 1213:
context.related("Deadlock");
break;
case 1366:
context.related("Incorrect character encoding in MySQL")
.whatToDo("Convert your MySQL database and tables to use uft8mb4: https://www.a2hosting.com/kb/developer-corner/mysql/convert-mysql-database-utf-8");
break;
default:
context.related("Unknown SQL Error code");
}

View File

@ -60,6 +60,9 @@ public class CreateTableBuilder {
finalizeColumn();
columnBuilder = new StringBuilder();
columnBuilder.append(column).append(" ").append(type);
if (dbType == DBType.MYSQL && type.contains("varchar(")) {
columnBuilder.append(" CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci");
}
return this;
}

View File

@ -31,7 +31,7 @@ class CreateTableBuilderTest {
@Test
void createsSameTablesAsOldParser() {
String expected = "CREATE TABLE IF NOT EXISTS plan_servers (id integer NOT NULL AUTO_INCREMENT,uuid varchar(36) NOT NULL UNIQUE,name varchar(100),web_address varchar(100),is_installed boolean NOT NULL DEFAULT 1,max_players integer NOT NULL DEFAULT -1,PRIMARY KEY (id))";
String expected = "CREATE TABLE IF NOT EXISTS plan_servers (id integer NOT NULL AUTO_INCREMENT,uuid varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL UNIQUE,name varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,web_address varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,is_installed boolean NOT NULL DEFAULT 1,max_players integer NOT NULL DEFAULT -1,PRIMARY KEY (id))";
String result = CreateTableBuilder.create(ServerTable.TABLE_NAME, DBType.MYSQL)
.column(ServerTable.SERVER_ID, Sql.INT)
.primaryKey()