Skip to content

[#2] Supabase schema migration and client helper#45

Merged
realproject7 merged 2 commits intomainfrom
task/2-supabase-schema
Mar 13, 2026
Merged

[#2] Supabase schema migration and client helper#45
realproject7 merged 2 commits intomainfrom
task/2-supabase-schema

Conversation

@realproject7
Copy link
Copy Markdown
Owner

Summary

  • P0-2a: SQL migration (supabase/migrations/00001_schema.sql) with storylines, plots, and donations tables per §4.1
    • UNIQUE(tx_hash, log_index) deduplication on all three tables
    • Foreign keys from plots and donations to storylines
    • writer_type (smallint, default 0) for indexer-set agent detection
    • hidden (boolean, default false) on storylines and plots for MVP moderation (§8)
  • P0-2b: lib/supabase.ts with browser-side (anon key) and server-side (service role) clients, following the dropcast pattern
    • Full TypeScript Database interface with Row/Insert/Update types for all three tables
    • Convenience type aliases: Storyline, Plot, Donation

Fixes #2

Test plan

  • Verify SQL migration runs cleanly against a fresh Supabase project
  • Verify lib/supabase.ts type-checks (CI confirms this)
  • Confirm schema matches §4.1 requirements: deduplication, FKs, writer_type, hidden columns

🤖 Generated with Claude Code

P0-2a: SQL migration with storylines, plots, donations tables per §4.1.
Includes UNIQUE(tx_hash, log_index), foreign keys, writer_type, hidden columns.
P0-2b: lib/supabase.ts with browser (anon) and server (service role) clients.

Fixes #2

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@project7-interns project7-interns left a comment

Choose a reason for hiding this comment

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

T2b review: APPROVE

Schema review against issue #2 requirements:

✅ Three tables (storylines, plots, donations) — all present
UNIQUE(tx_hash, log_index) deduplication constraints on all three tables
✅ Foreign keys: plots.storyline_id → storylines(id), donations.storyline_id → storylines(id)
writer_type smallint not null default 0 on storylines — indexer-set, well-commented
hidden boolean not null default false on storylines and plots (not on donations) — matches §8
✅ Useful indexes on writer_address and storyline_id FKs
amount text for wei — avoids precision loss, correct choice

lib/supabase.ts:
✅ Browser client (anon key, respects RLS) with null fallback when env vars missing
✅ Server client (service role, bypasses RLS) with auth disabled — correct pattern
✅ Full Database interface with Row/Insert/Update types matching SQL exactly
✅ Convenience type aliases exported

Minor note (non-blocking): plots.storyline_id references the surrogate PK storylines.id, not the on-chain storylines.storyline_id. The naming overlap could confuse future contributors — consider renaming the FK column to storyline_fk or adding a code comment in a future PR. Not blocking since the relational logic is correct.

Clean, spec-compliant implementation.

Copy link
Copy Markdown
Collaborator

@project7-interns project7-interns left a comment

Choose a reason for hiding this comment

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

Verdict: REQUEST CHANGES

Summary

The PR is close, but the schema currently bakes in one silent data-classification bug and one relationship mismatch that will make the indexer harder to implement correctly.

Findings

  • [high] writer_type is supposed to be indexer-set, but the migration gives it default 0

    • File: supabase/migrations/00001_schema.sql:16
    • Suggestion: remove the default and require inserts to provide writer_type explicitly, so rows cannot silently land as human when the indexer forgets or fails to set it.
  • [high] Child storyline_id foreign keys point at the surrogate row id instead of the storyline identifier

    • File: supabase/migrations/00001_schema.sql:31
    • Suggestion: align the relationship so plots.storyline_id and donations.storyline_id reference the canonical storyline identifier defined in the schema/proposal, rather than storylines.id. As written, a chain event carrying storyline_id cannot be inserted directly without an extra lookup/translation step, and the column name becomes misleading.

Decision

Request changes. The current migration risks silently misclassifying writer type and creates a storyline relationship that does not match the event-facing identifier implied by the schema.

…ne_id

1. writer_type: nullable (NULL=unclassified, 0=human, 1=agent) instead of
   default 0 — prevents silent misclassification on insert.
2. FK targets: plots/donations reference storylines(storyline_id) directly
   instead of surrogate id — indexer can insert with on-chain ID without
   an extra lookup. Added UNIQUE constraint on storylines.storyline_id.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@project7-interns project7-interns left a comment

Choose a reason for hiding this comment

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

Verdict: APPROVE

Summary

The follow-up commit addresses the two schema correctness issues from the prior review. writer_type no longer silently defaults to human, and the child tables now reference the canonical on-chain storyline_id directly.

Findings

  • None.

Decision

Approve. The migration and Supabase helper now match the issue scope, and the PR checks are green.

Copy link
Copy Markdown
Collaborator

@project7-interns project7-interns left a comment

Choose a reason for hiding this comment

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

T2b re-review: APPROVE

Both fixes verified:

  1. writer_type → nullable smallint (no default). NULL = unclassified, 0 = human, 1 = agent. No more silent classification. TS types updated to number | null.
  2. ✅ FKs on plots/donations now reference storylines(storyline_id) directly, backed by new storylines_onchain_unique constraint. Indexer can insert with on-chain ID — no surrogate lookup needed. Resolves my earlier non-blocking note as well.

Clean fix, no regressions.

@realproject7 realproject7 merged commit d8b8c9a into main Mar 13, 2026
1 check passed
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.

[P0-2] Supabase Schema

2 participants