Skip to content

Commit

Permalink
Add core database code (#66)
Browse files Browse the repository at this point in the history
Co-authored-by: nova <[email protected]>
Co-authored-by: nova <[email protected]>
Co-authored-by: wizzdom <[email protected]>
  • Loading branch information
4 people authored Jan 15, 2025
1 parent e47172c commit df37984
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ LDAP_USERNAME=
LDAP_PASSWORD=

AGENDA_TEMPLATE_URL="https://md.redbrick.dcu.ie/foo"

DB_HOST=postgres:5432
DB_NAME=blockbot
DB_USER=blockbot
DB_PASSWORD=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
postgres/

.vscode/
.idea/

Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .
COPY src src

CMD ["python3", "-m", "src"]
CMD ["python3", "-m", "src"]
23 changes: 23 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
bot:
build: .
restart: unless-stopped
depends_on:
- postgres
env_file:
- .env

postgres:
image: postgres:17-alpine
restart: unless-stopped
ports:
- 5432:5432
volumes:
- ./postgres:/var/lib/postgresql/data
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}

volumes:
postgres:
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
aiohttp==3.11.11
asyncpg==0.30.0
fortune-python==1.1.1
hikari==2.1.0
hikari-arc==1.4.0
hikari-miru==4.2.0
pyfiglet==1.0.2
python-dotenv==1.0.1
sqlalchemy==2.0.37
6 changes: 6 additions & 0 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import miru

from src.config import DEBUG, TOKEN
from src.database import init_db

bot = hikari.GatewayBot(
token=TOKEN,
Expand Down Expand Up @@ -56,3 +57,8 @@ async def error_handler(ctx: arc.GatewayContext, exc: Exception) -> None:
logging.error(exc)

raise exc


@client.set_startup_hook
async def startup_hook(_: arc.GatewayClient) -> None:
await init_db()
6 changes: 6 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ def get_required_var(var: str) -> str:

TOKEN = get_required_var("TOKEN") # required
DEBUG = os.environ.get("DEBUG", False)

DISCORD_UID_MAP = get_required_var("DISCORD_UID_MAP")

DB_HOST = os.environ.get("DB_HOST")
DB_NAME = os.environ.get("DB_NAME")
DB_USER = os.environ.get("DB_USER")
DB_PASSWORD = os.environ.get("DB_PASSWORD")

CHANNEL_IDS: dict[str, int] = {
"lobby": 627542044390457350,
"bot-private": 853071983452225536,
Expand Down
17 changes: 17 additions & 0 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.ext.declarative import declarative_base

from src.config import DB_HOST, DB_NAME, DB_PASSWORD, DB_USER

engine = create_async_engine(
f"postgresql+asyncpg://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}", echo=False
)


Base = declarative_base()
Session = async_sessionmaker(bind=engine)


async def init_db() -> None:
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)

0 comments on commit df37984

Please sign in to comment.