refactor(*):Address PR comments

This commit is contained in:
Josh Creek
2025-07-26 19:42:04 +01:00
parent bc9ce84615
commit fa38fa69ca
22 changed files with 749 additions and 412 deletions
@@ -6,102 +6,128 @@
-- Pokemon entries table (replaces pokedexentries MongoDB collection)
CREATE TABLE pokedex_entries (
id BIGSERIAL PRIMARY KEY,
pokedex_number INTEGER NOT NULL,
"pokedexNumber" INTEGER NOT NULL,
pokemon TEXT NOT NULL,
form TEXT,
can_gigantamax BOOLEAN DEFAULT FALSE,
region_to_catch_in TEXT,
games_to_catch_in TEXT[], -- Array of games
region_to_evolve_in TEXT,
evolution_information TEXT,
catch_information TEXT[], -- Array of catch info
"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
box_placement_forms_box INTEGER,
box_placement_forms_row INTEGER,
box_placement_forms_column INTEGER,
"boxPlacementFormsBox" INTEGER,
"boxPlacementFormsRow" INTEGER,
"boxPlacementFormsColumn" INTEGER,
-- Box placement for no-forms view
box_placement_box INTEGER,
box_placement_row INTEGER,
box_placement_column INTEGER,
"boxPlacementBox" INTEGER,
"boxPlacementRow" INTEGER,
"boxPlacementColumn" INTEGER,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
"createdAt" TIMESTAMPTZ DEFAULT NOW(),
"updatedAt" TIMESTAMPTZ DEFAULT NOW()
);
-- Add unique constraint to prevent duplicate pokemon forms
ALTER TABLE pokedex_entries ADD CONSTRAINT unique_pokemon_form
UNIQUE("pokedexNumber", form);
-- Catch records table (replaces catchrecords MongoDB collection)
CREATE TABLE catch_records (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
pokedex_entry_id BIGINT NOT NULL REFERENCES pokedex_entries(id) ON DELETE CASCADE,
"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
have_to_evolve BOOLEAN DEFAULT FALSE,
"haveToEvolve" BOOLEAN DEFAULT FALSE,
caught BOOLEAN DEFAULT FALSE,
in_home BOOLEAN DEFAULT FALSE,
has_gigantamaxed BOOLEAN DEFAULT FALSE,
personal_notes TEXT,
"inHome" BOOLEAN DEFAULT FALSE,
"hasGigantamaxed" BOOLEAN DEFAULT FALSE,
"personalNotes" TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
"createdAt" TIMESTAMPTZ DEFAULT NOW(),
"updatedAt" TIMESTAMPTZ DEFAULT NOW(),
-- Ensure one record per user per pokemon entry
UNIQUE(user_id, pokedex_entry_id)
UNIQUE("userId", "pokedexEntryId")
);
-- Region/Game mappings table (replaces regiongamemappings MongoDB collection)
-- Region-game mappings table for filtering
CREATE TABLE region_game_mappings (
id BIGSERIAL PRIMARY KEY,
region TEXT NOT NULL,
games TEXT[] NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
game TEXT NOT NULL,
UNIQUE(region, game)
);
-- Pokedex metadata table (replaces pokedexmetadata MongoDB collection)
CREATE TABLE pokedex_metadata (
-- Metadata table for migration tracking
CREATE TABLE metadata (
id BIGSERIAL PRIMARY KEY,
key TEXT UNIQUE NOT NULL,
value JSONB NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
value TEXT,
"createdAt" TIMESTAMPTZ DEFAULT NOW(),
"updatedAt" TIMESTAMPTZ DEFAULT NOW()
);
-- 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() = user_id)
WITH CHECK (auth.uid() = user_id);
-- Create indexes for better performance
CREATE INDEX idx_pokedex_entries_pokedex_number ON pokedex_entries(pokedex_number);
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(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_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(user_id);
CREATE INDEX idx_catch_records_pokedex_entry_id ON catch_records(pokedex_entry_id);
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(in_home);
CREATE INDEX idx_catch_records_in_home ON catch_records("inHome");
CREATE INDEX idx_catch_records_user_caught ON catch_records("userId", caught);
-- Create updated_at trigger function
-- Function to automatically update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
NEW."updatedAt" = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
$$ 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 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();
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();
-- Enable RLS on all tables
ALTER TABLE pokedex_entries ENABLE ROW LEVEL SECURITY;
ALTER TABLE catch_records ENABLE ROW LEVEL SECURITY;
ALTER TABLE region_game_mappings ENABLE ROW LEVEL SECURITY;
ALTER TABLE metadata ENABLE ROW LEVEL SECURITY;
-- RLS policies for pokedex_entries (public read)
CREATE POLICY "Anyone can view pokemon entries" ON pokedex_entries
FOR SELECT USING (true);
-- RLS policies for catch_records (user-specific)
CREATE POLICY "Users can view own catch records" ON catch_records
FOR SELECT USING (auth.uid() = "userId");
CREATE POLICY "Users can insert own catch records" ON catch_records
FOR INSERT WITH CHECK (auth.uid() = "userId");
CREATE POLICY "Users can update own catch records" ON catch_records
FOR UPDATE USING (auth.uid() = "userId");
CREATE POLICY "Users can delete own catch records" ON catch_records
FOR DELETE USING (auth.uid() = "userId");
-- RLS policies for region_game_mappings (public read)
CREATE POLICY "Anyone can view region game mappings" ON region_game_mappings
FOR SELECT USING (true);
-- RLS policies for metadata (public read)
CREATE POLICY "Anyone can view metadata" ON metadata
FOR SELECT USING (true);