Add database migrations

- use Alembic to migrate the PostgreSQL database
  + create initial migration script to set up the database,
    as an alternative to db.Base.metadata.create_all()
  + integrate Alembic into the test suite; the db_engine fixture
    now has two modes:
    * create the latest version of tables all at once
    * invoke `alembic upgrade head`
    => the "e2e" tests are all run twice, once in each mode; this
       ensures that the migration scripts re-create the same database
       schema as db.Base.metadata.create_all() would
    * in both modes, a temporary PostgreSQL schema is used to create the
      tables in
    => could now run "e2e" tests against production database and still
       have isolation
- make the configuration module public (to be used by Alembic)
- adjust linting rules for Alembic
This commit is contained in:
Alexander Hess 2020-08-09 17:14:23 +02:00
commit a16c260543
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
14 changed files with 1104 additions and 35 deletions

View file

@ -9,7 +9,7 @@ Example:
import os as _os
from importlib import metadata as _metadata
from urban_meal_delivery import _config # noqa:WPS450
from urban_meal_delivery import configuration as _configuration
try:
@ -26,8 +26,8 @@ else:
__version__ = _pkg_info['version']
# Little Hack: "Overwrites" the config module so that the environment is already set.
config: _config.Config = _config.get_config(
# Global `config` object to be used in the package.
config: _configuration.Config = _configuration.make_config(
'testing' if _os.getenv('TESTING') else 'production',
)

View file

@ -1,11 +1,12 @@
"""Provide package-wide configuration.
This module is "protected" so that it is only used
via the `config` proxy at the package's top level.
This module provides utils to create new `Config` objects
on the fly, mainly for testing and migrating!
That already loads the correct configuration
depending on the current environment.
Within this package, use the `config` proxy at the package's top level
to access the current configuration!
"""
import datetime
import os
import random
@ -20,8 +21,10 @@ dotenv.load_dotenv()
def random_schema_name() -> str:
"""Generate a random PostgreSQL schema name for testing."""
return ''.join(
random.choice(string.ascii_lowercase) for _ in range(10) # noqa:S311
return 'temp_{name}'.format(
name=''.join(
(random.choice(string.ascii_lowercase) for _ in range(10)), # noqa:S311
),
)
@ -44,6 +47,9 @@ class Config:
# The PostgreSQL schema that holds the tables with the cleaned data.
CLEAN_SCHEMA = os.getenv('CLEAN_SCHEMA') or 'clean'
ALEMBIC_TABLE = 'alembic_version'
ALEMBIC_TABLE_SCHEMA = 'public'
def __repr__(self) -> str:
"""Non-literal text representation."""
return '<configuration>'
@ -68,8 +74,8 @@ class TestingConfig(Config):
CLEAN_SCHEMA = os.getenv('CLEAN_SCHEMA_TESTING') or random_schema_name()
def get_config(env: str = 'production') -> Config:
"""Get the configuration for the package.
def make_config(env: str = 'production') -> Config:
"""Create a new `Config` object.
Args:
env: either 'production' or 'testing'; defaults to the first