feat: use apply server migration when server starts (#30)

* feat: use apply server migration when server starts

* fix: add docs and fix local run

* feat: refactor migration

* feat: local build server wait on gotrue

* fix: add back sqlx commands

* fix: add migrations directory

* fix: try SQLX_OFFLINE=true

* test: fix rustlint

* chore: clippy fmt

---------

Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
Zack 2023-09-12 15:12:08 +08:00 committed by GitHub
parent 853f089ca0
commit da03009004
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 5 deletions

View File

@ -6,4 +6,3 @@ deploy/
tests/ tests/
docker/Dockerfile docker/Dockerfile
scripts/ scripts/
migrations/

View File

@ -10,6 +10,7 @@ on:
env: env:
SQLX_VERSION: 0.7.1 SQLX_VERSION: 0.7.1
SQLX_FEATURES: "rustls,postgres" SQLX_FEATURES: "rustls,postgres"
SQLX_OFFLINE: true
jobs: jobs:
test: test:
@ -36,10 +37,11 @@ jobs:
- name: Check sqlx-data.json - name: Check sqlx-data.json
run: | run: |
cargo sqlx migrate run
cargo sqlx prepare --check -- --bin appflowy_cloud cargo sqlx prepare --check -- --bin appflowy_cloud
- name: Rustfmt - name: Rustfmt
run: cargo fmt --check run: cargo fmt --check
- name: Clippy - name: Clippy
run: SQLX_OFFLINE=true cargo clippy -- -D warnings run: cargo clippy -- -D warnings

7
Cargo.lock generated
View File

@ -446,6 +446,7 @@ dependencies = [
"collab-ws", "collab-ws",
"config", "config",
"derive_more", "derive_more",
"dotenv",
"fancy-regex", "fancy-regex",
"futures-util", "futures-util",
"gotrue", "gotrue",
@ -1184,6 +1185,12 @@ dependencies = [
"syn 2.0.32", "syn 2.0.32",
] ]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]] [[package]]
name = "dotenvy" name = "dotenvy"
version = "0.15.7" version = "0.15.7"

View File

@ -29,10 +29,19 @@ if [ $ATTEMPTS -eq $MAX_ATTEMPTS ]; then
exit 1 exit 1
fi fi
until curl localhost:9998/health; do
>&2 echo "Waiting on GoTrue"
sleep 1
done
# Kill any existing instances # Kill any existing instances
pkill -f appflowy_cloud || true pkill -f appflowy_cloud || true
# Run the migrations # Require if there are any changes to the database schema
# To build AppFlowy-Cloud binary, we requires the .sqlx files
# To generate the .sqlx files, we need to run the following command
# After the .sqlx files are generated, we build in SQLX_OFFLINE=true
# where we don't need to connect to the database
cargo sqlx database create && cargo sqlx migrate run && cargo sqlx prepare cargo sqlx database create && cargo sqlx migrate run && cargo sqlx prepare
cargo run cargo run

View File

@ -24,3 +24,6 @@ GOTRUE_REGISTERED_PASSWORD=your_password
# url to the postgres database # url to the postgres database
DATABASE_URL=postgres://postgres:password@localhost:5433/postgres DATABASE_URL=postgres://postgres:password@localhost:5433/postgres
# uncomment this to enable build without database
# .sqlx files must be pregenerated
SQLX_OFFLINE=false

View File

@ -12,7 +12,7 @@ services:
ports: ports:
- 5433:5432 - 5433:5432
volumes: volumes:
- ./migrations:/docker-entrypoint-initdb.d - ./migrations/before:/docker-entrypoint-initdb.d
redis: redis:
image: redis image: redis

View File

@ -12,7 +12,7 @@ services:
ports: ports:
- 5433:5432 - 5433:5432
volumes: volumes:
- ./migrations:/docker-entrypoint-initdb.d - ./migrations/before/:/docker-entrypoint-initdb.d
redis: redis:
image: redis image: redis

View File

@ -122,6 +122,8 @@ fn get_certificate_and_server_key(config: &Config) -> Option<(Secret<String>, Se
pub async fn init_state(config: &Config) -> State { pub async fn init_state(config: &Config) -> State {
let pg_pool = get_connection_pool(&config.database).await; let pg_pool = get_connection_pool(&config.database).await;
migrate(&pg_pool).await;
let gotrue_client = get_gotrue_client(&config.gotrue).await; let gotrue_client = get_gotrue_client(&config.gotrue).await;
State { State {
@ -141,6 +143,13 @@ async fn get_connection_pool(setting: &DatabaseSetting) -> PgPool {
.expect("Failed to connect to Postgres") .expect("Failed to connect to Postgres")
} }
async fn migrate(pool: &PgPool) {
sqlx::migrate!("./migrations")
.run(pool)
.await
.expect("Failed to run migrations");
}
async fn get_gotrue_client(setting: &GoTrueSetting) -> gotrue::api::Client { async fn get_gotrue_client(setting: &GoTrueSetting) -> gotrue::api::Client {
let gotrue_client = gotrue::api::Client::new(reqwest::Client::new(), &setting.base_url); let gotrue_client = gotrue::api::Client::new(reqwest::Client::new(), &setting.base_url);
gotrue_client gotrue_client