-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_db_schema.py
More file actions
159 lines (136 loc) · 5.07 KB
/
Copy pathtest_db_schema.py
File metadata and controls
159 lines (136 loc) · 5.07 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
#!/usr/bin/env python3
"""
Test module to diagnose database schema issues with market_data table.
This script:
1. Connects to the database
2. Tries to get the schema definition or sample record
3. Attempts various insertions with different field formats
4. Provides detailed error diagnostics
"""
import json
import logging
import sys
from datetime import datetime, timezone
from pathlib import Path
# Add project root to path
current_dir = Path(__file__).parent
sys.path.append(str(current_dir))
from src.database.client import DatabaseClient
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def inspect_db_schema():
"""Inspect the database schema to understand field names and types."""
db_client = DatabaseClient()
if not db_client.client:
logger.error("Failed to initialize database client.")
return False
# 1. Try to get one record to inspect schema
logger.info("Attempting to retrieve a sample market_data record...")
try:
# Try to select a single record to examine fields
response = db_client.client.table("market_data").select("*").limit(1).execute()
if response.data and len(response.data) > 0:
sample = response.data[0]
logger.info(f"Found sample record with fields: {list(sample.keys())}")
logger.info(f"Sample data: {sample}")
return sample
else:
logger.warning("No existing records found in market_data table.")
except Exception as e:
logger.error(f"Error querying market_data table: {str(e)}")
# 2. If no records, try to get the table schema
logger.info("Attempting to retrieve table schema information...")
try:
# Query schema information
response = db_client.client.rpc(
"get_schema_info", {"table_name": "market_data"}
).execute()
if response.data:
logger.info(f"Schema info: {response.data}")
return response.data
except Exception as e:
logger.error(f"Error querying schema: {str(e)}")
return None
def test_insertion_variants():
"""Test various field combinations to determine correct schema."""
db_client = DatabaseClient()
if not db_client.client:
logger.error("Failed to initialize database client.")
return
# Common fields that should be present in all variants
base_record = {
"asset_id": 1, # BTC
"timestamp": datetime.now(timezone.utc).isoformat(),
"interval": "1d",
"source": "test",
"additional_data": json.dumps({"test": True}),
}
# Test different field variants
variants = [
# Variant 1: Using price fields
{
**base_record,
"price": 50000.0,
"volume": 1000000.0,
"market_cap": 900000000.0,
},
# Variant 2: Using _usd suffix
{
**base_record,
"price_usd": 50000.0,
"volume_24h_usd": 1000000.0,
"market_cap_usd": 900000000.0,
},
# Variant 3: Using OHLC format
{
**base_record,
"open_price": 49000.0,
"high_price": 51000.0,
"low_price": 48000.0,
"close_price": 50000.0,
"volume": 1000000.0,
},
# Variant 4: Just minimal fields
{**base_record, "price": 50000.0},
]
# Try each variant
for i, variant in enumerate(variants):
logger.info(f"Testing variant {i+1} with fields: {list(variant.keys())}")
try:
# Try direct upsert to get more detailed error
response = (
db_client.client.table("market_data")
.upsert(variant, on_conflict="asset_id,timestamp,interval")
.execute()
)
logger.info(
f"Success! Variant {i+1} worked: {response.data if hasattr(response, 'data') else 'No response data'}"
)
return variant # Return first successful variant
except Exception as e:
if hasattr(e, "message"):
error_msg = e.message
elif hasattr(e, "details"):
error_msg = e.details
else:
error_msg = str(e)
logger.error(f"Variant {i+1} failed: {error_msg}")
return None
if __name__ == "__main__":
logger.info("Starting database schema inspection...")
# First try to get schema information
schema_info = inspect_db_schema()
# Then try different insertion variants
logger.info("\nTesting different field variations...")
successful_variant = test_insertion_variants()
if successful_variant:
logger.info(
f"\nSUCCESS: Found working field structure: {list(successful_variant.keys())}"
)
logger.info("Use these field names in your data population script.")
else:
logger.error("\nFAILED: Could not determine correct field structure.")
logger.info("Check database logs or contact your DBA for schema details.")