Add SQLite database to persist game session answers#32
Open
Add SQLite database to persist game session answers#32
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@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.