fix(plugin): gate usage credits on active balance#2059
Merged
Conversation
Contributor
Contributor
Contributor
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@supabase/migrations/20260507082135_active_usage_credits_flag.sql`:
- Around line 44-85: The function sync_org_has_usage_credits_from_grants() is
never attached to the usage_credit_grants table; add a CREATE TRIGGER that binds
this function to public.usage_credit_grants so orgs.has_usage_credits is updated
automatically on changes—create a trigger (e.g.,
sync_org_has_usage_credits_from_grants_trigger) AFTER INSERT OR UPDATE OR DELETE
ON public.usage_credit_grants FOR EACH ROW that EXECUTE FUNCTION
public.sync_org_has_usage_credits_from_grants(); ensure the trigger name is
unique and uses the same function schema/name.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 5241dbec-5b30-4b9b-bdb8-44b93812cd68
📒 Files selected for processing (3)
supabase/functions/_backend/utils/pg.tssupabase/migrations/20260507082135_active_usage_credits_flag.sqltests/plugin-credits-flag.test.ts
|
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.



Summary (AI generated)
orgs.has_usage_creditsfrom positive, unexpired usage credit grants./updateswith expired, exhausted, and active credits.Motivation (AI generated)
has_usage_creditspreviously stayed true when any credit grant row existed, including consumed or expired grants. Plugin endpoints used that flag as a plan gate, allowing exhausted credit-backed orgs to keep passing billing checks.Business Impact (AI generated)
This prevents continued plugin service access after prepaid credits are depleted or expired, protecting usage-based revenue while preserving the read-replica hot path.
Test Plan (AI generated)
bun run lint:backendbunx eslint tests/plugin-credits-flag.test.tssqlfluff lint --dialect postgres supabase/migrations/20260507082135_active_usage_credits_flag.sqlbun run supabase:db:resetbun run supabase:with-env -- bunx vitest run tests/plugin-credits-flag.test.tsbun run supabase:with-env -- bunx vitest run tests/files-security.test.ts tests/security-definer-execute-hardening.test.tsbun run supabase:with-env -- bunx vitest run tests/organization-put-stripe-sync.unit.test.tsbun run typecheckGenerated with AI
Summary by CodeRabbit
Bug Fixes
Chores
Tests