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:
parent
853f089ca0
commit
da03009004
|
|
@ -6,4 +6,3 @@ deploy/
|
|||
tests/
|
||||
docker/Dockerfile
|
||||
scripts/
|
||||
migrations/
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
3
dev.env
3
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
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ services:
|
|||
ports:
|
||||
- 5433:5432
|
||||
volumes:
|
||||
- ./migrations:/docker-entrypoint-initdb.d
|
||||
- ./migrations/before:/docker-entrypoint-initdb.d
|
||||
|
||||
redis:
|
||||
image: redis
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ services:
|
|||
ports:
|
||||
- 5433:5432
|
||||
volumes:
|
||||
- ./migrations:/docker-entrypoint-initdb.d
|
||||
- ./migrations/before/:/docker-entrypoint-initdb.d
|
||||
|
||||
redis:
|
||||
image: redis
|
||||
|
|
|
|||
|
|
@ -122,6 +122,8 @@ fn get_certificate_and_server_key(config: &Config) -> Option<(Secret<String>, 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
|
||||
|
|
|
|||
Loading…
Reference in New Issue