mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
refactor(*):Address PR comments
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
-- Migration: Create PostgreSQL schema for Pokemon data
|
||||
-- This replaces the M-- Create indexes for better p$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Create triggers for updated_at
|
||||
CREATE TRIGGER update_pokedex_entries_updated_at
|
||||
BEFORE UPDATE ON pokedex_entries
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
CREATE TRIGGER update_catch_records_updated_at
|
||||
BEFORE UPDATE ON catch_records
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();ance
|
||||
CREATE INDEX idx_pokedex_entries_pokedex_number ON pokedex_entries(pokedex_number);
|
||||
CREATE INDEX idx_pokedex_entries_pokemon ON pokedex_entries(pokemon);
|
||||
CREATE INDEX idx_pokedex_entries_box_placement_forms ON pokedex_entries(box_placement_forms_box, box_placement_forms_row, box_placement_forms_column);
|
||||
CREATE INDEX idx_pokedex_entries_box_placement ON pokedex_entries(box_placement_box, box_placement_row, box_placement_column);
|
||||
|
||||
CREATE INDEX idx_catch_records_user_id ON catch_records("userId");
|
||||
CREATE INDEX idx_catch_records_pokedex_entry_id ON catch_records("pokedexEntryId");
|
||||
CREATE INDEX idx_catch_records_caught ON catch_records(caught);
|
||||
CREATE INDEX idx_catch_records_in_home ON catch_records("inHome");
|
||||
CREATE INDEX idx_catch_records_user_caught ON catch_records("userId", caught);lections with proper PostgreSQL tables
|
||||
|
||||
-- Note: Using individual columns instead of composite types for simplicity
|
||||
|
||||
-- Pokemon entries table (replaces pokedexentries MongoDB collection)
|
||||
CREATE TABLE pokedex_entries (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
pokedexNumber INTEGER NOT NULL,
|
||||
pokemon TEXT NOT NULL,
|
||||
form TEXT,
|
||||
canGigantamax BOOLEAN DEFAULT FALSE,
|
||||
regionToCatchIn TEXT,
|
||||
gamesToCatchIn TEXT[], -- Array of games
|
||||
regionToEvolveIn TEXT,
|
||||
evolutionInformation TEXT,
|
||||
catchInformation TEXT[], -- Array of catch info
|
||||
|
||||
-- Box placement for forms view
|
||||
boxPlacementFormsBox INTEGER,
|
||||
boxPlacementFormsRow INTEGER,
|
||||
boxPlacementFormsColumn INTEGER,
|
||||
|
||||
-- Box placement for no-forms view
|
||||
boxPlacementBox INTEGER,
|
||||
boxPlacementRow INTEGER,
|
||||
boxPlacementColumn INTEGER,
|
||||
|
||||
createdAt TIMESTAMPTZ DEFAULT NOW(),
|
||||
updatedAt TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Catch records table (replaces catchrecords MongoDB collection)
|
||||
CREATE TABLE catch_records (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"userId" UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
|
||||
"pokedexEntryId" BIGINT NOT NULL REFERENCES pokedex_entries(id) ON DELETE CASCADE,
|
||||
|
||||
-- Catch status fields
|
||||
"haveToEvolve" BOOLEAN DEFAULT FALSE,
|
||||
caught BOOLEAN DEFAULT FALSE,
|
||||
"inHome" BOOLEAN DEFAULT FALSE,
|
||||
"hasGigantamaxed" BOOLEAN DEFAULT FALSE,
|
||||
"personalNotes" TEXT,
|
||||
|
||||
"createdAt" TIMESTAMPTZ DEFAULT NOW(),
|
||||
"updatedAt" TIMESTAMPTZ DEFAULT NOW(),
|
||||
|
||||
-- Ensure one record per user per pokemon entry
|
||||
UNIQUE("userId", "pokedexEntryId")
|
||||
);
|
||||
|
||||
-- Region/Game mappings table (replaces regiongamemappings MongoDB collection)
|
||||
CREATE TABLE region_game_mappings (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
region TEXT NOT NULL,
|
||||
games TEXT[] NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Pokedex metadata table (replaces pokedexmetadata MongoDB collection)
|
||||
CREATE TABLE pokedex_metadata (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
key TEXT UNIQUE NOT NULL,
|
||||
value JSONB NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Add unique constraint to prevent duplicate pokemon forms
|
||||
ALTER TABLE pokedex_entries ADD CONSTRAINT unique_pokemon_form
|
||||
UNIQUE(pokedex_number, form);
|
||||
|
||||
-- Enable Row Level Security (RLS) for user data isolation
|
||||
ALTER TABLE catch_records ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- RLS Policy: Users can only access their own catch records
|
||||
CREATE POLICY "Users can only access their own catch records"
|
||||
ON catch_records FOR ALL
|
||||
USING (auth.uid() = userId)
|
||||
WITH CHECK (auth.uid() = userId);
|
||||
|
||||
-- Create indexes for better performance
|
||||
CREATE INDEX idx_pokedex_entries_pokedex_number ON pokedex_entries(pokedexNumber);
|
||||
CREATE INDEX idx_pokedex_entries_pokemon ON pokedex_entries(pokemon);
|
||||
CREATE INDEX idx_pokedex_entries_box_placement_forms ON pokedex_entries(boxPlacementFormsBox, boxPlacementFormsRow, boxPlacementFormsColumn);
|
||||
CREATE INDEX idx_pokedex_entries_box_placement ON pokedex_entries(boxPlacementBox, boxPlacementRow, boxPlacementColumn);
|
||||
|
||||
CREATE INDEX idx_catch_records_user_id ON catch_records(userId);
|
||||
CREATE INDEX idx_catch_records_pokedex_entry_id ON catch_records(pokedexEntryId);
|
||||
CREATE INDEX idx_catch_records_caught ON catch_records(caught);
|
||||
CREATE INDEX idx_catch_records_in_home ON catch_records(inHome);
|
||||
|
||||
-- Create updated_at trigger function
|
||||
CREATE OR REPLACE FUNCTION update_updated_at_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = NOW();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ language 'plpgsql';
|
||||
|
||||
-- Add updated_at triggers
|
||||
CREATE TRIGGER update_pokedex_entries_updated_at BEFORE UPDATE ON pokedex_entries
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
CREATE TRIGGER update_catch_records_updated_at BEFORE UPDATE ON catch_records
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
CREATE TRIGGER update_pokedex_metadata_updated_at BEFORE UPDATE ON pokedex_metadata
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
Reference in New Issue
Block a user