wu981526092 commited on
Commit
9e37d03
Β·
1 Parent(s): 9e60d50

πŸ”§ Fix Sample Data Insertion Import Error

Browse files

βœ… Problem fixed:
β€’ Fixed 'cannot import name get_session' error in sample data insertion
β€’ Changed from non-existent get_session to SessionLocal
β€’ Proper session management with try/except/finally
β€’ Removed duplicate session.commit() to prevent conflicts

🎯 Root cause:
β€’ init_db.py was trying to import 'get_session' which doesn't exist
β€’ Backend database only provides 'get_db()' (generator) and 'SessionLocal' (factory)
β€’ Incorrect session management caused transaction conflicts

⚑ Technical changes:
β€’ backend/database/init_db.py: Use SessionLocal() instead of get_session()
β€’ backend/database/sample_data.py: Remove session.commit(), let caller manage
β€’ Proper transaction handling with rollback on errors

πŸš€ Expected result:
β€’ Empty databases will now successfully get 3 sample traces + KGs
β€’ Users will see working examples immediately on first visit
β€’ No more 'Database initialization completed without sample data' message

backend/database/README_sample_data.md CHANGED
@@ -7,19 +7,23 @@ The sample data system automatically inserts curated examples into new AgentGrap
7
  ## Features
8
 
9
  ### πŸ“Š Automatic Insertion
 
10
  - Triggered when initializing an empty database
11
  - Non-destructive: skips insertion if existing data is found
12
  - Logs all operations for transparency
13
 
14
  ### 🎯 Curated Examples
 
15
  The system includes 3 carefully selected examples showing different complexity levels:
16
 
17
  1. **Basic Q&A** (Simple)
 
18
  - Type: `conversation`
19
  - Example: Season pass cost calculation
20
  - 6 entities, 5 relations in knowledge graph
21
 
22
  2. **Research Task** (Medium)
 
23
  - Type: `research`
24
  - Example: Location and business hours research
25
  - 6 entities, 6 relations in knowledge graph
@@ -30,7 +34,9 @@ The system includes 3 carefully selected examples showing different complexity l
30
  - 10 entities, 16 relations in knowledge graph
31
 
32
  ### πŸ•ΈοΈ Knowledge Graph Examples
 
33
  Each trace comes with a pre-generated knowledge graph showing:
 
34
  - Agent interactions and roles
35
  - Task decomposition
36
  - Information flow
@@ -40,17 +46,20 @@ Each trace comes with a pre-generated knowledge graph showing:
40
  ## Technical Implementation
41
 
42
  ### Files
 
43
  - `backend/database/sample_data.py` - Contains sample data and insertion logic
44
  - `backend/database/init_db.py` - Modified to call sample data insertion
45
  - `backend/database/README_sample_data.md` - This documentation
46
 
47
  ### Database Integration
 
48
  - Insertion happens after table creation in `init_database()`
49
  - Only triggers when `trace_count == 0` (empty database)
50
  - Uses existing `save_trace()` and `save_knowledge_graph()` functions
51
  - Full transaction support with rollback on errors
52
 
53
  ### Data Structure
 
54
  ```python
55
  SAMPLE_TRACES = [
56
  {
@@ -79,12 +88,15 @@ SAMPLE_KNOWLEDGE_GRAPHS = [
79
  ## Usage
80
 
81
  ### Automatic (Default)
 
82
  Sample data is inserted automatically when:
 
83
  - Creating a new database
84
  - Resetting an existing database with `--reset --force`
85
  - Database has zero traces
86
 
87
  ### Manual Control
 
88
  ```python
89
  from backend.database.sample_data import insert_sample_data, get_sample_data_info
90
 
@@ -99,7 +111,9 @@ with get_session() as session:
99
  ```
100
 
101
  ### Disabling Sample Data
 
102
  To disable automatic sample data insertion, modify `init_db.py`:
 
103
  ```python
104
  # Comment out this section in init_database():
105
  # if trace_count == 0:
@@ -123,6 +137,7 @@ To disable automatic sample data insertion, modify `init_db.py`:
123
  ## Maintenance
124
 
125
  To update sample data:
 
126
  1. Modify `SAMPLE_TRACES` and `SAMPLE_KNOWLEDGE_GRAPHS` in `sample_data.py`
127
  2. Ensure trace_index links are correct between traces and KGs
128
  3. Test with a fresh database initialization
@@ -131,17 +146,20 @@ To update sample data:
131
  ## Troubleshooting
132
 
133
  ### Sample Data Not Appearing
 
134
  - Check logs for "Sample data already exists, skipping insertion"
135
  - Verify database is actually empty: `SELECT COUNT(*) FROM traces;`
136
  - Force insertion manually with `force_insert=True`
137
 
138
  ### Insertion Errors
 
139
  - Check logs for specific error messages
140
  - Verify database schema is up to date
141
  - Ensure all required tables exist
142
  - Check for foreign key constraint issues
143
 
144
  ### Performance Impact
 
145
  - Sample data insertion adds ~2-3 seconds to database initialization
146
  - Total size: ~4KB of text content + ~15KB of JSON data
147
  - Negligible impact on production systems
 
7
  ## Features
8
 
9
  ### πŸ“Š Automatic Insertion
10
+
11
  - Triggered when initializing an empty database
12
  - Non-destructive: skips insertion if existing data is found
13
  - Logs all operations for transparency
14
 
15
  ### 🎯 Curated Examples
16
+
17
  The system includes 3 carefully selected examples showing different complexity levels:
18
 
19
  1. **Basic Q&A** (Simple)
20
+
21
  - Type: `conversation`
22
  - Example: Season pass cost calculation
23
  - 6 entities, 5 relations in knowledge graph
24
 
25
  2. **Research Task** (Medium)
26
+
27
  - Type: `research`
28
  - Example: Location and business hours research
29
  - 6 entities, 6 relations in knowledge graph
 
34
  - 10 entities, 16 relations in knowledge graph
35
 
36
  ### πŸ•ΈοΈ Knowledge Graph Examples
37
+
38
  Each trace comes with a pre-generated knowledge graph showing:
39
+
40
  - Agent interactions and roles
41
  - Task decomposition
42
  - Information flow
 
46
  ## Technical Implementation
47
 
48
  ### Files
49
+
50
  - `backend/database/sample_data.py` - Contains sample data and insertion logic
51
  - `backend/database/init_db.py` - Modified to call sample data insertion
52
  - `backend/database/README_sample_data.md` - This documentation
53
 
54
  ### Database Integration
55
+
56
  - Insertion happens after table creation in `init_database()`
57
  - Only triggers when `trace_count == 0` (empty database)
58
  - Uses existing `save_trace()` and `save_knowledge_graph()` functions
59
  - Full transaction support with rollback on errors
60
 
61
  ### Data Structure
62
+
63
  ```python
64
  SAMPLE_TRACES = [
65
  {
 
88
  ## Usage
89
 
90
  ### Automatic (Default)
91
+
92
  Sample data is inserted automatically when:
93
+
94
  - Creating a new database
95
  - Resetting an existing database with `--reset --force`
96
  - Database has zero traces
97
 
98
  ### Manual Control
99
+
100
  ```python
101
  from backend.database.sample_data import insert_sample_data, get_sample_data_info
102
 
 
111
  ```
112
 
113
  ### Disabling Sample Data
114
+
115
  To disable automatic sample data insertion, modify `init_db.py`:
116
+
117
  ```python
118
  # Comment out this section in init_database():
119
  # if trace_count == 0:
 
137
  ## Maintenance
138
 
139
  To update sample data:
140
+
141
  1. Modify `SAMPLE_TRACES` and `SAMPLE_KNOWLEDGE_GRAPHS` in `sample_data.py`
142
  2. Ensure trace_index links are correct between traces and KGs
143
  3. Test with a fresh database initialization
 
146
  ## Troubleshooting
147
 
148
  ### Sample Data Not Appearing
149
+
150
  - Check logs for "Sample data already exists, skipping insertion"
151
  - Verify database is actually empty: `SELECT COUNT(*) FROM traces;`
152
  - Force insertion manually with `force_insert=True`
153
 
154
  ### Insertion Errors
155
+
156
  - Check logs for specific error messages
157
  - Verify database schema is up to date
158
  - Ensure all required tables exist
159
  - Check for foreign key constraint issues
160
 
161
  ### Performance Impact
162
+
163
  - Sample data insertion adds ~2-3 seconds to database initialization
164
  - Total size: ~4KB of text content + ~15KB of JSON data
165
  - Negligible impact on production systems
backend/database/init_db.py CHANGED
@@ -336,11 +336,18 @@ def init_database(reset=False, force=False):
336
  try:
337
  # Import here to avoid circular imports
338
  from .sample_data import insert_sample_data
339
- from . import get_session
340
 
341
  # Use SQLAlchemy session for sample data insertion
342
- with get_session() as session:
 
343
  results = insert_sample_data(session)
 
 
 
 
 
 
344
 
345
  if results["traces_inserted"] > 0 or results["knowledge_graphs_inserted"] > 0:
346
  logger.info(f"βœ… Sample data inserted successfully: {results['traces_inserted']} traces, {results['knowledge_graphs_inserted']} knowledge graphs")
 
336
  try:
337
  # Import here to avoid circular imports
338
  from .sample_data import insert_sample_data
339
+ from . import SessionLocal
340
 
341
  # Use SQLAlchemy session for sample data insertion
342
+ session = SessionLocal()
343
+ try:
344
  results = insert_sample_data(session)
345
+ session.commit()
346
+ except Exception as e:
347
+ session.rollback()
348
+ raise
349
+ finally:
350
+ session.close()
351
 
352
  if results["traces_inserted"] > 0 or results["knowledge_graphs_inserted"] > 0:
353
  logger.info(f"βœ… Sample data inserted successfully: {results['traces_inserted']} traces, {results['knowledge_graphs_inserted']} knowledge graphs")
backend/database/sample_data.py CHANGED
@@ -596,14 +596,13 @@ def insert_sample_data(session, force_insert=False):
596
  logger.error(error_msg)
597
  results["errors"].append(error_msg)
598
 
599
- session.commit()
600
  logger.info(f"Sample data insertion completed: {results}")
601
 
602
  except Exception as e:
603
- session.rollback()
604
  error_msg = f"Fatal error during sample data insertion: {str(e)}"
605
  logger.error(error_msg)
606
  results["errors"].append(error_msg)
 
607
 
608
  return results
609
 
 
596
  logger.error(error_msg)
597
  results["errors"].append(error_msg)
598
 
 
599
  logger.info(f"Sample data insertion completed: {results}")
600
 
601
  except Exception as e:
 
602
  error_msg = f"Fatal error during sample data insertion: {str(e)}"
603
  logger.error(error_msg)
604
  results["errors"].append(error_msg)
605
+ raise # Re-raise to trigger rollback in calling code
606
 
607
  return results
608