diff --git a/Cargo.lock b/Cargo.lock index 216a2b9..636e64d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -574,7 +574,7 @@ dependencies = [ [[package]] name = "postgres" -version = "0.1.4" +version = "0.1.5" dependencies = [ "anyhow", "extism-pdk", diff --git a/postgres/Cargo.toml b/postgres/Cargo.toml index 7135dce..3269eda 100644 --- a/postgres/Cargo.toml +++ b/postgres/Cargo.toml @@ -6,7 +6,7 @@ description = "Postgres plugin for FluentCI" edition = "2021" license = "MIT" name = "postgres" -version = "0.1.4" +version = "0.1.5" [lib] crate-type = [ diff --git a/postgres/README.md b/postgres/README.md index 7ba1383..c95e5ad 100644 --- a/postgres/README.md +++ b/postgres/README.md @@ -17,8 +17,8 @@ fluentci run --wasm postgres start | Name | Description | | ------ | --------------------------------------------| -| start | Start Postgres | -| stop | Stops Postgres | +| start | Start Postgres | +| stop | Stops Postgres | ## Code Usage @@ -26,7 +26,7 @@ Add `fluentci-pdk` crate to your `Cargo.toml`: ```toml [dependencies] -fluentci-pdk = "0.2.1" +fluentci-pdk = "0.2.3" ``` Use the following code to call the plugin: @@ -36,7 +36,7 @@ use fluentci_pdk::dag; // ... -dag().call("https://pkg.fluentci.io/postgres@v0.1.4?wasm=1", "start", vec![])?; +dag().call("https://pkg.fluentci.io/postgres@v0.1.5?wasm=1", "start", vec![])?; ``` ## 📚 Examples diff --git a/postgres/fluentci.toml b/postgres/fluentci.toml index 254fbf2..f7ddf74 100644 --- a/postgres/fluentci.toml +++ b/postgres/fluentci.toml @@ -8,4 +8,4 @@ keywords = [ ] license = "MIT" name = "postgres" -version = "0.1.4" +version = "0.1.5" diff --git a/postgres/src/lib.rs b/postgres/src/lib.rs index 8e4efdc..20a9cec 100644 --- a/postgres/src/lib.rs +++ b/postgres/src/lib.rs @@ -7,6 +7,24 @@ pub mod helpers; pub fn start(_args: String) -> FnResult { helpers::setup()?; let port = dag().get_env("PGPORT")?; + + let pg_user = dag().get_env("POSTGRES_USER")?; + let pg_password = dag().get_env("POSTGRES_PASSWORD")?; + let pg_database = dag().get_env("POSTGRES_DB")?; + + if pg_user.is_empty() { + dag().set_envs(vec![("POSTGRES_USER".into(), "postgres".into())])?; + } + + if pg_database.is_empty() { + dag().set_envs(vec![("POSTGRES_DB".into(), "demo".into())])?; + } + + let with_password = match pg_password.is_empty() { + true => "", + false => "WITH PASSWORD '${POSTGRES_PASSWORD}'", + }; + let stdout = dag() .flox()? .with_workdir(".fluentci/postgres")? @@ -19,6 +37,19 @@ pub fn start(_args: String) -> FnResult { "overmind start -f Procfile --daemonize || flox activate -- overmind restart postgres", ])? .wait_on(port.parse()?, None)? + .with_exec(vec![ + "psql --host=localhost -d postgres -U `whoami` -c \"CREATE DATABASE $POSTGRES_DB;\"", + ])? + .with_exec(vec![ + &format!( + "psql --host=localhost -d postgres -U `whoami` -c \"CREATE USER $POSTGRES_USER {} CREATEDB CREATEROLE;\"", + with_password + ) + ])? + .with_exec(vec!["psql --host=localhost -d $POSTGRES_DB -U `whoami` -c \"GRANT ALL PRIVILEGES ON DATABASE $POSTGRES_DB TO $POSTGRES_USER;\""])? + .with_exec(vec!["psql --host=localhost -d $POSTGRES_DB -U `whoami` -c \"GRANT ALL ON SCHEMA public TO $POSTGRES_USER;\""])? + .with_exec(vec!["psql --host=localhost -d $POSTGRES_DB -U `whoami` -c \"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $POSTGRES_USER;\""])? + .with_exec(vec!["psql --host=localhost -d s2 -U `whoami` -c \"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO $POSTGRES_USER;\""])? .with_exec(vec!["overmind", "status"])? .stdout()?; Ok(stdout)