diff --git a/backend/.env b/backend/.env new file mode 100644 index 0000000000..8ee6c6b1d5 --- /dev/null +++ b/backend/.env @@ -0,0 +1 @@ +DATABASE_URL="postgres://postgres:password@localhost:5433/flowy" \ No newline at end of file diff --git a/backend/Cargo.toml b/backend/Cargo.toml index f7c9bab3fd..f4de65394d 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -21,9 +21,22 @@ log = "0.4.14" serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } serde_repr = "0.1" - flowy-log = { path = "../rust-lib/flowy-log" } +[dependencies.sqlx] +version = "0.5.2" +default-features = false +features = [ + "runtime-actix-rustls", + "macros", + "postgres", + "uuid", + "chrono", + "migrate" +] + + + [lib] path = "src/lib.rs" diff --git a/backend/Makefile b/backend/Makefile new file mode 100644 index 0000000000..5b4e897a95 --- /dev/null +++ b/backend/Makefile @@ -0,0 +1,3 @@ +include scripts/database/database.mk + +.PHONY: init_database add_migrations run_migrations reset_db echo_db_url \ No newline at end of file diff --git a/backend/doc/database_setup.md b/backend/doc/database_setup.md new file mode 100644 index 0000000000..44ac3d83d0 --- /dev/null +++ b/backend/doc/database_setup.md @@ -0,0 +1,28 @@ + + + +### Docker +1. follow the [instructions](https://docs.docker.com/desktop/mac/install/) to install docker. +2. open terminal and run: `docker pull postgres` + +3. run `make init_docker` if you have not run before. You can find out the running container by run `docker ps` +``` +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +bfcdd6369e89 postgres "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 0.0.0.0:5433->5432/tcp, :::5433->5432/tcp brave_bassi +``` + +4. run `make init_database`. It will create the database on the remote specified by DATABASE_URL. You can connect you database using +pgAdmin. + + +![img_2.png](img_2.png) + +The information you enter must be the same as the `make init_docker`. e.g. +``` +export DB_USER=postgres +export DB_PASSWORD=password +export DB_NAME=flowy +export DB_PORT=5433 +``` + +![img_1.png](img_1.png) \ No newline at end of file diff --git a/backend/doc/img_1.png b/backend/doc/img_1.png new file mode 100644 index 0000000000..64fcbddfd9 Binary files /dev/null and b/backend/doc/img_1.png differ diff --git a/backend/doc/img_2.png b/backend/doc/img_2.png new file mode 100644 index 0000000000..1a9ddc53a2 Binary files /dev/null and b/backend/doc/img_2.png differ diff --git a/backend/migrations/20210819065837_user.sql b/backend/migrations/20210819065837_user.sql new file mode 100644 index 0000000000..42c1c381c7 --- /dev/null +++ b/backend/migrations/20210819065837_user.sql @@ -0,0 +1,9 @@ +-- Add migration script here +CREATE TABLE user_table( + id uuid NOT NULL, + PRIMARY KEY (id), + email TEXT NOT NULL UNIQUE, + name TEXT NOT NULL, + create_time timestamptz NOT NULL, + password TEXT NOT NULL +); \ No newline at end of file diff --git a/backend/scripts/database/database.mk b/backend/scripts/database/database.mk new file mode 100644 index 0000000000..0c29b65d32 --- /dev/null +++ b/backend/scripts/database/database.mk @@ -0,0 +1,27 @@ +.EXPORT_ALL_VARIABLES: +export DB_USER=postgres +export DB_PASSWORD=password +export DB_NAME=flowy +export DB_PORT=5433 +export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME} +export ROOT = "./scripts/database" + +init_docker: + ${ROOT}/docker.sh + +init_database: + ${ROOT}/init.sh + +reset_db: + sqlx database reset + +add_migrations: + #make table="the name of your table" add_migrations + sqlx migrate add $(table) + +run_migrations: + sqlx migrate run + +echo_db_url: + echo ${DATABASE_URL} + diff --git a/backend/scripts/database/db_init.sh b/backend/scripts/database/db_init.sh new file mode 100755 index 0000000000..ac59b7ac37 --- /dev/null +++ b/backend/scripts/database/db_init.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -x +set -eo pipefail + +until psql -h "localhost" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; +do + >&2 echo "Postgres is still unavailable - sleeping" + sleep 1 +done + +>&2 echo "Postgres is up and running on port ${DB_PORT}!" +sqlx database create diff --git a/backend/scripts/database/docker.sh b/backend/scripts/database/docker.sh new file mode 100755 index 0000000000..326fde3fe4 --- /dev/null +++ b/backend/scripts/database/docker.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -x +set -eo pipefail + +if [[ -z "${SKIP_DOCKER}" ]] +then + docker run \ + -e POSTGRES_USER=${DB_USER} \ + -e POSTGRES_PASSWORD=${DB_PASSWORD} \ + -e POSTGRES_DB=${DB_NAME} \ + -p "${DB_PORT}":5432 \ + -d postgres \ + postgres -N 1000 +fi + # ^ Increased maximum number of connections for testing purposes \ No newline at end of file diff --git a/backend/src/startup.rs b/backend/src/startup.rs index fd837614e1..c53ff6b0b8 100644 --- a/backend/src/startup.rs +++ b/backend/src/startup.rs @@ -20,11 +20,6 @@ fn ws_scope() -> Scope { web::scope("/ws").service(ws::start_connection) } pub async fn init_app_context() -> Arc { let _ = flowy_log::Builder::new("flowy").env_filter("Debug").build(); - - // std::env::set_var("RUST_LOG", "info"); - // env_logger::init(); - // log::debug!("EnvTask initialization"); - let ws_server = WSServer::new().start(); let ctx = AppContext::new(ws_server); Arc::new(ctx)