Add auto schema creation and deploy to macmini14
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
colin 2025-07-03 13:02:17 -04:00
parent 90501f0a1f
commit 5f920a7e37
2 changed files with 38 additions and 9 deletions

View File

@ -283,24 +283,50 @@ def update_transaction(id):
def bootstrap_database(): 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() conn = get_db_connection()
if conn is None: if conn is None:
logger.error("Database connection failed. Exiting.") logger.error("Database connection failed. Exiting.")
exit(1) exit(1)
try: try:
# Check if the transactions table exists
with conn.cursor() as cur: 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() result = cur.fetchone()
if result and 'count' in result:
count = result['count'] # If table doesn't exist, initialize the schema
if count == 0: if not result or not result.get('exists', False):
logger.info(f"Ploughshares v{VERSION} - Database is empty. You can populate it with test data using the tests/generate_test_data.py script.") 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: 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: except Exception as e:
logger.error(f"Error checking database: {e}") logger.error(f"Error checking database: {e}")
finally: finally:

View File

@ -9,6 +9,9 @@ services:
image: 'git.nixc.us/colin/ploughshares:latest' image: 'git.nixc.us/colin/ploughshares:latest'
deploy: deploy:
replicas: 1 replicas: 1
placement:
constraints:
- node.hostname == macmini14
labels: labels:
homepage.group: tools homepage.group: tools
homepage.name: Project Ploughshares homepage.name: Project Ploughshares
@ -43,7 +46,7 @@ services:
replicas: 1 replicas: 1
placement: placement:
constraints: constraints:
- node.role == manager - node.hostname == macmini14
environment: environment:
- POSTGRES_DB=ploughshares - POSTGRES_DB=ploughshares
- POSTGRES_USER=ploughshares - POSTGRES_USER=ploughshares