From 171a1b08406447009f4bd4306c4fe57ff8d25ddf Mon Sep 17 00:00:00 2001 From: imcompiling Date: Mon, 3 Mar 2025 14:20:54 -0500 Subject: [PATCH] Move sql models into their own file. --- database_setup/create_database.py | 13 --- database_setup/database_test.py | 129 +----------------------------- database_setup/models.py | 128 +++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 140 deletions(-) delete mode 100644 database_setup/create_database.py create mode 100644 database_setup/models.py diff --git a/database_setup/create_database.py b/database_setup/create_database.py deleted file mode 100644 index 612f614..0000000 --- a/database_setup/create_database.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Small script to create the database if it doesn't already exist.""" - -import sqlite3 -from pathlib import Path - -if not Path("database/dakiny.db").exists(): - with sqlite3.connect("database/dakiny.db") as con: - cur = con.cursor() - - with Path("database_setup/database.sql", encoding="utf-8").open() as file: - database_script = file.read() - cur.executescript(database_script) - con.commit() diff --git a/database_setup/database_test.py b/database_setup/database_test.py index 05e7090..91e9d3e 100644 --- a/database_setup/database_test.py +++ b/database_setup/database_test.py @@ -1,130 +1,5 @@ -from sqlalchemy.dialects.mysql import BIGINT, INTEGER, SMALLINT -from sqlmodel import Column, Field, ForeignKey, Relationship, Session, SQLModel, UniqueConstraint, create_engine - - -class Guild(SQLModel, table=True): - """ - Represents a Guild entity in the database. - - Attributes: - id (int): The unique identifier for the guild. - name (str): The name of the guild. - submission_channel (int): The ID of the submission channel associated with the guild. - verification_channel (int): The ID of the verification channel associated with the guild. - active (bool): Indicates whether the guild is active. Defaults to True. - category_groups (list[CategoryGroup]): A list of category groups associated with the guild. - """ - - __tablename__: str = "Guilds" - - id: int = Field(sa_column=Column(BIGINT(unsigned=True), primary_key=True, nullable=False)) - name: str = Field(max_length=95) - submission_channel: int = Field(sa_column=Column(BIGINT(unsigned=True))) - verification_channel: int = Field(sa_column=Column(BIGINT(unsigned=True))) - active: bool = Field(default=True) - - category_groups: list["CategoryGroup"] = Relationship(back_populates="guild", cascade_delete=True) - - -class CategoryGroup(SQLModel, table=True): - """ - Represents a category group in the database. - - Attributes: - id (int | None): The primary key of the category group. - name (str): The name of the category group, with a maximum length of 50 characters. - channel_id (int): The ID of the channel associated with the category group. - guild_id (int): The ID of the guild associated with the category group. - message_id (int): The ID of the message associated with the category group. - pad_length (int): The padding length for the category group, default is 12. - username_length (int): The username length for the category group, default is 12. - guild (Guild | None): The guild relationship back-populated from the Guild model. - """ - - __tablename__: str = "CategoryGroups" - - id: int | None = Field(default=None, primary_key=True) - name: str = Field(max_length=50) - channel_id: int = Field(sa_column=Column(BIGINT(unsigned=True))) - guild_id: int = Field( - sa_column=Column( - BIGINT(unsigned=True), - ForeignKey("Guilds.id", ondelete="CASCADE"), - nullable=False, - ) - ) - message_id: int = Field(sa_column=Column(BIGINT(unsigned=True))) - pad_length: int = Field(default=12, sa_column=Column(SMALLINT(unsigned=True))) - username_length: int = Field(default=12, sa_column=Column(SMALLINT(unsigned=True))) - - __table_args__ = (UniqueConstraint("guild_id", "name", name="UniqueGuildCategoryGroup"),) - - guild: Guild | None = Relationship(back_populates="category_groups") - categories: list["Category"] | None = Relationship(back_populates="category_group", cascade_delete=True) - - -class Category(SQLModel, table=True): - __tablename__: str = "Categories" - - id: int | None = Field(default=None, primary_key=True) - name: str = Field(max_length=30) - group_id: int = Field( - sa_column=Column( - INTEGER(unsigned=True), - ForeignKey("CategoryGroups.id", ondelete="CASCADE"), - nullable=False, - ) - ) - size: int = Field(sa_column=Column(SMALLINT(unsigned=True), default=3, nullable=False)) - is_ascending: bool = Field(default=True, nullable=False) - is_time_record: bool = Field(default=False, nullable=False) - is_validated: bool = Field(default=True, nullable=False) - - __table_args__ = (UniqueConstraint("group_id", "name", name="UniqueCategoryInGroup"),) - - category_group: CategoryGroup | None = Relationship(back_populates="categories") - scores: list["Score"] | None = Relationship(back_populates="category", cascade_delete=True) - - -class Score(SQLModel, table=True): - __tablename__: str = "Scores" - - username: str = Field(max_length=30, nullable=False, primary_key=True) - score: float = Field(nullable=False) - category_id: int = Field( - sa_column=Column( - INTEGER(unsigned=True), - ForeignKey("Categories.id", ondelete="CASCADE"), - nullable=False, - primary_key=True, - ), - ) - - category: Category | None = Relationship(back_populates="scores") - - -class Submission(SQLModel, table=True): - __tablename__: str = "Submissions" - - category_id: int = Field( - sa_column=Column( - INTEGER(unsigned=True), - nullable=False, - primary_key=True, - ), - ) - group_id: int = Field( - sa_column=Column( - INTEGER(unsigned=True), - nullable=False, - ) - ) - guild_id: int = Field(sa_column=Column(BIGINT(unsigned=True), nullable=False)) - message_id: int = Field(sa_column=Column(BIGINT(unsigned=True), nullable=False)) - score: str = Field(max_length=30, nullable=False) - user_id: int = Field(sa_column=Column(BIGINT(unsigned=True), nullable=False)) - username: str = Field(max_length=30, nullable=False, primary_key=True) - +from database_setup.models import CategoryGroup, Guild +from sqlmodel import Session, SQLModel, create_engine url = "sqlite:///database.db" engine = create_engine(url) diff --git a/database_setup/models.py b/database_setup/models.py new file mode 100644 index 0000000..cc6be82 --- /dev/null +++ b/database_setup/models.py @@ -0,0 +1,128 @@ +"""File holds all model definitions of SQL tables.""" + +from sqlalchemy.dialects.mysql import BIGINT, INTEGER, SMALLINT +from sqlmodel import Column, Field, ForeignKey, Relationship, SQLModel, UniqueConstraint + + +class Guild(SQLModel, table=True): + """ + Represents a Guild entity in the database. + + Attributes: + id (int): The unique identifier for the guild. + name (str): The name of the guild. + submission_channel (int): The ID of the submission channel associated with the guild. + verification_channel (int): The ID of the verification channel associated with the guild. + active (bool): Indicates whether the guild is active. Defaults to True. + category_groups (list[CategoryGroup]): A list of category groups associated with the guild. + """ + + __tablename__: str = "Guilds" + + id: int = Field(sa_column=Column(BIGINT(unsigned=True), primary_key=True, nullable=False)) + name: str = Field(max_length=95) + submission_channel: int = Field(sa_column=Column(BIGINT(unsigned=True))) + verification_channel: int = Field(sa_column=Column(BIGINT(unsigned=True))) + active: bool = Field(default=True) + + category_groups: list["CategoryGroup"] = Relationship(back_populates="guild", cascade_delete=True) + + +class CategoryGroup(SQLModel, table=True): + """ + Represents a category group in the database. + + Attributes: + id (int | None): The primary key of the category group. + name (str): The name of the category group, with a maximum length of 50 characters. + channel_id (int): The ID of the channel associated with the category group. + guild_id (int): The ID of the guild associated with the category group. + message_id (int): The ID of the message associated with the category group. + pad_length (int): The padding length for the category group, default is 12. + username_length (int): The username length for the category group, default is 12. + guild (Guild | None): The guild relationship back-populated from the Guild model. + """ + + __tablename__: str = "CategoryGroups" + + id: int | None = Field(default=None, primary_key=True) + name: str = Field(max_length=50) + channel_id: int = Field(sa_column=Column(BIGINT(unsigned=True))) + guild_id: int = Field( + sa_column=Column( + BIGINT(unsigned=True), + ForeignKey("Guilds.id", ondelete="CASCADE"), + nullable=False, + ) + ) + message_id: int = Field(sa_column=Column(BIGINT(unsigned=True))) + pad_length: int = Field(default=12, sa_column=Column(SMALLINT(unsigned=True))) + username_length: int = Field(default=12, sa_column=Column(SMALLINT(unsigned=True))) + + __table_args__ = (UniqueConstraint("guild_id", "name", name="UniqueGuildCategoryGroup"),) + + guild: Guild | None = Relationship(back_populates="category_groups") + categories: list["Category"] | None = Relationship(back_populates="category_group", cascade_delete=True) + + +class Category(SQLModel, table=True): + __tablename__: str = "Categories" + + id: int | None = Field(default=None, primary_key=True) + name: str = Field(max_length=30) + group_id: int = Field( + sa_column=Column( + INTEGER(unsigned=True), + ForeignKey("CategoryGroups.id", ondelete="CASCADE"), + nullable=False, + ) + ) + size: int = Field(sa_column=Column(SMALLINT(unsigned=True), default=3, nullable=False)) + is_ascending: bool = Field(default=True, nullable=False) + is_time_record: bool = Field(default=False, nullable=False) + is_validated: bool = Field(default=True, nullable=False) + + __table_args__ = (UniqueConstraint("group_id", "name", name="UniqueCategoryInGroup"),) + + category_group: CategoryGroup | None = Relationship(back_populates="categories") + scores: list["Score"] | None = Relationship(back_populates="category", cascade_delete=True) + + +class Score(SQLModel, table=True): + __tablename__: str = "Scores" + + username: str = Field(max_length=30, nullable=False, primary_key=True) + score: float = Field(nullable=False) + category_id: int = Field( + sa_column=Column( + INTEGER(unsigned=True), + ForeignKey("Categories.id", ondelete="CASCADE"), + nullable=False, + primary_key=True, + ), + ) + + category: Category | None = Relationship(back_populates="scores") + + +class Submission(SQLModel, table=True): + __tablename__: str = "Submissions" + + category_id: int = Field( + sa_column=Column( + INTEGER(unsigned=True), + nullable=False, + primary_key=True, + ), + ) + group_id: int = Field( + sa_column=Column( + INTEGER(unsigned=True), + nullable=False, + ) + ) + guild_id: int = Field(sa_column=Column(BIGINT(unsigned=True), nullable=False)) + message_id: int = Field(sa_column=Column(BIGINT(unsigned=True), nullable=False)) + score: str = Field(max_length=30, nullable=False) + user_id: int = Field(sa_column=Column(BIGINT(unsigned=True), nullable=False)) + username: str = Field(max_length=30, nullable=False, primary_key=True)