-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_database.py
More file actions
562 lines (461 loc) · 21.6 KB
/
Copy pathpostgres_database.py
File metadata and controls
562 lines (461 loc) · 21.6 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import psycopg2
import psycopg2.extras
from psycopg2.extras import RealDictCursor
import os
import uuid
from typing import Optional, List, Dict, Any
from datetime import datetime
import json
from urllib.parse import urlparse, parse_qs
class PostgreSQLCICDFixerDB:
def __init__(self, database_url: str = None):
if database_url is None:
database_url = os.getenv('DATABASE_URL')
if database_url:
self.database_url = self.fix_database_url(database_url)
else:
self.database_url = None
try:
if self.database_url:
self.init_database()
else:
print("⚠️ No DATABASE_URL provided, skipping database initialization")
except Exception as e:
print(f"⚠️ Database connection failed during startup: {e}")
print("🔄 App will continue without database (some features may be limited)")
self.database_url = None
def fix_database_url(self, url: str) -> str:
"""Fix common issues with cloud PostgreSQL URLs for Render deployment"""
try:
parsed = urlparse(url)
fixed_url = f"postgresql://{parsed.username}:{parsed.password}@{parsed.hostname}:{parsed.port or 5432}{parsed.path}"
if 'sslmode' not in url.lower():
fixed_url += "?sslmode=require"
elif 'sslmode' in url.lower():
query_params = []
if parsed.query:
for param in parsed.query.split('&'):
if '=' in param:
key, value = param.split('=', 1)
if key.lower() == 'sslmode':
query_params.append(f"sslmode={value}")
else:
continue
if query_params:
fixed_url += "?" + "&".join(query_params)
else:
fixed_url += "?sslmode=require"
print(f"🔧 Fixed database URL format for cloud deployment")
return fixed_url
except Exception as e:
print(f"⚠️ Error fixing database URL: {e}")
print(f"🔄 Using original URL: {url}")
return url
def get_connection(self):
"""Get a database connection."""
return psycopg2.connect(self.database_url)
def test_connection(self):
"""Test the database connection."""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT 1")
return True
except Exception:
return False
def init_database(self):
"""Initialize the PostgreSQL database with required tables."""
try:
if not self.test_connection():
raise Exception("Cannot connect to database")
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS workflow_runs (
id SERIAL PRIMARY KEY,
repo_name VARCHAR(255) NOT NULL,
owner VARCHAR(255) NOT NULL,
run_id BIGINT NOT NULL,
workflow_name TEXT,
status VARCHAR(50) NOT NULL,
conclusion VARCHAR(50),
error_log TEXT,
suggested_fix TEXT,
fix_status VARCHAR(50) DEFAULT 'pending',
confidence_score FLOAT,
error_category VARCHAR(100),
fix_complexity VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(repo_name, owner, run_id)
)
""")
try:
cursor.execute("""
ALTER TABLE workflow_runs
ADD COLUMN IF NOT EXISTS confidence_score FLOAT,
ADD COLUMN IF NOT EXISTS error_category VARCHAR(100),
ADD COLUMN IF NOT EXISTS fix_complexity VARCHAR(50),
ADD COLUMN IF NOT EXISTS analysis_result TEXT
""")
except Exception as e:
pass
cursor.execute("""
CREATE TABLE IF NOT EXISTS portia_plans (
id SERIAL PRIMARY KEY,
plan_id VARCHAR(255) UNIQUE NOT NULL,
workflow_run_id INTEGER,
status VARCHAR(50) NOT NULL,
steps_completed INTEGER DEFAULT 0,
total_steps INTEGER DEFAULT 0,
error_message TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (workflow_run_id) REFERENCES workflow_runs (id)
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS clarifications (
id SERIAL PRIMARY KEY,
plan_id VARCHAR(255) NOT NULL,
question TEXT NOT NULL,
response TEXT,
response_type VARCHAR(50),
status VARCHAR(50) DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (plan_id) REFERENCES portia_plans (plan_id)
)
""")
conn.commit()
print("✅ Database tables created successfully")
except Exception as e:
print(f"❌ Database initialization failed: {e}")
raise
def is_available(self) -> bool:
"""Check if database is available for operations"""
return self.database_url is not None and self.test_connection()
def insert_workflow_run(self, run_data: Dict[str, Any]) -> int:
"""Insert a new workflow run record."""
if not self.is_available():
print("⚠️ Database not available, skipping workflow run insertion")
return -1
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO workflow_runs
(repo_name, owner, run_id, workflow_name, status, conclusion, error_log)
VALUES (%s, %s, %s, %s, %s, %s, %s)
ON CONFLICT (repo_name, owner, run_id)
DO UPDATE SET
workflow_name = EXCLUDED.workflow_name,
status = EXCLUDED.status,
conclusion = EXCLUDED.conclusion,
error_log = EXCLUDED.error_log,
updated_at = CURRENT_TIMESTAMP
RETURNING id
""", (
run_data.get('repo_name'),
run_data.get('owner'),
run_data.get('run_id'),
run_data.get('workflow_name'),
run_data.get('status'),
run_data.get('conclusion'),
run_data.get('error_log')
))
result = cursor.fetchone()
return result[0] if result else None
except Exception as e:
print(f"⚠️ Error inserting workflow run: {e}")
return -1
def update_workflow_run_fix(self, run_id: int, suggested_fix: str, fix_status: str = 'suggested'):
"""Update workflow run with suggested fix."""
if not self.is_available():
print("⚠️ Database not available, skipping workflow run update")
return False
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
UPDATE workflow_runs
SET suggested_fix = %s, fix_status = %s, updated_at = CURRENT_TIMESTAMP
WHERE id = %s
""", (suggested_fix, fix_status, run_id))
conn.commit()
return True
except Exception as e:
print(f"⚠️ Error updating workflow run: {e}")
return False
def get_workflow_runs(self, limit: int = 50) -> List[Dict[str, Any]]:
"""Get all workflow runs with pagination."""
if not self.is_available():
print("⚠️ Database not available, returning empty workflow runs list")
return []
try:
with self.get_connection() as conn:
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""
SELECT * FROM workflow_runs
ORDER BY created_at DESC
LIMIT %s
""", (limit,))
return [dict(row) for row in cursor.fetchall()]
except Exception as e:
print(f"⚠️ Error fetching workflow runs: {e}")
return []
def get_workflow_run_by_id(self, run_id: int) -> Optional[Dict[str, Any]]:
"""Get a specific workflow run by ID."""
with self.get_connection() as conn:
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""
SELECT * FROM workflow_runs WHERE id = %s
""", (run_id,))
result = cursor.fetchone()
return dict(result) if result else None
def get_workflow_run_by_run_id(self, repo_name: str, owner: str, run_id: int) -> Optional[Dict[str, Any]]:
"""Get workflow run by run ID."""
with self.get_connection() as conn:
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""
SELECT * FROM workflow_runs
WHERE repo_name = %s AND owner = %s AND run_id = %s
""", (repo_name, owner, run_id))
result = cursor.fetchone()
return dict(result) if result else None
def update_fix_status(self, run_id: int, status: str):
"""Update the fix status of a workflow run."""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
UPDATE workflow_runs
SET fix_status = %s, updated_at = CURRENT_TIMESTAMP
WHERE id = %s
""", (status, run_id))
conn.commit()
def update_fix_application_result(self, run_id: int, status: str, pr_url: str = None, branch_name: str = None, error_message: str = None):
"""Update the fix status with application results."""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
UPDATE workflow_runs
SET
fix_status = %s,
pr_url = %s,
fix_branch = %s,
fix_error = %s,
updated_at = CURRENT_TIMESTAMP
WHERE id = %s
""", (status, pr_url, branch_name, error_message, run_id))
conn.commit()
def add_missing_columns_if_needed(self):
"""Add missing columns for fix application tracking."""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
ALTER TABLE workflow_runs
ADD COLUMN IF NOT EXISTS pr_url TEXT,
ADD COLUMN IF NOT EXISTS fix_branch TEXT,
ADD COLUMN IF NOT EXISTS fix_error TEXT
""")
conn.commit()
print("✅ Added missing columns for fix tracking")
except Exception as e:
print(f"⚠️ Error adding columns (may already exist): {e}")
def store_fix_metadata(self, failure_id: str, metadata: Dict[str, Any]) -> None:
"""Store additional metadata for a fix suggestion."""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
UPDATE workflow_runs
SET
confidence_score = %s,
error_category = %s,
fix_complexity = %s,
updated_at = CURRENT_TIMESTAMP
WHERE id = %s
""", (
metadata.get('confidence_score'),
metadata.get('error_category'),
metadata.get('fix_complexity'),
failure_id
))
conn.commit()
print(f"✅ Stored fix metadata for failure {failure_id}")
except Exception as e:
print(f"❌ Error storing fix metadata: {e}")
def insert_portia_plan(self, plan_data: Dict[str, Any]) -> int:
"""Insert a new Portia plan record."""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO portia_plans
(plan_id, workflow_run_id, status, total_steps)
VALUES (%s, %s, %s, %s)
RETURNING id
""", (
plan_data.get('plan_id'),
plan_data.get('workflow_run_id'),
plan_data.get('status', 'active'),
plan_data.get('total_steps', 0)
))
result = cursor.fetchone()
conn.commit()
return result[0] if result else None
def update_portia_plan(self, plan_id: str, updates: Dict[str, Any]):
"""Update a Portia plan record."""
with self.get_connection() as conn:
cursor = conn.cursor()
set_clauses = []
values = []
for key, value in updates.items():
if key in ['status', 'steps_completed', 'total_steps', 'error_message']:
set_clauses.append(f"{key} = %s")
values.append(value)
if set_clauses:
set_clauses.append("updated_at = CURRENT_TIMESTAMP")
values.append(plan_id)
query = f"UPDATE portia_plans SET {', '.join(set_clauses)} WHERE plan_id = %s"
cursor.execute(query, values)
conn.commit()
def get_portia_plans(self) -> List[Dict[str, Any]]:
"""Get all Portia plans."""
with self.get_connection() as conn:
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""
SELECT * FROM portia_plans
ORDER BY created_at DESC
""")
return [dict(row) for row in cursor.fetchall()]
def insert_clarification(self, clarification_data: Dict[str, Any]) -> int:
"""Insert a new clarification request."""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO clarifications
(plan_id, question, response_type)
VALUES (%s, %s, %s)
RETURNING id
""", (
clarification_data.get('plan_id'),
clarification_data.get('question'),
clarification_data.get('response_type', 'approval')
))
result = cursor.fetchone()
conn.commit()
return result[0] if result else None
def update_clarification(self, clarification_id: int, response: str, status: str = 'resolved'):
"""Update a clarification with response."""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
UPDATE clarifications
SET response = %s, status = %s, updated_at = CURRENT_TIMESTAMP
WHERE id = %s
""", (response, status, clarification_id))
conn.commit()
def get_pending_clarifications(self) -> List[Dict[str, Any]]:
"""Get all pending clarifications."""
with self.get_connection() as conn:
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""
SELECT c.*, w.repo_name, w.owner, w.run_id
FROM clarifications c
JOIN portia_plans p ON c.plan_id = p.plan_id
JOIN workflow_runs w ON p.workflow_run_id = w.id
WHERE c.status = 'pending'
ORDER BY c.created_at ASC
""")
return [dict(row) for row in cursor.fetchall()]
def get_stats(self) -> Dict[str, Any]:
"""Get database statistics."""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM workflow_runs")
total_runs = cursor.fetchone()[0]
cursor.execute("""
SELECT fix_status, COUNT(*)
FROM workflow_runs
GROUP BY fix_status
""")
status_counts = dict(cursor.fetchall())
cursor.execute("""
SELECT COUNT(*) FROM workflow_runs
WHERE created_at > NOW() - INTERVAL '24 hours'
""")
recent_runs = cursor.fetchone()[0]
return {
'total_runs': total_runs,
'status_counts': status_counts,
'recent_runs': recent_runs
}
def store_failure(self, failure_data: Dict[str, Any]) -> str:
"""Store failure data and return failure ID"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO workflow_runs (
repo_name, owner, run_id, workflow_name,
status, conclusion, error_log, created_at
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
ON CONFLICT (repo_name, owner, run_id)
DO UPDATE SET
status = EXCLUDED.status,
conclusion = EXCLUDED.conclusion,
error_log = EXCLUDED.error_log,
updated_at = NOW()
RETURNING id
""", (
failure_data.get('repo', '') or failure_data.get('repository', {}).get('name', ''),
failure_data.get('owner', '') or failure_data.get('repository', {}).get('owner', {}).get('login', ''),
failure_data.get('run_id', 0),
failure_data.get('workflow_name', ''),
failure_data.get('status', 'failed'),
failure_data.get('conclusion', 'failure'),
json.dumps(failure_data),
datetime.utcnow()
))
result = cursor.fetchone()
return str(result[0]) if result else None
def store_analysis(self, failure_id: str, analysis_result: Dict[str, Any]):
"""Store analysis result for a failure"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
UPDATE workflow_runs
SET analysis_result = %s, updated_at = NOW()
WHERE id = %s
""", (json.dumps(analysis_result), failure_id))
def get_pending_fixes(self) -> List[Dict[str, Any]]:
"""Get all pending fixes that require human approval"""
with self.get_connection() as conn:
cursor = conn.cursor(cursor_factory=RealDictCursor)
cursor.execute("""
SELECT
id,
repo_name,
owner,
run_id,
workflow_name,
suggested_fix,
fix_status,
confidence_score,
error_category,
fix_complexity,
created_at,
updated_at
FROM workflow_runs
WHERE fix_status IN ('pending', 'suggested', 'waiting_approval')
AND suggested_fix IS NOT NULL
ORDER BY created_at DESC
""")
return [dict(row) for row in cursor.fetchall()]
def get_fix(self, fix_id: str) -> Optional[Dict[str, Any]]:
"""Get a specific fix by ID"""
with self.get_connection() as conn:
cursor = conn.cursor(cursor_factory=RealDictCursor)
cursor.execute("SELECT * FROM workflow_runs WHERE id = %s", (fix_id,))
row = cursor.fetchone()
return dict(row) if row else None