-- Migration 016: Missing Columns and Additional Indexes
-- This migration adds missing columns and indexes from the provided migrations

-- Add missing column to github_repositories if it doesn't exist
ALTER TABLE all_repositories 
ADD COLUMN IF NOT EXISTS last_synced_commit_sha VARCHAR(64);

-- Add missing ID column to repository_files if it doesn't exist
ALTER TABLE repository_files 
ADD COLUMN IF NOT EXISTS id UUID PRIMARY KEY DEFAULT uuid_generate_v4();

-- Additional indexes for better performance that were missing
CREATE INDEX IF NOT EXISTS idx_repo_directories_level ON repository_directories(level);
CREATE INDEX IF NOT EXISTS idx_repo_directories_relative_path ON repository_directories(relative_path);

-- Note: The repository_files table has been optimized to use JSONB storage
-- These indexes are now handled by the optimized table structure in migration 003
-- The following indexes are already created in the optimized table:
-- - idx_repo_files_files_gin (GIN index on files JSONB column)
-- - idx_repo_files_filename (GIN index on files->>'filename')
-- - idx_repo_files_extension (GIN index on files->>'file_extension')
-- - idx_repo_files_is_binary (GIN index on files->>'is_binary')
-- - idx_repo_files_relative_path (B-tree index on relative_path)

-- Webhook indexes that might be missing
CREATE INDEX IF NOT EXISTS idx_bitbucket_webhooks_event_type ON bitbucket_webhooks(event_type);
CREATE INDEX IF NOT EXISTS idx_gitea_webhooks_repository_id ON gitea_webhooks(repository_id);
CREATE INDEX IF NOT EXISTS idx_gitea_webhooks_created_at ON gitea_webhooks(created_at);
CREATE INDEX IF NOT EXISTS idx_gitea_webhooks_event_type ON gitea_webhooks(event_type);
