Skip to content

Commit 2e7b9e0

Browse files
committed
fix(#78): use get_connection helper and add error handling to seed script
1 parent 7e2bd03 commit 2e7b9e0

1 file changed

Lines changed: 46 additions & 49 deletions

File tree

operations/seed_guid_sis_map.py

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,65 @@
55
for POC/demo purposes. Maps real Student_GUIDs to fake SIS IDs.
66
"""
77

8-
import psycopg2
9-
from psycopg2.extras import RealDictCursor
10-
from .db_config import DB_CONFIG
8+
from .db_utils import get_connection
119

1210

1311
def seed_guid_sis_map():
1412
"""Create guid_sis_map table and seed with demo data."""
15-
connection = psycopg2.connect(
16-
host=DB_CONFIG['host'],
17-
user=DB_CONFIG['user'],
18-
password=DB_CONFIG['password'],
19-
dbname=DB_CONFIG['database'],
20-
port=DB_CONFIG['port'],
21-
cursor_factory=RealDictCursor
22-
)
13+
connection = get_connection()
2314
cursor = connection.cursor()
2415

25-
# Create table
26-
cursor.execute("""
27-
CREATE TABLE IF NOT EXISTS guid_sis_map (
28-
student_guid TEXT PRIMARY KEY,
29-
sis_id TEXT NOT NULL
30-
);
31-
""")
32-
print("✓ guid_sis_map table created/verified")
16+
try:
17+
# Create table
18+
cursor.execute("""
19+
CREATE TABLE IF NOT EXISTS guid_sis_map (
20+
student_guid TEXT PRIMARY KEY,
21+
sis_id TEXT NOT NULL
22+
);
23+
""")
24+
print("✓ guid_sis_map table created/verified")
3325

34-
# Pick ~20 random GUIDs from student_level_with_predictions
35-
cursor.execute("""
36-
SELECT "Student_GUID"
37-
FROM student_level_with_predictions
38-
ORDER BY RANDOM()
39-
LIMIT 20
40-
""")
41-
guids = [row['Student_GUID'] for row in cursor.fetchall()]
26+
# Pick ~20 random GUIDs from student_level_with_predictions
27+
cursor.execute("""
28+
SELECT "Student_GUID"
29+
FROM student_level_with_predictions
30+
ORDER BY RANDOM()
31+
LIMIT 20
32+
""")
33+
guids = [row['Student_GUID'] for row in cursor.fetchall()]
4234

43-
if not guids:
44-
print("✗ No students found in student_level_with_predictions")
45-
cursor.close()
46-
connection.close()
47-
return False
35+
if not guids:
36+
print("✗ No students found in student_level_with_predictions")
37+
return False
38+
39+
# Clear existing demo data and insert fresh mappings
40+
cursor.execute("DELETE FROM guid_sis_map")
4841

49-
# Clear existing demo data and insert fresh mappings
50-
cursor.execute("DELETE FROM guid_sis_map")
42+
for i, guid in enumerate(guids, start=100001):
43+
sis_id = f"BSC-{i}"
44+
cursor.execute(
45+
"INSERT INTO guid_sis_map (student_guid, sis_id) VALUES (%s, %s)",
46+
(guid, sis_id)
47+
)
5148

52-
for i, guid in enumerate(guids, start=100001):
53-
sis_id = f"BSC-{i}"
54-
cursor.execute(
55-
"INSERT INTO guid_sis_map (student_guid, sis_id) VALUES (%s, %s)",
56-
(guid, sis_id)
57-
)
49+
connection.commit()
50+
print(f"✓ Seeded {len(guids)} GUID → SIS ID mappings (BSC-100001 .. BSC-{100000 + len(guids)})")
5851

59-
connection.commit()
60-
print(f"✓ Seeded {len(guids)} GUID → SIS ID mappings (BSC-100001 .. BSC-{100000 + len(guids)})")
52+
# Verify
53+
cursor.execute("SELECT COUNT(*) AS count FROM guid_sis_map")
54+
count = cursor.fetchone()['count']
55+
print(f"✓ Verified: {count} records in guid_sis_map")
6156

62-
# Verify
63-
cursor.execute("SELECT COUNT(*) AS count FROM guid_sis_map")
64-
count = cursor.fetchone()['count']
65-
print(f"✓ Verified: {count} records in guid_sis_map")
57+
return True
6658

67-
cursor.close()
68-
connection.close()
69-
return True
59+
except Exception as e:
60+
connection.rollback()
61+
print(f"✗ Failed to seed guid_sis_map: {e}")
62+
return False
63+
64+
finally:
65+
cursor.close()
66+
connection.close()
7067

7168

7269
if __name__ == "__main__":

0 commit comments

Comments
 (0)