Repository: translate/pootle

Issue:
Migrations that alter sensitive schema fail
I am using `mysql  Ver 14.14 Distrib 5.5.46, for debian-linux-gnu (x86_64) using readline 6.3`.

``` pytb
./manage.py migrate 
2016-01-22 09:33:09,204 INFO Using Python PO
Operations to perform:
  Synchronize unmigrated apps: django_rq, pootle_profile, staticfiles, pootle_terminology, debug_toolbar, pootle, allauth, humanize, contact, django_extensions, import_export, django_assets, pootle_misc, overextends
  Apply all migrations: account, pootle_store, pootle_language, virtualfolder, pootle_app, pootle_project, sites, auth, reports, contenttypes, pootle_translationproject, accounts, pootle_statistics, sessions, staticpages, socialaccount
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying pootle_app.0005_case_sensitive_schema...Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line()
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 112, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/db/migrations/operations/special.py", line 183, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/home/leo/Escritorio/core_pootle/pootle/apps/pootle_app/migrations/0005_case_sensitive_schema.py", line 17, in make_directory_paths_cs
    "varchar(255)")
  File "/home/leo/Escritorio/core_pootle/pootle/core/utils/db.py", line 44, in set_mysql_collation_for_column
    column, schema, collation))
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/db/utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/leo/.virtualenvs/coreptl/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-core-django-17.pootle_app_directory MODIFY pootle_path  varchar(255)  character' at line 1")
```



Discussion:

Resolving patch:
diff --git a/pootle/core/utils/db.py b/pootle/core/utils/db.py
--- a/pootle/core/utils/db.py
+++ b/pootle/core/utils/db.py
@@ -24,7 +24,7 @@ def set_mysql_collation_for_column(apps, cursor, model, column, collation, schem
     # Get the current collation
     cursor.execute(
         "SELECT COLLATION_NAME"
-        " FROM information_schema.columns"
+        " FROM `information_schema`.`columns`"
         " WHERE TABLE_SCHEMA = '%s'"
         "  AND TABLE_NAME = '%s'"
         "  AND COLUMN_NAME = '%s';"
@@ -34,11 +34,11 @@ def set_mysql_collation_for_column(apps, cursor, model, column, collation, schem
     if current_collation != collation:
         # set collation
         cursor.execute(
-            "ALTER TABLE %s.%s"
-            " MODIFY %s"
+            "ALTER TABLE `%s`.`%s`"
+            " MODIFY `%s`"
             "  %s"
-            "  character set utf8"
-            "  collate %s"
+            "  CHARACTER SET utf8"
+            "  COLLATE %s"
             "  NOT NULL;"
             % (db_name, table_name,
                column, schema, collation))


Test patch: