Skip to content

feat(dev): alembic check #16483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,41 @@ jobs:
key: ${{ runner.os }}-mypy-${{ env.pythonLocation }}-${{ hashFiles('requirements.txt', 'requirements/*.txt') }}
- name: Run ${{ matrix.name }}
run: ${{ matrix.command }}

check_db:
name: Check Database Consistency
needs: build
runs-on: depot-ubuntu-22.04-arm
continue-on-error: true
container:
image: registry.depot.dev/rltf7cln5v:${{ needs.build.outputs.buildId }}
credentials:
username: x-token
password: ${{ needs.build.outputs.token }}
services:
postgres:
image: postgres:16.1
ports:
- 5432:5432
env:
POSTGRES_DB: warehouse
POSTGRES_HOST_AUTH_METHOD: trust # never do this in production!
POSTGRES_INITDB_ARGS: '--no-sync --set fsync=off --set full_page_writes=off'
# Set health checks to wait until postgres has started
options: --health-cmd "pg_isready --username=postgres --dbname=postgres" --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Dotenv Action
# We need to load the environment variables to run the CLI
id: dotenv
uses: falti/dotenv-action@v1
with:
path: dev/environment
export-variables: true
keys-case: upper
- name: Check Database
run: bin/db-check
env:
# override the hostname set in `dev/environment`
DATABASE_URL: 'postgresql+psycopg://postgres@postgres/warehouse'
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ inittuf: .state/db-migrated
runmigrations: .state/docker-build-base
docker compose run --rm web python -m warehouse db upgrade head

checkdb: .state/docker-build-base
docker compose run --rm web bin/db-check

reindex: .state/docker-build-base
docker compose run --rm web python -m warehouse search reindex

Expand All @@ -168,4 +171,4 @@ purge: stop clean
stop:
docker compose stop

.PHONY: default build serve resetdb initdb shell dbshell tests dev-docs user-docs deps clean purge debug stop compile-pot runmigrations
.PHONY: default build serve resetdb initdb shell dbshell tests dev-docs user-docs deps clean purge debug stop compile-pot runmigrations checkdb
6 changes: 6 additions & 0 deletions bin/db-check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

# Check for outstanding database migrations
python -m warehouse db upgrade head
python -m warehouse db check
13 changes: 13 additions & 0 deletions tests/unit/cli/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import warehouse.db

from warehouse.cli.db.branches import branches
from warehouse.cli.db.check import check
from warehouse.cli.db.current import current
from warehouse.cli.db.downgrade import downgrade
from warehouse.cli.db.heads import heads
Expand Down Expand Up @@ -299,6 +300,18 @@ def test_upgrade_command(monkeypatch, cli, pyramid_config):
assert alembic_upgrade.calls == [pretend.call(alembic_config, "foo")]


def test_check_command(monkeypatch, cli, pyramid_config):
alembic_check = pretend.call_recorder(lambda config: None)
monkeypatch.setattr(alembic.command, "check", alembic_check)

alembic_config = pretend.stub(attributes={})
pyramid_config.alembic_config = lambda: alembic_config

result = cli.invoke(check, obj=pyramid_config)
assert result.exit_code == 0
assert alembic_check.calls == [pretend.call(alembic_config)]


def test_dbml_command(monkeypatch, cli):
generate_dbml_file = pretend.call_recorder(lambda tables, path: None)
monkeypatch.setattr(warehouse.cli.db.dbml, "generate_dbml_file", generate_dbml_file)
Expand Down
25 changes: 25 additions & 0 deletions warehouse/cli/db/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import alembic.command
import click

from warehouse.cli.db import db


@db.command()
@click.pass_obj
def check(config):
"""
Check if autogenerate will create new operations.
"""
alembic.command.check(config.alembic_config())