alembic-guard 0.1.0__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_guard-0.1.0/.github/workflows/ci.yml +44 -0
- alembic_guard-0.1.0/.github/workflows/publish.yml +20 -0
- alembic_guard-0.1.0/.gitignore +6 -0
- alembic_guard-0.1.0/LICENSE +21 -0
- alembic_guard-0.1.0/PKG-INFO +158 -0
- alembic_guard-0.1.0/README.md +138 -0
- alembic_guard-0.1.0/action.yml +59 -0
- alembic_guard-0.1.0/examples/migrations/env.py +0 -0
- alembic_guard-0.1.0/examples/migrations/versions/0001_create_users.py +23 -0
- alembic_guard-0.1.0/examples/migrations/versions/0002_add_email.py +23 -0
- alembic_guard-0.1.0/examples/migrations/versions/0003_add_email_safely.py +29 -0
- alembic_guard-0.1.0/pyproject.toml +38 -0
- alembic_guard-0.1.0/src/alembic_guard/__init__.py +8 -0
- alembic_guard-0.1.0/src/alembic_guard/__main__.py +5 -0
- alembic_guard-0.1.0/src/alembic_guard/checker.py +431 -0
- alembic_guard-0.1.0/src/alembic_guard/cli.py +171 -0
- alembic_guard-0.1.0/src/alembic_guard/config.py +34 -0
- alembic_guard-0.1.0/src/alembic_guard/report.py +84 -0
- alembic_guard-0.1.0/src/alembic_guard/rules.py +176 -0
- alembic_guard-0.1.0/tests/test_cli.py +127 -0
- alembic_guard-0.1.0/tests/test_rules.py +284 -0
- alembic_guard-0.1.0/uv.lock +160 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: astral-sh/setup-uv@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: ${{ matrix.python-version }}
|
|
19
|
+
- run: uv sync
|
|
20
|
+
- run: uv run pytest -q
|
|
21
|
+
|
|
22
|
+
# Dogfood the action itself: the safe examples must pass, the dangerous one must fail.
|
|
23
|
+
action:
|
|
24
|
+
runs-on: ubuntu-latest
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@v4
|
|
27
|
+
- name: Safe migrations pass
|
|
28
|
+
uses: ./
|
|
29
|
+
with:
|
|
30
|
+
paths: examples/migrations/versions/0001_create_users.py examples/migrations/versions/0003_add_email_safely.py
|
|
31
|
+
strict: "true"
|
|
32
|
+
changed-only: "false"
|
|
33
|
+
- name: Dangerous migration fails
|
|
34
|
+
id: dangerous
|
|
35
|
+
continue-on-error: true
|
|
36
|
+
uses: ./
|
|
37
|
+
with:
|
|
38
|
+
paths: examples/migrations/versions/0002_add_email.py
|
|
39
|
+
changed-only: "false"
|
|
40
|
+
- name: Assert the dangerous migration was caught
|
|
41
|
+
if: steps.dangerous.outcome != 'failure'
|
|
42
|
+
run: |
|
|
43
|
+
echo "alembic-guard should have failed on 0002_add_email.py"
|
|
44
|
+
exit 1
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
# Runs when a GitHub release is published. Uses PyPI trusted publishing,
|
|
4
|
+
# so no API token is stored in the repo.
|
|
5
|
+
on:
|
|
6
|
+
release:
|
|
7
|
+
types: [published]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
publish:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
environment: pypi
|
|
13
|
+
permissions:
|
|
14
|
+
id-token: write
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: astral-sh/setup-uv@v5
|
|
18
|
+
- run: uv run --with pytest pytest -q
|
|
19
|
+
- run: uv build
|
|
20
|
+
- run: uv publish --trusted-publishing always
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shalom Hunukumbura
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: alembic-guard
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Catch Alembic migrations that lock tables or break running code before they reach production.
|
|
5
|
+
Project-URL: Homepage, https://github.com/ShalomHunukumbura/alembic-guard
|
|
6
|
+
Project-URL: Issues, https://github.com/ShalomHunukumbura/alembic-guard/issues
|
|
7
|
+
Author: Shalom Hunukumbura
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: alembic,linter,migrations,postgresql,sqlalchemy,zero-downtime
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Database
|
|
16
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: tomli>=2.0; python_version < '3.11'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# alembic-guard
|
|
22
|
+
|
|
23
|
+
**Catch Alembic migrations that lock tables or break running code, before they reach production.**
|
|
24
|
+
|
|
25
|
+
This migration passes code review, passes tests on an empty dev database, and takes down production:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
def upgrade():
|
|
29
|
+
op.add_column("users", sa.Column("email", sa.String(255), nullable=False))
|
|
30
|
+
op.create_index("ix_users_email", "users", ["email"], unique=True)
|
|
31
|
+
op.alter_column("users", "name", new_column_name="full_name")
|
|
32
|
+
op.create_foreign_key("fk_orders_user", "orders", "users", ["user_id"], ["id"])
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```console
|
|
36
|
+
$ alembic-guard
|
|
37
|
+
migrations/versions/0002_add_email.py:13:5 AG001 error Adding NOT NULL column "users.email" without a server_default fails on a table with rows.
|
|
38
|
+
fix: Add a server_default, or add the column as nullable, backfill it, then set NOT NULL in a later migration.
|
|
39
|
+
migrations/versions/0002_add_email.py:14:5 AG002 warning Creating index "ix_users_email" on "users" blocks writes until it is built.
|
|
40
|
+
fix: Pass postgresql_concurrently=True and run it inside `with op.get_context().autocommit_block():`.
|
|
41
|
+
migrations/versions/0002_add_email.py:15:5 AG004 error Renaming "users.name" to "full_name" breaks code still using the old name.
|
|
42
|
+
fix: Expand and contract: add the new column or table, write to both, backfill, move reads over, then drop the old one in a later release.
|
|
43
|
+
migrations/versions/0002_add_email.py:16:5 AG008 warning Foreign key from "orders" to "users" is validated while blocking writes on both tables.
|
|
44
|
+
fix: Pass postgresql_not_valid=True, then run `ALTER TABLE ... VALIDATE CONSTRAINT ...` in a separate migration. Validation only takes a light lock.
|
|
45
|
+
Found 2 errors and 2 warnings in 3 migrations.
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Tools like [squawk](https://squawkhq.com) do this for raw SQL. alembic-guard reads your
|
|
49
|
+
**Alembic Python migrations** directly, so it understands `op.add_column`,
|
|
50
|
+
`batch_alter_table`, `autocommit_block` and the rest, with no database and no SQL
|
|
51
|
+
generation step.
|
|
52
|
+
|
|
53
|
+
## Install
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install alembic-guard # or: uv tool install alembic-guard
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
alembic-guard # finds every Alembic versions/ dir
|
|
63
|
+
alembic-guard migrations/versions # or point it at files / dirs
|
|
64
|
+
alembic-guard --diff-base origin/main # only migrations this branch added or changed
|
|
65
|
+
alembic-guard --strict # fail on warnings too
|
|
66
|
+
alembic-guard --explain AG003 # why a rule exists and how to fix it
|
|
67
|
+
alembic-guard --format github | json # CI annotations or machine-readable output
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Exit code `0` = clean, `1` = findings that fail the run (errors, or anything with `--strict`),
|
|
71
|
+
`2` = usage error.
|
|
72
|
+
|
|
73
|
+
### GitHub Action
|
|
74
|
+
|
|
75
|
+
Findings show up as annotations on the pull request diff.
|
|
76
|
+
|
|
77
|
+
```yaml
|
|
78
|
+
# .github/workflows/migrations.yml
|
|
79
|
+
on: pull_request
|
|
80
|
+
jobs:
|
|
81
|
+
alembic-guard:
|
|
82
|
+
runs-on: ubuntu-latest
|
|
83
|
+
steps:
|
|
84
|
+
- uses: actions/checkout@v4
|
|
85
|
+
with:
|
|
86
|
+
fetch-depth: 0 # needed to diff against the base branch
|
|
87
|
+
- uses: ShalomHunukumbura/alembic-guard@v0
|
|
88
|
+
with:
|
|
89
|
+
strict: "false" # set "true" to fail on warnings
|
|
90
|
+
# paths: "app/migrations/versions"
|
|
91
|
+
# ignore: "AG005"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
By default only migrations changed in the PR are checked, so adopting it on a repo
|
|
95
|
+
with years of history doesn't flood you with old findings.
|
|
96
|
+
|
|
97
|
+
## Rules
|
|
98
|
+
|
|
99
|
+
| ID | Severity | Catches |
|
|
100
|
+
|----|----------|---------|
|
|
101
|
+
| AG001 | error | `NOT NULL` column added without a `server_default`: fails on any table with rows |
|
|
102
|
+
| AG002 | warning | Index created without `CONCURRENTLY`: blocks writes while it builds |
|
|
103
|
+
| AG003 | error | `CONCURRENTLY` inside Alembic's transaction: Postgres rejects it |
|
|
104
|
+
| AG004 | error | Table or column rename: old app version breaks mid-deploy |
|
|
105
|
+
| AG005 | warning | Table or column drop: running code that still reads it breaks |
|
|
106
|
+
| AG006 | warning | Column type change: usually rewrites the table under an exclusive lock |
|
|
107
|
+
| AG007 | warning | `SET NOT NULL` on an existing column: full scan under an exclusive lock |
|
|
108
|
+
| AG008 | warning | Foreign key or check constraint without `NOT VALID`: validated while blocking writes |
|
|
109
|
+
| AG009 | warning | Column added with a volatile default (`gen_random_uuid()`, `nextval`): table rewrite |
|
|
110
|
+
| AG010 | warning | Unique or primary key constraint on an existing table: builds an index while blocking writes |
|
|
111
|
+
|
|
112
|
+
Operations on a table **created earlier in the same migration** are not flagged,
|
|
113
|
+
since nobody can be using it yet. Raw SQL in `op.execute("...")` is checked for
|
|
114
|
+
the common cases too.
|
|
115
|
+
|
|
116
|
+
## Silencing a finding
|
|
117
|
+
|
|
118
|
+
Sometimes you know better (the table has 12 rows, or it's a maintenance window):
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
op.drop_column("users", "legacy_flag") # alembic-guard: ignore[AG005]
|
|
122
|
+
|
|
123
|
+
# alembic-guard: ignore
|
|
124
|
+
op.rename_table("tmp_import", "imports")
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Or project-wide in `pyproject.toml`:
|
|
128
|
+
|
|
129
|
+
```toml
|
|
130
|
+
[tool.alembic-guard]
|
|
131
|
+
paths = ["app/migrations/versions"]
|
|
132
|
+
ignore = ["AG005"]
|
|
133
|
+
strict = false
|
|
134
|
+
include-downgrade = false
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## How it works
|
|
138
|
+
|
|
139
|
+
Migrations are parsed with Python's `ast` module and never imported or executed.
|
|
140
|
+
The checker walks `upgrade()` in order, keeping track of three things: which
|
|
141
|
+
tables were created in this migration, which variables are `batch_alter_table`
|
|
142
|
+
contexts (and for which table), and whether it's inside an `autocommit_block()`.
|
|
143
|
+
Each `op.*` call is matched against Alembic's real signatures, so positional and
|
|
144
|
+
keyword arguments are both understood.
|
|
145
|
+
|
|
146
|
+
The rules target **PostgreSQL**, where these locking behaviours are well documented.
|
|
147
|
+
|
|
148
|
+
## Development
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
uv sync
|
|
152
|
+
uv run pytest
|
|
153
|
+
uv run alembic-guard examples/migrations/versions
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
MIT
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# alembic-guard
|
|
2
|
+
|
|
3
|
+
**Catch Alembic migrations that lock tables or break running code, before they reach production.**
|
|
4
|
+
|
|
5
|
+
This migration passes code review, passes tests on an empty dev database, and takes down production:
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
def upgrade():
|
|
9
|
+
op.add_column("users", sa.Column("email", sa.String(255), nullable=False))
|
|
10
|
+
op.create_index("ix_users_email", "users", ["email"], unique=True)
|
|
11
|
+
op.alter_column("users", "name", new_column_name="full_name")
|
|
12
|
+
op.create_foreign_key("fk_orders_user", "orders", "users", ["user_id"], ["id"])
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```console
|
|
16
|
+
$ alembic-guard
|
|
17
|
+
migrations/versions/0002_add_email.py:13:5 AG001 error Adding NOT NULL column "users.email" without a server_default fails on a table with rows.
|
|
18
|
+
fix: Add a server_default, or add the column as nullable, backfill it, then set NOT NULL in a later migration.
|
|
19
|
+
migrations/versions/0002_add_email.py:14:5 AG002 warning Creating index "ix_users_email" on "users" blocks writes until it is built.
|
|
20
|
+
fix: Pass postgresql_concurrently=True and run it inside `with op.get_context().autocommit_block():`.
|
|
21
|
+
migrations/versions/0002_add_email.py:15:5 AG004 error Renaming "users.name" to "full_name" breaks code still using the old name.
|
|
22
|
+
fix: Expand and contract: add the new column or table, write to both, backfill, move reads over, then drop the old one in a later release.
|
|
23
|
+
migrations/versions/0002_add_email.py:16:5 AG008 warning Foreign key from "orders" to "users" is validated while blocking writes on both tables.
|
|
24
|
+
fix: Pass postgresql_not_valid=True, then run `ALTER TABLE ... VALIDATE CONSTRAINT ...` in a separate migration. Validation only takes a light lock.
|
|
25
|
+
Found 2 errors and 2 warnings in 3 migrations.
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Tools like [squawk](https://squawkhq.com) do this for raw SQL. alembic-guard reads your
|
|
29
|
+
**Alembic Python migrations** directly, so it understands `op.add_column`,
|
|
30
|
+
`batch_alter_table`, `autocommit_block` and the rest, with no database and no SQL
|
|
31
|
+
generation step.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install alembic-guard # or: uv tool install alembic-guard
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
alembic-guard # finds every Alembic versions/ dir
|
|
43
|
+
alembic-guard migrations/versions # or point it at files / dirs
|
|
44
|
+
alembic-guard --diff-base origin/main # only migrations this branch added or changed
|
|
45
|
+
alembic-guard --strict # fail on warnings too
|
|
46
|
+
alembic-guard --explain AG003 # why a rule exists and how to fix it
|
|
47
|
+
alembic-guard --format github | json # CI annotations or machine-readable output
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Exit code `0` = clean, `1` = findings that fail the run (errors, or anything with `--strict`),
|
|
51
|
+
`2` = usage error.
|
|
52
|
+
|
|
53
|
+
### GitHub Action
|
|
54
|
+
|
|
55
|
+
Findings show up as annotations on the pull request diff.
|
|
56
|
+
|
|
57
|
+
```yaml
|
|
58
|
+
# .github/workflows/migrations.yml
|
|
59
|
+
on: pull_request
|
|
60
|
+
jobs:
|
|
61
|
+
alembic-guard:
|
|
62
|
+
runs-on: ubuntu-latest
|
|
63
|
+
steps:
|
|
64
|
+
- uses: actions/checkout@v4
|
|
65
|
+
with:
|
|
66
|
+
fetch-depth: 0 # needed to diff against the base branch
|
|
67
|
+
- uses: ShalomHunukumbura/alembic-guard@v0
|
|
68
|
+
with:
|
|
69
|
+
strict: "false" # set "true" to fail on warnings
|
|
70
|
+
# paths: "app/migrations/versions"
|
|
71
|
+
# ignore: "AG005"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
By default only migrations changed in the PR are checked, so adopting it on a repo
|
|
75
|
+
with years of history doesn't flood you with old findings.
|
|
76
|
+
|
|
77
|
+
## Rules
|
|
78
|
+
|
|
79
|
+
| ID | Severity | Catches |
|
|
80
|
+
|----|----------|---------|
|
|
81
|
+
| AG001 | error | `NOT NULL` column added without a `server_default`: fails on any table with rows |
|
|
82
|
+
| AG002 | warning | Index created without `CONCURRENTLY`: blocks writes while it builds |
|
|
83
|
+
| AG003 | error | `CONCURRENTLY` inside Alembic's transaction: Postgres rejects it |
|
|
84
|
+
| AG004 | error | Table or column rename: old app version breaks mid-deploy |
|
|
85
|
+
| AG005 | warning | Table or column drop: running code that still reads it breaks |
|
|
86
|
+
| AG006 | warning | Column type change: usually rewrites the table under an exclusive lock |
|
|
87
|
+
| AG007 | warning | `SET NOT NULL` on an existing column: full scan under an exclusive lock |
|
|
88
|
+
| AG008 | warning | Foreign key or check constraint without `NOT VALID`: validated while blocking writes |
|
|
89
|
+
| AG009 | warning | Column added with a volatile default (`gen_random_uuid()`, `nextval`): table rewrite |
|
|
90
|
+
| AG010 | warning | Unique or primary key constraint on an existing table: builds an index while blocking writes |
|
|
91
|
+
|
|
92
|
+
Operations on a table **created earlier in the same migration** are not flagged,
|
|
93
|
+
since nobody can be using it yet. Raw SQL in `op.execute("...")` is checked for
|
|
94
|
+
the common cases too.
|
|
95
|
+
|
|
96
|
+
## Silencing a finding
|
|
97
|
+
|
|
98
|
+
Sometimes you know better (the table has 12 rows, or it's a maintenance window):
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
op.drop_column("users", "legacy_flag") # alembic-guard: ignore[AG005]
|
|
102
|
+
|
|
103
|
+
# alembic-guard: ignore
|
|
104
|
+
op.rename_table("tmp_import", "imports")
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Or project-wide in `pyproject.toml`:
|
|
108
|
+
|
|
109
|
+
```toml
|
|
110
|
+
[tool.alembic-guard]
|
|
111
|
+
paths = ["app/migrations/versions"]
|
|
112
|
+
ignore = ["AG005"]
|
|
113
|
+
strict = false
|
|
114
|
+
include-downgrade = false
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## How it works
|
|
118
|
+
|
|
119
|
+
Migrations are parsed with Python's `ast` module and never imported or executed.
|
|
120
|
+
The checker walks `upgrade()` in order, keeping track of three things: which
|
|
121
|
+
tables were created in this migration, which variables are `batch_alter_table`
|
|
122
|
+
contexts (and for which table), and whether it's inside an `autocommit_block()`.
|
|
123
|
+
Each `op.*` call is matched against Alembic's real signatures, so positional and
|
|
124
|
+
keyword arguments are both understood.
|
|
125
|
+
|
|
126
|
+
The rules target **PostgreSQL**, where these locking behaviours are well documented.
|
|
127
|
+
|
|
128
|
+
## Development
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
uv sync
|
|
132
|
+
uv run pytest
|
|
133
|
+
uv run alembic-guard examples/migrations/versions
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
MIT
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
name: alembic-guard
|
|
2
|
+
description: Catch Alembic migrations that lock tables or break running code before they reach production.
|
|
3
|
+
author: Shalom Hunukumbura
|
|
4
|
+
branding:
|
|
5
|
+
icon: shield
|
|
6
|
+
color: blue
|
|
7
|
+
|
|
8
|
+
inputs:
|
|
9
|
+
paths:
|
|
10
|
+
description: Space-separated migration files or directories. Defaults to every Alembic versions/ directory found.
|
|
11
|
+
required: false
|
|
12
|
+
default: ""
|
|
13
|
+
strict:
|
|
14
|
+
description: Fail on warnings as well as errors.
|
|
15
|
+
required: false
|
|
16
|
+
default: "false"
|
|
17
|
+
ignore:
|
|
18
|
+
description: Comma-separated rule ids to skip, e.g. AG002,AG005.
|
|
19
|
+
required: false
|
|
20
|
+
default: ""
|
|
21
|
+
changed-only:
|
|
22
|
+
description: On pull requests, only check migrations the PR adds or changes. Needs actions/checkout with fetch-depth 0.
|
|
23
|
+
required: false
|
|
24
|
+
default: "true"
|
|
25
|
+
python-version:
|
|
26
|
+
description: Python version used to run the checker.
|
|
27
|
+
required: false
|
|
28
|
+
default: "3.12"
|
|
29
|
+
|
|
30
|
+
runs:
|
|
31
|
+
using: composite
|
|
32
|
+
steps:
|
|
33
|
+
- uses: actions/setup-python@v5
|
|
34
|
+
with:
|
|
35
|
+
python-version: ${{ inputs.python-version }}
|
|
36
|
+
|
|
37
|
+
- name: Install alembic-guard
|
|
38
|
+
shell: bash
|
|
39
|
+
run: pip install --quiet "${{ github.action_path }}"
|
|
40
|
+
|
|
41
|
+
- name: Check migrations
|
|
42
|
+
shell: bash
|
|
43
|
+
env:
|
|
44
|
+
AG_PATHS: ${{ inputs.paths }}
|
|
45
|
+
AG_STRICT: ${{ inputs.strict }}
|
|
46
|
+
AG_IGNORE: ${{ inputs.ignore }}
|
|
47
|
+
AG_CHANGED_ONLY: ${{ inputs.changed-only }}
|
|
48
|
+
AG_BASE_REF: ${{ github.base_ref }}
|
|
49
|
+
run: |
|
|
50
|
+
args=(--format github)
|
|
51
|
+
[[ "$AG_STRICT" == "true" ]] && args+=(--strict)
|
|
52
|
+
[[ -n "$AG_IGNORE" ]] && args+=(--ignore "$AG_IGNORE")
|
|
53
|
+
if [[ "$AG_CHANGED_ONLY" == "true" && -n "$AG_BASE_REF" ]]; then
|
|
54
|
+
git fetch --no-tags --quiet origin "$AG_BASE_REF"
|
|
55
|
+
args+=(--diff-base "origin/$AG_BASE_REF")
|
|
56
|
+
fi
|
|
57
|
+
# Word-splitting on AG_PATHS is intended: it is a space-separated list.
|
|
58
|
+
# shellcheck disable=SC2086
|
|
59
|
+
alembic-guard "${args[@]}" $AG_PATHS
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""create users
|
|
2
|
+
|
|
3
|
+
Revision ID: 0001
|
|
4
|
+
"""
|
|
5
|
+
from alembic import op
|
|
6
|
+
import sqlalchemy as sa
|
|
7
|
+
|
|
8
|
+
revision = "0001"
|
|
9
|
+
down_revision = None
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def upgrade():
|
|
13
|
+
# Brand-new table: indexes and constraints on it can't block anyone.
|
|
14
|
+
op.create_table(
|
|
15
|
+
"users",
|
|
16
|
+
sa.Column("id", sa.Integer(), primary_key=True),
|
|
17
|
+
sa.Column("name", sa.String(120), nullable=False),
|
|
18
|
+
)
|
|
19
|
+
op.create_index("ix_users_name", "users", ["name"])
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def downgrade():
|
|
23
|
+
op.drop_table("users")
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""add email to users -- the migration that looks fine in review
|
|
2
|
+
|
|
3
|
+
Revision ID: 0002
|
|
4
|
+
"""
|
|
5
|
+
from alembic import op
|
|
6
|
+
import sqlalchemy as sa
|
|
7
|
+
|
|
8
|
+
revision = "0002"
|
|
9
|
+
down_revision = "0001"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def upgrade():
|
|
13
|
+
op.add_column("users", sa.Column("email", sa.String(255), nullable=False))
|
|
14
|
+
op.create_index("ix_users_email", "users", ["email"], unique=True)
|
|
15
|
+
op.alter_column("users", "name", new_column_name="full_name")
|
|
16
|
+
op.create_foreign_key("fk_orders_user", "orders", "users", ["user_id"], ["id"])
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def downgrade():
|
|
20
|
+
op.drop_constraint("fk_orders_user", "orders")
|
|
21
|
+
op.alter_column("users", "full_name", new_column_name="name")
|
|
22
|
+
op.drop_index("ix_users_email", "users")
|
|
23
|
+
op.drop_column("users", "email")
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""add email to users -- the zero-downtime version
|
|
2
|
+
|
|
3
|
+
Revision ID: 0003
|
|
4
|
+
"""
|
|
5
|
+
from alembic import op
|
|
6
|
+
import sqlalchemy as sa
|
|
7
|
+
|
|
8
|
+
revision = "0003"
|
|
9
|
+
down_revision = "0001"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def upgrade():
|
|
13
|
+
# Nullable first; the app backfills it, and a later migration sets NOT NULL.
|
|
14
|
+
op.add_column("users", sa.Column("email", sa.String(255), nullable=True))
|
|
15
|
+
|
|
16
|
+
# Built without blocking writes. CONCURRENTLY can't run inside a transaction.
|
|
17
|
+
with op.get_context().autocommit_block():
|
|
18
|
+
op.create_index("ix_users_email", "users", ["email"], unique=True, postgresql_concurrently=True)
|
|
19
|
+
|
|
20
|
+
# Skip the full-table check now; VALIDATE CONSTRAINT runs later under a light lock.
|
|
21
|
+
op.create_foreign_key(
|
|
22
|
+
"fk_orders_user", "orders", "users", ["user_id"], ["id"], postgresql_not_valid=True
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def downgrade():
|
|
27
|
+
op.drop_constraint("fk_orders_user", "orders")
|
|
28
|
+
op.drop_index("ix_users_email", "users")
|
|
29
|
+
op.drop_column("users", "email")
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "alembic-guard"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Catch Alembic migrations that lock tables or break running code before they reach production."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{ name = "Shalom Hunukumbura" }]
|
|
13
|
+
keywords = ["alembic", "postgresql", "migrations", "linter", "sqlalchemy", "zero-downtime"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Environment :: Console",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Topic :: Database",
|
|
20
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
21
|
+
]
|
|
22
|
+
dependencies = ["tomli>=2.0; python_version < '3.11'"]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/ShalomHunukumbura/alembic-guard"
|
|
26
|
+
Issues = "https://github.com/ShalomHunukumbura/alembic-guard/issues"
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
alembic-guard = "alembic_guard.cli:main"
|
|
30
|
+
|
|
31
|
+
[dependency-groups]
|
|
32
|
+
dev = ["pytest>=8"]
|
|
33
|
+
|
|
34
|
+
[tool.hatch.build.targets.wheel]
|
|
35
|
+
packages = ["src/alembic_guard"]
|
|
36
|
+
|
|
37
|
+
[tool.pytest.ini_options]
|
|
38
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""Catch Alembic migrations that lock tables or break running code."""
|
|
2
|
+
|
|
3
|
+
from .checker import Finding, check_file, check_source
|
|
4
|
+
from .rules import RULES, Rule, Severity
|
|
5
|
+
|
|
6
|
+
__version__ = "0.1.0"
|
|
7
|
+
|
|
8
|
+
__all__ = ["Finding", "RULES", "Rule", "Severity", "check_file", "check_source", "__version__"]
|