diff --git a/async_pg/README.md b/async_pg/README.md index 50d55aae..5552fb6e 100644 --- a/async_pg/README.md +++ b/async_pg/README.md @@ -9,13 +9,36 @@ ## Instructions -1. Set up the testing database by running the included script: +1. Create database user ```shell - ./sql/create_db.sh + createuser -P test_user ``` -2. Create `.env` file: + Enter a password of your choice. The following instructions assume you + used `testing` as password. + + This step is **optional** and you can also use an existing database user + for that. Just make sure to replace `test_user` by the database user + of your choice in the following steps and change the `.env` file + containing the configuration accordingly. + +2. Create database + + ```shell + createdb -O test_user testing_db + ``` + +3. Initialize database + + ```shell + psql -f sql/schema.sql testing_db + ``` + + This step can be repeated and clears the database as it drops and + recreates the schema `testing` which is used within the database. + +4. Create `.env` file: ```ini SERVER_ADDR=127.0.0.1:8080 @@ -27,17 +50,23 @@ PG.POOL.MAX_SIZE=16 ``` -3. Run the server: +5. Run the server: ```shell cargo run ``` -4. Using a different terminal send a HTTP POST request to the running server: +6. Using a different terminal send an HTTP POST request to the running server: ```shell echo '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' | http -f --json --print h POST http://127.0.0.1:8080/users ``` + **...or using curl...** + + ```shell + curl -d '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/users + ``` + A unique constraint exists for username, so sending this request twice will return an internal server error (HTTP 500). diff --git a/async_pg/sql/create_db.sh b/async_pg/sql/create_db.sh deleted file mode 100755 index 6505d073..00000000 --- a/async_pg/sql/create_db.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -psql -U postgres -h 127.0.0.1 -f setup_db_and_user.sql diff --git a/async_pg/sql/setup_db_and_user.sql b/async_pg/sql/schema.sql similarity index 65% rename from async_pg/sql/setup_db_and_user.sql rename to async_pg/sql/schema.sql index dc515dd2..28efcc38 100644 --- a/async_pg/sql/setup_db_and_user.sql +++ b/async_pg/sql/schema.sql @@ -1,15 +1,6 @@ -DROP DATABASE IF EXISTS testing_db; - -CREATE USER test_user WITH PASSWORD 'testing'; - -CREATE DATABASE testing_db OWNER test_user; - -\connect testing_db; - DROP SCHEMA IF EXISTS testing CASCADE; CREATE SCHEMA testing; - CREATE TABLE testing.users ( id BIGSERIAL PRIMARY KEY, email VARCHAR(200) NOT NULL,