1 | """ |
---|
2 | MySQL database specific implementations of changeset classes. |
---|
3 | """ |
---|
4 | |
---|
5 | from migrate.changeset import ansisql, exceptions |
---|
6 | from sqlalchemy.databases import mysql as sa_base |
---|
7 | #import sqlalchemy as sa |
---|
8 | |
---|
9 | MySQLSchemaGenerator = sa_base.MySQLSchemaGenerator |
---|
10 | |
---|
11 | |
---|
12 | class MySQLColumnGenerator(MySQLSchemaGenerator, ansisql.ANSIColumnGenerator): |
---|
13 | |
---|
14 | def _do_quote_table_identifier(self, identifier): |
---|
15 | return '%s'%identifier |
---|
16 | pass |
---|
17 | |
---|
18 | |
---|
19 | class MySQLColumnDropper(ansisql.ANSIColumnDropper): |
---|
20 | |
---|
21 | def _do_quote_table_identifier(self, identifier): |
---|
22 | return '%s'%identifier |
---|
23 | |
---|
24 | def _do_quote_column_identifier(self, identifier): |
---|
25 | return '%s'%identifier |
---|
26 | |
---|
27 | |
---|
28 | class MySQLSchemaChanger(MySQLSchemaGenerator, ansisql.ANSISchemaChanger): |
---|
29 | |
---|
30 | def visit_column(self, delta): |
---|
31 | keys = delta.keys() |
---|
32 | if 'type' in keys or 'nullable' in keys or 'name' in keys: |
---|
33 | self._run_subvisit(delta, self._visit_column_change) |
---|
34 | if 'server_default' in keys: |
---|
35 | # Column name might have changed above |
---|
36 | col_name = delta.get('name', delta.current_name) |
---|
37 | self._run_subvisit(delta, self._visit_column_default, |
---|
38 | col_name=col_name) |
---|
39 | |
---|
40 | def _visit_column_change(self, table_name, col_name, delta): |
---|
41 | if not hasattr(delta, 'result_column'): |
---|
42 | # Mysql needs the whole column definition, not just a lone |
---|
43 | # name/type |
---|
44 | raise exceptions.NotSupportedError( |
---|
45 | "A column object is required to do this") |
---|
46 | |
---|
47 | column = delta.result_column |
---|
48 | # needed by get_column_specification |
---|
49 | if not column.table: |
---|
50 | column.table = delta.table |
---|
51 | colspec = self.get_column_specification(column) |
---|
52 | self.start_alter_table(table_name) |
---|
53 | self.append("CHANGE COLUMN ") |
---|
54 | self.append(col_name) |
---|
55 | self.append(' ') |
---|
56 | self.append(colspec) |
---|
57 | |
---|
58 | def visit_index(self, param): |
---|
59 | # If MySQL can do this, I can't find how |
---|
60 | raise exceptions.NotSupportedError("MySQL cannot rename indexes") |
---|
61 | |
---|
62 | def _do_quote_table_identifier(self, identifier): |
---|
63 | return '%s'%identifier |
---|
64 | |
---|
65 | |
---|
66 | class MySQLConstraintGenerator(ansisql.ANSIConstraintGenerator): |
---|
67 | |
---|
68 | def _do_quote_table_identifier(self, identifier): |
---|
69 | return '%s'%identifier |
---|
70 | |
---|
71 | |
---|
72 | class MySQLConstraintDropper(ansisql.ANSIConstraintDropper): |
---|
73 | #def visit_constraint(self,constraint): |
---|
74 | # if isinstance(constraint,sqlalchemy.schema.PrimaryKeyConstraint): |
---|
75 | # return self._visit_constraint_pk(constraint) |
---|
76 | # elif isinstance(constraint,sqlalchemy.schema.ForeignKeyConstraint): |
---|
77 | # return self._visit_constraint_fk(constraint) |
---|
78 | # return super(MySQLConstraintDropper,self).visit_constraint(constraint) |
---|
79 | |
---|
80 | def visit_migrate_primary_key_constraint(self, constraint): |
---|
81 | self.start_alter_table(constraint) |
---|
82 | self.append("DROP PRIMARY KEY") |
---|
83 | self.execute() |
---|
84 | |
---|
85 | def visit_migrate_foreign_key_constraint(self, constraint): |
---|
86 | self.start_alter_table(constraint) |
---|
87 | self.append("DROP FOREIGN KEY ") |
---|
88 | self.append(constraint.name) |
---|
89 | self.execute() |
---|
90 | |
---|
91 | def _do_quote_table_identifier(self, identifier): |
---|
92 | return '%s'%identifier |
---|
93 | |
---|
94 | |
---|
95 | class MySQLDialect(ansisql.ANSIDialect): |
---|
96 | columngenerator = MySQLColumnGenerator |
---|
97 | columndropper = MySQLColumnDropper |
---|
98 | schemachanger = MySQLSchemaChanger |
---|
99 | constraintgenerator = MySQLConstraintGenerator |
---|
100 | constraintdropper = MySQLConstraintDropper |
---|