Skip to content

Conversation

@icecrasher321
Copy link
Collaborator

Summary

Just add missing user stats rows via migration

Type of Change

  • Bug fix

Testing

Manually by running migration

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Sep 22, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
sim Ready Ready Preview Comment Sep 22, 2025 5:47pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Sep 22, 2025 5:47pm

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Summary

This PR adds a database migration (0091_backfill_user_stats.sql) that addresses a data integrity issue by creating missing user_stats records for existing users. The migration uses an INSERT SELECT statement with a LEFT JOIN to identify users who don't have corresponding entries in the user_stats table and creates them with default values.

The migration reveals that the user_stats table schema has evolved significantly since its original creation in migration 0021. While the original table had basic columns like total_manual_executions, total_api_calls, and total_cost, this backfill migration shows the table now includes many additional fields for copilot tracking (total_copilot_cost, total_copilot_tokens, total_copilot_calls), billing periods (current_period_cost, last_period_cost), usage limits (current_usage_limit, usage_limit_updated_at), and administrative controls (billing_blocked).

The migration addresses a common scenario where user creation and stats initialization might not happen atomically, leaving some users without proper tracking records. This is particularly important for billing and usage monitoring systems where every user needs comprehensive statistics tracking. The migration ensures all existing users get the same baseline stats configuration, setting most counters to 0, costs to '0' (decimal), timestamps to NOW(), and boolean flags to FALSE.

Confidence score: 4/5

  • This migration is safe to run with low risk of causing immediate issues
  • Score reflects well-structured SQL with proper NULL checking, though some design choices could be cleaner
  • Pay attention to the mixed data type handling and potential confusion from using user.id for both id and user_id fields

1 file reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

Comment on lines +35 to +38
'0'::decimal AS total_cost,
'0'::decimal AS current_period_cost,
'0'::decimal AS last_period_cost,
'0'::decimal AS total_copilot_cost,
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Inconsistent decimal casting - some use '0'::decimal while current_usage_limit uses NULL::decimal. Consider using consistent casting for all decimal fields.

@icecrasher321 icecrasher321 merged commit 16f5819 into staging Sep 22, 2025
6 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/user-stats-init branch October 7, 2025 23:22
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