a05c0714debc33525ebed5b9e61ec05919b89d67
[wolnelektury.git] / apps / south / db / mysql.py
1
2 from django.db import connection
3 from django.conf import settings
4 from south.db import generic
5
6 class DatabaseOperations(generic.DatabaseOperations):
7
8     """
9     MySQL implementation of database operations.
10     """
11     
12     alter_string_set_type = ''
13     alter_string_set_null = 'MODIFY %(column)s %(type)s NULL;'
14     alter_string_drop_null = 'MODIFY %(column)s %(type)s NOT NULL;'
15     drop_index_string = 'DROP INDEX %(index_name)s ON %(table_name)s'
16     allows_combined_alters = False
17     has_ddl_transactions = False
18     
19     def execute(self, sql, params=[]):
20         if hasattr(settings, "DATABASE_STORAGE_ENGINE") and \
21            settings.DATABASE_STORAGE_ENGINE:
22             generic.DatabaseOperations.execute(self, "SET storage_engine=%s;" %
23                 settings.DATABASE_STORAGE_ENGINE)
24         return generic.DatabaseOperations.execute(self, sql, params)
25     execute.__doc__ = generic.DatabaseOperations.execute.__doc__
26
27     def rename_column(self, table_name, old, new):
28         if old == new or self.dry_run:
29             return []
30         
31         qn = connection.ops.quote_name
32         
33         rows = [x for x in self.execute('DESCRIBE %s' % (qn(table_name),)) if x[0] == old]
34         
35         if not rows:
36             raise ValueError("No column '%s' in '%s'." % (old, table_name))
37         
38         params = (
39             qn(table_name),
40             qn(old),
41             qn(new),
42             rows[0][1],
43             rows[0][2] == "YES" and "NULL" or "NOT NULL",
44             rows[0][3] == "PRI" and "PRIMARY KEY" or "",
45             rows[0][4] and "DEFAULT " or "",
46             rows[0][4] and "%s" or "",
47             rows[0][5] or "",
48         )
49         
50         sql = 'ALTER TABLE %s CHANGE COLUMN %s %s %s %s %s %s %s %s;' % params
51         
52         if rows[0][4]:
53             self.execute(sql, (rows[0][4],))
54         else:
55             self.execute(sql)
56     
57     
58     def rename_table(self, old_table_name, table_name):
59         """
60         Renames the table 'old_table_name' to 'table_name'.
61         """
62         if old_table_name == table_name:
63             # No Operation
64             return
65         qn = connection.ops.quote_name
66         params = (qn(old_table_name), qn(table_name))
67         self.execute('RENAME TABLE %s TO %s;' % params)