-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sql
More file actions
164 lines (139 loc) · 5.8 KB
/
Copy pathinit.sql
File metadata and controls
164 lines (139 loc) · 5.8 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- IntentForge Database Initialization
-- Creates core tables for intent caching and code storage
-- =============================================================================
-- Extensions
-- =============================================================================
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pg_trgm"; -- For text search
-- =============================================================================
-- Intent Cache Table
-- =============================================================================
CREATE TABLE IF NOT EXISTS intent_cache (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
fingerprint VARCHAR(32) UNIQUE NOT NULL,
description TEXT NOT NULL,
intent_type VARCHAR(50) NOT NULL,
target_platform VARCHAR(50) NOT NULL,
context JSONB DEFAULT '{}',
generated_code TEXT,
language VARCHAR(20),
validation_passed BOOLEAN DEFAULT FALSE,
hit_count INTEGER DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP WITH TIME ZONE
);
CREATE INDEX idx_intent_cache_fingerprint ON intent_cache(fingerprint);
CREATE INDEX idx_intent_cache_created_at ON intent_cache(created_at);
CREATE INDEX idx_intent_cache_intent_type ON intent_cache(intent_type);
-- =============================================================================
-- Generated Code History
-- =============================================================================
CREATE TABLE IF NOT EXISTS code_history (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
intent_id UUID REFERENCES intent_cache(id),
code TEXT NOT NULL,
language VARCHAR(20) NOT NULL,
version INTEGER DEFAULT 1,
validation_result JSONB,
execution_result JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_code_history_intent_id ON code_history(intent_id);
-- =============================================================================
-- Schema Registry
-- =============================================================================
CREATE TABLE IF NOT EXISTS schema_registry (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name VARCHAR(100) UNIQUE NOT NULL,
schema_type VARCHAR(50) NOT NULL,
schema_definition JSONB NOT NULL,
version INTEGER DEFAULT 1,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- =============================================================================
-- Usage Analytics
-- =============================================================================
CREATE TABLE IF NOT EXISTS usage_analytics (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
client_id VARCHAR(100),
intent_type VARCHAR(50),
target_platform VARCHAR(50),
processing_time_ms FLOAT,
cache_hit BOOLEAN DEFAULT FALSE,
validation_passed BOOLEAN,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_usage_analytics_created_at ON usage_analytics(created_at);
CREATE INDEX idx_usage_analytics_client_id ON usage_analytics(client_id);
-- =============================================================================
-- Triggers
-- =============================================================================
-- Auto-update updated_at
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_intent_cache_updated_at
BEFORE UPDATE ON intent_cache
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_schema_registry_updated_at
BEFORE UPDATE ON schema_registry
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Increment hit count on cache access
CREATE OR REPLACE FUNCTION increment_cache_hit()
RETURNS TRIGGER AS $$
BEGIN
UPDATE intent_cache
SET hit_count = hit_count + 1
WHERE fingerprint = NEW.fingerprint;
RETURN NEW;
END;
$$ language 'plpgsql';
-- =============================================================================
-- Sample Data (for testing)
-- =============================================================================
-- Insert some default schemas
INSERT INTO schema_registry (name, schema_type, schema_definition) VALUES
('intent_request', 'core', '{"type": "object", "required": ["description"]}'),
('form_data', 'form', '{"type": "object", "required": ["form_id", "fields"]}'),
('sql_query', 'database', '{"type": "object", "required": ["operation"]}')
ON CONFLICT (name) DO NOTHING;
-- =============================================================================
-- Views
-- =============================================================================
-- Cache statistics
CREATE OR REPLACE VIEW cache_stats AS
SELECT
intent_type,
target_platform,
COUNT(*) as total_cached,
SUM(hit_count) as total_hits,
AVG(hit_count) as avg_hits,
MIN(created_at) as first_cached,
MAX(updated_at) as last_accessed
FROM intent_cache
GROUP BY intent_type, target_platform;
-- Daily usage
CREATE OR REPLACE VIEW daily_usage AS
SELECT
DATE(created_at) as date,
intent_type,
COUNT(*) as requests,
SUM(CASE WHEN cache_hit THEN 1 ELSE 0 END) as cache_hits,
AVG(processing_time_ms) as avg_time_ms
FROM usage_analytics
GROUP BY DATE(created_at), intent_type
ORDER BY date DESC;
-- =============================================================================
-- Grants (if using separate app user)
-- =============================================================================
-- GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO intentforge_app;
-- GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO intentforge_app;