diff --git a/.dockerignore b/.dockerignore index ed4de9a2..fcc007d4 100644 --- a/.dockerignore +++ b/.dockerignore @@ -6,4 +6,3 @@ deploy/ tests/ docker/Dockerfile scripts/ -migrations/ \ No newline at end of file diff --git a/.github/workflows/rustlint.yml b/.github/workflows/rustlint.yml index 32af152c..a432958a 100644 --- a/.github/workflows/rustlint.yml +++ b/.github/workflows/rustlint.yml @@ -10,6 +10,7 @@ on: env: SQLX_VERSION: 0.7.1 SQLX_FEATURES: "rustls,postgres" + SQLX_OFFLINE: true jobs: test: @@ -36,10 +37,11 @@ jobs: - name: Check sqlx-data.json run: | + cargo sqlx migrate run cargo sqlx prepare --check -- --bin appflowy_cloud - name: Rustfmt run: cargo fmt --check - name: Clippy - run: SQLX_OFFLINE=true cargo clippy -- -D warnings + run: cargo clippy -- -D warnings diff --git a/Cargo.lock b/Cargo.lock index 535146f3..f97863c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,6 +446,7 @@ dependencies = [ "collab-ws", "config", "derive_more", + "dotenv", "fancy-regex", "futures-util", "gotrue", @@ -1184,6 +1185,12 @@ dependencies = [ "syn 2.0.32", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dotenvy" version = "0.15.7" diff --git a/build/run_local_server.sh b/build/run_local_server.sh index 20f56505..180c1550 100755 --- a/build/run_local_server.sh +++ b/build/run_local_server.sh @@ -29,10 +29,19 @@ if [ $ATTEMPTS -eq $MAX_ATTEMPTS ]; then exit 1 fi +until curl localhost:9998/health; do + >&2 echo "Waiting on GoTrue" + sleep 1 +done + # Kill any existing instances 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 run diff --git a/dev.env b/dev.env index 4f6dd7a8..298d0982 100644 --- a/dev.env +++ b/dev.env @@ -24,3 +24,6 @@ GOTRUE_REGISTERED_PASSWORD=your_password # url to the postgres database DATABASE_URL=postgres://postgres:password@localhost:5433/postgres +# uncomment this to enable build without database +# .sqlx files must be pregenerated +SQLX_OFFLINE=false diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index 2f7b6f84..ecf117c4 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -12,7 +12,7 @@ services: ports: - 5433:5432 volumes: - - ./migrations:/docker-entrypoint-initdb.d + - ./migrations/before:/docker-entrypoint-initdb.d redis: image: redis diff --git a/docker-compose.yml b/docker-compose.yml index d88dd27d..76f6d3e6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,7 +12,7 @@ services: ports: - 5433:5432 volumes: - - ./migrations:/docker-entrypoint-initdb.d + - ./migrations/before/:/docker-entrypoint-initdb.d redis: image: redis diff --git a/migrations/20230312043000_supabase_auth.sql b/migrations/before/20230312043000_supabase_auth.sql similarity index 100% rename from migrations/20230312043000_supabase_auth.sql rename to migrations/before/20230312043000_supabase_auth.sql diff --git a/src/application.rs b/src/application.rs index 3daedf08..a148c657 100644 --- a/src/application.rs +++ b/src/application.rs @@ -122,6 +122,8 @@ fn get_certificate_and_server_key(config: &Config) -> Option<(Secret, Se pub async fn init_state(config: &Config) -> State { let pg_pool = get_connection_pool(&config.database).await; + migrate(&pg_pool).await; + let gotrue_client = get_gotrue_client(&config.gotrue).await; State { @@ -141,6 +143,13 @@ async fn get_connection_pool(setting: &DatabaseSetting) -> PgPool { .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 { let gotrue_client = gotrue::api::Client::new(reqwest::Client::new(), &setting.base_url); gotrue_client