Compare commits

..

No commits in common. "37dd5ba3e868f166f909b910326664adb90d0d61" and "3a35718d847c717b613be4677630be512c06c761" have entirely different histories.

7 changed files with 14 additions and 155 deletions

View File

@ -1 +0,0 @@
Generic single-database configuration.

View File

@ -1,71 +0,0 @@
import asyncio
from logging.config import fileConfig
from alembic import context
from database.engine import engine
from database.models import Base
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
render_as_batch=True,
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection):
context.configure(connection=connection, target_metadata=target_metadata, render_as_batch=True)
with context.begin_transaction():
context.run_migrations()
async def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(run_migrations_online())

View File

@ -1,24 +0,0 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

@ -1,26 +0,0 @@
import logging
from alembic import config, script
from alembic.runtime import migration
from database.engine import engine
async def ensure_latest_migration():
"""Make sure we are currently on the latest revision, otherwise raise an exception"""
alembic_config = config.Config("alembic.ini")
alembic_script = script.ScriptDirectory.from_config(alembic_config)
async with engine.begin() as connection:
current_revision = await connection.run_sync(
lambda sync_connection: migration.MigrationContext.configure(sync_connection).get_current_revision()
)
alembic_head = alembic_script.get_current_head()
if current_revision != alembic_head:
error_message = (
f"Pending migrations (current revision is {current_revision}, while head is at {alembic_head})"
)
logging.error(error_message)
raise RuntimeError(error_message)

View File

@ -1,4 +1,5 @@
import discord
from discord import Message
from discord.ext import commands
from sqlalchemy.ext.asyncio import AsyncSession
@ -14,14 +15,9 @@ class Didier(commands.Bot):
activity = discord.Activity(type=discord.ActivityType.playing, name=settings.DISCORD_STATUS_MESSAGE)
status = discord.Status.online
intents = discord.Intents(
guilds=True,
members=True,
message_content=True,
emojis=True,
messages=True,
reactions=True,
)
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
super().__init__(
command_prefix=get_prefix, case_insensitive=True, intents=intents, activity=activity, status=status
@ -31,3 +27,10 @@ class Didier(commands.Bot):
def db_session(self) -> AsyncSession:
"""Obtain a database session"""
return DBSession()
async def on_ready(self):
"""Event triggered when the bot is ready"""
print(settings.DISCORD_READY_MESSAGE)
async def on_message(self, message: Message, /) -> None:
print(message.content)

View File

@ -1,16 +0,0 @@
version: '3.9'
services:
db:
image: postgres:14
container_name: didier
restart: always
environment:
- POSTGRES_DB=${DB_NAME:-didier_dev}
- POSTGRES_USER=${DB_USERNAME:-postgres}
- POSTGRES_PASSWORD=${DB_PASSWORD:-postgres}
ports:
- "${DB_PORT:-5432}:${DB_PORT:-5432}"
volumes:
- db:/var/lib/postgresql/data
volumes:
db:

12
main.py
View File

@ -4,7 +4,6 @@ from logging.handlers import RotatingFileHandler
import asyncio
import settings
from database.migrations import ensure_latest_migration
from didier import Didier
@ -29,12 +28,7 @@ def setup_logging():
logging.getLogger("discord").setLevel(logging.ERROR)
async def main():
"""Do some setup & checks, and then run the bot"""
setup_logging()
await ensure_latest_migration()
await run_bot()
if __name__ == "__main__":
asyncio.run(main())
setup_logging()
asyncio.run(run_bot())