From 3a4ac066a2087d0d12e3e1d7a0f92a281b6b8f10 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Mon, 17 Mar 2025 17:40:21 -0400 Subject: [PATCH] Add database code. Starting with birthday table. --- database/__init__.py | 1 + database/db.py | 11 +++++++++++ database/models.py | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 database/__init__.py create mode 100644 database/db.py create mode 100644 database/models.py diff --git a/database/__init__.py b/database/__init__.py new file mode 100644 index 0000000..920815a --- /dev/null +++ b/database/__init__.py @@ -0,0 +1 @@ +"""Package for database interaction.""" diff --git a/database/db.py b/database/db.py new file mode 100644 index 0000000..983f3c9 --- /dev/null +++ b/database/db.py @@ -0,0 +1,11 @@ +"""Simple file to set up the sqlmodel connection to database.""" + +from sqlmodel import SQLModel, create_engine + +from . import models # noqa: F401 - Need this import for creating the models. + +url = "sqlite:///database.db" +engine = create_engine(url) + +# Don't need to create all when connected to prod. +SQLModel.metadata.create_all(engine) diff --git a/database/models.py b/database/models.py new file mode 100644 index 0000000..c45215e --- /dev/null +++ b/database/models.py @@ -0,0 +1,20 @@ +"""File holds all model definitions of SQL tables.""" + +from sqlalchemy.dialects.mysql import BIGINT, SMALLINT +from sqlmodel import Column, Field, SQLModel + + +class UserBirthday(SQLModel, table=True): + """ + Represents a user's birthday. + + Attributes: + user_id: The user's discord id. Primary key. + month: The month the user was born. + day: The day the user was born. + + """ + + user_id: int = Field(sa_column=Column(BIGINT(unsigned=True), primary_key=True, nullable=False)) + month: int = Field(sa_column=Column(SMALLINT(unsigned=True), nullable=False)) + day: int = Field(sa_column=Column(SMALLINT(unsigned=True), nullable=False))