Skip to content

Commit 42a6ad1

Browse files
authored
feat(dev): alembic check (#16483)
1 parent 1185eef commit 42a6ad1

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

.github/workflows/ci.yml

+38
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,41 @@ jobs:
9393
key: ${{ runner.os }}-mypy-${{ env.pythonLocation }}-${{ hashFiles('requirements.txt', 'requirements/*.txt') }}
9494
- name: Run ${{ matrix.name }}
9595
run: ${{ matrix.command }}
96+
97+
check_db:
98+
name: Check Database Consistency
99+
needs: build
100+
runs-on: depot-ubuntu-22.04-arm
101+
continue-on-error: true
102+
container:
103+
image: registry.depot.dev/rltf7cln5v:${{ needs.build.outputs.buildId }}
104+
credentials:
105+
username: x-token
106+
password: ${{ needs.build.outputs.token }}
107+
services:
108+
postgres:
109+
image: postgres:16.1
110+
ports:
111+
- 5432:5432
112+
env:
113+
POSTGRES_DB: warehouse
114+
POSTGRES_HOST_AUTH_METHOD: trust # never do this in production!
115+
POSTGRES_INITDB_ARGS: '--no-sync --set fsync=off --set full_page_writes=off'
116+
# Set health checks to wait until postgres has started
117+
options: --health-cmd "pg_isready --username=postgres --dbname=postgres" --health-interval 10s --health-timeout 5s --health-retries 5
118+
steps:
119+
- name: Check out repository
120+
uses: actions/checkout@v4
121+
- name: Dotenv Action
122+
# We need to load the environment variables to run the CLI
123+
id: dotenv
124+
uses: falti/dotenv-action@v1
125+
with:
126+
path: dev/environment
127+
export-variables: true
128+
keys-case: upper
129+
- name: Check Database
130+
run: bin/db-check
131+
env:
132+
# override the hostname set in `dev/environment`
133+
DATABASE_URL: 'postgresql+psycopg://postgres@postgres/warehouse'

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ inittuf: .state/db-migrated
145145
runmigrations: .state/docker-build-base
146146
docker compose run --rm web python -m warehouse db upgrade head
147147

148+
checkdb: .state/docker-build-base
149+
docker compose run --rm web bin/db-check
150+
148151
reindex: .state/docker-build-base
149152
docker compose run --rm web python -m warehouse search reindex
150153

@@ -168,4 +171,4 @@ purge: stop clean
168171
stop:
169172
docker compose stop
170173

171-
.PHONY: default build serve resetdb initdb shell dbshell tests dev-docs user-docs deps clean purge debug stop compile-pot runmigrations
174+
.PHONY: default build serve resetdb initdb shell dbshell tests dev-docs user-docs deps clean purge debug stop compile-pot runmigrations checkdb

bin/db-check

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# When on GitHub Actions, if a label is present on the PR, skip the check.
5+
if [ -n "$GITHUB_ACTIONS" ]; then
6+
if [ -n "$GITHUB_EVENT_PATH" ]; then
7+
if jq -e '.pull_request.labels | map(.name) | index("skip-db-check")' < "$GITHUB_EVENT_PATH" > /dev/null; then
8+
echo "Skipping database check due to 'skip-db-check' label"
9+
exit 0
10+
fi
11+
fi
12+
fi
13+
14+
# Check for outstanding database migrations
15+
python -m warehouse db upgrade head
16+
python -m warehouse db check

tests/unit/cli/test_db.py

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import warehouse.db
2525

2626
from warehouse.cli.db.branches import branches
27+
from warehouse.cli.db.check import check
2728
from warehouse.cli.db.current import current
2829
from warehouse.cli.db.downgrade import downgrade
2930
from warehouse.cli.db.heads import heads
@@ -299,6 +300,18 @@ def test_upgrade_command(monkeypatch, cli, pyramid_config):
299300
assert alembic_upgrade.calls == [pretend.call(alembic_config, "foo")]
300301

301302

303+
def test_check_command(monkeypatch, cli, pyramid_config):
304+
alembic_check = pretend.call_recorder(lambda config: None)
305+
monkeypatch.setattr(alembic.command, "check", alembic_check)
306+
307+
alembic_config = pretend.stub(attributes={})
308+
pyramid_config.alembic_config = lambda: alembic_config
309+
310+
result = cli.invoke(check, obj=pyramid_config)
311+
assert result.exit_code == 0
312+
assert alembic_check.calls == [pretend.call(alembic_config)]
313+
314+
302315
def test_dbml_command(monkeypatch, cli):
303316
generate_dbml_file = pretend.call_recorder(lambda tables, path: None)
304317
monkeypatch.setattr(warehouse.cli.db.dbml, "generate_dbml_file", generate_dbml_file)

warehouse/cli/db/check.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
import alembic.command
14+
import click
15+
16+
from warehouse.cli.db import db
17+
18+
19+
@db.command()
20+
@click.pass_obj
21+
def check(config):
22+
"""
23+
Check if autogenerate will create new operations.
24+
"""
25+
alembic.command.check(config.alembic_config())

0 commit comments

Comments
 (0)