From 9518bbe168c33944a3c24fa48489c5069683ac95 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Sat, 11 Jun 2022 01:15:05 +0200 Subject: [PATCH] Add db through docker, configure alembic & fix pylint warning --- alembic/README | 1 + alembic/env.py | 71 ++++++++++++++++++++++++++++++++++++++++++ alembic/script.py.mako | 24 ++++++++++++++ didier/didier.py | 19 +++++------ docker-compose.yml | 16 ++++++++++ 5 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 alembic/README create mode 100644 alembic/env.py create mode 100644 alembic/script.py.mako create mode 100644 docker-compose.yml diff --git a/alembic/README b/alembic/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 0000000..f28faf3 --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,71 @@ +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()) diff --git a/alembic/script.py.mako b/alembic/script.py.mako new file mode 100644 index 0000000..55df286 --- /dev/null +++ b/alembic/script.py.mako @@ -0,0 +1,24 @@ +"""${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"} diff --git a/didier/didier.py b/didier/didier.py index ff47281..1329163 100644 --- a/didier/didier.py +++ b/didier/didier.py @@ -1,5 +1,4 @@ import discord -from discord import Message from discord.ext import commands from sqlalchemy.ext.asyncio import AsyncSession @@ -15,9 +14,14 @@ class Didier(commands.Bot): activity = discord.Activity(type=discord.ActivityType.playing, name=settings.DISCORD_STATUS_MESSAGE) status = discord.Status.online - intents = discord.Intents.default() - intents.members = True - intents.message_content = True + intents = discord.Intents( + guilds=True, + members=True, + message_content=True, + emojis=True, + messages=True, + reactions=True, + ) super().__init__( command_prefix=get_prefix, case_insensitive=True, intents=intents, activity=activity, status=status @@ -27,10 +31,3 @@ 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) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d34f4a7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +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: