Browse Source

setup script to create the postgres database

appflowy 3 năm trước cách đây
mục cha
commit
3ae2295193

+ 1 - 0
backend/.env

@@ -0,0 +1 @@
+DATABASE_URL="postgres://postgres:password@localhost:5433/flowy"

+ 14 - 1
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"

+ 3 - 0
backend/Makefile

@@ -0,0 +1,3 @@
+include scripts/database/database.mk
+
+.PHONY: init_database  add_migrations run_migrations reset_db echo_db_url

+ 28 - 0
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)

BIN
backend/doc/img_1.png


BIN
backend/doc/img_2.png


+ 9 - 0
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
+);

+ 27 - 0
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}
+

+ 12 - 0
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

+ 15 - 0
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

+ 0 - 5
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<AppContext> {
     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)