-
Notifications
You must be signed in to change notification settings - Fork 7
Adding Timestamps #160
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
Adding Timestamps #160
Conversation
Code Review Completed! 🔥The code review was successfully completed based on your current configurations. Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
| document.updated_at = now() | ||
|
|
||
| self.session.add(document) | ||
| self.session.commit() | ||
| self.session.refresh(document) |
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.
try:
document.updated_at = now()
self.session.add(document)
self.session.commit()
self.session.refresh(document)
except Exception as e:
self.session.rollback()
raise eDatabase operations lack error handling, which can lead to crashes and unhandled exceptions.
This issue appears in multiple locations:
- backend/app/crud/document.py: Lines 57-61
- backend/app/crud/project.py: Lines 14-16
- backend/app/crud/project_user.py: Lines 68-68
Please add try-catch blocks around database operations to handle potential errors gracefully and provide meaningful error messages.
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.
| sa.Column("id", sa.Integer(), nullable=False), | ||
| sa.Column("inserted_at", sa.DateTime(), nullable=False), | ||
| sa.Column("updated_at", sa.DateTime(), nullable=False), | ||
| sa.Column("deleted_at", sa.DateTime(), nullable=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.
sa.Column('deleted_at', sa.DateTime(), nullable=True, index=True)Timestamp columns like deleted_at and inserted_at lack indexes, which can degrade query performance.
This issue appears in multiple locations:
- backend/app/alembic/versions/99f4fc325617_add_organization_project_setup.py: Lines 29-29
- backend/app/models/project.py: Lines 28-29
Please add indexes to timestamp columns to improve query performance.
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.
backend/app/alembic/versions/99f4fc325617_add_organization_project_setup.py
Outdated
Show resolved
Hide resolved
| sa.Column("project_id", sa.Integer(), nullable=False), | ||
| sa.Column("user_id", sa.Uuid(), nullable=False), | ||
| sa.Column("is_admin", sa.Boolean(), nullable=False), | ||
| sa.Column("id", sa.Integer(), nullable=False), | ||
| sa.Column("created_at", sa.DateTime(), nullable=False), | ||
| sa.Column("updated_at", sa.DateTime(), nullable=False), | ||
| sa.Column("is_deleted", sa.Boolean(), nullable=False), | ||
| sa.Column("inserted_at", sa.DateTime(), nullable=False), | ||
| sa.Column("updated_at", sa.DateTime(), nullable=False), | ||
| sa.Column("deleted_at", sa.DateTime(), nullable=True), | ||
| sa.ForeignKeyConstraint(["project_id"], ["project.id"], ondelete="CASCADE"), | ||
| sa.ForeignKeyConstraint(["user_id"], ["user.id"], ondelete="CASCADE"), |
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.
sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('project_id', 'user_id', name='uq_project_user')Consider adding a unique constraint on project_id and user_id in projectuser table to prevent duplicate associations
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.
backend/app/crud/project.py
Outdated
| project_data = project_in.model_dump(exclude_unset=True) | ||
| for key, value in project_data.items(): | ||
| setattr(project, key, value) |
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.
project_data = project_in.model_dump(exclude_unset=True)
valid_fields = {f.name for f in Project.__fields__.values()}
for key, value in project_data.items():
if key not in valid_fields:
raise ValueError(f"Invalid field '{key}' for project update")
setattr(project, key, value)Consider adding validation for project_in data before updating to prevent invalid updates.
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.
backend/app/models/organization.py
Outdated
| class Organization(OrganizationBase, table=True): | ||
| id: int = Field(default=None, primary_key=True) | ||
| inserted_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) | ||
| updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) |
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 field requires an update mechanism, e.g., using SQLAlchemy events:
# from sqlalchemy import event
#
# @event.listens_for(Organization, 'before_update')
# def receive_before_update(mapper, connection, target):
# target.updated_at = datetime.utcnow() # Or datetime.now(timezone.utc)
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)The updated_at field is defined with default_factory=datetime.utcnow, which sets the timestamp only upon creation. To ensure this field accurately reflects the last modification time, an automatic update mechanism is needed. This typically involves using ORM event listeners (e.g., SQLAlchemy's before_update event) or database-level triggers. Without such a mechanism, the updated_at field will remain static after the initial insertion.
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.
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 error can be avoided (I think) by using the now function. See *_at factory methods in backend/app/models/document.py
Kody Review CompleteGreat news! 🎉 Keep up the excellent work! 🚀 Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
Kody Review CompleteGreat news! 🎉 Keep up the excellent work! 🚀 Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
Kody Review CompleteGreat news! 🎉 Keep up the excellent work! 🚀 Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
Kody Review CompleteGreat news! 🎉 Keep up the excellent work! 🚀 Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
Kody Review CompleteGreat news! 🎉 Keep up the excellent work! 🚀 Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
backend/app/models/organization.py
Outdated
| class Organization(OrganizationBase, table=True): | ||
| id: int = Field(default=None, primary_key=True) | ||
| inserted_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) | ||
| updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) |
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 error can be avoided (I think) by using the now function. See *_at factory methods in backend/app/models/document.py
Summary
Target issue is #161
Explain the motivation for making this change. What existing problem does the pull request solve?
Checklist
Before submitting a pull request, please ensure that you mark these task.
fastapi run --reload app/main.pyordocker compose upin the repository root and test.Notes
This pull request introduces several enhancements to the
ai-platformrepository, primarily focusing on adding and updating timestamp fields across various database tables and models to improve data tracking and auditability. Key changes include:Database Schema Updates:
created_atwithinserted_atandupdated_atcolumns in thedocumenttable.CRUD Operations Enhancements:
Model and API Response Updates:
inserted_at,updated_at) and soft-delete fields (is_deleted,deleted_at) to theOrganizationandProjectmodels.OrganizationPublicandProjectPublicschemas to include new timestamp fields for API responses.Miscellaneous Improvements:
created_attoinserted_atin theproject_usermodel.Overall, these changes enhance the platform's data tracking capabilities and improve the separation of concerns in the codebase. However, some areas, such as error handling and security considerations for sensitive data, could benefit from further attention.