summaryrefslogtreecommitdiff
path: root/tests/requirements.py
blob: dbbb88a5676037ad4ca03efda985b8ded267ec7a (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
from sqlalchemy import exc as sqla_exc
from sqlalchemy import text

from alembic.testing import exclusions
from alembic.testing.requirements import SuiteRequirements
from alembic.util import compat
from alembic.util import sqla_compat


class DefaultRequirements(SuiteRequirements):
    @property
    def unicode_string(self):
        return exclusions.skip_if(["oracle"])

    @property
    def alter_column(self):
        return exclusions.skip_if(["sqlite"], "no ALTER COLUMN support")

    @property
    def schemas(self):
        """Target database must support external schemas, and have one
        named 'test_schema'."""

        return exclusions.skip_if(["sqlite", "firebird"], "no schema support")

    @property
    def no_referential_integrity(self):
        """test will fail if referential integrity is enforced"""

        return exclusions.fails_on_everything_except("sqlite")

    @property
    def non_native_boolean(self):
        """test will fail if native boolean is provided"""

        return exclusions.fails_if(
            exclusions.LambdaPredicate(
                lambda config: config.db.dialect.supports_native_boolean
            )
        )

    @property
    def non_native_boolean_check_constraint(self):
        """backend creates a check constraint for booleans if enabled"""

        return exclusions.only_on(
            exclusions.LambdaPredicate(
                lambda config: not config.db.dialect.supports_native_boolean
                and config.db.dialect.non_native_boolean_check_constraint
            )
        )

    @property
    def check_constraints_w_enforcement(self):
        return exclusions.fails_on(["mysql", "mariadb"])

    @property
    def unnamed_constraints(self):
        """constraints without names are supported."""
        return exclusions.only_on(["sqlite"])

    @property
    def fk_names(self):
        """foreign key constraints always have names in the DB"""
        return exclusions.fails_on("sqlite")

    @property
    def reflects_fk_options(self):
        return exclusions.open()

    @property
    def fk_initially(self):
        """backend supports INITIALLY option in foreign keys"""
        return exclusions.only_on(["postgresql"])

    @property
    def fk_deferrable(self):
        """backend supports DEFERRABLE option in foreign keys"""
        return exclusions.only_on(["postgresql", "oracle"])

    @property
    def fk_deferrable_is_reflected(self):
        return self.fk_deferrable + exclusions.fails_on("oracle")

    @property
    def fk_ondelete_restrict(self):
        return exclusions.only_on(["postgresql", "sqlite", "mysql"])

    @property
    def fk_onupdate_restrict(self):
        return self.fk_onupdate + exclusions.fails_on(["mssql"])

    @property
    def fk_ondelete_noaction(self):
        return exclusions.only_on(
            ["postgresql", "mysql", "mariadb", "sqlite", "mssql"]
        )

    @property
    def fk_ondelete_is_reflected(self):
        def go(config):
            if exclusions.against(config, "mssql"):
                return not sqla_compat.sqla_14_26
            else:
                return False

        return exclusions.fails_if(go)

    @property
    def fk_onupdate_is_reflected(self):
        def go(config):
            if exclusions.against(config, "mssql"):
                return not sqla_compat.sqla_14_26
            else:
                return False

        return self.fk_onupdate + exclusions.fails_if(go)

    @property
    def fk_onupdate(self):
        return exclusions.only_on(
            ["postgresql", "mysql", "mariadb", "sqlite", "mssql"]
        )

    @property
    def reflects_unique_constraints_unambiguously(self):
        return exclusions.fails_on(["mysql", "mariadb", "oracle", "mssql"])

    @property
    def reports_unique_constraints_as_indexes(self):
        return exclusions.only_on(["mysql", "mariadb", "oracle"])

    @property
    def reports_unnamed_constraints(self):
        return exclusions.skip_if(["sqlite"])

    @property
    def reflects_indexes_w_sorting(self):
        # TODO: figure out what's happening on the SQLAlchemy side
        # when we reflect an index that has asc() / desc() on the column
        # Tracked by https://github.com/sqlalchemy/sqlalchemy/issues/9597
        return exclusions.fails_on(["oracle"])

    @property
    def reflects_indexes_column_sorting(self):
        "Actually reflect column_sorting on the indexes"
        return exclusions.only_on(["postgresql"])

    @property
    def long_names(self):
        if sqla_compat.sqla_14:
            return exclusions.skip_if("oracle<18")
        else:
            return exclusions.skip_if("oracle")

    @property
    def reflects_pk_names(self):
        """Target driver reflects the name of primary key constraints."""

        return exclusions.fails_on_everything_except(
            "postgresql", "oracle", "mssql", "sybase", "sqlite"
        )

    @property
    def datetime_timezone(self):
        """target dialect supports timezone with datetime types."""

        return exclusions.only_on(["postgresql"])

    @property
    def postgresql(self):
        return exclusions.only_on(["postgresql"])

    @property
    def mysql(self):
        return exclusions.only_on(["mysql", "mariadb"])

    @property
    def oracle(self):
        return exclusions.only_on(["oracle"])

    @property
    def mssql(self):
        return exclusions.only_on(["mssql"])

    @property
    def covering_indexes(self):
        return exclusions.only_on(["postgresql >= 11", "mssql"])

    @property
    def postgresql_uuid_ossp(self):
        def check_uuid_ossp(config):
            if not exclusions.against(config, "postgresql"):
                return False
            try:
                config.db.execute("SELECT uuid_generate_v4()")
                return True
            except:
                return False

        return exclusions.only_if(check_uuid_ossp)

    def _has_pg_extension(self, name):
        def check(config):
            if not exclusions.against(config, "postgresql"):
                return False
            with config.db.connect() as conn:
                count = conn.scalar(
                    text(
                        "SELECT count(*) FROM pg_extension "
                        "WHERE extname='%s'" % name
                    )
                )
            return bool(count)

        return exclusions.only_if(check, "needs %s extension" % name)

    @property
    def hstore(self):
        return self._has_pg_extension("hstore")

    @property
    def btree_gist(self):
        return self._has_pg_extension("btree_gist")

    @property
    def autoincrement_on_composite_pk(self):
        return exclusions.skip_if(["sqlite"], "not supported by database")

    @property
    def integer_subtype_comparisons(self):
        """if a compare of Integer and BigInteger is supported yet."""
        return exclusions.skip_if(["oracle"], "not supported by alembic impl")

    @property
    def autocommit_isolation(self):
        """target database should support 'AUTOCOMMIT' isolation level"""

        return exclusions.only_on(["postgresql", "mysql", "mariadb"])

    @property
    def computed_columns(self):
        # TODO: in theory if these could come from SQLAlchemy dialects
        # that would be helpful
        return self.computed_columns_api + exclusions.skip_if(
            ["postgresql < 12", "sqlite < 3.31", "mysql < 5.7"]
        )

    @property
    def computed_reflects_as_server_default(self):
        # note that this rule will go away when SQLAlchemy correctly
        # supports reflection of the "computed" construct; the element
        # will consistently be present as both column.computed and
        # column.server_default for all supported backends.
        return (
            self.computed_columns
            + exclusions.only_if(
                ["postgresql", "oracle"],
                "backend reflects computed construct as a server default",
            )
            + exclusions.skip_if(self.computed_reflects_normally)
        )

    @property
    def computed_doesnt_reflect_as_server_default(self):
        # note that this rule will go away when SQLAlchemy correctly
        # supports reflection of the "computed" construct; the element
        # will consistently be present as both column.computed and
        # column.server_default for all supported backends.
        return (
            self.computed_columns
            + exclusions.skip_if(
                ["postgresql", "oracle"],
                "backend reflects computed construct as a server default",
            )
            + exclusions.skip_if(self.computed_reflects_normally)
        )

    @property
    def check_constraint_reflection(self):
        return exclusions.fails_on_everything_except(
            "postgresql",
            "sqlite",
            "oracle",
            self._mysql_and_check_constraints_exist,
        )

    def mysql_check_col_name_change(self, config):
        # MySQL has check constraints that enforce an reflect, however
        # they prevent a column's name from being changed due to a bug in
        # MariaDB 10.2 as well as MySQL 8.0.16
        if exclusions.against(config, ["mysql", "mariadb"]):
            if sqla_compat._is_mariadb(config.db.dialect):
                mnvi = sqla_compat._mariadb_normalized_version_info
                norm_version_info = mnvi(config.db.dialect)
                return norm_version_info >= (10, 2) and norm_version_info < (
                    10,
                    2,
                    22,
                )
            else:
                norm_version_info = config.db.dialect.server_version_info
                return norm_version_info >= (8, 0, 16)

        else:
            return True

    def _mysql_and_check_constraints_exist(self, config):
        # 1. we have mysql / mariadb and
        # 2. it enforces check constraints
        if exclusions.against(config, ["mysql", "mariadb"]):
            if sqla_compat._is_mariadb(config.db.dialect):
                mnvi = sqla_compat._mariadb_normalized_version_info
                norm_version_info = mnvi(config.db.dialect)
                return norm_version_info >= (10, 2)
            else:
                norm_version_info = config.db.dialect.server_version_info
                return norm_version_info >= (8, 0, 16)
        else:
            return False

    @property
    def json_type(self):
        return exclusions.only_on(
            [
                lambda config: exclusions.against(config, "mysql")
                and (
                    (
                        not config.db.dialect._is_mariadb
                        and exclusions.against(config, "mysql >= 5.7")
                    )
                    or (
                        config.db.dialect._mariadb_normalized_version_info
                        >= (10, 2, 7)
                    )
                ),
                "mariadb>=10.2.7",
                "postgresql >= 9.3",
                self._sqlite_json,
                self._mssql_json,
            ]
        )

    def _mssql_json(self, config):
        if not sqla_compat.sqla_14:
            return False
        else:
            return exclusions.against(config, "mssql")

    def _sqlite_json(self, config):
        if not sqla_compat.sqla_14:
            return False
        elif not exclusions.against(config, "sqlite >= 3.9"):
            return False
        else:
            with config.db.connect() as conn:
                try:
                    return (
                        conn.execute(
                            text(
                                """select json_extract('{"foo": "bar"}', """
                                """'$."foo"')"""
                            )
                        ).scalar()
                        == "bar"
                    )
                except sqla_exc.DBAPIError:
                    return False

    @property
    def identity_columns(self):
        # TODO: in theory if these could come from SQLAlchemy dialects
        # that would be helpful
        return self.identity_columns_api + exclusions.only_on(
            ["postgresql >= 10", "oracle >= 12", "mssql"]
        )

    @property
    def identity_columns_alter(self):
        # TODO: in theory if these could come from SQLAlchemy dialects
        # that would be helpful
        return self.identity_columns_api + exclusions.only_on(
            ["postgresql >= 10", "oracle >= 12"]
        )

    @property
    def supports_identity_on_null(self):
        return self.identity_columns + exclusions.only_on(["oracle"])

    @property
    def legacy_engine(self):
        return exclusions.only_if(
            lambda config: not getattr(config.db, "_is_future", False)
        )

    @property
    def stubs_test(self):
        def requirements():
            try:
                import black  # noqa
                import zimports  # noqa

                return False
            except Exception:
                return True

        imports = exclusions.skip_if(
            requirements, "black and zimports are required for this test"
        )
        version = exclusions.only_if(
            lambda _: compat.py311, "python 3.11 is required"
        )

        sqlalchemy = exclusions.only_if(
            lambda _: sqla_compat.sqla_2, "sqlalchemy 2 is required"
        )

        return imports + version + sqlalchemy

    @property
    def reflect_indexes_with_expressions(self):
        sqlalchemy = exclusions.only_if(
            lambda _: sqla_compat.sqla_2, "sqlalchemy 2 is required"
        )

        postgresql = exclusions.only_on(["postgresql"])

        return sqlalchemy + postgresql

    @property
    def indexes_with_expressions(self):
        return exclusions.only_on(["postgresql", "sqlite>=3.9.0"])