This was not a day of visible product-building. It was a day of fixing the foundation the product sits on โ the kind of work that produces nothing to screenshot but everything to trust. We set out to run UAT and build role-specific kits. Before that could be safe, the schema underneath had to be made sound. It now is.
The schema had accumulated 4โ5 competing representations of the same concepts (roles, memberships, pairings, invites) because each new persona got a parallel copy of an existing feature instead of generalizing it. This session consolidated that sprawl:
Established one canonical owner for each concept and wrote it into AGENTS.md, which Lovable now reads on every request as a guardrail against the next duplicate.
-
AGENTS.md โ Schema OwnershipThe schema principles (one owner per concept, identifier rules, authorization rules). Lovable reads it on every request to avoid duplicating patterns. Lives in the repo, carries the reasoning independent of any chat.
-
docs/SCHEMA_MIGRATION_LOG.md โ Full Phase RecordThe complete phase-by-phase record, including reversals and why they happened. Not in chat history โ it's in the repo where the team can find it anytime.
-
1. Entitlement scattered across four tables (Enterprise Launch Blocker)
organizations.subscription_tier,profiles.tier,testhive_leads.tier,subscriptions. Same duplication pattern as roles. An SSO user currently lands on the free tier regardless of their company's contract, with no reconciliation. Highest-value next target for the architecture team. -
2. Tier has no database-level enforcementEvery free/paid check is in application code; a direct API call bypasses all of them. Revenue leak + an isolation claim enterprise buyers ask about.
-
3. Aggregation functions leak across tenant boundaries
refresh_call_activity_dailyaggregates without an org boundary โ cross-tenant leak. Harmless only because nothing invokes it. Must not be scheduled until fixed. -
4. Two snapshot tables permanently emptyUI shows zeros as truth. Audit needed.
-
5. Leader pages disagree on the same metricTwo pages reading different tables for the same handoff number.
-
6. ae_id is null on all 76 accountsProduct gap, not a bug. The AE-filter path has never been exercised.
-
7. Dead-feature audit pending130 tables with zero rows + zero code references. Worth running to identify what else can be removed.
Every meaningful find came from reading the actual schema rather than trusting the summary of it. The platform-vs-org role distinction, the empty security check, the two-different-ID-columns bug, the demo contamination โ none were visible from the outside. The repeatable discipline that surfaced everything:
- Read-only diagnostic first
- Guard clauses that abort on unexpected data
- Verification queries with expected values
- Never cascade โ always explicit
- Dual-write before cutover
- Keep a rollback script
-
Consolidate entitlements (Priority #1)Replace the four-table scatter with one canonical subscription_tier. This is the launch blocker โ no enterprise deal closes with SSO landing everyone on free tier.
-
Push tier to database layerMove all free/paid checks out of application code into column-level constraints. RLS policies enforce tier rules at query time.
-
Fix cross-tenant aggregationAdd org_id filter to all aggregation functions before any are scheduled. Non-negotiable for multi-tenant.
-
UAT feedback loopRun SDR team through the product with real data. Catch usability issues that schema fixes don't surface.
The schema is now clean. Demo data is no longer invisible. Security holes are closed. The 1:1 and MBO system is verified to work. This clears the way for:
-
UAT with real SDR cohort (This Week)Team tests with clean data. Catch usability issues. Gather feedback on workflows.
-
Role-specific kit build (Next)SDR kit, AE kit, Leader kit. Each optimized for their workflow, no dead features.
-
Entitlement consolidation (Parallel Track)Four tables โ one, pushed to database layer. Unblocks enterprise SSO flow.
-
DCN pilot onboarding (With Mike's Contact)Once assigned, onboard Mike's dedicated person with clean schema, verified product, and clear MBO inspection trails.