#!/usr/bin/env python3 import json import psycopg2 import uuid from datetime import datetime def import_to_database(): # Read the transformed JSON data with open('/opt/rtsda/church-api/quarterly_import_data.json', 'r') as f: data = json.load(f) # Connect to the database conn = psycopg2.connect("postgresql://rtsda_user:SaviourofMyLife!!@localhost:5432/church_db") cursor = conn.cursor() print(f"Importing {len(data['orders'])} quarterly orders to database...") imported = 0 errors = 0 for order in data["orders"]: try: # Generate a new UUID for each order order_id = str(uuid.uuid4()) # Insert the order cursor.execute(""" INSERT INTO quarterly_orders (id, name, quarterly_type, amount) VALUES (%s, %s, %s, %s) """, (order_id, order["name"], order["type"], order["amount"])) imported += 1 if imported % 10 == 0: print(f" Imported {imported} orders...") except Exception as e: print(f" Error importing '{order['name']}': {e}") errors += 1 # Commit all changes conn.commit() cursor.close() conn.close() print(f"\nImport completed!") print(f" Successfully imported: {imported} orders") print(f" Errors: {errors}") if __name__ == "__main__": import_to_database()