-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to setup db test user (#12914)
- Loading branch information
1 parent
f0439ec
commit 28df745
Showing
5 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"chainlink": minor | ||
--- | ||
|
||
#internal Add script to create test database user and update docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#/bin/sh | ||
|
||
# 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) | ||
|
||
username="chainlink_dev" | ||
password="insecurepassword" | ||
database="chainlink_development_test" | ||
# here document for the SQL commands | ||
cat << EOF > $tdir/db-dev-user.sql | ||
-- create a new user and database for development if they don't exist | ||
DO \$\$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '$username') THEN | ||
CREATE ROLE $username WITH LOGIN PASSWORD '$password'; | ||
END IF; | ||
END \$\$; | ||
SELECT 'CREATE DATABASE $database WITH OWNER $username;' | ||
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"; | ||
-- Create a pristine database for testing | ||
SELECT 'CREATE DATABASE chainlink_test_pristine WITH OWNER $username;' | ||
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'chainlink_test_pristine')\gexec | ||
EOF | ||
|
||
# Print the SQL commands | ||
echo "SQL commands to be run: $tdir/db-dev-user.sql" | ||
echo "##########################################################################################################" | ||
echo "##########################################################################################################" | ||
|
||
cat $tdir/db-dev-user.sql | ||
echo "##########################################################################################################" | ||
echo "##########################################################################################################" | ||
echo "" | ||
# Run the SQL commands | ||
psql -U postgres -h localhost -f $tdir/db-dev-user.sql | ||
|
||
|
||
#test the connection | ||
PGPASSWORD=$password psql -U $username -h localhost -d $database -c "SELECT 1" && echo "Connection successful" || echo "Connection failed" | ||
|
||
db_url=$(echo "CL_DATABASE_URL=postgresql://chainlink_dev:insecurepassword@localhost:5432/chainlink_development_test") | ||
echo $db_url | ||
repo=$(git rev-parse --show-toplevel) | ||
pushd $repo | ||
export $db_url | ||
make testdb || echo "Failed to create test database" | ||
popd | ||
|
||
# Set the database URL in the .dbenv file | ||
dbenv=$repo/.dbenv | ||
echo "\n!Success!\n" | ||
echo "Datbase URL: $db_url" | ||
|
||
echo "export $db_url" >> $dbenv | ||
echo "Has been set in the $dbenv file" | ||
|
||
echo "Either" | ||
echo " source $dbenv" | ||
echo "Or explicitly set environment variable in your shell" | ||
echo " export $db_url" |