feat: 🗃️ Add database integration with Tortoise ORM and Aerich setup#44
feat: 🗃️ Add database integration with Tortoise ORM and Aerich setup#44Paillat-dev merged 5 commits intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements database integration for the Versa Discord bot using Tortoise ORM as the database layer and Aerich for database migrations. The implementation addresses issue #9 by adding persistent storage capabilities, which will enable features like the planned tag command.
Changes:
- Added Tortoise ORM (v0.25.4) and Aerich (v0.9.2) dependencies for database management
- Created a Guild model to store Discord guild information with a BigIntField primary key
- Implemented database initialization and shutdown functions with automatic migration support
- Updated bot startup flow to initialize the database before starting the Discord bot
Reviewed changes
Copilot reviewed 7 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| uv.lock | Added dependencies for Tortoise ORM, Aerich, and their transitive dependencies (aiosqlite, anyio, asyncclick, dictdiffer, iso8601, pypika-tortoise, pytz, tomli-w, colorama) |
| pyproject.toml | Added aerich[toml] and tortoise-orm to project dependencies; configured Aerich tool settings and ruff linter exclusions for autogenerated migrations |
| src/config.py | Created configuration module to load environment variables including bot TOKEN and database path (defaults to data/database.db) |
| src/database/init.py | Implemented database initialization with TORTOISE_ORM config, init_db() for running migrations, and shutdown_db() for cleanup |
| src/database/models/guild.py | Created Guild model with discord_id as BigIntField primary key |
| src/database/models/init.py | Exported Guild model for use in other modules |
| src/database/migrations/models/0_20260223163429_init.py | Auto-generated initial migration to create guild and aerich tables |
| src/main.py | Refactored bot startup to use async/await pattern with database initialization before bot.start() and cleanup in finally block |
| .gitignore | Added data/ directory to gitignore to exclude database files from version control |
Comments suppressed due to low confidence (3)
src/main.py:9
- There's an inconsistency in import style. Lines 6-7 use absolute imports (
from src import ...) while line 9 uses a relative import (from .config import TOKEN). For consistency, all imports from the src package should use the same style. Consider changing line 9 to use an absolute import:from src.config import TOKEN
from src import log_setup
from src.database import init_db, shutdown_db
from .config import TOKEN
src/config.py:10
- The DB_PATH configuration uses os.getenv with a default Path object, but the default should be a string. When os.getenv returns None, the expression evaluates to
Path("data/database.db")which is correct, but this pattern is unconventional and could be confusing. Consider using:DB_PATH = Path(os.getenv("DB_PATH", "data/database.db")).absolute()
DB_PATH = Path(os.getenv("DB_PATH") or Path("data/database.db")).absolute()
src/database/init.py:37
- The init_db and shutdown_db functions lack docstrings. According to the codebase conventions seen in src/log_setup.py and src/cogs/slash_commands.py, functions should have NumPy-style docstrings. Consider adding docstrings to document the purpose and behavior of these functions, especially for init_db which performs migration and initialization.
async def init_db() -> None:
command = aerich.Command(
TORTOISE_ORM,
app="models",
location="./src/database/migrations/",
)
await command.init()
migrated = await command.upgrade(run_in_transaction=True)
if migrated:
logger.info("Successfully migrated %s migrations: %s", len(migrated), migrated)
else:
logger.info("No migrations to apply.")
await Tortoise.init(config=TORTOISE_ORM)
async def shutdown_db() -> None:
await Tortoise.close_connections()
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Bump |
|
Reminder to probably make use of volumes and not store it within the container |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
ToothyDev
left a comment
There was a problem hiding this comment.
Looks good. The only 2 things I'm not sure about are
- Keeping "our own" ID and a Discord ID for the Guild and User, not sure why
- We will manually have to create a docker volume at the correct filepath for this to work, right?
|
|
lgtm merge as you please |
Closes #9