summaryrefslogtreecommitdiff
path: root/tests/test_mysql.py
blob: 7ce71a9720428e93bfbeaf3e56bd686b6f1141c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from sqlalchemy import Integer, func
from alembic.testing.fixtures import TestBase
from alembic.testing import config
from sqlalchemy import TIMESTAMP, MetaData, Table, Column, text
from sqlalchemy.engine.reflection import Inspector
from alembic import op, util

from alembic.testing import eq_, assert_raises_message
from alembic.testing.fixtures import capture_context_buffer, op_fixture
from alembic.testing.env import staging_env, _no_sql_testing_config, \
    three_rev_fixture, clear_staging_env

from alembic.migration import MigrationContext


class MySQLOpTest(TestBase):

    def test_rename_column(self):
        context = op_fixture('mysql')
        op.alter_column(
            't1', 'c1', new_column_name="c2", existing_type=Integer)
        context.assert_(
            'ALTER TABLE t1 CHANGE c1 c2 INTEGER NULL'
        )

    def test_rename_column_quotes_needed_one(self):
        context = op_fixture('mysql')
        op.alter_column('MyTable', 'ColumnOne', new_column_name="ColumnTwo",
                        existing_type=Integer)
        context.assert_(
            'ALTER TABLE `MyTable` CHANGE `ColumnOne` `ColumnTwo` INTEGER NULL'
        )

    def test_rename_column_quotes_needed_two(self):
        context = op_fixture('mysql')
        op.alter_column('my table', 'column one', new_column_name="column two",
                        existing_type=Integer)
        context.assert_(
            'ALTER TABLE `my table` CHANGE `column one` '
            '`column two` INTEGER NULL'
        )

    def test_rename_column_serv_default(self):
        context = op_fixture('mysql')
        op.alter_column(
            't1', 'c1', new_column_name="c2", existing_type=Integer,
            existing_server_default="q")
        context.assert_(
            "ALTER TABLE t1 CHANGE c1 c2 INTEGER NULL DEFAULT 'q'"
        )

    def test_rename_column_serv_compiled_default(self):
        context = op_fixture('mysql')
        op.alter_column(
            't1', 'c1', existing_type=Integer,
            server_default=func.utc_thing(func.current_timestamp()))
        # this is not a valid MySQL default but the point is to just
        # test SQL expression rendering
        context.assert_(
            "ALTER TABLE t1 ALTER COLUMN c1 "
            "SET DEFAULT utc_thing(CURRENT_TIMESTAMP)"
        )

    def test_rename_column_autoincrement(self):
        context = op_fixture('mysql')
        op.alter_column(
            't1', 'c1', new_column_name="c2", existing_type=Integer,
            existing_autoincrement=True)
        context.assert_(
            'ALTER TABLE t1 CHANGE c1 c2 INTEGER NULL AUTO_INCREMENT'
        )

    def test_col_add_autoincrement(self):
        context = op_fixture('mysql')
        op.alter_column('t1', 'c1', existing_type=Integer,
                        autoincrement=True)
        context.assert_(
            'ALTER TABLE t1 MODIFY c1 INTEGER NULL AUTO_INCREMENT'
        )

    def test_col_remove_autoincrement(self):
        context = op_fixture('mysql')
        op.alter_column('t1', 'c1', existing_type=Integer,
                        existing_autoincrement=True,
                        autoincrement=False)
        context.assert_(
            'ALTER TABLE t1 MODIFY c1 INTEGER NULL'
        )

    def test_col_dont_remove_server_default(self):
        context = op_fixture('mysql')
        op.alter_column('t1', 'c1', existing_type=Integer,
                        existing_server_default='1',
                        server_default=False)

        context.assert_()

    def test_alter_column_drop_default(self):
        context = op_fixture('mysql')
        op.alter_column("t", "c", existing_type=Integer, server_default=None)
        context.assert_(
            'ALTER TABLE t ALTER COLUMN c DROP DEFAULT'
        )

    def test_alter_column_modify_default(self):
        context = op_fixture('mysql')
        # notice we dont need the existing type on this one...
        op.alter_column("t", "c", server_default='1')
        context.assert_(
            "ALTER TABLE t ALTER COLUMN c SET DEFAULT '1'"
        )

    def test_col_not_nullable(self):
        context = op_fixture('mysql')
        op.alter_column('t1', 'c1', nullable=False, existing_type=Integer)
        context.assert_(
            'ALTER TABLE t1 MODIFY c1 INTEGER NOT NULL'
        )

    def test_col_not_nullable_existing_serv_default(self):
        context = op_fixture('mysql')
        op.alter_column('t1', 'c1', nullable=False, existing_type=Integer,
                        existing_server_default='5')
        context.assert_(
            "ALTER TABLE t1 MODIFY c1 INTEGER NOT NULL DEFAULT '5'"
        )

    def test_col_nullable(self):
        context = op_fixture('mysql')
        op.alter_column('t1', 'c1', nullable=True, existing_type=Integer)
        context.assert_(
            'ALTER TABLE t1 MODIFY c1 INTEGER NULL'
        )

    def test_col_multi_alter(self):
        context = op_fixture('mysql')
        op.alter_column(
            't1', 'c1', nullable=False, server_default="q", type_=Integer)
        context.assert_(
            "ALTER TABLE t1 MODIFY c1 INTEGER NOT NULL DEFAULT 'q'"
        )

    def test_alter_column_multi_alter_w_drop_default(self):
        context = op_fixture('mysql')
        op.alter_column(
            't1', 'c1', nullable=False, server_default=None, type_=Integer)
        context.assert_(
            "ALTER TABLE t1 MODIFY c1 INTEGER NOT NULL"
        )

    def test_col_alter_type_required(self):
        op_fixture('mysql')
        assert_raises_message(
            util.CommandError,
            "MySQL CHANGE/MODIFY COLUMN operations require the existing type.",
            op.alter_column, 't1', 'c1', nullable=False, server_default="q"
        )

    def test_drop_fk(self):
        context = op_fixture('mysql')
        op.drop_constraint("f1", "t1", "foreignkey")
        context.assert_(
            "ALTER TABLE t1 DROP FOREIGN KEY f1"
        )

    def test_drop_constraint_primary(self):
        context = op_fixture('mysql')
        op.drop_constraint('primary', 't1', type_='primary')
        context.assert_(
            "ALTER TABLE t1 DROP PRIMARY KEY "
        )

    def test_drop_unique(self):
        context = op_fixture('mysql')
        op.drop_constraint("f1", "t1", "unique")
        context.assert_(
            "ALTER TABLE t1 DROP INDEX f1"
        )

    def test_drop_check(self):
        op_fixture('mysql')
        assert_raises_message(
            NotImplementedError,
            "MySQL does not support CHECK constraints.",
            op.drop_constraint, "f1", "t1", "check"
        )

    def test_drop_unknown(self):
        op_fixture('mysql')
        assert_raises_message(
            TypeError,
            "'type' can be one of 'check', 'foreignkey', "
            "'primary', 'unique', None",
            op.drop_constraint, "f1", "t1", "typo"
        )

    def test_drop_generic_constraint(self):
        op_fixture('mysql')
        assert_raises_message(
            NotImplementedError,
            "No generic 'DROP CONSTRAINT' in MySQL - please "
            "specify constraint type",
            op.drop_constraint, "f1", "t1"
        )


class MySQLDefaultCompareTest(TestBase):
    __only_on__ = 'mysql'

    @classmethod
    def setup_class(cls):
        cls.bind = config.db
        staging_env()
        context = MigrationContext.configure(
            connection=cls.bind.connect(),
            opts={
                'compare_type': True,
                'compare_server_default': True
            }
        )
        connection = context.bind
        cls.autogen_context = {
            'imports': set(),
            'connection': connection,
            'dialect': connection.dialect,
            'context': context
        }

    @classmethod
    def teardown_class(cls):
        clear_staging_env()

    def setUp(self):
        self.metadata = MetaData(self.bind)

    def tearDown(self):
        self.metadata.drop_all()

    def _compare_default_roundtrip(self, type_, txt, alternate=None):
        if alternate:
            expected = True
        else:
            alternate = txt
            expected = False
        t = Table(
            "test", self.metadata,
            Column(
                "somecol", type_,
                server_default=text(txt) if txt else None
            )
        )
        t2 = Table("test", MetaData(),
                   Column("somecol", type_, server_default=text(alternate))
                   )
        assert self._compare_default(
            t, t2, t2.c.somecol, alternate
        ) is expected

    def _compare_default(
        self,
        t1, t2, col,
        rendered
    ):
        t1.create(self.bind)
        insp = Inspector.from_engine(self.bind)
        cols = insp.get_columns(t1.name)
        ctx = self.autogen_context['context']
        return ctx.impl.compare_server_default(
            None,
            col,
            rendered,
            cols[0]['default'])

    def test_compare_timestamp_current_timestamp(self):
        self._compare_default_roundtrip(
            TIMESTAMP(),
            "CURRENT_TIMESTAMP",
        )

    def test_compare_timestamp_current_timestamp_diff(self):
        self._compare_default_roundtrip(
            TIMESTAMP(),
            None, "CURRENT_TIMESTAMP",
        )