diff --git a/docker/ploughshares/app.py b/docker/ploughshares/app.py index fa2c552..2305516 100644 --- a/docker/ploughshares/app.py +++ b/docker/ploughshares/app.py @@ -283,24 +283,50 @@ def update_transaction(id): def bootstrap_database(): """ - Checks if the database is empty. Test data can be loaded separately using the tests/generate_test_data.py script. + Checks if the database is empty and initializes the schema if needed. + Test data can be loaded separately using the tests/generate_test_data.py script. """ - logger.info(f"Ploughshares v{VERSION} - Checking database for existing data...") + logger.info(f"Ploughshares v{VERSION} - Checking database...") conn = get_db_connection() if conn is None: logger.error("Database connection failed. Exiting.") exit(1) try: + # Check if the transactions table exists with conn.cursor() as cur: - cur.execute("SELECT COUNT(*) FROM transactions") + cur.execute("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'transactions')") result = cur.fetchone() - if result and 'count' in result: - count = result['count'] - if count == 0: - logger.info(f"Ploughshares v{VERSION} - Database is empty. You can populate it with test data using the tests/generate_test_data.py script.") + + # If table doesn't exist, initialize the schema + if not result or not result.get('exists', False): + logger.info(f"Ploughshares v{VERSION} - Transactions table not found. Initializing schema...") + + # Read schema.sql file + try: + with open('schema.sql', 'r') as f: + schema_sql = f.read() + + # Execute schema SQL + with conn.cursor() as schema_cur: + schema_cur.execute(schema_sql) + logger.info(f"Ploughshares v{VERSION} - Schema initialized successfully.") + except Exception as schema_error: + logger.error(f"Error initializing schema: {schema_error}") else: - logger.error("Could not get count from database") + # Check if the table is empty + try: + with conn.cursor() as cur: + cur.execute("SELECT COUNT(*) FROM transactions") + result = cur.fetchone() + if result and 'count' in result: + count = result['count'] + if count == 0: + logger.info(f"Ploughshares v{VERSION} - Database is empty. You can populate it with test data using the tests/generate_test_data.py script.") + else: + logger.error("Could not get count from database") + except Exception as count_error: + logger.error(f"Error counting transactions: {count_error}") except Exception as e: logger.error(f"Error checking database: {e}") finally: diff --git a/stack.production.yml b/stack.production.yml index e676911..a2c6538 100644 --- a/stack.production.yml +++ b/stack.production.yml @@ -9,6 +9,9 @@ services: image: 'git.nixc.us/colin/ploughshares:latest' deploy: replicas: 1 + placement: + constraints: + - node.hostname == macmini14 labels: homepage.group: tools homepage.name: Project Ploughshares @@ -43,7 +46,7 @@ services: replicas: 1 placement: constraints: - - node.role == manager + - node.hostname == macmini14 environment: - POSTGRES_DB=ploughshares - POSTGRES_USER=ploughshares