Skip to content

Commit

Permalink
Update docker compose & make db session async
Browse files Browse the repository at this point in the history
  • Loading branch information
novanai committed Jan 15, 2025
1 parent 0ed2641 commit ff06c0c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
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"]
13 changes: 6 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ services:
restart: unless-stopped
depends_on:
- postgres
environment:
- POSTGRES_DB=blockbot
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password
env_file:
- .env

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

volumes:
postgres:
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
asyncpg==0.30.0
hikari==2.0.0.dev122
hikari-arc==1.1.0
ruff==0.2.0
pre-commit==3.6.0
python-dotenv==1.0.1
asyncpg==0.30.0
sqlalchemy==2.0.36
sqlalchemy==2.0.37
5 changes: 2 additions & 3 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ async def error_handler(ctx: arc.GatewayContext, exc: Exception) -> None:


@client.set_startup_hook
async def startup_hook(client: arc.GatewayClient) -> None:
init_db()
pass
async def startup_hook(_: arc.GatewayClient) -> None:
await init_db()
8 changes: 4 additions & 4 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
TOKEN = os.environ.get("TOKEN") # required
DEBUG = os.environ.get("DEBUG", False)

DB_HOST = os.environ.get("DB_HOST","localhost")
DB_NAME = os.environ.get("DB_NAME", "blockbot")
DB_USER = os.environ.get("DB_USER", "blockbot")
DB_PASSWORD = os.environ.get("DB_PASSWORD","blockbot")
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}
13 changes: 6 additions & 7 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import logging

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

logger = logging.getLogger(__name__)

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


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


# Issue with db on startup recommended drop all tables
def init_db():
Base.metadata.create_all(engine)
async def init_db():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)

0 comments on commit ff06c0c

Please sign in to comment.