Skip to content

Prevent setting invalid MySQL TIMESTAMPs for assessment dates #2283

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

NicholasMy
Copy link
Contributor

Description

This update ensures that the "start at," "due at," and "end at," assessment dates are valid for the MySQL TIMESTAMP type.

Motivation and Context

Instructors at UB ran into a bug where they set a due date several years into the future (after 2038) and they weren't able to load the course page afterwards. The invalid date got stored in the database as 0000-00-00 00:00:00.

I did some further research into why this could happen. Modern versions of MySQL have "strict mode" enabled by default, which wouldn't allow an invalid date to be inserted, but strict mode is disabled in database.yml with sql_mode: NO_ENGINE_SUBSTITUTION. It could be enabled by setting that field to NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO, but I tested that and wasn't able to load the course page. It appears we're currently depending on a lack of validation for functionality.

I think it would be a good idea to move towards complying with strict mode in the long term, but it may take major refactoring, so this is a reasonable fix in the meantime.

How Has This Been Tested?

Before making this update, I created an assessment with a due date in the year 2039. I was unable to load the course page afterwards.

image

I had to edit the record in the database to a real value in order to view the course page again.

image

image

After applying this update, trying to set an invalid date too far into the future gives the user a warning instead of locking people out of the course:

image

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have run rubocop and erblint for style check. If you haven't, run overcommit --install && overcommit --sign to use pre-commit hook for linting
  • My change requires a change to the documentation, which is located at Autolab Docs
  • I have updated the documentation accordingly, included in this PR

Copy link
Contributor

coderabbitai bot commented Mar 27, 2025

📝 Walkthrough

Walkthrough

The change enhances the Assessment model by introducing a new validation method, verify_dates_valid_for_mysql, which ensures that the date attributes (start_at, due_at, and end_at) are within the valid MySQL TIMESTAMP range (January 1, 1970 to January 19, 2038). If a date attribute is out of range, an error is added to that attribute. This update complements the existing verify_dates_order validation without altering its behavior.

Changes

File(s) Summary
app/models/assessment.rb Added verify_dates_valid_for_mysql to validate that start_at, due_at, and end_at fall within MySQL TIMESTAMP valid range.

Sequence Diagram(s)

sequenceDiagram
  participant Assessment as Assessment Model
  participant Validator as verify_dates_valid_for_mysql
  participant DBRange as MySQL Timestamp Range

  Assessment->>Validator: Trigger validation for start_at, due_at, end_at
  Validator->>DBRange: Validate each date against [1970, 2038]
  DBRange-->>Validator: Return valid/invalid result
  Validator->>Assessment: Append error message on invalid dates
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
app/models/assessment.rb (2)

680-693: Good implementation, but consider guard clauses for cleaner code

The validation logic correctly handles the MySQL TIMESTAMP range constraints. The use of Time.at() with the Unix timestamp values is a precise way to define the boundaries.

However, as noted in the pipeline failure, you could use guard clauses to make the code more concise and address the Rubocop warning.

Here's a refactored version using guard clauses:

  def verify_dates_valid_for_mysql
    # MySQL TIMESTAMP range is 1970-01-01 00:00:01 to 2038-01-19 03:14:07
    min_time = Time.at(1).utc # 1970-01-01 00:00:01 UTC
    max_time = Time.at(2**31 - 1).utc # 2038-01-19 03:14:07 UTC
-   if start_at < min_time || start_at > max_time
-     errors.add :start_at, "must be between 1970-01-01 and 2038-01-19"
-   end
-   if due_at < min_time || due_at > max_time
-     errors.add :due_at, "must be between 1970-01-01 and 2038-01-19"
-   end
-   if end_at < min_time || end_at > max_time
-     errors.add :end_at, "must be between 1970-01-01 and 2038-01-19"
-   end
+   errors.add :start_at, "must be between 1970-01-01 and 2038-01-19" if start_at < min_time || start_at > max_time
+   errors.add :due_at, "must be between 1970-01-01 and 2038-01-19" if due_at < min_time || due_at > max_time
+   errors.add :end_at, "must be between 1970-01-01 and 2038-01-19" if end_at < min_time || end_at > max_time
  end

This addresses the Rubocop warning while maintaining the same functionality.

🧰 Tools
🪛 GitHub Actions: Ruby on Rails CI

[warning] 690-690: Rubocop: Use a guard clause (return unless end_at < min_time || end_at > max_time) instead of wrapping the code inside a conditional expression.


680-693: Consider adding a helper method to reduce duplication

The error messages and validation logic are repeated for each date attribute, which introduces some duplication. Consider extracting this into a helper method to make the code more DRY.

Here's an implementation with a helper method:

  def verify_dates_valid_for_mysql
    # MySQL TIMESTAMP range is 1970-01-01 00:00:01 to 2038-01-19 03:14:07
    min_time = Time.at(1).utc # 1970-01-01 00:00:01 UTC
    max_time = Time.at(2**31 - 1).utc # 2038-01-19 03:14:07 UTC
-   if start_at < min_time || start_at > max_time
-     errors.add :start_at, "must be between 1970-01-01 and 2038-01-19"
-   end
-   if due_at < min_time || due_at > max_time
-     errors.add :due_at, "must be between 1970-01-01 and 2038-01-19"
-   end
-   if end_at < min_time || end_at > max_time
-     errors.add :end_at, "must be between 1970-01-01 and 2038-01-19"
-   end
+   validate_timestamp_range(:start_at, start_at, min_time, max_time)
+   validate_timestamp_range(:due_at, due_at, min_time, max_time)
+   validate_timestamp_range(:end_at, end_at, min_time, max_time)
  end
+
+  def validate_timestamp_range(attribute, value, min_time, max_time)
+    errors.add attribute, "must be between 1970-01-01 and 2038-01-19" if value < min_time || value > max_time
+  end

This makes the validation method cleaner and more maintainable.

🧰 Tools
🪛 GitHub Actions: Ruby on Rails CI

[warning] 690-690: Rubocop: Use a guard clause (return unless end_at < min_time || end_at > max_time) instead of wrapping the code inside a conditional expression.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 398b5c5 and 8930bbe.

📒 Files selected for processing (1)
  • app/models/assessment.rb (2 hunks)
🧰 Additional context used
🪛 GitHub Actions: Ruby on Rails CI
app/models/assessment.rb

[warning] 690-690: Rubocop: Use a guard clause (return unless end_at < min_time || end_at > max_time) instead of wrapping the code inside a conditional expression.

🔇 Additional comments (1)
app/models/assessment.rb (1)

27-27: LGTM: Good addition of MySQL TIMESTAMP validation

The addition of this validation is a solid preventive measure that will help avoid database issues when dates exceed the MySQL TIMESTAMP limits.

@KesterTan KesterTan requested review from a team and dwang3851 and removed request for a team April 21, 2025 20:06
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.

2 participants