Skip to content

Fix tournament and match timestamps defaulting to Unix epoch - #22

Merged
Horus0305 merged 3 commits into
mainfrom
copilot/fix-tournament-dates
Jun 3, 2026
Merged

Horus0305 merged 3 commits into
mainfrom
copilot/fix-tournament-dates

Conversation

Copilot AI commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Tournaments and matches were being stored with 01/01/1970 timestamps when timestamp data was missing or unparseable, due to NULL values cascading to database epoch defaults.

Changes

Explicit timestamp initialization

  • backend/realtime/tournament.py: Set timestamp=datetime.utcnow() when creating TournamentHistory records (previously relied on unreliable server_default)
  • backend/realtime/match/match_persistence.py: Set timestamp=datetime.utcnow() when creating MatchHistory records

Import robustness

  • restore_data.py: Added _dt_with_fallback() helper that returns current UTC time instead of None when timestamp parsing fails
  • Applies to both match and tournament history imports

Data migration

  • fix_tournament_timestamps.py: New script to retroactively fix existing records with NULL or epoch timestamps
    • Targets specific problematic tournament IDs (c6e5, 64c5, 8247, 369c, 1738, 088b)
    • Also catches any other tournament/match records with invalid timestamps

Documentation

  • TOURNAMENT_TIMESTAMP_FIX.md: Comprehensive guide for applying the migration and understanding the fix

Example

# Before: timestamp=None → stored as 1970-01-01
history = TournamentHistory(
    tournament_id=room.tournament_id,
    room_code=room.code,
    # timestamp omitted, defaults to NULL → epoch
)

# After: explicit UTC timestamp
history = TournamentHistory(
    tournament_id=room.tournament_id,
    room_code=room.code,
    timestamp=datetime.utcnow(),  # Always a valid time
)

To fix existing records in the database, run: python fix_tournament_timestamps.py

@vercel

vercel Bot commented Jun 3, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
very-important-study-material Ready Ready Preview, Comment Jun 3, 2026 10:05am

@Horus0305
Horus0305 marked this pull request as ready for review June 3, 2026 10:05
Copilot AI review requested due to automatic review settings June 3, 2026 10:05
@Horus0305
Horus0305 merged commit 648ef2a into main Jun 3, 2026
2 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR prevents tournament and match history timestamps from defaulting to Unix epoch (1970-01-01) by explicitly setting UTC timestamps at write-time, adding safer timestamp parsing during data restore, and introducing a one-off migration script + documentation to repair existing bad rows.

Changes:

  • Explicitly sets timestamp=datetime.utcnow() when persisting new TournamentHistory and MatchHistory records.
  • Adds restore_data.py::_dt_with_fallback() to ensure imports never write None timestamps when parsing fails.
  • Adds fix_tournament_timestamps.py plus TOURNAMENT_TIMESTAMP_FIX.md to retroactively repair NULL/epoch timestamps.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
TOURNAMENT_TIMESTAMP_FIX.md Documents the root cause, remediation steps, and verification commands for fixing timestamps.
CricketGame/restore_data.py Adds timestamp parsing fallback to avoid NULL timestamps during data import.
CricketGame/fix_tournament_timestamps.py New migration script to repair NULL/epoch tournament timestamps (and targeted IDs).
CricketGame/backend/realtime/tournament.py Sets tournament history timestamps explicitly at persistence time.
CricketGame/backend/realtime/match/match_persistence.py Sets match history timestamps explicitly at persistence time.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +51 to +52
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
Session = sessionmaker(bind=engine)
Comment on lines +95 to +109
for tid in PROBLEMATIC_IDS:
tournament = db.query(TournamentHistory).filter(
TournamentHistory.tournament_id == tid
).first()
if tournament:
old_ts = tournament.timestamp
tournament.timestamp = datetime.utcnow()
db.commit()
print(f" ✓ {tid}: Updated from {old_ts} to {tournament.timestamp}")
fixed_count += 1
else:
print(f" - {tid}: Not found in database")

print(f" Fixed {fixed_count} specific tournament IDs")
print()
Comment on lines +114 to +127
# Find NULL timestamps
null_count = 0
null_tournaments = db.query(TournamentHistory).filter(
TournamentHistory.timestamp == None
).all()

for tournament in null_tournaments:
tournament.timestamp = datetime.utcnow()
db.commit()
print(f" ✓ {tournament.tournament_id}: Updated from NULL to {tournament.timestamp}")
null_count += 1

print(f" Fixed {null_count} tournaments with NULL timestamps")
print()
Comment on lines +129 to +143
# Find epoch (01/01/1970) timestamps
epoch_count = 0
all_tournaments = db.query(TournamentHistory).all()

for tournament in all_tournaments:
if tournament.timestamp and tournament.timestamp.year == 1970 and \
tournament.timestamp.month == 1 and tournament.timestamp.day == 1:
old_ts = tournament.timestamp
tournament.timestamp = datetime.utcnow()
db.commit()
print(f" ✓ {tournament.tournament_id}: Updated from {old_ts} to {tournament.timestamp}")
epoch_count += 1

print(f" Fixed {epoch_count} tournaments with epoch (01/01/1970) timestamps")
print()
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