church-api/import_to_db.py
Benjamin Slingo e206ce3332 Add quarterlies management system with complete CRUD operations
- New quarterly_orders table with validation and constraints
- Full CRUD API endpoints for quarterly orders management
- Import functionality for bulk quarterly data from JSON
- Python scripts for data migration and database import
- Consistent validation for quarterly types and amounts
- Follows established DRY/KISS architectural patterns
2025-09-06 16:53:14 -04:00

50 lines
1.5 KiB
Python

#!/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()