-- Users table CREATE TABLE IF NOT EXISTS users ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), email VARCHAR(255) UNIQUE NOT NULL, password_hash VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX idx_users_email ON users(email); -- Shooting styles table CREATE TABLE IF NOT EXISTS shooting_styles ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name VARCHAR(255) NOT NULL UNIQUE, description TEXT, scoring_format VARCHAR(50) NOT NULL, max_points_per_arrow INT NOT NULL DEFAULT 10, configuration JSONB DEFAULT '{}', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- Tournaments table CREATE TABLE IF NOT EXISTS tournaments ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, name VARCHAR(255) NOT NULL, shooting_style_id UUID NOT NULL REFERENCES shooting_styles(id), status VARCHAR(50) DEFAULT 'draft', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX idx_tournaments_user_id ON tournaments(user_id); CREATE INDEX idx_tournaments_status ON tournaments(status); -- Archers table CREATE TABLE IF NOT EXISTS archers ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), tournament_id UUID NOT NULL REFERENCES tournaments(id) ON DELETE CASCADE, user_id UUID REFERENCES users(id) ON DELETE SET NULL, name VARCHAR(255) NOT NULL, bib_number VARCHAR(50), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX idx_archers_tournament_id ON archers(tournament_id); -- Rounds table CREATE TABLE IF NOT EXISTS rounds ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), tournament_id UUID NOT NULL REFERENCES tournaments(id) ON DELETE CASCADE, round_number INT NOT NULL, distance_meters INT, arrow_count INT NOT NULL DEFAULT 72, max_score_per_arrow INT NOT NULL DEFAULT 10, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX idx_rounds_tournament_id ON rounds(tournament_id); -- Shots table CREATE TABLE IF NOT EXISTS shots ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), round_id UUID NOT NULL REFERENCES rounds(id) ON DELETE CASCADE, archer_id UUID NOT NULL REFERENCES archers(id) ON DELETE CASCADE, arrow_number INT NOT NULL, score INT NOT NULL, notes TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX idx_shots_round_id ON shots(round_id); CREATE INDEX idx_shots_archer_id ON shots(archer_id); -- Insert default shooting styles INSERT INTO shooting_styles (name, description, scoring_format, max_points_per_arrow, configuration) VALUES ('Olympic Recurve', 'Standard Olympic archery format', 'point_based', 10, '{"rounds": 1, "arrows_per_round": 72}'), ('3D Field', 'Field archery with variable distances', 'zone_based', 12, '{"variable_distance": true}'), ('Indoor 18m', 'Indoor competition at 18 meters', 'point_based', 10, '{"distance_meters": 18, "arrows": 60}'), ('Compound', 'Compound bow format', 'point_based', 10, '{"bow_type": "compound"}') ON CONFLICT (name) DO NOTHING;