comparison rhodecode/lib/dbmigrate/migrate/changeset/databases/mysql.py @ 833:9753e0907827 beta

added dbmigrate package, added model changes moved out upgrade db command to that package
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 11 Dec 2010 01:54:12 +0100
parents
children 08d2dcd71666
comparison
equal deleted inserted replaced
832:634596f81cfd 833:9753e0907827
1 """
2 MySQL database specific implementations of changeset classes.
3 """
4
5 from sqlalchemy.databases import mysql as sa_base
6 from sqlalchemy import types as sqltypes
7
8 from migrate import exceptions
9 from migrate.changeset import ansisql, SQLA_06
10
11
12 if not SQLA_06:
13 MySQLSchemaGenerator = sa_base.MySQLSchemaGenerator
14 else:
15 MySQLSchemaGenerator = sa_base.MySQLDDLCompiler
16
17 class MySQLColumnGenerator(MySQLSchemaGenerator, ansisql.ANSIColumnGenerator):
18 pass
19
20
21 class MySQLColumnDropper(ansisql.ANSIColumnDropper):
22 pass
23
24
25 class MySQLSchemaChanger(MySQLSchemaGenerator, ansisql.ANSISchemaChanger):
26
27 def visit_column(self, delta):
28 table = delta.table
29 colspec = self.get_column_specification(delta.result_column)
30 if delta.result_column.autoincrement:
31 primary_keys = [c for c in table.primary_key.columns
32 if (c.autoincrement and
33 isinstance(c.type, sqltypes.Integer) and
34 not c.foreign_keys)]
35
36 if primary_keys:
37 first = primary_keys.pop(0)
38 if first.name == delta.current_name:
39 colspec += " AUTO_INCREMENT"
40 old_col_name = self.preparer.quote(delta.current_name, table.quote)
41
42 self.start_alter_table(table)
43
44 self.append("CHANGE COLUMN %s " % old_col_name)
45 self.append(colspec)
46 self.execute()
47
48 def visit_index(self, param):
49 # If MySQL can do this, I can't find how
50 raise exceptions.NotSupportedError("MySQL cannot rename indexes")
51
52
53 class MySQLConstraintGenerator(ansisql.ANSIConstraintGenerator):
54 pass
55
56 if SQLA_06:
57 class MySQLConstraintDropper(MySQLSchemaGenerator, ansisql.ANSIConstraintDropper):
58 def visit_migrate_check_constraint(self, *p, **k):
59 raise exceptions.NotSupportedError("MySQL does not support CHECK"
60 " constraints, use triggers instead.")
61
62 else:
63 class MySQLConstraintDropper(ansisql.ANSIConstraintDropper):
64
65 def visit_migrate_primary_key_constraint(self, constraint):
66 self.start_alter_table(constraint)
67 self.append("DROP PRIMARY KEY")
68 self.execute()
69
70 def visit_migrate_foreign_key_constraint(self, constraint):
71 self.start_alter_table(constraint)
72 self.append("DROP FOREIGN KEY ")
73 constraint.name = self.get_constraint_name(constraint)
74 self.append(self.preparer.format_constraint(constraint))
75 self.execute()
76
77 def visit_migrate_check_constraint(self, *p, **k):
78 raise exceptions.NotSupportedError("MySQL does not support CHECK"
79 " constraints, use triggers instead.")
80
81 def visit_migrate_unique_constraint(self, constraint, *p, **k):
82 self.start_alter_table(constraint)
83 self.append('DROP INDEX ')
84 constraint.name = self.get_constraint_name(constraint)
85 self.append(self.preparer.format_constraint(constraint))
86 self.execute()
87
88
89 class MySQLDialect(ansisql.ANSIDialect):
90 columngenerator = MySQLColumnGenerator
91 columndropper = MySQLColumnDropper
92 schemachanger = MySQLSchemaChanger
93 constraintgenerator = MySQLConstraintGenerator
94 constraintdropper = MySQLConstraintDropper