76 lines
2.3 KiB
Bash
Executable File
76 lines
2.3 KiB
Bash
Executable File
#!/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', 'admin123')),
|
||
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 "$@" |