]> git.openstreetmap.org Git - osqa.git/blob - forum/management/__init__.py
629678e70d94717c1a31dc59e03cf143556e79bb
[osqa.git] / forum / management / __init__.py
1 from forum.modules import get_modules_script
2
3 get_modules_script('management')
4
5 from django.db.models.signals import post_syncdb
6 import forum.models
7
8 def post_syncdb_callback(sender, **kwargs):
9     # Import the needed libraries to use the database and detect the
10     # DB engine/sever type being currently employed.
11     from django.db import connection, connections
12
13     verbosity = int(kwargs["verbosity"])
14
15     # Get the DB engine being used for persistence for this app.
16     current_db_engine = connections.databases[connection.alias]['ENGINE']
17
18     # Make sure the updates are only executed for a MySQL DB.
19     if current_db_engine.find("mysql") > 0:
20         # Ok, mysql was found in the engine description.  Go ahead
21         # and attempt to execute the alter table statements.
22         cursor = connection.cursor()
23
24         # Pair the table names with the columns that need to be updated.
25         updatable_table_columns = {
26             "forum_tag": "name",
27             "auth_user": "username"
28         }
29
30         # Update each column in turn.
31         for table_name, column_name in updatable_table_columns.iteritems():
32             alter_table_statement = "ALTER TABLE %(table_name)s MODIFY %(column_name)s varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL;" % {
33                 "table_name": table_name, "column_name": column_name}
34             log_status(verbosity,"Updating MySQL column with this statement: " + alter_table_statement)
35             cursor.execute(alter_table_statement)
36
37 def log_status(verbosity, message):
38     if verbosity == 2:
39         print "[DEBUG] " + str(message)
40
41 # Link the callback to the post_syncdb signal.
42 post_syncdb.connect(post_syncdb_callback, sender=forum.models)