Skip to content

Commit

Permalink
feat(postgres): create database if POSTGRES_* envs are defined
Browse files Browse the repository at this point in the history
feat(postgres): create database if POSTGRES_* envs are defined

feat(postgres): create database if POSTGRES_* envs are defined
  • Loading branch information
tsirysndr committed Aug 22, 2024
1 parent 5bde4c5 commit bec54c8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ jobs:
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NIX_CONFIG: extra-access-tokens = github.com=${{ secrets.GH_ACCESS_TOKEN }}
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
8 changes: 4 additions & 4 deletions postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ fluentci run --wasm postgres start

| Name | Description |
| ------ | --------------------------------------------|
| start | Start Postgres |
| stop | Stops Postgres |
| start | Start Postgres |
| stop | Stops Postgres |

## Code Usage

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:
Expand All @@ -36,7 +36,7 @@ use fluentci_pdk::dag;

// ...

dag().call("https://pkg.fluentci.io/[email protected].4?wasm=1", "start", vec![])?;
dag().call("https://pkg.fluentci.io/[email protected].5?wasm=1", "start", vec![])?;
```

## 📚 Examples
Expand Down
2 changes: 1 addition & 1 deletion postgres/fluentci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ keywords = [
]
license = "MIT"
name = "postgres"
version = "0.1.4"
version = "0.1.5"
31 changes: 31 additions & 0 deletions postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ pub mod helpers;
pub fn start(_args: String) -> FnResult<String> {
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")?
Expand All @@ -19,6 +37,19 @@ pub fn start(_args: String) -> FnResult<String> {
"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 $POSTGRES_DB -U `whoami` -c \"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO $POSTGRES_USER;\""])?
.with_exec(vec!["overmind", "status"])?
.stdout()?;
Ok(stdout)
Expand Down

0 comments on commit bec54c8

Please sign in to comment.