72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
import os
|
|
import psycopg2
|
|
import logging
|
|
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
)
|
|
logger = logging.getLogger('ploughshares')
|
|
|
|
def get_db_connection():
|
|
host = os.environ.get('POSTGRES_HOST', 'db')
|
|
port = os.environ.get('POSTGRES_PORT', '5432')
|
|
dbname = os.environ.get('POSTGRES_DB', 'ploughshares')
|
|
user = os.environ.get('POSTGRES_USER', 'ploughshares')
|
|
password = os.environ.get('POSTGRES_PASSWORD', 'ploughshares_password')
|
|
|
|
conn = psycopg2.connect(
|
|
host=host,
|
|
port=port,
|
|
dbname=dbname,
|
|
user=user,
|
|
password=password
|
|
)
|
|
conn.autocommit = True
|
|
return conn
|
|
|
|
def migrate_database():
|
|
"""
|
|
Adds approval fields to the transactions table if they don't exist
|
|
"""
|
|
conn = get_db_connection()
|
|
cursor = conn.cursor()
|
|
|
|
try:
|
|
# Check if the approved column already exists
|
|
cursor.execute("""
|
|
SELECT column_name
|
|
FROM information_schema.columns
|
|
WHERE table_name = 'transactions' AND column_name = 'approved'
|
|
""")
|
|
|
|
if cursor.fetchone() is None:
|
|
logger.info("Adding approval fields to transactions table...")
|
|
|
|
# Add the new columns
|
|
cursor.execute("""
|
|
ALTER TABLE transactions
|
|
ADD COLUMN IF NOT EXISTS approved BOOLEAN DEFAULT FALSE,
|
|
ADD COLUMN IF NOT EXISTS approved_at TIMESTAMP,
|
|
ADD COLUMN IF NOT EXISTS approved_by VARCHAR(255)
|
|
""")
|
|
|
|
# Create an index on the approved column
|
|
cursor.execute("""
|
|
CREATE INDEX IF NOT EXISTS idx_transactions_approved
|
|
ON transactions(approved)
|
|
""")
|
|
|
|
logger.info("Successfully added approval fields to transactions table")
|
|
else:
|
|
logger.info("Approval fields already exist in transactions table")
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error migrating database: {e}")
|
|
finally:
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
if __name__ == "__main__":
|
|
migrate_database() |