Skip to content

Commit

Permalink
fix setup-testdb script (#13403)
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann committed Jun 3, 2024
1 parent 6ed7a33 commit d774ced
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions core/scripts/setup_testdb.sh
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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;'
Expand All @@ -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
Expand Down

0 comments on commit d774ced

Please sign in to comment.