Skip to content

Commit

Permalink
Merge pull request #46 from alan-turing-institute/typing-alembic
Browse files Browse the repository at this point in the history
Merging, as the only failures have nothing to do with the current changes.
  • Loading branch information
Iain-S authored Jan 8, 2025
2 parents 2a3e076 + 3bd8438 commit d27a908
Show file tree
Hide file tree
Showing 9 changed files with 1,933 additions and 1,783 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
uses: abatilo/actions-poetry@v3

- name: Configure poetry
shell: bash
run: poetry config virtualenvs.in-project true

- name: Set up cache
Expand All @@ -44,8 +45,13 @@ jobs:
run: poetry check --lock

- name: Install dependencies
shell: bash
run: poetry install --all-extras

- name: Ready the MyPy Cache
shell: bash
run: mkdir .mypy_cache

- uses: pre-commit/[email protected]
env:
SAFETY_API_KEY: ${{ secrets.SAFETY_API_KEY }}
3 changes: 0 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ repos:
entry: poetry run mypy --install-types --non-interactive
language: system
types: ['python']
exclude: ^alembic/
- id: pylint
name: Pylint
entry: poetry run pylint --rcfile=.python-lint
language: system
types: ['python']
exclude: ^alembic/
- id: flake8
name: Flake8
entry: poetry run flake8
Expand All @@ -62,7 +60,6 @@ repos:
language: system
types: ['python']
exclude: (?x)(
alembic|
tests|
scripts
)
Expand Down
2 changes: 1 addition & 1 deletion .python-lint
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ ignored-classes=optparse.Values,thread._local,_thread._local
# and thus existing member attributes cannot be deduced by static analysis). It
# supports qualified module names, as well as Unix pattern matching.
# We ignore Pulumi modules since we don't list them in pyproject.toml.
ignored-modules=pulumi,pulumi_random,pulumi_azure_native,pulumi_postgresql
ignored-modules=

# Show a hint with possible names when a member name was not found. The aspect
# of finding the hint is based on edit distance.
Expand Down
33 changes: 26 additions & 7 deletions alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
"""Configuration for Alembic migrations."""

from logging.config import fileConfig
from typing import Literal, Optional

from sqlalchemy import engine_from_config, pool
from sqlalchemy.sql.schema import SchemaItem

from alembic import context
from rctab.crud.models import metadata
from rctab.settings import Settings

# pylint: disable=unused-argument
# pylint: disable=no-member
# pylint: disable=redefined-builtin

settings = Settings()
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
config.set_main_option("sqlalchemy.url", str(settings.postgres_dsn))
# Interpret the config file for Python logging.
# This line sets up loggers basically.
assert config.config_file_name is not None
fileConfig(config.config_file_name)

# add your model's MetaData object here
Expand All @@ -26,16 +35,26 @@
# my_important_option = config.get_main_option("my_important_option")
# ... etc.

SchemaType = Literal[
"schema", "table", "column", "index", "unique_constraint", "foreign_key_constraint"
]

def include_object(object, name, type_, reflected, compare_to):
"""
Exclude views from Alembic's consideration.
"""

return not object.info.get("is_view", False)
def include_object(
object: SchemaItem,
name: Optional[str],
type_: SchemaType,
reflected: bool,
compare_to: Optional[SchemaItem],
) -> bool:
"""Exclude views from Alembic's consideration."""
if object.info:
if object.info.get("is_view", False):
return False
return True


def run_migrations_offline():
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
Expand All @@ -61,7 +80,7 @@ def run_migrations_offline():
context.run_migrations()


def run_migrations_online():
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
Expand Down
17 changes: 10 additions & 7 deletions alembic/versions/b65796c99771_squash.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Squash
"""Squash (previous migrations).
Revision ID: b65796c99771
Revises:
Expand All @@ -12,6 +12,9 @@

from alembic import op

# pylint: disable=no-member
# pylint: disable=invalid-name

# revision identifiers, used by Alembic.
revision = "b65796c99771"
down_revision = None
Expand Down Expand Up @@ -67,7 +70,8 @@ class FinanceHistorySql:
DROP_FUNCTION = "DROP FUNCTION {schema}.finance_changed();"


def upgrade():
def upgrade() -> None:
"""Upgrade the database."""
op.execute(text("create schema accounting"))
op.create_table(
"user_cache",
Expand Down Expand Up @@ -362,9 +366,7 @@ def upgrade():
sa.Column("subscription_id", postgresql.UUID(), nullable=False),
sa.Column("display_name", sa.String(), nullable=False),
sa.Column("state", sa.String(), nullable=False),
sa.Column(
"role_assignments", postgresql.JSONB(astext_type=sa.Text()), nullable=True
),
sa.Column("role_assignments", postgresql.JSONB(), nullable=True),
sa.Column(
"time_created",
sa.DateTime(timezone=True),
Expand All @@ -385,7 +387,7 @@ def upgrade():
sa.Column("id", sa.String(), nullable=False),
sa.Column("name", sa.String(), nullable=True),
sa.Column("type", sa.String(), nullable=True),
sa.Column("tags", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("tags", postgresql.JSONB(), nullable=True),
sa.Column("billing_account_id", sa.String(), nullable=True),
sa.Column("billing_account_name", sa.String(), nullable=True),
sa.Column("billing_period_start_date", sa.Date(), nullable=True),
Expand Down Expand Up @@ -488,7 +490,8 @@ def upgrade():
)


def downgrade():
def downgrade() -> None:
"""Downgrade the database."""
op.drop_table("cost_recovery", schema="accounting")
op.execute(
"DROP MATERIALIZED VIEW {schema}.usage_view;".format(schema="accounting")
Expand Down
6 changes: 5 additions & 1 deletion docs/content/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ TESTING=true pytest tests/

The `TESTING=true` env var is important so that database commits are rolled back between each unit test.

**Note:** Visual Studio Code will try to import your .env file into your environment, which will cause a number of tests to fail.
To prevent this, we recommend changing the `python.envFile` setting to point to a non-existent file.
See <https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file> for more.

**Note:** This will remove the contents of any [postgreSQL containers](#postgresql-container) you have running. If you don't want to lose them use [the helper script](#with-the-helper-script).

The tests for background tasks require Redis
The tests for background tasks require Redis.
Once you have a [Redis server](https://redis.io/docs/install/install-redis/) running (or a [Redis Docker](https://hub.docker.com/_/redis) container running), you can run the unit tests, including the background task tests, with:

```bash
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[mypy]
disallow_untyped_defs = True
ignore_missing_imports = True
plugins = sqlmypy, pydantic.mypy
plugins = pydantic.mypy
Loading

0 comments on commit d27a908

Please sign in to comment.