alembic-verify 0.1.4__tar.gz → 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- alembic_verify-1.0.1/LICENSE +203 -0
- alembic_verify-1.0.1/PKG-INFO +491 -0
- alembic_verify-1.0.1/README.md +453 -0
- alembic_verify-1.0.1/pyproject.toml +152 -0
- alembic_verify-1.0.1/src/alembicverify/__init__.py +20 -0
- alembic_verify-1.0.1/src/alembicverify/_factories.py +96 -0
- alembic_verify-1.0.1/src/alembicverify/pyfixtures.py +35 -0
- alembic_verify-1.0.1/src/alembicverify/util.py +109 -0
- alembic-verify-0.1.4/PKG-INFO +0 -38
- alembic-verify-0.1.4/README.rst +0 -17
- alembic-verify-0.1.4/alembic_verify.egg-info/PKG-INFO +0 -38
- alembic-verify-0.1.4/alembic_verify.egg-info/SOURCES.txt +0 -19
- alembic-verify-0.1.4/alembic_verify.egg-info/dependency_links.txt +0 -1
- alembic-verify-0.1.4/alembic_verify.egg-info/entry_points.txt +0 -3
- alembic-verify-0.1.4/alembic_verify.egg-info/requires.txt +0 -13
- alembic-verify-0.1.4/alembic_verify.egg-info/top_level.txt +0 -2
- alembic-verify-0.1.4/alembic_verify.egg-info/zip-safe +0 -1
- alembic-verify-0.1.4/alembicverify/__init__.py +0 -1
- alembic-verify-0.1.4/alembicverify/pyfixtures.py +0 -32
- alembic-verify-0.1.4/alembicverify/util.py +0 -51
- alembic-verify-0.1.4/docs/__init__.py +0 -0
- alembic-verify-0.1.4/docs/testing/__init__.py +0 -1
- alembic-verify-0.1.4/docs/testing/conftest.py +0 -6
- alembic-verify-0.1.4/docs/testing/models.py +0 -55
- alembic-verify-0.1.4/docs/testing/test_alembic.py +0 -220
- alembic-verify-0.1.4/docs/testing/test_example.py +0 -71
- alembic-verify-0.1.4/docs/testing/test_unittest.py +0 -81
- alembic-verify-0.1.4/setup.cfg +0 -5
- alembic-verify-0.1.4/setup.py +0 -60
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Fabrizio Romano, Mark McArdle
|
|
190
|
+
|
|
191
|
+
Originally developed for Student.com
|
|
192
|
+
|
|
193
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
194
|
+
you may not use this file except in compliance with the License.
|
|
195
|
+
You may obtain a copy of the License at
|
|
196
|
+
|
|
197
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
198
|
+
|
|
199
|
+
Unless required by applicable law or agreed to in writing, software
|
|
200
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
201
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
202
|
+
See the License for the specific language governing permissions and
|
|
203
|
+
limitations under the License.
|
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: alembic-verify
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: A library to verify alembic migrations.
|
|
5
|
+
Keywords: alembic,migration,verify,sqlalchemy
|
|
6
|
+
Author-Email: Fabrizio Romano <gianchub@gmail.com>, Mark McArdle <m.mc4rdle@gmail.com>
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
17
|
+
Classifier: Topic :: Database
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Project-URL: Homepage, https://github.com/gianchub/alembic-verify
|
|
20
|
+
Project-URL: Issues, https://github.com/gianchub/alembic-verify/issues
|
|
21
|
+
Project-URL: License, https://github.com/gianchub/alembic-verify/blob/master/LICENSE
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Requires-Dist: pytest>=7.0
|
|
24
|
+
Requires-Dist: alembic>=1.8
|
|
25
|
+
Requires-Dist: sqlalchemy<3,>=1.4
|
|
26
|
+
Requires-Dist: sqlalchemy-utils>=0.40.0
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pdbpp; extra == "dev"
|
|
29
|
+
Requires-Dist: ty; extra == "dev"
|
|
30
|
+
Provides-Extra: lint
|
|
31
|
+
Requires-Dist: ruff~=0.14.13; extra == "lint"
|
|
32
|
+
Provides-Extra: test
|
|
33
|
+
Requires-Dist: pytest-cov; extra == "test"
|
|
34
|
+
Requires-Dist: psycopg2-binary; extra == "test"
|
|
35
|
+
Provides-Extra: all
|
|
36
|
+
Requires-Dist: alembic-verify[dev,lint,test]; extra == "all"
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
|
|
39
|
+
# Alembic Verify
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## Description
|
|
43
|
+
|
|
44
|
+
Alembic Verify is a library that provides utilities and pytest fixtures to verify that migrations
|
|
45
|
+
produce the expected database schema and to prepare the database schema from migrations.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## Table of Contents
|
|
49
|
+
|
|
50
|
+
- [Alembic Verify](#alembic-verify)
|
|
51
|
+
- [Description](#description)
|
|
52
|
+
- [Table of Contents](#table-of-contents)
|
|
53
|
+
- [Notable changes in version 1.\*](#notable-changes-in-version-1)
|
|
54
|
+
- [Requirements](#requirements)
|
|
55
|
+
- [Installation](#installation)
|
|
56
|
+
- [Usage](#usage)
|
|
57
|
+
- [Quick Start](#quick-start)
|
|
58
|
+
- [A quick test example](#a-quick-test-example)
|
|
59
|
+
- [Fixtures Provided](#fixtures-provided)
|
|
60
|
+
- [`alembic_new_db`](#alembic_new_db)
|
|
61
|
+
- [`alembic_config`](#alembic_config)
|
|
62
|
+
- [Utility Functions](#utility-functions)
|
|
63
|
+
- [`prepare_schema_from_migrations(uri, config, revision="head")`](#prepare_schema_from_migrationsuri-config-revisionhead)
|
|
64
|
+
- [`get_current_revision(config, engine, script)`](#get_current_revisionconfig-engine-script)
|
|
65
|
+
- [`get_head_revision(config, engine, script)`](#get_head_revisionconfig-engine-script)
|
|
66
|
+
- [Testing Upgrade/Downgrade Cycles](#testing-upgradedowngrade-cycles)
|
|
67
|
+
- [Branched Migrations](#branched-migrations)
|
|
68
|
+
- [Session for Engine](#session-for-engine)
|
|
69
|
+
- [Deprecated Fixtures](#deprecated-fixtures)
|
|
70
|
+
- [Fixtures that are no longer needed](#fixtures-that-are-no-longer-needed)
|
|
71
|
+
- [Creating custom fixtures](#creating-custom-fixtures)
|
|
72
|
+
- [Development](#development)
|
|
73
|
+
- [Setup](#setup)
|
|
74
|
+
- [Running Tests](#running-tests)
|
|
75
|
+
- [Linting and Formatting](#linting-and-formatting)
|
|
76
|
+
- [Using Tox](#using-tox)
|
|
77
|
+
- [Compatibility](#compatibility)
|
|
78
|
+
- [License](#license)
|
|
79
|
+
- [Contributing](#contributing)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
## Notable changes in version 1.*
|
|
83
|
+
|
|
84
|
+
In this version, the library has been rewritten to be completely untied from the [sqlalchemy-diff](https://github.com/gianchub/sqlalchemy-diff) project.
|
|
85
|
+
|
|
86
|
+
We have renamed the fixtures to be more consistent and easier to understand. The reason why this work was needed is because the library was born as part of the sqlalchemy-diff project, but then published as a separate project. As a consequence, the naming of the fixtures of the editions prior to 1.* was consistent with the needs of the [sqlalchemy-diff](https://github.com/gianchub/sqlalchemy-diff) project, which is now a totally separate library, to which alembic-verify is no longer related.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
## Requirements
|
|
90
|
+
|
|
91
|
+
- Python 3.10, 3.11, 3.12, 3.13, or 3.14
|
|
92
|
+
- SQLAlchemy 1.4.* or 2.0+
|
|
93
|
+
- Alembic >= 1.8.0
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
## Installation
|
|
97
|
+
|
|
98
|
+
Install using pip:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
$ pip install alembic-verify
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Or using uv:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
$ uv pip install alembic-verify
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Usage
|
|
111
|
+
|
|
112
|
+
The library supports any database supported by both [SQLAlchemy](https://www.sqlalchemy.org/) and [sqlalchemy-utils](https://sqlalchemy-utils.readthedocs.io/en/latest/).
|
|
113
|
+
|
|
114
|
+
### Quick Start
|
|
115
|
+
|
|
116
|
+
You need to provide two fixtures, which can either live in your test modules, or in the `conftest.py` file of your project, according to your needs.
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from uuid import uuid4
|
|
120
|
+
import pytest
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@pytest.fixture(scope="module")
|
|
124
|
+
def alembic_ini_location():
|
|
125
|
+
"""Path to your alembic.ini file."""
|
|
126
|
+
return str("path/to/alembic.ini")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@pytest.fixture
|
|
130
|
+
def alembic_db_uri():
|
|
131
|
+
"""Database URI. Normally in tests you will want a unique temporary database name."""
|
|
132
|
+
base_uri = "postgresql://postgres:postgres@localhost:5432/"
|
|
133
|
+
return f"{base_uri}test_{uuid4().hex}"
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Once the fixtures are defined, you can proceed to write your migration tests.
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
### A quick test example
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
import pytest
|
|
143
|
+
from alembicverify.util import prepare_schema_from_migrations
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@pytest.mark.usefixtures("alembic_new_db")
|
|
147
|
+
def test_migrations(alembic_config, alembic_db_uri):
|
|
148
|
+
with prepare_schema_from_migrations(
|
|
149
|
+
alembic_db_uri, alembic_config, revision="head"
|
|
150
|
+
) as (engine, script):
|
|
151
|
+
# All migrations applied successfully
|
|
152
|
+
with engine.connect() as conn:
|
|
153
|
+
# Query your tables, verify schema, etc.
|
|
154
|
+
pass
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
You can also specify a specific revision to apply:
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
with prepare_schema_from_migrations(
|
|
161
|
+
alembic_db_uri, alembic_config, revision="some_revision"
|
|
162
|
+
) as (engine, script):
|
|
163
|
+
# All migrations applied successfully
|
|
164
|
+
pass
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
### Fixtures Provided
|
|
169
|
+
|
|
170
|
+
The library provides two pytest fixtures:
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
#### `alembic_new_db`
|
|
174
|
+
|
|
175
|
+
Creates a temporary database before the test and drops it after. Depends on the `alembic_db_uri` fixture. Use it as a marker:
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
@pytest.mark.usefixtures("alembic_new_db")
|
|
179
|
+
def test_migrations(alembic_config, alembic_db_uri):
|
|
180
|
+
...
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
This fixture depends on the `alembic_db_uri` fixture.
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
#### `alembic_config`
|
|
187
|
+
|
|
188
|
+
Returns a configured `alembic.config.Config` object with:
|
|
189
|
+
- Database URL set from your `alembic_db_uri` fixture
|
|
190
|
+
- Script location loaded from your `alembic_ini_location` fixture
|
|
191
|
+
|
|
192
|
+
This fixture depends on both the `alembic_db_uri` and `alembic_ini_location` fixtures.
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
### Utility Functions
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
#### `prepare_schema_from_migrations(uri, config, revision="head")`
|
|
199
|
+
|
|
200
|
+
Applies migrations to the database and returns an engine and script directory.
|
|
201
|
+
|
|
202
|
+
**Context manager usage** (recommended - automatically disposes the engine):
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
import pytest
|
|
206
|
+
from alembicverify.util import prepare_schema_from_migrations
|
|
207
|
+
|
|
208
|
+
@pytest.mark.usefixtures("alembic_new_db")
|
|
209
|
+
def test_migrations(alembic_config, alembic_db_uri):
|
|
210
|
+
with prepare_schema_from_migrations(
|
|
211
|
+
alembic_db_uri, alembic_config, revision="head"
|
|
212
|
+
) as (engine, script):
|
|
213
|
+
# `engine` will be automatically disposed after the context manager exits
|
|
214
|
+
pass
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
**Regular function-style usage** (requires manual disposal of the engine):
|
|
218
|
+
|
|
219
|
+
```python
|
|
220
|
+
import pytest
|
|
221
|
+
from alembicverify.util import prepare_schema_from_migrations
|
|
222
|
+
|
|
223
|
+
@pytest.mark.usefixtures("alembic_new_db")
|
|
224
|
+
def test_migration_upgrade_and_downgrade(alembic_config, alembic_db_uri):
|
|
225
|
+
engine, script = prepare_schema_from_migrations(
|
|
226
|
+
alembic_db_uri, alembic_config, revision="head"
|
|
227
|
+
)
|
|
228
|
+
try:
|
|
229
|
+
# Use engine and script
|
|
230
|
+
pass
|
|
231
|
+
finally:
|
|
232
|
+
engine.dispose() # Must dispose engine manually
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
#### `get_current_revision(config, engine, script)`
|
|
237
|
+
|
|
238
|
+
Returns the current applied revision from the database:
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
import pytest
|
|
242
|
+
from alembicverify.util import get_current_revision, prepare_schema_from_migrations
|
|
243
|
+
|
|
244
|
+
@pytest.mark.usefixtures("alembic_new_db")
|
|
245
|
+
def test_migrations(alembic_config, alembic_db_uri):
|
|
246
|
+
with prepare_schema_from_migrations(
|
|
247
|
+
alembic_db_uri, alembic_config, revision="head"
|
|
248
|
+
) as (engine, script):
|
|
249
|
+
current = get_current_revision(alembic_config, engine, script)
|
|
250
|
+
assert current == "abc123def456"
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
#### `get_head_revision(config, engine, script)`
|
|
254
|
+
|
|
255
|
+
Returns the latest (head) revision from the migration chain:
|
|
256
|
+
|
|
257
|
+
```python
|
|
258
|
+
import pytest
|
|
259
|
+
from alembicverify.util import get_head_revision, prepare_schema_from_migrations
|
|
260
|
+
|
|
261
|
+
@pytest.mark.usefixtures("alembic_new_db")
|
|
262
|
+
def test_migrations(alembic_config, alembic_db_uri):
|
|
263
|
+
with prepare_schema_from_migrations(
|
|
264
|
+
alembic_db_uri, alembic_config, revision="head"
|
|
265
|
+
) as (engine, script):
|
|
266
|
+
head = get_head_revision(alembic_config, engine, script)
|
|
267
|
+
assert head == "abc123def456"
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Testing Upgrade/Downgrade Cycles
|
|
271
|
+
|
|
272
|
+
You can test that migrations can be applied and rolled back:
|
|
273
|
+
|
|
274
|
+
```python
|
|
275
|
+
import pytest
|
|
276
|
+
from alembic import command
|
|
277
|
+
from alembicverify.util import prepare_schema_from_migrations, get_current_revision
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
@pytest.mark.usefixtures("alembic_new_db")
|
|
281
|
+
def test_migration_upgrade_and_downgrade(alembic_config, alembic_db_uri):
|
|
282
|
+
with prepare_schema_from_migrations(
|
|
283
|
+
alembic_db_uri, alembic_config, revision="head"
|
|
284
|
+
) as (engine, script):
|
|
285
|
+
# Collect all revisions by downgrading one at a time
|
|
286
|
+
revisions = []
|
|
287
|
+
while True:
|
|
288
|
+
rev = get_current_revision(alembic_config, engine, script)
|
|
289
|
+
if rev is None:
|
|
290
|
+
break
|
|
291
|
+
command.downgrade(alembic_config, "-1")
|
|
292
|
+
revisions.append(rev)
|
|
293
|
+
|
|
294
|
+
# Verify the expected revision chain
|
|
295
|
+
assert revisions == [
|
|
296
|
+
"latest_revision",
|
|
297
|
+
"previous_revision",
|
|
298
|
+
"initial_revision",
|
|
299
|
+
]
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Branched Migrations
|
|
303
|
+
|
|
304
|
+
If your migration history has branches, you can target a specific branch using the `revision@head` syntax:
|
|
305
|
+
|
|
306
|
+
```python
|
|
307
|
+
with prepare_schema_from_migrations(
|
|
308
|
+
alembic_db_uri, alembic_config, revision="branch_revision@head"
|
|
309
|
+
) as (engine, script):
|
|
310
|
+
# Migrations applied up to head of the specified branch
|
|
311
|
+
pass
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### Session for Engine
|
|
315
|
+
|
|
316
|
+
The library provides a utility function to create a session for an engine. This is useful for testing, for example to verify the application of a migration in detail. You might need to get two different session instances to use before and after the application of a migration.
|
|
317
|
+
|
|
318
|
+
Here's a full example:
|
|
319
|
+
|
|
320
|
+
```python
|
|
321
|
+
import pytest
|
|
322
|
+
from alembic import command
|
|
323
|
+
from alembicverify.util import prepare_schema_from_migrations, session_for_engine
|
|
324
|
+
|
|
325
|
+
down_revision = "44352f0a4052"
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
@pytest.mark.usefixtures("alembic_new_db")
|
|
329
|
+
def test_upgrade(alembic_config, alembic_db_uri):
|
|
330
|
+
with prepare_schema_from_migrations(alembic_db_uri, alembic_config, revision=down_revision) as (
|
|
331
|
+
engine,
|
|
332
|
+
_,
|
|
333
|
+
):
|
|
334
|
+
with session_for_engine(engine) as session:
|
|
335
|
+
# populate the database, inspect the schema, etc.
|
|
336
|
+
...
|
|
337
|
+
|
|
338
|
+
# then apply the desired migration
|
|
339
|
+
command.upgrade(alembic_config, "+1")
|
|
340
|
+
|
|
341
|
+
with session_for_engine(engine) as session:
|
|
342
|
+
# after the migration, verify the schema, its data, etc.
|
|
343
|
+
...
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
### Deprecated Fixtures
|
|
347
|
+
|
|
348
|
+
The library provides four deprecated fixtures:
|
|
349
|
+
|
|
350
|
+
- `new_db_left`: Deprecated in favor of `alembic_new_db`
|
|
351
|
+
- Depends on the `uri_left` fixture
|
|
352
|
+
- `new_db_right`: Deprecated in favor of `alembic_new_db`
|
|
353
|
+
- Depends on the `uri_right` fixture
|
|
354
|
+
- `alembic_config_left`: Deprecated in favor of `alembic_config`
|
|
355
|
+
- Depends on the `uri_left` fixture and the `alembic_ini_location` fixture
|
|
356
|
+
- `alembic_config_right`: Deprecated in favor of `alembic_config`
|
|
357
|
+
- Depends on the `uri_right` fixture and the `alembic_ini_location` fixture
|
|
358
|
+
|
|
359
|
+
These fixtures are still available for backwards compatibility, but will issue a deprecation warning.
|
|
360
|
+
|
|
361
|
+
**Note**: The `uri_left` and `uri_right` fixtures are no longer needed when using the library in the new way, they have been replaced by the `alembic_db_uri` fixture.
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
### Fixtures that are no longer needed
|
|
365
|
+
|
|
366
|
+
The library no longer uses the `alembic_root` fixture, so you don't need to provide it.
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
### Creating custom fixtures
|
|
370
|
+
|
|
371
|
+
In some situations, you may want to spawn more than one random database for your tests.
|
|
372
|
+
For example, if you are using `alembic-verify` in combination with `sqlalchemy-diff`, you will
|
|
373
|
+
need to spawn two random databases.
|
|
374
|
+
|
|
375
|
+
You can create custom fixtures by using the factory functions, like in the following example:
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
```python
|
|
379
|
+
from uuid import uuid4
|
|
380
|
+
import pytest
|
|
381
|
+
|
|
382
|
+
from alembicverify import alembic_config_factory, new_db_factory
|
|
383
|
+
from alembicverify.util import prepare_schema_from_migrations
|
|
384
|
+
from test.integration.conftest import get_temporary_uri
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
@pytest.fixture
|
|
388
|
+
def alembic_db_uri_custom():
|
|
389
|
+
base_uri = "postgresql://postgres:postgres@localhost:5432/"
|
|
390
|
+
return f"{base_uri}test_{uuid4().hex}"
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
alembic_config_custom = alembic_config_factory(
|
|
394
|
+
alembic_db_uri_fixture_name="alembic_db_uri_custom",
|
|
395
|
+
alembic_ini_location_fixture_name="alembic_ini_location",
|
|
396
|
+
name="alembic_config_custom",
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
alembic_new_db_custom = new_db_factory(
|
|
400
|
+
alembic_db_uri_fixture_name="alembic_db_uri_custom",
|
|
401
|
+
name="alembic_new_db_custom",
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
@pytest.mark.usefixtures("alembic_new_db_custom")
|
|
406
|
+
def test_migration_upgrade_and_downgrade_context_manager(
|
|
407
|
+
alembic_config_custom, alembic_db_uri_custom
|
|
408
|
+
):
|
|
409
|
+
with prepare_schema_from_migrations(
|
|
410
|
+
alembic_db_uri_custom, alembic_config_custom, revision="head"
|
|
411
|
+
) as (
|
|
412
|
+
engine,
|
|
413
|
+
script,
|
|
414
|
+
):
|
|
415
|
+
# use engine and script
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
## Development
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
### Setup
|
|
423
|
+
|
|
424
|
+
1. Clone the repository:
|
|
425
|
+
```bash
|
|
426
|
+
$ git clone https://github.com/gianchub/alembic-verify.git
|
|
427
|
+
$ cd alembic-verify
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
2. Install development dependencies:
|
|
431
|
+
```bash
|
|
432
|
+
$ uv pip install -e .[all]
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
### Running Tests
|
|
437
|
+
|
|
438
|
+
Use the Makefile:
|
|
439
|
+
|
|
440
|
+
```bash
|
|
441
|
+
$ make test
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
### Linting and Formatting
|
|
446
|
+
|
|
447
|
+
The project uses [Ruff](https://github.com/astral-sh/ruff) for linting and formatting.
|
|
448
|
+
|
|
449
|
+
```bash
|
|
450
|
+
$ make format
|
|
451
|
+
$ make lint
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
### Using Tox
|
|
456
|
+
|
|
457
|
+
Test across multiple Python and SQLAlchemy versions:
|
|
458
|
+
|
|
459
|
+
```bash
|
|
460
|
+
# Install tox with uv
|
|
461
|
+
$ make install-tox-uv
|
|
462
|
+
|
|
463
|
+
# Run all test environments
|
|
464
|
+
$ make tox
|
|
465
|
+
|
|
466
|
+
# Run specific environment
|
|
467
|
+
$ tox -e py314-test-sa14
|
|
468
|
+
$ tox -e py314-test-sa20
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
## Compatibility
|
|
473
|
+
|
|
474
|
+
See the [pyproject.toml](pyproject.toml) file for the requirements.
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
## License
|
|
478
|
+
|
|
479
|
+
Apache 2.0. See [LICENSE](LICENSE) for details.
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
## Contributing
|
|
483
|
+
|
|
484
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
485
|
+
|
|
486
|
+
Before submitting a pull request, please ensure that:
|
|
487
|
+
|
|
488
|
+
- You have added any tests needed to cover the changes you have made
|
|
489
|
+
- All tests pass
|
|
490
|
+
- The code is formatted correctly
|
|
491
|
+
- The documentation (including docstrings and README.md) is up to date
|