mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(*): Migrate from MongoDB to unified Supabase PostgreSQL architecture
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
-- Initial setup migration for LivingDexTracker
|
||||
-- This sets up any additional schema needed
|
||||
|
||||
-- Note: RLS is already enabled on auth.users by default in Supabase
|
||||
-- and proper policies are already in place
|
||||
|
||||
-- Create a profiles table if we need additional user data
|
||||
-- This is optional for the current setup since we only use Supabase for auth
|
||||
-- and store everything else in MongoDB
|
||||
|
||||
-- CREATE TABLE profiles (
|
||||
-- id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
|
||||
-- username TEXT,
|
||||
-- avatar_url TEXT,
|
||||
-- created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
-- updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
-- );
|
||||
|
||||
-- Enable RLS on profiles table
|
||||
-- ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create RLS policies for profiles
|
||||
-- CREATE POLICY "Users can view own profile"
|
||||
-- ON profiles
|
||||
-- FOR SELECT
|
||||
-- USING (auth.uid() = id);
|
||||
|
||||
-- CREATE POLICY "Users can update own profile"
|
||||
-- ON profiles
|
||||
-- FOR UPDATE
|
||||
-- USING (auth.uid() = id);
|
||||
|
||||
-- Note: MongoDB collections (pokedexentries, catchrecords) are handled by the app
|
||||
-- Supabase is only used for authentication in this hybrid setup
|
||||
@@ -0,0 +1,107 @@
|
||||
-- Migration: Create PostgreSQL schema for Pokemon data
|
||||
-- This replaces the MongoDB collections 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,
|
||||
pokedex_number 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
|
||||
|
||||
-- Box placement for forms view
|
||||
box_placement_forms_box INTEGER,
|
||||
box_placement_forms_row INTEGER,
|
||||
box_placement_forms_column INTEGER,
|
||||
|
||||
-- Box placement for no-forms view
|
||||
box_placement_box INTEGER,
|
||||
box_placement_row INTEGER,
|
||||
box_placement_column INTEGER,
|
||||
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- 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,
|
||||
|
||||
-- Catch status fields
|
||||
have_to_evolve BOOLEAN DEFAULT FALSE,
|
||||
caught BOOLEAN DEFAULT FALSE,
|
||||
in_home BOOLEAN DEFAULT FALSE,
|
||||
has_gigantamaxed BOOLEAN DEFAULT FALSE,
|
||||
personal_notes TEXT,
|
||||
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
|
||||
-- Ensure one record per user per pokemon entry
|
||||
UNIQUE(user_id, pokedex_entry_id)
|
||||
);
|
||||
|
||||
-- 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()
|
||||
);
|
||||
|
||||
-- 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_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(user_id);
|
||||
CREATE INDEX idx_catch_records_pokedex_entry_id ON catch_records(pokedex_entry_id);
|
||||
CREATE INDEX idx_catch_records_caught ON catch_records(caught);
|
||||
CREATE INDEX idx_catch_records_in_home ON catch_records(in_home);
|
||||
|
||||
-- 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();
|
||||
@@ -0,0 +1,54 @@
|
||||
-- Migration: Seed Pokemon data from TSV
|
||||
-- This replaces the separate tsv-parser project and MongoDB seeding
|
||||
|
||||
-- Insert Pokemon data based on the TSV structure
|
||||
-- Note: This is a sample of the data structure - you'll need to convert the full TSV
|
||||
|
||||
INSERT INTO pokedex_entries (
|
||||
pokedex_number,
|
||||
pokemon,
|
||||
form,
|
||||
can_gigantamax,
|
||||
region_to_catch_in,
|
||||
games_to_catch_in,
|
||||
region_to_evolve_in,
|
||||
evolution_information,
|
||||
box_placement_forms_box,
|
||||
box_placement_forms_row,
|
||||
box_placement_forms_column,
|
||||
box_placement_box,
|
||||
box_placement_row,
|
||||
box_placement_column
|
||||
) VALUES
|
||||
-- Sample data from the TSV (first few entries)
|
||||
(1, 'Bulbasaur', NULL, FALSE, 'Kanto', ARRAY['Red','Blue','Yellow','LG: Pikachu','LG: Eevee'], NULL, NULL, 1, 1, 1, 1, 1, 1),
|
||||
(2, 'Ivysaur', NULL, FALSE, 'Kanto', ARRAY['Red','Blue','Yellow','LG: Pikachu','LG: Eevee'], NULL, NULL, 1, 1, 2, 1, 1, 2),
|
||||
(3, 'Venusaur', NULL, TRUE, 'Kanto', ARRAY['Red','Blue','Yellow','LG: Pikachu','LG: Eevee'], NULL, NULL, 1, 1, 3, 1, 1, 3),
|
||||
(3, 'Venusaur', 'Female', FALSE, 'Kanto', ARRAY['Red','Blue','Yellow','LG: Pikachu','LG: Eevee'], NULL, NULL, 1, 1, 4, NULL, NULL, NULL),
|
||||
(4, 'Charmander', NULL, FALSE, 'Kanto', ARRAY['Red','Blue','Yellow','LG: Pikachu','LG: Eevee'], NULL, NULL, 1, 1, 5, 1, 1, 4),
|
||||
(5, 'Charmeleon', NULL, FALSE, 'Kanto', ARRAY['Red','Blue','Yellow','LG: Pikachu','LG: Eevee'], NULL, NULL, 1, 1, 6, 1, 1, 5),
|
||||
(6, 'Charizard', NULL, TRUE, 'Kanto', ARRAY['Red','Blue','Yellow','LG: Pikachu','LG: Eevee'], NULL, NULL, 1, 2, 1, 1, 1, 6),
|
||||
(7, 'Squirtle', NULL, FALSE, 'Kanto', ARRAY['Red','Blue','Yellow','LG: Pikachu','LG: Eevee'], NULL, NULL, 1, 2, 2, 1, 2, 1),
|
||||
(8, 'Wartortle', NULL, FALSE, 'Kanto', ARRAY['Red','Blue','Yellow','LG: Pikachu','LG: Eevee'], NULL, NULL, 1, 2, 3, 1, 2, 2);
|
||||
|
||||
-- Insert region game mappings
|
||||
INSERT INTO region_game_mappings (region, games) VALUES
|
||||
('Kanto', ARRAY['Red', 'Blue', 'Yellow', 'LG: Pikachu', 'LG: Eevee']),
|
||||
('Johto', ARRAY['Gold', 'Silver', 'Crystal', 'HeartGold', 'SoulSilver']),
|
||||
('Hoenn', ARRAY['Ruby', 'Sapphire', 'Emerald', 'Omega Ruby', 'Alpha Sapphire']),
|
||||
('Sinnoh', ARRAY['Diamond', 'Pearl', 'Platinum', 'Brilliant Diamond', 'Shining Pearl']),
|
||||
('Unova', ARRAY['Black', 'White', 'Black 2', 'White 2']),
|
||||
('Kalos', ARRAY['X', 'Y']),
|
||||
('Alola', ARRAY['Sun', 'Moon', 'Ultra Sun', 'Ultra Moon']),
|
||||
('Galar', ARRAY['Sword', 'Shield']),
|
||||
('Paldea', ARRAY['Scarlet', 'Violet']);
|
||||
|
||||
-- Insert some metadata
|
||||
INSERT INTO pokedex_metadata (key, value) VALUES
|
||||
('total_pokemon', '{"count": 1025}'),
|
||||
('last_updated', '{"timestamp": "2025-01-26T00:00:00Z"}'),
|
||||
('supported_generations', '{"generations": [1,2,3,4,5,6,7,8,9]}');
|
||||
|
||||
-- Note: This is a sample seed. For the full migration, we'll need to:
|
||||
-- 1. Convert the entire TSV file to SQL inserts
|
||||
-- 2. Or create a function to import from the TSV data directly
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user