Skip to content

Commit

Permalink
Add script to setup db test user (#12914)
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann authored Apr 22, 2024
1 parent f0439ec commit 28df745
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-bikes-unite.md
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tools/clroot/db.sqlite3-wal
.DS_Store
.envrc
.env*
.dbenv
!charts/chainlink-cluster/.env.example
!.github/actions/setup-postgres/.env
.direnv
Expand Down
4 changes: 4 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ testscripts: chainlink-test ## Install and run testscript against testdata/scrip
testscripts-update: ## Update testdata/scripts/* files via testscript.
make testscripts TS_FLAGS="-u"

.PHONY: setup-testdb
setup-testdb: ## Setup the test database.
./core/scripts/setup_testdb.sh

.PHONY: testdb
testdb: ## Prepares the test database.
go run . local db preparetest
Expand Down
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,28 @@ go generate ./...

5. Prepare your development environment:

```bash
export CL_DATABASE_URL=postgresql://127.0.0.1:5432/chainlink_test?sslmode=disable
```
The tests require a postgres database. In turn, the environment variable
`CL_DATABASE_URL` must be set to value that can connect to `_test` database, and the user must be able to create and drop
the given `_test` database.

Note: Other environment variables should not be set for all tests to pass

6. Drop/Create test database and run migrations:
There helper script for initial setup to create an appropriate test user. It requires postgres to be running on localhost at port 5432. You will be prompted for
the `postgres` user password

```bash
make setup-testdb
```

This script will save the `CL_DATABASE_URL` in `.dbenv`

Changes to database require migrations to be run. Similarly, `pull`'ing the repo may require migrations to run.
After the one-time setup above:
```
source .dbenv
make testdb
```

If you do end up modifying the migrations for the database, you will need to rerun

7. Run tests:

```bash
Expand Down
66 changes: 66 additions & 0 deletions core/scripts/setup_testdb.sh
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"

0 comments on commit 28df745

Please sign in to comment.