Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to SQLModel Database stuff #93

Merged
merged 19 commits into from
May 27, 2024
Merged

Switch to SQLModel Database stuff #93

merged 19 commits into from
May 27, 2024

Conversation

jontyms
Copy link
Member

@jontyms jontyms commented May 14, 2024

TODO:

  • rework config options
  • Alembic (db migrations
  • Testing
  • Remove Print statements
  • Code review
  • Even more Testing

docker-compose-tests.yml Fixed Show fixed Hide fixed
docker-compose-tests.yml Fixed Show fixed Hide fixed
app/util/forms.py Fixed Show fixed Hide fixed
@Helithumper
Copy link
Contributor

We'll need some sort of migration system for at least up-migrations on the SQL DB

@@ -22,7 +22,7 @@ RUN mv bws /usr/local/bin
RUN rm -r /tmp/

# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
Copy link
Contributor

Choose a reason for hiding this comment

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

Unsure if this is a good idea or not but haven't seen it used before

Copy link
Member Author

Choose a reason for hiding this comment

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

This create a pip cache that is stored outside of the docker container, speeding up rebuilds
https://pythonspeed.com/articles/docker-cache-pip-downloads/

Choose a reason for hiding this comment

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

Just looking at random repo and came across this. If you want a faster build, you can always setup CI pipeline with GH Action and store it on PKG registry, then fetch it on the server.

# Clean up
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

ADD https://github.com/bitwarden/sdk/releases/download/bws-v0.4.0/bws-x86_64-unknown-linux-gnu-0.4.0.zip /tmp
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the official installation method for the bitwarden SDK?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes https://bitwarden.com/help/secrets-manager-cli/

I didn't see they had an sdk before might switch to that https://bitwarden.com/help/secrets-manager-sdk/

Copy link
Member Author

Choose a reason for hiding this comment

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

app/main.py Outdated

statement = select(UserModel).where(UserModel.discord_id == discordData["id"])
user = session.exec(statement).one_or_none()
print(user)
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace with logger usage

UpdateExpression="SET discord = :discord",
ExpressionAttributeValues={":discord": full_data["discord"]},
)
"color": discordData.get("accent_color"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these default to something other than None?

app/main.py Outdated
# Get data from SqlModel
statement = (
select(UserModel)
.where(UserModel.id == user_jwt["id"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Handling of KeyError?

Copy link
Member Author

Choose a reason for hiding this comment

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

Should not occur @Authentication.member will fail if the user has an invalid JWT



class DiscordModel(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it a good idea to make all these fields nullable? Why not just make the reference to this table nullable instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably not




class UserModel(SQLModel, table=True):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should almost all of these fields be nullable?

Copy link
Member Author

Choose a reason for hiding this comment

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

I will review, but most of the values are unknown when we first write the user to the DB before they do the onboarding forms

router = APIRouter(prefix="/api", tags=["API"], responses=Errors.basic_http())


"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Docstrings go under the function signature


# Update the ethics form with new values
validated_data = apply_fuzzy_parsing(ethics_form_data.model_dump(exclude_unset=True), EthicsFormModel)
print(validated_data.dict())
Copy link
Contributor

Choose a reason for hiding this comment

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

Logger

from sqlmodel import Session, SQLModel, create_engine
from sqlmodel.pool import StaticPool

DATABASE_URL = "sqlite:///:memory:"
Copy link
Contributor

Choose a reason for hiding this comment

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

Read from ENVVAR w/ Defaults

@Helithumper
Copy link
Contributor

Did a quick sweep with some notes, Overall a good first step but needs some polish 🥳

@@ -22,7 +22,7 @@ RUN mv bws /usr/local/bin
RUN rm -r /tmp/

# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt

# Copy the application code to the container
COPY . .
Copy link
Contributor

Choose a reason for hiding this comment

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

Include a .Dockerignore for files that don't need to be in the container

Copy link
Member Author

Choose a reason for hiding this comment

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

#95

WORKDIR /app

# Copy the requirements file to the container
COPY requirements.txt .
Copy link
Contributor

Choose a reason for hiding this comment

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

Move this further down, any changes to requirements will cause the apt execution to restart

Copy link
Member Author

Choose a reason for hiding this comment

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

#95

@@ -8,6 +8,7 @@ services:
- 8000:8000
volumes:
- ./config/options.yml:/app/config/options.yml
- ./database/:/app/database
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: usually this is called ./data

Copy link
Member Author

Choose a reason for hiding this comment

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

#95

@jontyms
Copy link
Member Author

jontyms commented May 16, 2024

We'll need some sort of migration system for at least up-migrations on the SQL DB

https://testdriven.io/blog/fastapi-sqlmodel/

most likely we will use Alembic

@Helithumper
Copy link
Contributor

Alembic's fine, that works

app/util/forms.py Dismissed Show dismissed Hide dismissed
app/util/forms.py Dismissed Show dismissed Hide dismissed
@jontyms jontyms marked this pull request as ready for review May 27, 2024 17:28
@jontyms jontyms changed the title WIP - Switch to SQLModel Database stuff Switch to SQLModel Database stuff May 27, 2024
@jontyms jontyms merged commit 7522882 into HackUCF:main May 27, 2024
7 checks passed
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.

3 participants