浏览代码

fix(mysql): configure mysql to support 4-byte utf8 chars

The utf8 charset of mysql does not support 4byte utf8 chars like 汉语 or
emojis. This commit changes the charset in use to utf8mb4, which does
support 4-byte chars. As the key length in mysql/mariadb is limited to
a certain number of *bytes* (not chars), this decreases the maximum key
length that is legal to 191 chars. It was hence necessary to provide a
migration that can update old databases as well as changing all old
migrations to only use 191 chars per key field.

Bonus: this commit fixes a typo in the mysql password env variable.
Nils Wisiol 8 年之前
父节点
当前提交
81b3e44816

+ 6 - 8
api-settings.py.default

@@ -15,14 +15,12 @@ DATABASES = {
         'ENGINE': 'django.db.backends.mysql',
         'NAME': 'desec',
         'USER': 'desec',
-        'PASSWORD': os.environ['DESECSTACK_DB_PASSWORD_desec'],
-        'HOST': 'db',
-        #'CHARSET': 'utf8',
-        #'COLLATION': 'utf8_general_ci',
-        #'TEST': {
-        #    'CHARSET': 'utf8',
-        #    'COLLATION': 'utf8_general_ci',
-        #}
+        'PASSWORD': os.environ['DESECSTACK_DBAPI_PASSWORD_desec'],
+        'HOST': 'dbapi',
+        'CHARSET': 'utf8mb4',
+        'TEST': {
+            'CHARSET': 'utf8mb4',
+        },
        'OPTIONS': {
             'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
         }

+ 2 - 2
api/desecapi/migrations/0001_initial.py

@@ -18,7 +18,7 @@ class Migration(migrations.Migration):
                 ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                 ('password', models.CharField(max_length=128, verbose_name='password')),
                 ('last_login', models.DateTimeField(default=django.utils.timezone.now, verbose_name='last login')),
-                ('email', models.EmailField(unique=True, max_length=255, verbose_name=b'email address')),
+                ('email', models.EmailField(unique=True, max_length=191, verbose_name=b'email address')),
                 ('is_active', models.BooleanField(default=True)),
                 ('is_admin', models.BooleanField(default=False)),
             ],
@@ -32,7 +32,7 @@ class Migration(migrations.Migration):
             fields=[
                 ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                 ('created', models.DateTimeField(auto_now_add=True)),
-                ('name', models.CharField(unique=True, max_length=255)),
+                ('name', models.CharField(unique=True, max_length=191)),
                 ('arecord', models.CharField(max_length=255, blank=True)),
                 ('aaaarecord', models.CharField(max_length=1024, blank=True)),
                 ('dyn', models.BooleanField(default=False)),

+ 2 - 2
api/desecapi/migrations/0002_donation.py

@@ -16,11 +16,11 @@ class Migration(migrations.Migration):
             fields=[
                 ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                 ('created', models.DateTimeField(auto_now_add=True)),
-                ('name', models.CharField(unique=True, max_length=255)),
+                ('name', models.CharField(unique=True, max_length=191)),
                 ('iban', models.CharField(max_length=34, blank=True)),
                 ('bic', models.CharField(max_length=11, blank=True)),
                 ('amount', models.DecimalField(max_digits=8, decimal_places=2)),
-                ('message', models.CharField(unique=True, max_length=255)),
+                ('message', models.CharField(unique=True, max_length=191)),
                 ('due', models.DateTimeField()),
                 ('mref', models.CharField(max_length=11, blank=True)),
                 ('rip', models.CharField(max_length=39, blank=True)),

+ 1 - 1
api/desecapi/migrations/0008_django_update_1-10.py

@@ -15,7 +15,7 @@ class Migration(migrations.Migration):
         migrations.AlterField(
             model_name='user',
             name='email',
-            field=models.EmailField(max_length=255, unique=True, verbose_name='email address'),
+            field=models.EmailField(max_length=191, unique=True, verbose_name='email address'),
         ),
         migrations.AlterField(
             model_name='user',

+ 25 - 0
api/desecapi/migrations/0009_auto_20161201_1548.py

@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2016-12-01 15:48
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('desecapi', '0008_django_update_1-10'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='domain',
+            name='name',
+            field=models.CharField(max_length=191, unique=True),
+        ),
+        migrations.AlterField(
+            model_name='user',
+            name='email',
+            field=models.EmailField(max_length=191, unique=True, verbose_name='email address'),
+        ),
+    ]

+ 2 - 2
api/desecapi/models.py

@@ -43,7 +43,7 @@ class MyUserManager(BaseUserManager):
 class User(AbstractBaseUser):
     email = models.EmailField(
         verbose_name='email address',
-        max_length=255,
+        max_length=191,
         unique=True,
     )
     is_active = models.BooleanField(default=True)
@@ -83,7 +83,7 @@ class User(AbstractBaseUser):
 class Domain(models.Model):
     created = models.DateTimeField(auto_now_add=True)
     updated = models.DateTimeField(null=True)
-    name = models.CharField(max_length=255, unique=True)
+    name = models.CharField(max_length=191, unique=True)
     arecord = models.CharField(max_length=255, blank=True)
     aaaarecord = models.CharField(max_length=1024, blank=True)
     dyn = models.BooleanField(default=False)

+ 2 - 4
api/desecapi/settings.py

@@ -68,11 +68,9 @@ DATABASES = {
         'USER': '',
         'PASSWORD': '',
         'HOST': '127.0.0.1',
-        'CHARSET': 'utf8',
-        'COLLATION': 'utf8_general_ci',
+        'CHARSET': 'utf8mb4',
         'TEST': {
-            'CHARSET': 'utf8',
-            'COLLATION': 'utf8_general_ci',
+            'CHARSET': 'utf8mb4',
         },
         'OPTIONS': {
             'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",