-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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"), |
There was a problem hiding this comment.
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"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handling of KeyError
?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
app/routes/api.py
Outdated
router = APIRouter(prefix="/api", tags=["API"], responses=Errors.basic_http()) | ||
|
||
|
||
""" |
There was a problem hiding this comment.
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
app/routes/api.py
Outdated
|
||
# 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()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logger
app/util/database.py
Outdated
from sqlmodel import Session, SQLModel, create_engine | ||
from sqlmodel.pool import StaticPool | ||
|
||
DATABASE_URL = "sqlite:///:memory:" |
There was a problem hiding this comment.
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
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 . . |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WORKDIR /app | ||
|
||
# Copy the requirements file to the container | ||
COPY requirements.txt . |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docker-compose.yml
Outdated
@@ -8,6 +8,7 @@ services: | |||
- 8000:8000 | |||
volumes: | |||
- ./config/options.yml:/app/config/options.yml | |||
- ./database/:/app/database |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://testdriven.io/blog/fastapi-sqlmodel/ most likely we will use Alembic |
Alembic's fine, that works |
TODO: