From d774ced613508cddfa65356f04af96b17893d47a Mon Sep 17 00:00:00 2001 From: krehermann Date: Mon, 3 Jun 2024 15:21:38 -0600 Subject: [PATCH] fix setup-testdb script (#13403) --- core/scripts/setup_testdb.sh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core/scripts/setup_testdb.sh b/core/scripts/setup_testdb.sh index 8f30e159d1c..534c980feb9 100755 --- a/core/scripts/setup_testdb.sh +++ b/core/scripts/setup_testdb.sh @@ -1,5 +1,9 @@ #/bin/sh +function exit_error { + echo "Error: $1" + exit 1 +} # Create a new user and database for development # This script is intended to be run on a local development machine tdir=$(mktemp -d -t db-dev-user) @@ -9,6 +13,7 @@ password="insecurepassword" database="chainlink_development_test" # here document for the SQL commands cat << EOF > $tdir/db-dev-user.sql +DROP DATABASE IF EXISTS $database; -- create a new user and database for development if they don't exist DO \$\$ BEGIN @@ -22,6 +27,7 @@ WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$database')\gexec -- Grant all privileges on the database to the user ALTER DATABASE $database OWNER TO $username; GRANT ALL PRIVILEGES ON DATABASE "$database" TO "$username"; +ALTER USER $username CREATEDB; -- Create a pristine database for testing SELECT 'CREATE DATABASE chainlink_test_pristine WITH OWNER $username;' @@ -38,18 +44,20 @@ echo "########################################################################## echo "##########################################################################################################" echo "" # Run the SQL commands -psql -U postgres -h localhost -f $tdir/db-dev-user.sql +psql -U postgres -h localhost -f $tdir/db-dev-user.sql || exit_error "Failed to create user $username and database $database" #test the connection -PGPASSWORD=$password psql -U $username -h localhost -d $database -c "SELECT 1" && echo "Connection successful" || echo "Connection failed" +PGPASSWORD=$password psql -U $username -h localhost -d $database -c "SELECT 1" || exit_error "Connection failed for $username to $database" -db_url=$(echo "CL_DATABASE_URL=postgresql://chainlink_dev:insecurepassword@localhost:5432/chainlink_development_test") + + +db_url=$(echo "CL_DATABASE_URL=postgresql://$username:$password@localhost:5432/$database?sslmode=disable") echo $db_url repo=$(git rev-parse --show-toplevel) pushd $repo export $db_url -make testdb || echo "Failed to create test database" +make testdb || exit_error "Failed to create test database" popd # Set the database URL in the .dbenv file