Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
fix(db): add missing FK constraints on graph_edges + notes (#179, #180)#245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cf46140
chore(db): scaffold FK-integrity migration with audit references (#17…
Jose-Gael-Cruz-Lopez db92cf3
fix(db): delete orphan graph_edges before adding user_id FK (#179)
Jose-Gael-Cruz-Lopez f8a1451
fix(db): add graph_edges.user_id FK to users(id), idempotently guarde…
Jose-Gael-Cruz-Lopez f6b761b
fix(db): delete orphan notes before adding user/course FKs (#180)
Jose-Gael-Cruz-Lopez 5ea6a39
fix(db): add notes.user_id FK to users(id), idempotently guarded (#180)
Jose-Gael-Cruz-Lopez aef9ff0
fix(db): add notes.course_id FK to courses(id), idempotently guarded …
Jose-Gael-Cruz-Lopez 41c3d86
fix(db): add REFERENCES users(id) to graph_edges.user_id in schema (#…
Jose-Gael-Cruz-Lopez 3bfb2b5
fix(db): add REFERENCES to notes.user_id/course_id in schema (#180)
Jose-Gael-Cruz-Lopez 4c319d8
docs(db): clarify notes FK rationale — only the graph_node link stays…
Jose-Gael-Cruz-Lopez 75d3729
test(db): drift-guard FK-integrity migration constraints + orphan cle…
Jose-Gael-Cruz-Lopez 1ba451f
fix(db): add idx_graph_edges_user_id on FK referencing column (#179)
Jose-Gael-Cruz-Lopez cc1ec58
docs(db): document actual ON DELETE NO ACTION/RESTRICT semantics of t…
Jose-Gael-Cruz-Lopez 471d1b5
Merge remote-tracking branch 'origin/main' into fix/fk-integrity
Jose-Gael-Cruz-Lopez 1a20cd8
fix(db): move FK-integrity DDL into numbered migration 0020 (#179, #180)
Jose-Gael-Cruz-Lopez 4f8655e
test(db): point FK-integrity drift guard at the canonical migration f…
Jose-Gael-Cruz-Lopez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| -- Migration: foreign-key integrity for graph_edges + notes (#179, #180) | ||
| -- | ||
| -- Backfills, on already-migrated databases, the FK constraints that fresh | ||
| -- databases now get inline from 0001_baseline_schema.sql. graph_edges.user_id | ||
| -- and notes.user_id / notes.course_id historically shipped as bare TEXT columns | ||
| -- with no REFERENCES, inconsistent with every sibling learning table: | ||
| -- #179 graph_edges.user_id -> users(id) | ||
| -- #180 notes.user_id -> users(id) | ||
| -- #180 notes.course_id -> courses(id) | ||
| -- | ||
| -- migrate.py wraps each migration in a single transaction, so this is plain | ||
| -- (non-CONCURRENT) DDL. Each constraint is added behind a pg_constraint guard | ||
| -- because Postgres has no ADD CONSTRAINT IF NOT EXISTS, which also makes this | ||
| -- migration a no-op on fresh databases that already have the inline FKs from | ||
| -- the baseline. Pre-existing orphan rows are deleted first so the ALTER TABLE | ||
| -- can validate. | ||
| -- | ||
| -- ON DELETE semantics: these FKs have no ON DELETE clause, so they default to | ||
| -- NO ACTION (RESTRICT). A referenced users/courses row cannot be hard-deleted | ||
| -- while a graph_edges/notes row still points at it. This guarantees no orphans | ||
| -- but does NOT cascade-delete dependents. Today nothing hard-deletes | ||
| -- users/courses (delete_account is a soft delete; delete_course only removes | ||
| -- the user_courses enrollment row), so RESTRICT never actually fires. Switch | ||
| -- to ON DELETE CASCADE (and add a hard-delete path) if cleanup is ever wanted. | ||
| -- #179 graph_edges.user_id: remove edges whose user_id has no users row, then | ||
| -- add the FK other learning tables already enforce. | ||
| DELETE FROM graph_edges | ||
| WHERE user_id NOT IN (SELECT id FROM users); | ||
| DO $$ | ||
| BEGIN | ||
| IF NOT EXISTS ( | ||
| SELECT 1 FROM pg_constraint WHERE conname = 'graph_edges_user_id_fkey' | ||
| ) THEN | ||
| ALTER TABLE graph_edges | ||
| ADD CONSTRAINT graph_edges_user_id_fkey | ||
| FOREIGN KEY (user_id) REFERENCES users(id); | ||
| END IF; | ||
| END $$; | ||
| -- Index the FK referencing column: Postgres does not auto-index the | ||
| -- referencing side of a foreign key, and sibling tables index this path. | ||
| CREATE INDEX IF NOT EXISTS idx_graph_edges_user_id ON graph_edges(user_id); | ||
| -- #180 notes.user_id / notes.course_id: notes is core user data but both | ||
| -- columns are bare TEXT. Remove rows pointing at a non-existent user or course | ||
| -- (e.g. notes left dangling after a course delete) before adding the FKs. | ||
| DELETE FROM notes | ||
| WHERE user_id NOT IN (SELECT id FROM users) | ||
| OR course_id NOT IN (SELECT id FROM courses); | ||
| DO $$ | ||
| BEGIN | ||
| IF NOT EXISTS ( | ||
| SELECT 1 FROM pg_constraint WHERE conname = 'notes_user_id_fkey' | ||
| ) THEN | ||
| ALTER TABLE notes | ||
| ADD CONSTRAINT notes_user_id_fkey | ||
| FOREIGN KEY (user_id) REFERENCES users(id); | ||
| END IF; | ||
| END $$; | ||
| DO $$ | ||
| BEGIN | ||
| IF NOT EXISTS ( | ||
| SELECT 1 FROM pg_constraint WHERE conname = 'notes_course_id_fkey' | ||
| ) THEN | ||
| ALTER TABLE notes | ||
| ADD CONSTRAINT notes_course_id_fkey | ||
| FOREIGN KEY (course_id) REFERENCES courses(id); | ||
| END IF; | ||
| END $$; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """ | ||
| Drift guard for the FK-integrity migration (#179, #180). | ||
| No live Postgres in the unit suite, so we assert the textual invariants that | ||
| matter: the numbered migration adds each constraint behind the pg_constraint | ||
| guard (re-runnable) and cleans orphans first, and the canonical baseline schema | ||
| declares the same REFERENCES inline so a fresh database is born with the FKs. | ||
| After main restructured db/ into ordered migrations applied by migrate.py, the | ||
| FK DDL lives in migrations/0020_fk_integrity.sql (for already-migrated DBs) and | ||
| the inline REFERENCES live in migrations/0001_baseline_schema.sql (for fresh | ||
| DBs). The old flat db/supabase_schema.sql / migration_*.sql files were deleted. | ||
| """ | ||
| import os | ||
| _MIGRATIONS = os.path.join( | ||
| os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "db", "migrations" | ||
| ) | ||
| FK_MIGRATION = "0020_fk_integrity.sql" | ||
| BASELINE = "0001_baseline_schema.sql" | ||
| CONSTRAINTS = ( | ||
| "graph_edges_user_id_fkey", | ||
| "notes_user_id_fkey", | ||
| "notes_course_id_fkey", | ||
| ) | ||
| def _read(name: str) -> str: | ||
| with open(os.path.join(_MIGRATIONS, name), encoding="utf-8") as fh: | ||
| return fh.read() | ||
| def test_migration_adds_each_constraint_behind_a_guard(): | ||
| sql = _read(FK_MIGRATION) | ||
| for name in CONSTRAINTS: | ||
| assert name in sql, f"{name} missing from migration" | ||
| # Every ADD CONSTRAINT must be inside an IF NOT EXISTS pg_constraint guard. | ||
| assert sql.count("IF NOT EXISTS") >= len(CONSTRAINTS) | ||
| assert "pg_constraint" in sql | ||
| def test_migration_cleans_orphans_before_altering(): | ||
| sql = _read(FK_MIGRATION) | ||
| # Orphan deletes must precede the ALTER TABLE that validates the FK. | ||
| assert "DELETE FROM graph_edges" in sql | ||
| assert "DELETE FROM notes" in sql | ||
| assert sql.index("DELETE FROM graph_edges") < sql.index("graph_edges_user_id_fkey") | ||
| assert sql.index("DELETE FROM notes") < sql.index("notes_user_id_fkey") | ||
| def test_migration_indexes_the_referencing_column(): | ||
| # graph_edges.user_id needs an index on the FK referencing side (#179). | ||
| sql = _read(FK_MIGRATION) | ||
| assert "CREATE INDEX IF NOT EXISTS idx_graph_edges_user_id" in sql | ||
| def test_baseline_declares_inline_references(): | ||
| # Fresh databases must be born with the FKs, declared inline in the baseline. | ||
| sql = _read(BASELINE) | ||
| assert "user_id TEXT NOT NULL REFERENCES users(id)" in sql # graph_edges | ||
| assert "user_id TEXT NOT NULL REFERENCES users(id)" in sql # notes | ||
| assert "course_id TEXT NOT NULL REFERENCES courses(id)" in sql # notes |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify the rationale for not adding FK to
note_concepts.concept_node_id.The comment states the link is "intentionally NOT a hard FK, because graph_nodes uses TEXT ids managed by application code." However,
graph_edges.source_node_idandgraph_edges.target_node_id(lines 98-99) both have hard FKs tograph_nodes(id), so the stated reason is inconsistent.If the real reason is different (e.g., concepts can exist before graph nodes are created, or there's an application-level design consideration), please update the comment to explain the actual rationale.
🤖 Prompt for AI Agents