Conversation
22f0243 to
579a3aa
Compare
this is a first step toward saving other user-related data in the database (e.g. keys)
579a3aa to
ed76801
Compare
There was a problem hiding this comment.
Pull request overview
Adds a new user table and backfills it from existing interval data, then rebuilds the interval table to add a foreign-key relationship as a foundation for storing additional user-related data (e.g., keys) in the DB.
Changes:
- Introduce a
usertable keyed byuser_idand seed it from distinctinterval.user_idvalues. - Recreate
intervalas a new table with aFOREIGN KEY (user_id) REFERENCES user(user_id) ON DELETE CASCADE, then swap it in. - Provide a down migration that rebuilds
intervalwithout the FK and drops theusertable.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| storage/migrations/20260417200302_add_user_table.up.sql | Creates and seeds user, rebuilds interval to add FK + cascade semantics. |
| storage/migrations/20260417200302_add_user_table.down.sql | Restores interval schema without FK and removes the user table. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| CREATE TABLE user ( | ||
| user_id integer PRIMARY KEY AUTOINCREMENT | ||
| ); |
There was a problem hiding this comment.
Other migrations in this repo use IF NOT EXISTS / IF EXISTS for CREATE/DROP. To keep migrations resilient to partial runs and consistent with existing style, consider using CREATE TABLE IF NOT EXISTS user and DROP TABLE IF EXISTS ... (including temp tables like new_interval/old_interval if you keep them).
|
|
||
| ALTER TABLE old_interval RENAME TO interval; | ||
|
|
||
| DROP TABLE user; |
There was a problem hiding this comment.
For consistency with existing migrations (e.g., initialize.down.sql uses DROP TABLE IF EXISTS), consider using DROP TABLE IF EXISTS user here. This also makes rollback safer if the table was already removed or the migration was partially applied.
| DROP TABLE user; | |
| DROP TABLE IF EXISTS user; |
| @@ -0,0 +1,25 @@ | |||
| CREATE TABLE user ( | |||
| user_id integer PRIMARY KEY AUTOINCREMENT | |||
There was a problem hiding this comment.
AUTOINCREMENT is usually unnecessary in SQLite and can have downsides (forces monotonically increasing ids and prevents reuse, can increase sqlite_sequence churn). If you don’t need those semantics, prefer INTEGER PRIMARY KEY without AUTOINCREMENT for the user_id column.
| user_id integer PRIMARY KEY AUTOINCREMENT | |
| user_id integer PRIMARY KEY |
| INSERT INTO new_interval (user_id, start_time, end_time, tags, annotation) | ||
| SELECT user_id, start_time, end_time, tags, annotation FROM interval; | ||
|
|
||
| DROP TABLE interval; | ||
|
|
||
| ALTER TABLE new_interval RENAME TO interval; |
There was a problem hiding this comment.
This migration is multi-step (copy data, drop table, rename). Consider wrapping the whole sequence in a single transaction so a failure can’t leave the DB in a partially migrated state (e.g., interval dropped but new_interval not renamed).
| PRIMARY KEY (user_id, start_time, end_time, tags, annotation), | ||
| FOREIGN KEY (user_id) REFERENCES user (user_id) ON DELETE CASCADE | ||
| ); |
There was a problem hiding this comment.
SQLite only enforces FOREIGN KEY constraints (and runs ON DELETE CASCADE) when foreign key enforcement is enabled on the connection. I couldn’t find any code enabling PRAGMA foreign_keys = ON / DSN _foreign_keys=on, so this constraint may be a no-op at runtime.
this is a first step toward saving other user-related data in the database (e.g. keys)