add coloring, fix logging

This commit is contained in:
elianddb
2025-09-15 09:56:07 -07:00
parent ceb8936dcd
commit ede37be613
3 changed files with 65 additions and 7 deletions

View File

@@ -3,11 +3,17 @@ set -eo pipefail
# logging functions
mysql_log() {
local type="$1"; shift
# accept argument string or stdin
local text="$*"; if [ "$#" -eq 0 ]; then text="$(cat)"; fi
local dt; dt="$(date --rfc-3339=seconds)"
printf '%s [%s] [Entrypoint]: %s\n' "$dt" "$type" "$text"
local type="$1"; shift
local text="$*"; if [ "$#" -eq 0 ]; then text="$(cat)"; fi
local dt; dt="$(date --rfc-3339=seconds)"
local color_reset="\033[0m"
local color
case "$type" in
Warn) color="\033[1;33m" ;; # yellow
ERROR) color="\033[1;31m" ;; # red
*) color="" ;;
esac
printf '%b%s [%s] [Entrypoint]: %s%b\n' "$color" "$dt" "$type" "$text" "$color_reset"
}
mysql_note() {
mysql_log Note "$@"
@@ -17,6 +23,7 @@ mysql_warn() {
}
mysql_error() {
mysql_log ERROR "$@" >&2
mysql_note "Remove this container with 'docker rm -f <container_name>' before retrying"
exit 1
}
docker_process_sql() {
@@ -160,7 +167,7 @@ EOF
fi
if [ -n "$user" ] && [ -z "$password" ]; then
mysql_error "$(get_env_var_name "USER") specified, but missing $(get_env_var_name "PASSWORD"); user creation requires a password"
mysql_error "$(get_env_var_name "USER") specified, but missing $(get_env_var_name "PASSWORD"); user creation requires a password."
elif [ -z "$user" ] && [ -n "$password" ]; then
mysql_warn "$(get_env_var_name "PASSWORD") specified, but missing $(get_env_var_name "USER"); password will be ignored"
return
@@ -219,6 +226,8 @@ _main() {
# run any file provided in /docker-entrypoint-initdb.d directory before the server starts
if ls /docker-entrypoint-initdb.d/* >/dev/null 2>&1; then
docker_process_init_files /docker-entrypoint-initdb.d/*
else
mysql_warn "No files found in /docker-entrypoint-initdb.d/ to process"
fi
touch $INIT_COMPLETED
fi
@@ -250,7 +259,7 @@ _main() {
# Ensure root user exists with correct password and permissions
mysql_note "Configuring root@${root_host} user"
dolt sql -q "CREATE USER IF NOT EXISTS 'root'@'${root_host}' IDENTIFIED BY '${DOLT_ROOT_PASSWORD}'; ALTER USER 'root'@'${root_host}' IDENTIFIED BY '${DOLT_ROOT_PASSWORD}'; GRANT ALL ON *.* TO 'root'@'${root_host}' WITH GRANT OPTION;" >/dev/null 2>&1 || mysql_error "Failed to configure root@${root_host} user. Check logs for details."
dolt sql -q "CREATE USER IF NOT EXISTS 'root'@'${root_host}' IDENTIFIED BY '${DOLT_ROOT_PASSWORD}'; ALTER USER 'root'@'${root_host}' IDENTIFIED BY '${DOLT_ROOT_PASSWORD}'; GRANT ALL ON *.* TO 'root'@'${root_host}' WITH GRANT OPTION;" >/dev/null 2>&1 || mysql_error "Failed to configure root@${root_host} user."
# If DOLT_DATABASE or MYSQL_DATABASE has been specified, create the database if it does not exist
create_default_database_from_env

View File

@@ -150,6 +150,8 @@ on the host system, it can also be mounted to this default location.
$ docker run -p 3307:3306 -v /Users/jennifer/docker/databases/:/var/lib/dolt/ dolthub/dolt-sql-server:latest
```
If the run command errors out during the entrypoint process, you'll have to remove the created container from the last run.
## Environment Variables
The Dolt SQL Server image supports the following environment variables:

View File

@@ -561,3 +561,50 @@ EOF
db_count=$(echo "$output" | grep -c "testdb\|information_schema" || true)
[ "$db_count" -eq 2 ] || false
}
@test "docker-entrypoint: docker-entrypoint-initdb.d script execution" {
cname="${TEST_PREFIX}initdb-scripts"
# Create temporary directory for init scripts
local temp_dir="/tmp/initdb-test-$$"
mkdir -p "$temp_dir"
# Create various types of init scripts
cat > "$temp_dir/01-create-table.sql" << 'EOF'
CREATE DATABASE IF NOT EXISTS testinit;
USE testinit;
CREATE TABLE init_test (id INT, message VARCHAR(100));
INSERT INTO init_test VALUES (1, 'SQL script executed');
EOF
cat > "$temp_dir/02-bash-script.sh" << 'EOF'
#!/bin/bash
echo "Bash script executed"
dolt sql -q "USE testinit; INSERT INTO init_test VALUES (2, 'Bash script executed');"
EOF
chmod +x "$temp_dir/02-bash-script.sh"
cat > "$temp_dir/03-data.sql" << 'EOF'
USE testinit;
INSERT INTO init_test VALUES (3, 'Compressed SQL executed');
EOF
gzip "$temp_dir/03-data.sql"
# Run container with init scripts mounted
run_container_with_port "$cname" 3306 -e DOLT_ROOT_PASSWORD=rootpass -e DOLT_ROOT_HOST=% -v "$temp_dir:/docker-entrypoint-initdb.d"
# Verify init scripts were executed
run docker run --rm --network host mysql:8.0 mysql -h 127.0.0.1 -P 3306 -u root --password=rootpass -e "USE testinit; SELECT * FROM init_test ORDER BY id;"
[ $status -eq 0 ]
[[ "$output" =~ "SQL script executed" ]] || false
[[ "$output" =~ "Bash script executed" ]] || false
[[ "$output" =~ "Compressed SQL executed" ]] || false
# Verify database was created
run docker run --rm --network host mysql:8.0 mysql -h 127.0.0.1 -P 3306 -u root --password=rootpass -e "SHOW DATABASES;"
[ $status -eq 0 ]
[[ "$output" =~ "testinit" ]] || false
# Cleanup temp directory
rm -rf "$temp_dir"
}