-- Feature Engineering MCP Server Database Schema -- Creates tables for feature engineering operations and explanations with user isolation -- Enable UUID extension if not already enabled CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -- Feature engineering plans table CREATE TABLE IF NOT EXISTS feature_engineering_plans ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE, user_id VARCHAR NOT NULL, hypotheses_used JSONB DEFAULT '[]', interaction_features JSONB DEFAULT '[]', transformation_features JSONB DEFAULT '[]', aggregation_features JSONB DEFAULT '[]', created_at TIMESTAMP DEFAULT NOW() ); -- Engineered features table CREATE TABLE IF NOT EXISTS engineered_features ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), plan_id UUID NOT NULL REFERENCES feature_engineering_plans(id) ON DELETE CASCADE, engineered_dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE, user_id VARCHAR NOT NULL, feature_name VARCHAR NOT NULL, feature_type VARCHAR NOT NULL, -- 'interaction', 'transformation', 'aggregation' source_columns JSONB DEFAULT '[]', formula TEXT, hypothesis_basis VARCHAR, description TEXT, importance_score FLOAT, created_at TIMESTAMP DEFAULT NOW() ); -- Feature complexity metrics table CREATE TABLE IF NOT EXISTS feature_complexity_metrics ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), engineered_dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE, user_id VARCHAR NOT NULL, total_features INTEGER DEFAULT 0, original_features INTEGER DEFAULT 0, engineered_features INTEGER DEFAULT 0, interaction_features INTEGER DEFAULT 0, transformation_features INTEGER DEFAULT 0, aggregation_features INTEGER DEFAULT 0, complexity_score FLOAT DEFAULT 0.0, dimensionality_reduction_applied BOOLEAN DEFAULT false, created_at TIMESTAMP DEFAULT NOW() ); -- Feature validation results table CREATE TABLE IF NOT EXISTS feature_validations ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), engineered_dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE, user_id VARCHAR NOT NULL, has_data_leakage BOOLEAN DEFAULT false, leakage_warnings JSONB DEFAULT '[]', validation_errors JSONB DEFAULT '[]', validation_passed BOOLEAN DEFAULT true, validated_at TIMESTAMP DEFAULT NOW() ); -- Create indexes for better performance CREATE INDEX IF NOT EXISTS idx_feature_engineering_plans_dataset_id ON feature_engineering_plans(dataset_id); CREATE INDEX IF NOT EXISTS idx_feature_engineering_plans_user_id ON feature_engineering_plans(user_id); CREATE INDEX IF NOT EXISTS idx_engineered_features_plan_id ON engineered_features(plan_id); CREATE INDEX IF NOT EXISTS idx_engineered_features_dataset_id ON engineered_features(engineered_dataset_id); CREATE INDEX IF NOT EXISTS idx_engineered_features_user_id ON engineered_features(user_id); CREATE INDEX IF NOT EXISTS idx_feature_complexity_metrics_dataset_id ON feature_complexity_metrics(engineered_dataset_id); CREATE INDEX IF NOT EXISTS idx_feature_complexity_metrics_user_id ON feature_complexity_metrics(user_id); CREATE INDEX IF NOT EXISTS idx_feature_validations_dataset_id ON feature_validations(engineered_dataset_id); CREATE INDEX IF NOT EXISTS idx_feature_validations_user_id ON feature_validations(user_id); -- Enable Row Level Security (RLS) ALTER TABLE feature_engineering_plans ENABLE ROW LEVEL SECURITY; ALTER TABLE engineered_features ENABLE ROW LEVEL SECURITY; ALTER TABLE feature_complexity_metrics ENABLE ROW LEVEL SECURITY; ALTER TABLE feature_validations ENABLE ROW LEVEL SECURITY; -- Create RLS policies for user isolation -- Users can only access their own data -- Feature engineering plans policies DROP POLICY IF EXISTS feature_engineering_plans_isolation ON feature_engineering_plans; CREATE POLICY feature_engineering_plans_isolation ON feature_engineering_plans FOR ALL TO public USING (user_id = current_setting('app.current_user_id', true)); -- Engineered features policies DROP POLICY IF EXISTS engineered_features_isolation ON engineered_features; CREATE POLICY engineered_features_isolation ON engineered_features FOR ALL TO public USING (user_id = current_setting('app.current_user_id', true)); -- Feature complexity metrics policies DROP POLICY IF EXISTS feature_complexity_metrics_isolation ON feature_complexity_metrics; CREATE POLICY feature_complexity_metrics_isolation ON feature_complexity_metrics FOR ALL TO public USING (user_id = current_setting('app.current_user_id', true)); -- Feature validations policies DROP POLICY IF EXISTS feature_validations_isolation ON feature_validations; CREATE POLICY feature_validations_isolation ON feature_validations FOR ALL TO public USING (user_id = current_setting('app.current_user_id', true)); -- Grant necessary permissions GRANT ALL ON feature_engineering_plans TO public; GRANT ALL ON engineered_features TO public; GRANT ALL ON feature_complexity_metrics TO public; GRANT ALL ON feature_validations TO public; GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO public;