Untitled
# Function to wait for MySQL to be ready wait_for_mysql() { log "Waiting for MySQL to be ready..." local retries=30 local count=0 while ! docker exec resource-lifecycle-mysql mysql -uspecuser -pspecpass -e "SELECT 1;" >/dev/null 2>&1; do count=$((count + 1)) if [ $count -ge $retries ]; then handle_error "MySQL failed to start after ${retries} attempts" fi log "Waiting for MySQL... attempt $count/$retries" sleep 2 done log "MySQL is ready!" } # Function to start MySQL start_mysql() { log "Starting MySQL container..." # Ensure clean state first docker-compose down -v || true sleep 2 # Start MySQL container if ! docker-compose up -d mysql; then handle_error "Failed to start MySQL container" fi # Wait for MySQL to be ready wait_for_mysql } # Function to initialize database initialize_database() { log "Initializing database..." # Get absolute path to schema file local schema_file="${PROJECT_ROOT}/resources/schema.sql" if [ ! -f "$schema_file" ]; then handle_error "Schema file not found at $schema_file" } # First wait for MySQL to be fully ready wait_for_mysql # Initialize schema if ! docker exec -i resource-lifecycle-mysql mysql -uspecuser -pspecpass < "$schema_file"; then handle_error "Failed to initialize database schema" } log "Database initialization complete" }
Leave a Comment