Files
delphi-database/scripts/init-container.sh
HotSwapp bac8cc4bd5 changes
2025-08-18 20:20:04 -05:00

76 lines
2.3 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Initialization script for Delphi Database System Docker container
set -e
echo "🚀 Initializing Delphi Consulting Group Database System..."
# Create necessary directories
mkdir -p /app/data /app/uploads /app/backups /app/exports /app/logs
# Set permissions
chmod 755 /app/data /app/uploads /app/backups /app/exports
chmod 750 /app/logs
# Check if database exists
if [ ! -f "/app/data/delphi_database.db" ]; then
echo "📊 Database not found. Creating new database..."
# Initialize database tables
python -c "
from app.database.base import engine
from app.models import BaseModel
BaseModel.metadata.create_all(bind=engine)
print('✅ Database tables created successfully')
"
# Check if we should create admin user
if [ "${CREATE_ADMIN_USER}" = "true" ]; then
echo "👤 Creating default admin user..."
python -c "
from sqlalchemy.orm import sessionmaker
from app.database.base import engine
from app.models.user import User
from app.auth.security import get_password_hash
import os
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
db = SessionLocal()
try:
# Check if admin already exists
existing_admin = db.query(User).filter(User.username == 'admin').first()
if not existing_admin:
admin_user = User(
username=os.getenv('ADMIN_USERNAME', 'admin'),
email=os.getenv('ADMIN_EMAIL', 'admin@delphicg.local'),
full_name=os.getenv('ADMIN_FULLNAME', 'System Administrator'),
hashed_password=get_password_hash(os.getenv('ADMIN_PASSWORD')),
is_active=True,
is_admin=True
)
db.add(admin_user)
db.commit()
print('✅ Default admin user created')
print(f' Username: {admin_user.username}')
print(f' Email: {admin_user.email}')
print(' Password: See ADMIN_PASSWORD environment variable')
else:
print(' Admin user already exists, skipping creation')
finally:
db.close()
"
fi
else
echo "✅ Database found, skipping initialization"
fi
# Run database migrations if needed (future feature)
# echo "🔄 Running database migrations..."
# python -m alembic upgrade head
echo "🎉 Initialization complete!"
# Start the application
echo "🌟 Starting Delphi Database System..."
exec "$@"