Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
run: pip install -e ".[dev]"

- name: Black formatting check
run: black --check bot tests alembic
run: black --check bot tests db

- name: Mypy type check
run: mypy bot tests
Expand Down
149 changes: 0 additions & 149 deletions alembic.ini

This file was deleted.

1 change: 0 additions & 1 deletion alembic/README

This file was deleted.

76 changes: 0 additions & 76 deletions alembic/env.py

This file was deleted.

28 changes: 0 additions & 28 deletions alembic/script.py.mako

This file was deleted.

15 changes: 15 additions & 0 deletions bot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import matrix
from coloredlogs import install
from sqlmodel import SQLModel
from pelican import get_runner, get_registry

from bot import ada
from bot.config import BotConfig
Expand Down Expand Up @@ -77,6 +78,20 @@ def _load_database(config: BotConfig) -> None:
Model.set_engine(engine)
SQLModel.metadata.create_all(engine)

if config.is_production:
_run_migrations()


def _run_migrations() -> None:
runner = get_runner()
registry = get_registry()

applied = list(runner.get_applied_versions())
migrations = [m for m in registry.get_all() if m.revision not in applied]

for migration in migrations:
runner.upgrade(migration)


def _show_app_info(config: BotConfig) -> None:
logger.info(
Expand Down
30 changes: 15 additions & 15 deletions bot/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from os import getenv
from click import UsageError, pass_context, group, option, argument
from click import UsageError, pass_context, group, option

from pelican import use_context
from pelican.cli import cli as pelican_cli

from bot.app import start
from bot.config import BotConfig
from bot.migration import up_migration, down_migration

DEFAULT_ENV = "development"
CONFIG_DIR = "config"
Expand All @@ -27,26 +29,24 @@ def cli(ctx, env: str | None, config_file: str | None) -> None:
raise UsageError("Use either --env or --config, not both.")

path = config_file or resolve_config_path(env)
ctx.obj["config"] = BotConfig(path)
config = BotConfig(path)

ctx.obj["config"] = config
ctx.with_resource(use_context(database_url=config.database_url))


@cli.command("start")
@cli.command("start", help="Start the bot")
@pass_context
def start_command(ctx):
config = ctx.obj["config"]
start(config.config_file)


@cli.command()
@argument("revision", default="head")
@pass_context
def up(ctx, revision):
config = ctx.obj["config"]
up_migration(config, revision)
@cli.group(help="Database utilities commands")
def db():
pass


@cli.command()
@argument("revision", default="head")
def down(ctx, revision):
config = ctx.obj["config"]
down_migration(config, revision)
# we add each pelican command to the db group
for cmd in pelican_cli.commands.values():
db.add_command(cmd)
4 changes: 4 additions & 0 deletions bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def __init__(self, config_file: str) -> None:
"[%(asctime)s] %(programname)s %(funcName)s %(module)s %(levelname)s %(message)s"
)

@property
def is_production(self) -> bool:
return self.environment in ["production"]

@property
def extensions(self) -> Iterator[Extension]:
from bot import extensions
Expand Down
33 changes: 0 additions & 33 deletions bot/migration.py

This file was deleted.

Loading
Loading