Skip to content

Commit

Permalink
Docker Working* db
Browse files Browse the repository at this point in the history
  • Loading branch information
HypnoAnt committed Jan 15, 2025
1 parent 4cae070 commit c5ea9db
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
bot:
build: .
restart: unless-stopped
depends_on:
- postgres
environment:
- POSTGRES_DB=blockbot
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password

postgres:
image: postgres:latest
restart: unless-stopped
ports:
- 5432:5432
volumes:
- ./postgres:/var/lib/postgresql/data
environment:
- POSTGRES_DB=blockbot

volumes:
postgres:
7 changes: 7 additions & 0 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hikari

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

if TOKEN is None:
print("TOKEN environment variable not set. Exiting.")
Expand Down Expand Up @@ -34,3 +35,9 @@ async def error_handler(ctx: arc.GatewayContext, exc: Exception) -> None:
logging.error(exc)

raise exc


@client.set_startup_hook
async def startup_hook(client: arc.GatewayClient) -> None:
init_db()
pass
5 changes: 5 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +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")

CHANNEL_IDS: dict[str, int] = {"lobby": 627542044390457350}
20 changes: 20 additions & 0 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.asyncio import create_async_engine
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)


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


# Issue with db on startup recommended drop all tables
def init_db():
Base.metadata.create_all(engine)

0 comments on commit c5ea9db

Please sign in to comment.