Skip to content
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

When time to live of redis expires, we need to save this data in a historical table #6

Open
nang-dev opened this issue Aug 27, 2024 · 1 comment

Comments

@nang-dev
Copy link

@nang-dev thoughts on this addition to the schema

-- Ensure the table does not already exist to avoid errors

DROP TABLE IF EXISTS historical_quotas;



-- Create the table for storing historical quotas

CREATE TABLE IF NOT EXISTS historical_quotas (

    id SERIAL PRIMARY KEY,

    user_id UUID NOT NULL,

    quota_type VARCHAR(50) NOT NULL,

    usage_value INTEGER NOT NULL CHECK (usage_value >= 0),

    recorded_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES profiles (id) ON DELETE CASCADE

);



-- Create an index to optimize querying by user_id/recorded_at/type

CREATE INDEX IF NOT EXISTS idx_historical_quotas_recorded_at

ON historical_quotas (recorded_at);



CREATE INDEX IF NOT EXISTS idx_historical_quotas_user_id

ON historical_quotas (user_id);



CREATE INDEX IF NOT EXISTS idx_historical_quotas_quota_type

ON historical_quotas (quota_type);



-- Enable row-level security on the historical_quotas table

ALTER TABLE historical_quotas ENABLE ROW LEVEL SECURITY;



-- Policy to allow users to read their own historical quota data

CREATE POLICY "User can read their own historical quota data."

ON historical_quotas FOR SELECT

USING ( (SELECT auth.uid()) = user_id );

Originally posted by @ItWasEnder in #88

@nang-dev
Copy link
Author

nang-dev commented Sep 2, 2024

oh also if requests from redis become < curr requests in supabase that means we should have another row (quota got reset)

@nang-dev nang-dev changed the title Add historical table When time to live of redis expires, we need to save this data in a historical table Sep 13, 2024
@Fryingpannn Fryingpannn transferred this issue from another repository Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant