Compare commits
5
Commits
bd365d575f
...
36eae17751
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36eae17751 | ||
|
|
7a57b75ca3 | ||
|
|
0ea9ef5b41 | ||
|
|
e13790ba08 | ||
|
|
171a1b0840 |
@@ -5,7 +5,7 @@ from nextcord.ext import commands
|
||||
from sqlalchemy import exc, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from database_setup.sql_data_classes import Category, CategoryGroup, engine
|
||||
from database_setup.db import Category, CategoryGroup, engine
|
||||
from utilities.utils import MAX_LINE_LENGTH, autofill_groups, send_highscore_message
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ from sqlalchemy import distinct, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import Category, CategoryGroup, Score, engine
|
||||
from database_setup.db import Category, CategoryGroup, Score, engine
|
||||
from utilities.utils import (
|
||||
autofill_categories,
|
||||
autofill_groups,
|
||||
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import CategoryGroup, engine
|
||||
from database_setup.db import CategoryGroup, engine
|
||||
from utilities.utils import autofill_groups, send_highscore_message
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import CategoryGroup, Guild, engine
|
||||
from database_setup.db import CategoryGroup, Guild, engine
|
||||
from utilities.utils import MAX_LINE_LENGTH, autofill_groups
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from cogs.submission.verification_view import VerificationView
|
||||
from database_setup.sql_data_classes import Category, CategoryGroup, Guild, Submission, engine
|
||||
from database_setup.db import Category, CategoryGroup, Guild, Submission, engine
|
||||
from main import logger
|
||||
from utilities.utils import autofill_categories, autofill_groups, submit_score
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import Guild, engine
|
||||
from database_setup.db import Guild, engine
|
||||
from utilities.utils import autofill_users_in_guild
|
||||
|
||||
from .views.update_username_verify import UpdateUsernameVerifyView
|
||||
|
||||
@@ -7,7 +7,7 @@ from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import Category, CategoryGroup, Score, engine
|
||||
from database_setup.db import Category, CategoryGroup, Score, engine
|
||||
from utilities.utils import MAX_LINE_LENGTH, autofill_categories, autofill_groups, format_score_string
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ from sqlalchemy import delete, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import Category, CategoryGroup, engine
|
||||
from database_setup.db import Category, CategoryGroup, engine
|
||||
from utilities.utils import send_highscore_message
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ from sqlalchemy import distinct, exc, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import Category, CategoryGroup, Score, engine
|
||||
from database_setup.db import Category, CategoryGroup, Score, engine
|
||||
from utilities.utils import send_highscore_message
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import Guild, engine
|
||||
from database_setup.db import Guild, engine
|
||||
|
||||
|
||||
class GuildListener(commands.Cog, name="Guild Listener"):
|
||||
|
||||
@@ -7,7 +7,7 @@ from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import Guild, engine
|
||||
from database_setup.db import Guild, engine
|
||||
|
||||
|
||||
class GuildData(commands.Cog, name="Guild Data"):
|
||||
|
||||
@@ -4,7 +4,7 @@ import nextcord
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from database_setup.sql_data_classes import Submission, engine
|
||||
from database_setup.db import Submission, engine
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.utils import submit_score
|
||||
|
||||
@@ -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()
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
"""Simple file to set up the sqlmodel connection to database."""
|
||||
|
||||
from sqlmodel import SQLModel, create_engine
|
||||
from utilities import config
|
||||
|
||||
from . import models # noqa: F401 Need this import for creating the models.
|
||||
|
||||
if config.ENVIRONMENT == "prod":
|
||||
url = f"mysql+pymysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
|
||||
else:
|
||||
url = "sqlite:///database.db"
|
||||
engine = create_engine(url)
|
||||
|
||||
# Don't need to create all when connected to prod.
|
||||
SQLModel.metadata.create_all(engine)
|
||||
@@ -0,0 +1,177 @@
|
||||
"""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)
|
||||
# deprecated, not currently used.
|
||||
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.
|
||||
|
||||
Table Constraints:
|
||||
__table_args__: Unique constraint on guild_id and name to ensure unique category groups within a guild.
|
||||
"""
|
||||
|
||||
__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):
|
||||
"""
|
||||
Represents a category in the database.
|
||||
|
||||
Attributes:
|
||||
id (int): The unique identifier for the category. Primary key.
|
||||
name (str): The name of the category with a maximum length of 30 characters.
|
||||
group_id (int): The identifier of the group to which the category belongs.
|
||||
Foreign key referencing CategoryGroups.id.
|
||||
size (int): The size attribute of the category, default is 3.
|
||||
is_ascending (bool): Indicates if the category is in ascending order. Default is True.
|
||||
is_time_record (bool): Indicates if the category is a time record. Default is False.
|
||||
is_validated (bool): Indicates if the category is validated. Default is True.
|
||||
category_group (CategoryGroup | None): The relationship to the CategoryGroup model.
|
||||
scores (list["Score"] | None): The relationship to the Score model with cascade delete enabled.
|
||||
|
||||
Table Constraints:
|
||||
__table_args__: Unique constraint on group_id and name to ensure unique category names within a group.
|
||||
"""
|
||||
|
||||
__tablename__: str = "Categories"
|
||||
|
||||
id: int = 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):
|
||||
"""
|
||||
Represents a score entry in the database.
|
||||
|
||||
Attributes:
|
||||
username (str): The username associated with the score. This is a primary key.
|
||||
score (float): The score value.
|
||||
category_id (int): The ID of the category this score belongs to. This is a primary key and a foreign key
|
||||
referencing the Categories table.
|
||||
category (Category | None): The relationship to the Category model, back-populated by the scores attribute
|
||||
in the Category model.
|
||||
"""
|
||||
|
||||
__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):
|
||||
"""
|
||||
Represents a submission in the database.
|
||||
|
||||
Attributes:
|
||||
category_id (int): The ID of the category. This is a primary key.
|
||||
group_id (int): The ID of the group.
|
||||
guild_id (int): The ID of the guild.
|
||||
message_id (int): The ID of the message.
|
||||
score (str): The score of the submission, with a maximum length of 30 characters.
|
||||
user_id (int): The ID of the user.
|
||||
username (str): The username of the user. This is a primary key.
|
||||
"""
|
||||
|
||||
__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 is stored as a string until converted during submission.
|
||||
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)
|
||||
@@ -1,32 +0,0 @@
|
||||
"""Simple file to set up the sqlalchemy connection to database."""
|
||||
|
||||
# from sqlalchemy import create_engine
|
||||
# from sqlalchemy.ext.automap import automap_base
|
||||
from sqlmodel import BigInteger, Column, Field, SQLModel, create_engine
|
||||
from utilities import config
|
||||
|
||||
|
||||
class Guilds(SQLModel, table=True):
|
||||
id: int = Field(sa_column=Column(BigInteger(), primary_key=True))
|
||||
name: str
|
||||
submission_channel: int = Field(sa_column=Column(BigInteger()))
|
||||
verification_channel: int = Field(sa_column=Column(BigInteger()))
|
||||
active: bool = Field(default=True)
|
||||
|
||||
|
||||
# connect with sql alchemy and autoload schema
|
||||
if config.ENVIRONMENT == "prod":
|
||||
url = f"mysql+pymysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
|
||||
else:
|
||||
url = "sqlite:///database.db"
|
||||
engine = create_engine(url)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
# Base = automap_base()
|
||||
# Base.prepare(autoload_with=engine)
|
||||
|
||||
# # define the classes
|
||||
# Category = Base.classes.Categories
|
||||
# CategoryGroup = Base.classes.CategoryGroups
|
||||
# Guild = Base.classes.Guilds
|
||||
# Score = Base.classes.Scores
|
||||
# Submission = Base.classes.Submissions
|
||||
+1
-1
@@ -8,7 +8,7 @@ from sqlalchemy import distinct, select
|
||||
from sqlalchemy.orm import Session, aliased
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database_setup.sql_data_classes import Category, CategoryGroup, Guild, Score, engine
|
||||
from database_setup.db import Category, CategoryGroup, Guild, Score, engine
|
||||
|
||||
MAX_LINE_LENGTH = 142
|
||||
|
||||
|
||||
Reference in New Issue
Block a user