Skip to content

feat: 🗃️ Add database integration with Tortoise ORM and Aerich setup#44

Merged
Paillat-dev merged 5 commits intomainfrom
feat/db
Mar 7, 2026
Merged

feat: 🗃️ Add database integration with Tortoise ORM and Aerich setup#44
Paillat-dev merged 5 commits intomainfrom
feat/db

Conversation

@Paillat-dev
Copy link
Copy Markdown
Member

@Paillat-dev Paillat-dev commented Feb 23, 2026

Closes #9

ToothyDev
ToothyDev previously approved these changes Feb 23, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/config.py
Comment thread src/__main__.py Outdated
@ToothyDev
Copy link
Copy Markdown
Collaborator

Bump

@ToothyDev
Copy link
Copy Markdown
Collaborator

Reminder to probably make use of volumes and not store it within the container

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Paillat-dev Paillat-dev requested a review from ToothyDev March 7, 2026 11:47
Copy link
Copy Markdown
Collaborator

@ToothyDev ToothyDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@Paillat-dev
Copy link
Copy Markdown
Member Author

  1. Although the discord id is not strictly speaking a natural key, it is at the edge of being one, and they should generally be avoided for stability reasons. Realistically we wouldn't risk anything using it directly here, it's more of a "best practice" kind of thing, which could very well matter a lot for some people, and since this repo also wants to serve as some kind of reference, I think we should do it with separate keys.
  2. I've done that already in coolify :)

@ToothyDev
Copy link
Copy Markdown
Collaborator

lgtm merge as you please

@Paillat-dev Paillat-dev merged commit 384c527 into main Mar 7, 2026
9 checks passed
@Paillat-dev Paillat-dev deleted the feat/db branch March 7, 2026 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a database

3 participants