-- Preprocessing MCP Server Database Schema -- Creates tables for preprocessing plans and feature mappings with user isolation -- Enable UUID extension if not already enabled CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -- Preprocessing plans table CREATE TABLE IF NOT EXISTS preprocessing_plans ( id UUID PRIMARY KEY, dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE, user_id VARCHAR NOT NULL, scaling_operations JSONB DEFAULT '[]', encoding_operations JSONB DEFAULT '[]', feature_selection JSONB DEFAULT '[]', target_column VARCHAR, created_at TIMESTAMP DEFAULT NOW() ); -- Feature mappings table CREATE TABLE IF NOT EXISTS feature_mappings ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), preprocessed_dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE, user_id VARCHAR NOT NULL, original_name VARCHAR NOT NULL, transformed_name VARCHAR NOT NULL, transformation_type VARCHAR NOT NULL, parameters JSONB DEFAULT '{}', created_at TIMESTAMP DEFAULT NOW() ); -- Preprocessing validation results table CREATE TABLE IF NOT EXISTS preprocessing_validations ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), original_dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE, preprocessed_dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE, user_id VARCHAR NOT NULL, is_valid BOOLEAN DEFAULT true, warnings JSONB DEFAULT '[]', errors JSONB DEFAULT '[]', statistics JSONB DEFAULT '{}', validated_at TIMESTAMP DEFAULT NOW() ); -- Create indexes for better performance CREATE INDEX IF NOT EXISTS idx_preprocessing_plans_dataset_id ON preprocessing_plans(dataset_id); CREATE INDEX IF NOT EXISTS idx_preprocessing_plans_user_id ON preprocessing_plans(user_id); CREATE INDEX IF NOT EXISTS idx_feature_mappings_preprocessed_dataset_id ON feature_mappings(preprocessed_dataset_id); CREATE INDEX IF NOT EXISTS idx_feature_mappings_user_id ON feature_mappings(user_id); CREATE INDEX IF NOT EXISTS idx_preprocessing_validations_user_id ON preprocessing_validations(user_id); CREATE INDEX IF NOT EXISTS idx_preprocessing_validations_preprocessed_dataset_id ON preprocessing_validations(preprocessed_dataset_id); -- Enable Row Level Security (RLS) ALTER TABLE preprocessing_plans ENABLE ROW LEVEL SECURITY; ALTER TABLE feature_mappings ENABLE ROW LEVEL SECURITY; ALTER TABLE preprocessing_validations ENABLE ROW LEVEL SECURITY; -- Create RLS policies for user isolation -- Users can only access their own data -- Preprocessing plans policies DROP POLICY IF EXISTS preprocessing_plans_isolation ON preprocessing_plans; CREATE POLICY preprocessing_plans_isolation ON preprocessing_plans FOR ALL TO public USING (user_id = current_setting('app.current_user_id', true)); -- Feature mappings policies DROP POLICY IF EXISTS feature_mappings_isolation ON feature_mappings; CREATE POLICY feature_mappings_isolation ON feature_mappings FOR ALL TO public USING (user_id = current_setting('app.current_user_id', true)); -- Preprocessing validations policies DROP POLICY IF EXISTS preprocessing_validations_isolation ON preprocessing_validations; CREATE POLICY preprocessing_validations_isolation ON preprocessing_validations FOR ALL TO public USING (user_id = current_setting('app.current_user_id', true)); -- Grant necessary permissions GRANT ALL ON preprocessing_plans TO public; GRANT ALL ON feature_mappings TO public; GRANT ALL ON preprocessing_validations TO public; GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO public;