mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
refactor(db): move rate-limit extras into Prisma migration and remove db-extras script
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
-- Rate limiting extras migration
|
||||
-- Keeps compatibility with existing environments created via prisma db push.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS rate_limits (
|
||||
id SERIAL PRIMARY KEY,
|
||||
key VARCHAR(255) NOT NULL,
|
||||
action VARCHAR(50) NOT NULL,
|
||||
count INTEGER NOT NULL DEFAULT 1,
|
||||
window_start TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM pg_constraint
|
||||
WHERE conname = 'rate_limits_key_action_key'
|
||||
) THEN
|
||||
ALTER TABLE rate_limits
|
||||
ADD CONSTRAINT rate_limits_key_action_key UNIQUE (key, action);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_rate_limits_key_action ON rate_limits(key, action);
|
||||
CREATE INDEX IF NOT EXISTS idx_rate_limits_window_start ON rate_limits(window_start);
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM pg_class
|
||||
WHERE relname = 'rate_limits'
|
||||
AND relkind = 'r'
|
||||
AND relpersistence <> 'u'
|
||||
) THEN
|
||||
ALTER TABLE rate_limits SET UNLOGGED;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE OR REPLACE FUNCTION cleanup_rate_limits() RETURNS void AS $$
|
||||
BEGIN
|
||||
DELETE FROM rate_limits WHERE window_start < NOW() - INTERVAL '1 hour';
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (e.g., Git)
|
||||
provider = "postgresql"
|
||||
@@ -1,31 +0,0 @@
|
||||
-- Rate Limiting Table (UNLOGGED for performance)
|
||||
-- Run this migration manually: psql $DATABASE_URL -f prisma/migrations/rate_limit.sql
|
||||
|
||||
-- Drop if exists (for re-running)
|
||||
DROP TABLE IF EXISTS rate_limits;
|
||||
|
||||
-- Create UNLOGGED table for rate limiting
|
||||
-- UNLOGGED = no WAL writes = faster, but data lost on crash (acceptable for rate limits)
|
||||
CREATE UNLOGGED TABLE rate_limits (
|
||||
id SERIAL PRIMARY KEY,
|
||||
key VARCHAR(255) NOT NULL, -- e.g., "register:192.168.1.1" or "login:[email protected]"
|
||||
action VARCHAR(50) NOT NULL, -- e.g., "register", "login", "api"
|
||||
count INTEGER NOT NULL DEFAULT 1,
|
||||
window_start TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
|
||||
-- Unique constraint for upsert operations
|
||||
UNIQUE(key, action)
|
||||
);
|
||||
|
||||
-- Index for fast lookups
|
||||
CREATE INDEX idx_rate_limits_key_action ON rate_limits(key, action);
|
||||
|
||||
-- Index for cleanup operations
|
||||
CREATE INDEX idx_rate_limits_window_start ON rate_limits(window_start);
|
||||
|
||||
-- Auto-cleanup function: removes expired entries
|
||||
CREATE OR REPLACE FUNCTION cleanup_rate_limits() RETURNS void AS $$
|
||||
BEGIN
|
||||
DELETE FROM rate_limits WHERE window_start < NOW() - INTERVAL '1 hour';
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
Reference in New Issue
Block a user