Skip to content

Add SQLite database to persist game session answers#32

Open
primalpop wants to merge 1 commit intomasterfrom
add-database-persistence
Open

Add SQLite database to persist game session answers#32
primalpop wants to merge 1 commit intomasterfrom
add-database-persistence

Conversation

@primalpop
Copy link
Copy Markdown
Contributor

@primalpop primalpop commented Mar 30, 2026

@TinaMcComiskey I have tested this but I suggest you do the same before approving this PR.

If everything works as expected, please redeploy the website.

Previously, all game data (scores, answers) was stored only in the
browser's sessionStorage and lost on page refresh. This commit adds
a persistent SQLite database layer so that every completed game session
is recorded server-side for future analysis and research.

## What was added

### Database (Flask-SQLAlchemy + Flask-Migrate)
- `app/models.py`: Two new SQLAlchemy models:
  - `GameSession`: records session UUID, game type, final score,
    total questions, duration in seconds, and completion timestamp
  - `QuestionAnswer`: records each question's text, the user's selected
    answer, and whether it was correct, linked to its parent session
- `migrations/`: Alembic migration folder with initial schema migration
- `mypal.db` is excluded from git via .gitignore

### Backend
- `app/__init__.py`: Initialized Flask-SQLAlchemy and Flask-Migrate
- `app/config.py`: Added SQLALCHEMY_DATABASE_URI (SQLite locally,
  reads DATABASE_URL env var for Heroku Postgres)
- `app/routes.py`: Added two POST endpoints:
  - POST /api/save-quiz — saves Spot the Sensitive Information sessions
  - POST /api/save-ci  — saves Safe to Share (CI) sessions
  Both endpoints include idempotency checks to prevent duplicate writes
- `requirements.txt`: Added Flask-SQLAlchemy==3.1.1 and Flask-Migrate==4.0.7

### Frontend (JS)
- `myPal-Bots-Quiz.js` and `ContextualIntegrity.js`:
  - Generate a UUID per game session using crypto.randomUUID()
  - Track each answer as the user plays (question text, selection, correctness)
  - Start a timer on the first answer interaction
  - POST the full session payload to the API when the results screen appears
  - Network failures are caught silently so the game always works
  - Restart clears all state and generates a fresh UUID

### Deployment
- `Procfile`: Added for Heroku — runs gunicorn as the web server and
  executes `flask db upgrade` as a release phase on every deploy

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant