File size: 3,967 Bytes
56c063a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- Hypothesis Testing MCP Server Database Schema
-- Creates tables for hypotheses and test results with user isolation

-- Enable UUID extension if not already enabled
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Hypotheses table
CREATE TABLE IF NOT EXISTS hypotheses (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id VARCHAR NOT NULL,
    dataset_id UUID NOT NULL,
    text TEXT NOT NULL,
    test_type VARCHAR NOT NULL CHECK (test_type IN ('chi_square', 't_test', 'anova', 'correlation')),
    variables JSONB NOT NULL DEFAULT '[]',
    expected_outcome TEXT NOT NULL,
    python_code TEXT,
    business_relevance TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Hypothesis results table
CREATE TABLE IF NOT EXISTS hypothesis_results (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    hypothesis_id UUID NOT NULL REFERENCES hypotheses(id) ON DELETE CASCADE,
    user_id VARCHAR NOT NULL,
    p_value FLOAT NOT NULL,
    is_significant BOOLEAN NOT NULL,
    confidence_interval JSONB, -- [lower, upper] bounds
    effect_size FLOAT,
    interpretation TEXT NOT NULL,
    test_statistic FLOAT,
    significance_level FLOAT DEFAULT 0.05,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Statistical test metadata table (for tracking test parameters and diagnostics)
CREATE TABLE IF NOT EXISTS test_metadata (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    hypothesis_result_id UUID NOT NULL REFERENCES hypothesis_results(id) ON DELETE CASCADE,
    user_id VARCHAR NOT NULL,
    test_parameters JSONB DEFAULT '{}', -- Test-specific parameters
    diagnostics JSONB DEFAULT '{}', -- Test diagnostics (e.g., assumptions checks)
    sample_size INTEGER,
    degrees_of_freedom INTEGER,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_hypotheses_user_id ON hypotheses(user_id);
CREATE INDEX IF NOT EXISTS idx_hypotheses_dataset_id ON hypotheses(dataset_id);
CREATE INDEX IF NOT EXISTS idx_hypotheses_test_type ON hypotheses(test_type);
CREATE INDEX IF NOT EXISTS idx_hypotheses_created_at ON hypotheses(created_at);

CREATE INDEX IF NOT EXISTS idx_hypothesis_results_hypothesis_id ON hypothesis_results(hypothesis_id);
CREATE INDEX IF NOT EXISTS idx_hypothesis_results_user_id ON hypothesis_results(user_id);
CREATE INDEX IF NOT EXISTS idx_hypothesis_results_is_significant ON hypothesis_results(is_significant);
CREATE INDEX IF NOT EXISTS idx_hypothesis_results_created_at ON hypothesis_results(created_at);

CREATE INDEX IF NOT EXISTS idx_test_metadata_hypothesis_result_id ON test_metadata(hypothesis_result_id);
CREATE INDEX IF NOT EXISTS idx_test_metadata_user_id ON test_metadata(user_id);

-- Enable Row Level Security (RLS)
ALTER TABLE hypotheses ENABLE ROW LEVEL SECURITY;
ALTER TABLE hypothesis_results ENABLE ROW LEVEL SECURITY;
ALTER TABLE test_metadata ENABLE ROW LEVEL SECURITY;

-- Create RLS policies for user isolation
-- Users can only access their own data

-- Hypotheses policies
DROP POLICY IF EXISTS hypotheses_isolation ON hypotheses;
CREATE POLICY hypotheses_isolation ON hypotheses
    FOR ALL TO public
    USING (user_id = current_setting('app.current_user_id', true));

-- Hypothesis results policies
DROP POLICY IF EXISTS hypothesis_results_isolation ON hypothesis_results;
CREATE POLICY hypothesis_results_isolation ON hypothesis_results
    FOR ALL TO public
    USING (user_id = current_setting('app.current_user_id', true));

-- Test metadata policies
DROP POLICY IF EXISTS test_metadata_isolation ON test_metadata;
CREATE POLICY test_metadata_isolation ON test_metadata
    FOR ALL TO public
    USING (user_id = current_setting('app.current_user_id', true));

-- Grant necessary permissions
GRANT USAGE ON SCHEMA public TO public;
GRANT ALL ON ALL TABLES IN SCHEMA public TO public;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO public;