Skip to content

Commit

Permalink
DQB-27 create weekly menu (#38)
Browse files Browse the repository at this point in the history
* DQB-27 Created menu tables

* Add initial implementation of MenuService

* DQB-27 Fix black alembic hook to no longer think line lenght is 79

* Clean up DockerFile (#37)

* Add .env to .gitignore file

* Update precommit with hook that auto adds ticket from branch name

* Update pre commit config so things are install into the correct stages

* Update docker to use pipx rather than a curl command

* Update the docker file to utilize pipx and an up to date version of poetry

* Fix comments in Dockerfile

---------

Co-authored-by: JP Hanna <[email protected]>

* Introduce initial retrieve tavern menu of the week support

* Fix commit-msg hook

* Created IntEnum model type

* Update pre commit versions and set giticket mode

* Update ars for giticket

* DQB-27 Test

* Update MenuItem to use Enum and update Menu query to be more specific

* Add menu data in tavern menu command response

* DQB-27 Create test for menu item grouping property

* DQB-27 Fix commit msg hooks

Replace ticket commit msg hook to work when not using -m.
Add hook for commit message linting.

* DQB-27 Update gitlint rules

Title match now matches what should always be expected.
Required file names updated to include more configuration level files.

DQB-27 #comment # Please enter the commit message for your changes. Lines starting

* DQB-27 Write tests for the new menu retrieval command

Updated ctx fixture to include guild and be modifiable per test.
Unit tests created for Menu property, and all variations of the command controller

DQB-27 #comment # Please enter the commit message for your changes. Lines starting

* DQB-27 Migrate dictionary configuration to pydantic-settings

Dictionary based config does not support defaulting or validation.
Pydantic supports an extensible env reader configuration.
Updated pyproject.toml with pydantic and pydantic-settings.
As well as including pydantic within mypy

* DQB-27 Update poetry lock file with new package versions

dependency-injector does not support 3.12 yet so updating all existing packages.
pydantic requires usage of typing-extension.
Update pyproject.toml to allow upward bounds of sqlalchemy, asyncpg.

* DQB-27 Finish migration from dict based settings to pydantic

docker-compose.yml updated to support testing through env variables.
Also updated docker-compose to reduce redundancy.
Resolve mypy issues.

* DQB-27 Update to better support local setup

Update Dockerfile and docker-compose.yml to better support unique local setup
Update README.md to include all env variables that are required

* DQB-27 Fix issues with the Pydantic Setings configuration

Dependency-injector does not support pydantic settings v2 so must still use dictionary.
Defaults were also not defined correctly.

* DQB-27 Enforce non empty strings in configuration

Utilize type Annotation and StringConstraint to enforce that config values cannot be an empty string.
Thus not allowing the server to start if not all the configuration values are there.

* DQB-27 Migrate to pydantic and SQLModel

Update pyproject.toml to include SQLModel.
Update docker-compose.yml to continue to better support dynamic local development

* DQB-27 Updates to testing suite

Update more of testing suite to work

* DQB-27 Remove bad fixture, update get first

get first was using replace but not setting it to anything.

* DQB-27 Fix integration tests by fixing event loop creation

Event loop was not being created within the event loop causing 2 differents loops to be used.

* DQB-27 Update sqlalchemy and pytest versioning

Update pyproject.toml to cap pytest version, 8.0.0 currently breaks pytest-asyncio
Update sqlalchemy to now use non beta versions and ensure asyncio support is included.

* DQB-27 Fix pydantic config not properly dumping db uri

Pydantic requires properties to be marked as computed fields
Alembic config also needed to be updated to use this new uri

* DQB-27 Align tests with actual run of bot

Tests and bot run should both use sqlmodles AsyncSession class
Updated repository to utilize this new class

* DQB-27 Remove unnecessary mypy workaround

Previously discord did not properly support mypy typing, has since been fixed

* DQB-27 Introduce menu tables and update alembic for sqlmodel

Update pyproject.toml to introduce new version of ruff.
Update alembic setup to support usage of sqlmodel

* DQB-27 Update ignore files

Update ignore files to support alembic and daemon mypy

* DQB-27 Update tavern group to add more layers

Tavern group now has menu as a sub group.
Create initial command to add items to the tavern menu

* DQB-27 Introduce code behind add menu item

Update menu service to be a more generic as a tavern service
Fill in code for adding a new item to the menu
DQB-27 #comment # Please enter the commit message for your changes. Lines starting

* DQB-27 Fix menu lookup query

Menu needs a big into for server id and look up needs to use between correctly

* DQB-27 Update group commands and clean up menu format

Group commands cannot be called directly and must have a fallback
Menu format is currently hard to read, adding some touch ups

* DQB-27 Create unit tests for upsert controller

Update existing tests for menu look up, and create new ones for the upsert to menu

* DQB-27 Introduce new integration tests

Introduce missing join test
Introduce test for update method
Update query arg to remove type in name of join attribute

* DQB-27 Update more of query arg to remove type in name

Rather than have list in name of all the query attributes have names that point
more to their functionality

* DQB-27 Update ruff config to support new style

Update pyproject.toml so that ruff lint configuration matches new standard

* DQB-27 Update service code to support multiple repos

Current service design does not support multiple repos.
Introduce a handler class that allows service to dynamically access
multiple repos by a defined name.
Implement command to remove items from menu

* DQB-27 Create tests for remove item

Create controller and service level unit tests for the remove item command

* DQB-27 Create more tests for deleting a menu item

Created a success test and created a some empty list tests

* DQB-27 Fix typo in docker ignore

Accidental typo in .dockerignore

* DQB-27 Update github actions

Update checkout to most recent version of 4.1.1
Update setup-python to version 5

---------

Co-authored-by: JP Hanna <[email protected]>
  • Loading branch information
jplhanna and JP Hanna authored Feb 8, 2024
1 parent 7dc9d33 commit 3543134
Show file tree
Hide file tree
Showing 43 changed files with 2,065 additions and 1,251 deletions.
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.github
.mypy_cache
.pytest_cache
alembic
alembic.ini
logs
local.env
.gitignore
6 changes: 3 additions & 3 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: actions/checkout@v4.1.1
- uses: actions/setup-python@v5
with:
python-version: '3.10'
python-version: '3.11'
- name: Build Docker images
run: |
docker-compose build test-pipeline
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage.xml
local.env
docker-compose.override.yml
.env
.dmypy.json
77 changes: 77 additions & 0 deletions .gitlint
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
### GENERAL CONFIG ###
[general]
# Ignore rules, reference them by id or name (comma-separated)
ignore=title-trailing-punctuation, T3

# verbosity should be a value between 1 and 3
verbosity = 2

# By default gitlint will ignore certain commits
ignore-merge-commits=true
ignore-revert-commits=true
ignore-fixup-commits=true
ignore-fixup-amend-commits=true
ignore-squash-commits=true

# Ignore any data sent to gitlint via stdin
ignore-stdin=true

# Fetch additional meta-data from the local repository when manually passing a
# commit message to gitlint via stdin or --commit-msg. Disabled by default.
staged=true

# Hard fail when the target commit range is empty.
fail-without-commits=true

# Whether to use Python `search` instead of `match` semantics in rules
regex-style-search=true

### RULE CONFIGURATION ###
[title-max-length]
line-length=100

[title-min-length]
min-length=5

[title-must-not-contain-word]
# Comma-separated list of words that should not occur in
# the commit message title (case-insensitive).
words=wip,foobar

[title-match-regex]
regex=^DQB-\d+\s.*

[body-max-line-length]
line-length=120

[body-min-length]
min-length=5

[body-changed-file-mention]
# Files that need to be explicitly mentioned in the body when they change
files=gitlint-core/gitlint/rules.py,README.md,pyproject.toml,pre-commit-config.yaml,Dockerfile,docker-compose.yaml


### NAMED RULES ###
[title-must-not-contain-word:Additional-Words]
words=foo,bar


### IGNORE RULES CONFIGURATION ###
[ignore-by-title]
# Ignore rules for commits of which the title matches a regex
regex=^Release(.*) #
ignore=T1,body-min-length #

[ignore-by-body]
# Ignore rules for commits of which the body has a line that matches a regex
regex=(.*)release(.*) #
ignore=T1,body-min-length

[ignore-body-lines]
# Ignore all lines that start with 'Co-Authored-By'
regex=^Co-Authored-By #

[ignore-by-author-name]
regex=(.*)dependabot(.*) #
ignore=T1,body-min-length
18 changes: 13 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
default_install_hook_types:
- pre-commit
- prepare-commit-msg
- commit-msg
default_stages:
- pre-commit
Expand All @@ -14,7 +15,7 @@ repos:
args: [ --line-length=120 ]

- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.10.0
rev: v2.12.0
hooks:
- id: pretty-format-toml
args: [--autofix]
Expand All @@ -25,7 +26,7 @@ repos:
- id: check-poetry

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: check-merge-conflict
Expand Down Expand Up @@ -66,9 +67,16 @@ repos:
exclude: alembic|test_.*|conftest.py
args: [ -c, pyproject.toml ]

- repo: https://github.com/milin/giticket
rev: v1.4
- repo: https://github.com/radix-ai/auto-smart-commit
rev: v1.0.3
hooks:
- id: giticket
- id: auto-smart-commit
stages:
- prepare-commit-msg

- repo: https://github.com/jorisroovers/gitlint
rev: v0.19.1
hooks:
- id: gitlint
stages:
- commit-msg
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ ENV PATH="$POETRY_VIRTUALENVS_PATH/bin:$PIPX_BIN_DIR:$PATH"
RUN pip install --upgrade pip \
&& pip install pipx \
&& pipx install poetry==$POETRY_VERSION
RUN apt-get update && apt-get install -y --no-install-recommends gcc

# Install python dependencies
RUN poetry install --no-root --no-ansi --no-interaction --only=main --no-directory
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ DISCORD_ACCOUNT_TOKEN="account token for you discord bot"
DATABASE_NAME="Name for the database"
DATABASE_USER="Name for user with access to the database"
DATABASE_PASSWORD="Password to the database"
DISCORD_OWNER_ID="The discord id of the owner of the channel you are using."
```

Install all packages and start the server using `Docker` and `docker-compose`
Expand Down
2 changes: 1 addition & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
hooks = black
black.type = console_scripts
black.entrypoint = black
black.options = -l 79 REVISION_SCRIPT_FILENAME
black.options = -l 120 REVISION_SCRIPT_FILENAME

# Logging configuration
[loggers]
Expand Down
6 changes: 4 additions & 2 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
from src.config import DATABASE_URI
from src.config import DBSettings
from src.helpers.sqlalchemy_helpers import BaseModel
from src import model_hub

config = context.config
db_settings = DBSettings()

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name) # type: ignore

config.set_main_option("sqlalchemy.url", DATABASE_URI)
config.set_main_option("sqlalchemy.url", db_settings.database_uri)

# add your model's MetaData object here
# for 'autogenerate' support
Expand Down
1 change: 1 addition & 0 deletions alembic/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
import sqlmodel
${imports if imports else ""}

# revision identifiers, used by Alembic.
Expand Down
59 changes: 59 additions & 0 deletions alembic/versions/4232a8d67776_introduce_menu_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Introduce menu tables
Revision ID: 4232a8d67776
Revises: 2f0c6e712786
Create Date: 2024-02-06 02:34:23.627469
"""

from alembic import op
import sqlalchemy as sa
import sqlmodel

from src.constants import DayOfWeek
from src.helpers.sqlalchemy_helpers import EnumColumn

# revision identifiers, used by Alembic.
revision = "4232a8d67776"
down_revision = "2f0c6e712786"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"menu",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("datetime_created", sa.DateTime(), nullable=False),
sa.Column("datetime_edited", sa.DateTime(), nullable=False),
sa.Column("server_id", sa.Integer(), nullable=False),
sa.Column("start_date", sa.Date(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"menu_item",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("datetime_created", sa.DateTime(), nullable=False),
sa.Column("datetime_edited", sa.DateTime(), nullable=False),
sa.Column("food", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column("day_of_the_week", EnumColumn(DayOfWeek), nullable=True),
sa.Column("menu_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(
["menu_id"],
["menu.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.drop_constraint("user_quest_quest_id_fkey", "user_quest", type_="foreignkey")
op.create_foreign_key(None, "user_quest", "quest", ["quest_id"], ["id"])
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, "user_quest", type_="foreignkey")
op.create_foreign_key("user_quest_quest_id_fkey", "user_quest", "quest", ["quest_id"], ["id"], ondelete="CASCADE")
op.drop_table("menu_item")
op.drop_table("menu")
# ### end Alembic commands ###
30 changes: 30 additions & 0 deletions alembic/versions/9deebb8552c0_update_server_id_to_big_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Update server id to big int
Revision ID: 9deebb8552c0
Revises: 4232a8d67776
Create Date: 2024-02-06 06:16:05.689061
"""

from alembic import op
import sqlalchemy as sa
import sqlmodel


# revision identifiers, used by Alembic.
revision = "9deebb8552c0"
down_revision = "4232a8d67776"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column("menu", "server_id", existing_type=sa.INTEGER(), type_=sa.BigInteger(), existing_nullable=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column("menu", "server_id", existing_type=sa.BigInteger(), type_=sa.INTEGER(), existing_nullable=False)
# ### end Alembic commands ###
44 changes: 26 additions & 18 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
version: "3.7"
x-common-variables: &common-variables
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_USER: ${DATABASE_USER}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}

x-common-db-variables: &common-db-variables
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_USER: ${DATABASE_USER}

services:
backend:
container_name: backend-${IMAGE_TAG-latest}
image: discord_bot/backend:${IMAGE_TAG-latest}
environment:
<<: *common-variables
DISCORD_ACCOUNT_TOKEN: ${DISCORD_ACCOUNT_TOKEN}
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_USER: ${DATABASE_USER}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
DATABASE_HOST: 'postgres'
DISCORD_OWNER_ID: ${DISCORD_OWNER_ID}
volumes:
Expand All @@ -29,39 +36,40 @@ services:
command: [ "postgres", "-c", "listen_addresses=*", "-c", "log_statement=all" ]
restart: always
environment:
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_USER: ${DATABASE_USER}
<<: *common-db-variables
POSTGRES_DB: ${DATABASE_NAME}
PGDATA: /var/lib/postgresql/data
networks:
- network
ports:
- "5432:5432"
expose:
- "5432"
volumes:
- db:/var/lib/postgresql/data

test-db:
image: postgres:14
hostname: "test_db"
environment:
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_USER: ${DATABASE_USER}
<<: *common-db-variables
POSTGRES_DB: ${TEST_DATABASE_NAME}
ports:
- "5433:5432"
networks:
- network

test-pipeline:
container_name: test-pipeline-${IMAGE_TAG-latest}
image: discord_bot/test_pipeline:${IMAGE_TAG-latest}
environment:
DATABASE_HOST: 'test-db'
TEST_DATABASE_NAME: ${TEST_DATABASE_NAME}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
TEST_DATABASE_PORT: '5432'
DATABASE_USER: ${DATABASE_USER}
<<: *common-variables
DATABASE_HOST: 'test_db'
DATABASE_NAME: ${TEST_DATABASE_NAME}
DATABASE_PORT: '5432'
DISCORD_ACCOUNT_TOKEN: "fake_token"
LOGGER__HANDLERS__BASIC_HANDLER__HANDLER_CLASS: 'logging.StreamHandler'
LOGGER__HANDLERS__BASIC_HANDLER__LEVEL: 20
LOGGER__HANDLERS__BASIC_HANDLER__FORMATTER: 'simple_formatter'
LOGGER__HANDLERS__BASIC_HANDLER__STREAM: 'ext://sys.stdout'
volumes:
- .:/app
networks:
- network
build:
context: .
dockerfile: Dockerfile
Expand Down
Loading

0 comments on commit 3543134

Please sign in to comment.