-- Multi-user support: profiles table + profile_id in all per-user entities CREATE TABLE IF NOT EXISTS profiles ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL UNIQUE, password_hash VARCHAR(72) NOT NULL DEFAULT '', created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -- Seed profile 1 for the existing data owner (Manoel) INSERT INTO profiles (id, name, password_hash) VALUES (1, 'manoel', '') ON CONFLICT DO NOTHING; SELECT setval('profiles_id_seq', GREATEST(1, (SELECT MAX(id) FROM profiles))); -- Add profile_id to all per-user tables (DEFAULT 1 makes existing rows belong to Manoel) ALTER TABLE categories ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id); ALTER TABLE transactions ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id); ALTER TABLE recurring_expenses ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id); ALTER TABLE accounts ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id); ALTER TABLE player_profile ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id); ALTER TABLE xp_events ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id); ALTER TABLE player_quests ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id); ALTER TABLE player_achievements ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id); ALTER TABLE player_cosmetics ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id); -- Fix unique constraints to be scoped per profile ALTER TABLE player_quests DROP CONSTRAINT IF EXISTS player_quests_quest_id_period_key; ALTER TABLE player_quests ADD CONSTRAINT player_quests_quest_id_period_profile_key UNIQUE (quest_id, period, profile_id); ALTER TABLE player_achievements DROP CONSTRAINT IF EXISTS player_achievements_achievement_id_key; ALTER TABLE player_achievements ADD CONSTRAINT player_achievements_achievement_id_profile_key UNIQUE (achievement_id, profile_id); ALTER TABLE player_cosmetics DROP CONSTRAINT IF EXISTS player_cosmetics_cosmetic_id_key; ALTER TABLE player_cosmetics ADD CONSTRAINT player_cosmetics_cosmetic_id_profile_key UNIQUE (cosmetic_id, profile_id); INSERT INTO schema_migrations (version) VALUES (14) ON CONFLICT DO NOTHING;