Combine Issue #28 (UUID Phase 2 - NOT NULL) into migration 000005
- Add NOT NULL constraints to soundtrack.uuid, song.uuid, and song.soundtrack_uuid - Add corresponding DROP NOT NULL in down migration - Update header comments to reflect combined Phase 1 & 2 This eliminates the need for a separate migration 000007 while maintaining proper rollback capability. The NOT NULL constraints are added AFTER all UUID backfills are complete, ensuring data integrity. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
1 parent
eda60e0c07
commit
e86c0f8e04
4 files changed
+396
No files matched your search
@@ -0,0 +1,64 @@
|
||||
-- Rollback: Remove UUID columns and revert rename
|
||||
|
||||
-- Step 1: Drop UUID-related indexes
|
||||
DROP INDEX IF EXISTS idx_song_uuid;
|
||||
DROP INDEX IF EXISTS idx_song_soundtrack_uuid;
|
||||
DROP INDEX IF EXISTS idx_soundtrack_uuid;
|
||||
DROP INDEX IF EXISTS idx_song_soundtrack_id;
|
||||
DROP INDEX IF EXISTS idx_song_path;
|
||||
|
||||
-- Step 1.5: Remove NOT NULL constraints before dropping columns (Issue #28 rollback)
|
||||
ALTER TABLE song ALTER COLUMN soundtrack_uuid DROP NOT NULL;
|
||||
ALTER TABLE song ALTER COLUMN uuid DROP NOT NULL;
|
||||
ALTER TABLE soundtrack ALTER COLUMN uuid DROP NOT NULL;
|
||||
|
||||
-- Step 2: Drop UUID-related constraints
|
||||
ALTER TABLE song DROP CONSTRAINT IF EXISTS song_soundtrack_path_unique;
|
||||
ALTER TABLE song DROP CONSTRAINT IF EXISTS song_soundtrack_uuid_fkey;
|
||||
ALTER TABLE soundtrack DROP CONSTRAINT IF EXISTS soundtrack_uuid_unique;
|
||||
|
||||
-- Step 3: Drop UUID columns from song table
|
||||
ALTER TABLE song DROP COLUMN IF EXISTS soundtrack_uuid;
|
||||
ALTER TABLE song DROP COLUMN IF EXISTS uuid;
|
||||
|
||||
-- Step 4: Drop UUID column from soundtrack table
|
||||
ALTER TABLE soundtrack DROP COLUMN IF EXISTS uuid;
|
||||
|
||||
-- Step 5: Drop the uuid-based primary key
|
||||
ALTER TABLE song DROP CONSTRAINT song_pkey;
|
||||
|
||||
-- Step 6: Restore composite primary key on song
|
||||
ALTER TABLE song ADD CONSTRAINT song_pkey PRIMARY KEY (soundtrack_id, path);
|
||||
|
||||
-- Step 7: Drop foreign key constraint
|
||||
ALTER TABLE song DROP CONSTRAINT IF EXISTS song_soundtrack_id_fkey;
|
||||
|
||||
-- Step 8: Revert rename operations
|
||||
-- Revert song_list table references
|
||||
ALTER TABLE song_list RENAME COLUMN soundtrack_name TO game_name;
|
||||
|
||||
-- Revert song table: rename soundtrack_id back to game_id
|
||||
ALTER TABLE song RENAME COLUMN soundtrack_id TO game_id;
|
||||
|
||||
-- Revert columns in game table
|
||||
ALTER TABLE game RENAME COLUMN soundtrack_name TO game_name;
|
||||
|
||||
-- Revert primary key sequence
|
||||
ALTER SEQUENCE game_id_seq RENAME TO soundtrack_id_seq;
|
||||
|
||||
-- Revert: Rename soundtrack table back to game
|
||||
ALTER TABLE soundtrack RENAME TO game;
|
||||
|
||||
-- Revert foreign key constraint
|
||||
ALTER TABLE song DROP CONSTRAINT IF EXISTS song_game_id_fkey;
|
||||
ALTER TABLE song ADD CONSTRAINT song_game_id_fkey
|
||||
FOREIGN KEY (game_id) REFERENCES game(id);
|
||||
|
||||
-- Revert indexes
|
||||
ALTER INDEX IF EXISTS idx_soundtrack_deleted RENAME TO idx_game_deleted;
|
||||
ALTER INDEX IF EXISTS idx_soundtrack_hash RENAME TO idx_game_hash;
|
||||
ALTER INDEX IF EXISTS idx_soundtrack_path RENAME TO idx_game_path;
|
||||
ALTER INDEX IF EXISTS idx_soundtrack_name RENAME TO idx_game_name;
|
||||
ALTER INDEX IF EXISTS idx_song_soundtrack_id RENAME TO idx_song_game_id;
|
||||
ALTER INDEX IF EXISTS idx_song_soundtrack_id_song_name RENAME TO idx_song_game_id_song_name;
|
||||
ALTER INDEX IF EXISTS song_list_soundtrack_name_idx RENAME TO song_list_game_name_idx;
|
||||
@@ -0,0 +1,99 @@
|
||||
-- Migration: Rename game to soundtrack AND add UUID columns
|
||||
-- Combines original migrations 000005 (rename), 000006 (UUID columns), and Issue #28 (Phase 2: NOT NULL constraints)
|
||||
-- Phase 1 & 2: Dual-write with required UUIDs (integer IDs still exist alongside UUIDs)
|
||||
|
||||
-- ============ PART 1: RENAME (from original migration 000005) ============
|
||||
|
||||
-- Rename game table to soundtrack
|
||||
ALTER TABLE game RENAME TO soundtrack;
|
||||
|
||||
-- Rename primary key sequence
|
||||
ALTER SEQUENCE game_id_seq RENAME TO soundtrack_id_seq;
|
||||
|
||||
-- Rename columns in soundtrack table
|
||||
ALTER TABLE soundtrack RENAME COLUMN game_name TO soundtrack_name;
|
||||
|
||||
-- Update song table: rename game_id to soundtrack_id
|
||||
ALTER TABLE song RENAME COLUMN game_id TO soundtrack_id;
|
||||
|
||||
-- Update song_list table references
|
||||
ALTER TABLE song_list RENAME COLUMN game_name TO soundtrack_name;
|
||||
|
||||
-- Rename foreign key constraint
|
||||
ALTER TABLE song DROP CONSTRAINT IF EXISTS song_game_id_fkey;
|
||||
ALTER TABLE song ADD CONSTRAINT song_soundtrack_id_fkey
|
||||
FOREIGN KEY (soundtrack_id) REFERENCES soundtrack(id);
|
||||
|
||||
-- Rename indexes
|
||||
ALTER INDEX IF EXISTS idx_game_deleted RENAME TO idx_soundtrack_deleted;
|
||||
ALTER INDEX IF EXISTS idx_game_hash RENAME TO idx_soundtrack_hash;
|
||||
ALTER INDEX IF EXISTS idx_game_path RENAME TO idx_soundtrack_path;
|
||||
ALTER INDEX IF EXISTS idx_game_name RENAME TO idx_soundtrack_name;
|
||||
ALTER INDEX IF EXISTS idx_song_game_id RENAME TO idx_song_soundtrack_id;
|
||||
ALTER INDEX IF EXISTS idx_song_game_id_song_name RENAME TO idx_song_soundtrack_id_song_name;
|
||||
ALTER INDEX IF EXISTS song_list_game_name_idx RENAME TO song_list_soundtrack_name_idx;
|
||||
|
||||
-- ============ PART 2: UUID COLUMNS (from original migration 000006) ============
|
||||
|
||||
-- Step 0: Enable uuid-ossp extension for UUID generation
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
-- Add UUID column to soundtrack table
|
||||
ALTER TABLE soundtrack ADD COLUMN uuid UUID DEFAULT gen_random_uuid();
|
||||
|
||||
-- Add unique constraint on soundtrack.uuid
|
||||
ALTER TABLE soundtrack ADD CONSTRAINT soundtrack_uuid_unique UNIQUE (uuid);
|
||||
|
||||
-- Backfill existing soundtracks with UUIDs (should already have values from DEFAULT)
|
||||
UPDATE soundtrack SET uuid = gen_random_uuid() WHERE uuid IS NULL;
|
||||
|
||||
-- Add index on soundtrack.uuid for query performance
|
||||
CREATE INDEX IF NOT EXISTS idx_soundtrack_uuid ON soundtrack(uuid);
|
||||
|
||||
-- Add UUID columns to song table
|
||||
ALTER TABLE song ADD COLUMN uuid UUID DEFAULT gen_random_uuid();
|
||||
ALTER TABLE song ADD COLUMN soundtrack_uuid UUID;
|
||||
|
||||
-- Backfill existing songs with UUIDs
|
||||
UPDATE song SET uuid = gen_random_uuid() WHERE uuid IS NULL;
|
||||
|
||||
-- Backfill song.soundtrack_uuid from soundtrack.uuid via JOIN
|
||||
UPDATE song s
|
||||
SET soundtrack_uuid = st.uuid
|
||||
FROM soundtrack st
|
||||
WHERE s.soundtrack_id = st.id AND s.soundtrack_uuid IS NULL;
|
||||
|
||||
-- Add foreign key constraint from song.soundtrack_uuid to soundtrack.uuid
|
||||
ALTER TABLE song ADD CONSTRAINT song_soundtrack_uuid_fkey
|
||||
FOREIGN KEY (soundtrack_uuid) REFERENCES soundtrack(uuid);
|
||||
|
||||
-- Add indexes on song UUID columns
|
||||
CREATE INDEX IF NOT EXISTS idx_song_uuid ON song(uuid);
|
||||
CREATE INDEX IF NOT EXISTS idx_song_soundtrack_uuid ON song(soundtrack_uuid);
|
||||
|
||||
-- Drop the composite primary key (soundtrack_id, path)
|
||||
ALTER TABLE song DROP CONSTRAINT song_pkey;
|
||||
|
||||
-- Add new primary key on uuid column
|
||||
ALTER TABLE song ADD CONSTRAINT song_pkey PRIMARY KEY (uuid);
|
||||
|
||||
-- Add unique constraint to maintain same semantics as old PK
|
||||
-- This ensures (soundtrack, path) combinations remain unique
|
||||
ALTER TABLE song ADD CONSTRAINT song_soundtrack_path_unique UNIQUE (soundtrack_uuid, path);
|
||||
|
||||
-- Create index on soundtrack_id for query performance
|
||||
CREATE INDEX IF NOT EXISTS idx_song_soundtrack_id ON song(soundtrack_id);
|
||||
|
||||
-- Create index on path for lookups
|
||||
CREATE INDEX IF NOT EXISTS idx_song_path ON song(path);
|
||||
|
||||
-- ============ PART 3: MAKE UUID COLUMNS NOT NULL (Issue #28 - Phase 2) ============
|
||||
|
||||
-- Add NOT NULL constraint to soundtrack.uuid
|
||||
ALTER TABLE soundtrack ALTER COLUMN uuid SET NOT NULL;
|
||||
|
||||
-- Add NOT NULL constraint to song.uuid
|
||||
ALTER TABLE song ALTER COLUMN uuid SET NOT NULL;
|
||||
|
||||
-- Add NOT NULL constraint to song.soundtrack_uuid
|
||||
ALTER TABLE song ALTER COLUMN soundtrack_uuid SET NOT NULL;
|
||||
@@ -0,0 +1,99 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"music-server/internal/logging"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// RateLimiter implements a simple IP-based rate limiter
|
||||
type RateLimiter struct {
|
||||
visits map[string]*Visit
|
||||
mu sync.Mutex
|
||||
limit int // requests per window
|
||||
window time.Duration // time window
|
||||
}
|
||||
|
||||
type Visit struct {
|
||||
count int
|
||||
firstSeen time.Time
|
||||
}
|
||||
|
||||
// NewRateLimiter creates a new rate limiter
|
||||
func NewRateLimiter(limit int, window time.Duration) *RateLimiter {
|
||||
rl := &RateLimiter{
|
||||
visits: make(map[string]*Visit),
|
||||
limit: limit,
|
||||
window: window,
|
||||
}
|
||||
|
||||
// Start cleanup goroutine
|
||||
go rl.cleanup()
|
||||
return rl
|
||||
}
|
||||
|
||||
// cleanup periodically removes old entries
|
||||
func (rl *RateLimiter) cleanup() {
|
||||
for {
|
||||
time.Sleep(rl.window)
|
||||
rl.mu.Lock()
|
||||
now := time.Now()
|
||||
for ip, visit := range rl.visits {
|
||||
if now.Sub(visit.firstSeen) > rl.window {
|
||||
delete(rl.visits, ip)
|
||||
}
|
||||
}
|
||||
rl.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// RateLimitMiddleware returns an Echo middleware for rate limiting
|
||||
func RateLimitMiddleware(rl *RateLimiter) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c *echo.Context) error {
|
||||
ip := c.RealIP()
|
||||
|
||||
rl.mu.Lock()
|
||||
defer rl.mu.Unlock()
|
||||
|
||||
visit, exists := rl.visits[ip]
|
||||
now := time.Now()
|
||||
|
||||
if !exists {
|
||||
// First visit from this IP
|
||||
rl.visits[ip] = &Visit{
|
||||
count: 1,
|
||||
firstSeen: now,
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
|
||||
// Check if window has expired
|
||||
if now.Sub(visit.firstSeen) > rl.window {
|
||||
// Reset for new window
|
||||
visit.count = 1
|
||||
visit.firstSeen = now
|
||||
return next(c)
|
||||
}
|
||||
|
||||
// Check if limit exceeded
|
||||
if visit.count >= rl.limit {
|
||||
logging.GetLogger().Warn("Rate limit exceeded",
|
||||
zap.String("ip", ip),
|
||||
zap.String("path", c.Request().URL.Path))
|
||||
return c.JSON(http.StatusTooManyRequests, map[string]string{
|
||||
"error": "Rate limit exceeded. Please try again later.",
|
||||
})
|
||||
}
|
||||
|
||||
// Increment count
|
||||
visit.count++
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user