Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SQLAlchemy==2.0.39
53 changes: 53 additions & 0 deletions scripts/assign_missing_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
from sqlalchemy import create_engine, text
from dotenv import load_dotenv

# Load config.env file
load_dotenv("config.env")

# Load DB credentials
username = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
database_name = os.getenv("DB_NAME")

# PostgreSQL connection
connection_string = f"postgresql+psycopg2://{username}:{password}@{host}:{port}/{database_name}"
engine = create_engine(connection_string)

# Schema and table
schema_name = "raw"
table_name = "general_information_sheet"
id_column = "Student_id" # Case sensitive column name

with engine.connect() as conn:
# Step 1: Get max existing ID (use double quotes around column name)
max_id_result = conn.execute(
text(f'SELECT MAX("{id_column}") FROM {schema_name}.{table_name}')
)
max_id = max_id_result.scalar() or 0
next_id = max_id + 1

# Step 2: Get rows with NULL ID (double quotes around id_column)
null_id_rows = conn.execute(
text(f'SELECT ctid FROM {schema_name}.{table_name} WHERE "{id_column}" IS NULL')
).fetchall()

# Step 3: Update each row with a new ID
for row in null_id_rows:
ctid = row[0] # ctid is a unique row identifier in PostgreSQL
conn.execute(
text(f'''
UPDATE {schema_name}.{table_name}
SET "{id_column}" = :new_id
WHERE ctid = :ctid
'''),
{"new_id": next_id, "ctid": ctid}
)
print(f"Assigned ID {next_id} to row with CTID {ctid}")
next_id += 1

conn.commit()

print("✅ All missing IDs have been filled.")
6 changes: 6 additions & 0 deletions scripts/config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DB_HOST=my_db_endpoint
DB_NAME=my_db_name
DB_USER=my_db_username
DB_PASSWORD=my_db_password
DB_PORT=my_db_port