|
5 | 5 | for POC/demo purposes. Maps real Student_GUIDs to fake SIS IDs. |
6 | 6 | """ |
7 | 7 |
|
8 | | -import psycopg2 |
9 | | -from psycopg2.extras import RealDictCursor |
10 | | -from .db_config import DB_CONFIG |
| 8 | +from .db_utils import get_connection |
11 | 9 |
|
12 | 10 |
|
13 | 11 | def seed_guid_sis_map(): |
14 | 12 | """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() |
23 | 14 | cursor = connection.cursor() |
24 | 15 |
|
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") |
33 | 25 |
|
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()] |
42 | 34 |
|
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") |
48 | 41 |
|
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 | + ) |
51 | 48 |
|
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)})") |
58 | 51 |
|
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") |
61 | 56 |
|
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 |
66 | 58 |
|
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() |
70 | 67 |
|
71 | 68 |
|
72 | 69 | if __name__ == "__main__": |
|
0 commit comments