-- Model Training MCP Server Database Schema -- Handles model training, evaluation, and prediction explanation storage -- Table for storing trained models CREATE TABLE IF NOT EXISTS trained_models ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id VARCHAR NOT NULL, dataset_id UUID NOT NULL, algorithm VARCHAR NOT NULL, hyperparameters JSONB DEFAULT '{}', performance_metrics JSONB DEFAULT '{}', feature_importance JSONB DEFAULT '{}', training_time_seconds FLOAT DEFAULT 0, task_type VARCHAR NOT NULL CHECK (task_type IN ('classification', 'regression')), target_column VARCHAR NOT NULL, model_file_path VARCHAR, cross_val_scores JSONB DEFAULT '[]', created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP DEFAULT NOW() ); -- Table for storing model training sessions CREATE TABLE IF NOT EXISTS model_training_sessions ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id VARCHAR NOT NULL, dataset_id UUID NOT NULL, target_column VARCHAR NOT NULL, task_type VARCHAR NOT NULL CHECK (task_type IN ('classification', 'regression')), algorithms_trained JSONB DEFAULT '[]', best_model_id UUID REFERENCES trained_models(id), training_status VARCHAR DEFAULT 'running' CHECK (training_status IN ('running', 'completed', 'failed')), error_message TEXT, total_training_time_seconds FLOAT DEFAULT 0, created_at TIMESTAMP DEFAULT NOW(), completed_at TIMESTAMP ); -- Table for storing prediction explanations CREATE TABLE IF NOT EXISTS prediction_explanations ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id VARCHAR NOT NULL, model_id UUID REFERENCES trained_models(id), sample_index INTEGER NOT NULL, prediction_value FLOAT, prediction_class VARCHAR, confidence_score FLOAT, feature_contributions JSONB DEFAULT '{}', explanation_text TEXT, created_at TIMESTAMP DEFAULT NOW() ); -- Table for storing ensemble models CREATE TABLE IF NOT EXISTS ensemble_models ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id VARCHAR NOT NULL, name VARCHAR NOT NULL, base_model_ids JSONB NOT NULL, -- Array of model IDs ensemble_method VARCHAR NOT NULL CHECK (ensemble_method IN ('voting', 'stacking', 'bagging')), weights JSONB DEFAULT '{}', performance_metrics JSONB DEFAULT '{}', model_file_path VARCHAR, created_at TIMESTAMP DEFAULT NOW() ); -- Table for storing model comparison results CREATE TABLE IF NOT EXISTS model_comparisons ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id VARCHAR NOT NULL, training_session_id UUID REFERENCES model_training_sessions(id), model_rankings JSONB NOT NULL, -- Ranked list of models with scores comparison_metrics JSONB DEFAULT '{}', best_model_id UUID REFERENCES trained_models(id), created_at TIMESTAMP DEFAULT NOW() ); -- Indexes for performance CREATE INDEX IF NOT EXISTS idx_trained_models_user_id ON trained_models(user_id); CREATE INDEX IF NOT EXISTS idx_trained_models_dataset_id ON trained_models(dataset_id); CREATE INDEX IF NOT EXISTS idx_trained_models_algorithm ON trained_models(algorithm); CREATE INDEX IF NOT EXISTS idx_model_training_sessions_user_id ON model_training_sessions(user_id); CREATE INDEX IF NOT EXISTS idx_model_training_sessions_dataset_id ON model_training_sessions(dataset_id); CREATE INDEX IF NOT EXISTS idx_prediction_explanations_user_id ON prediction_explanations(user_id); CREATE INDEX IF NOT EXISTS idx_prediction_explanations_model_id ON prediction_explanations(model_id); CREATE INDEX IF NOT EXISTS idx_ensemble_models_user_id ON ensemble_models(user_id); CREATE INDEX IF NOT EXISTS idx_model_comparisons_user_id ON model_comparisons(user_id); -- Row Level Security (RLS) policies ALTER TABLE trained_models ENABLE ROW LEVEL SECURITY; ALTER TABLE model_training_sessions ENABLE ROW LEVEL SECURITY; ALTER TABLE prediction_explanations ENABLE ROW LEVEL SECURITY; ALTER TABLE ensemble_models ENABLE ROW LEVEL SECURITY; ALTER TABLE model_comparisons ENABLE ROW LEVEL SECURITY; -- RLS Policies for user isolation CREATE POLICY trained_models_user_isolation ON trained_models FOR ALL TO application_role USING (user_id = current_setting('app.current_user_id')); CREATE POLICY model_training_sessions_user_isolation ON model_training_sessions FOR ALL TO application_role USING (user_id = current_setting('app.current_user_id')); CREATE POLICY prediction_explanations_user_isolation ON prediction_explanations FOR ALL TO application_role USING (user_id = current_setting('app.current_user_id')); CREATE POLICY ensemble_models_user_isolation ON ensemble_models FOR ALL TO application_role USING (user_id = current_setting('app.current_user_id')); CREATE POLICY model_comparisons_user_isolation ON model_comparisons FOR ALL TO application_role USING (user_id = current_setting('app.current_user_id'));