Skip to content

Commit

Permalink
feat(mysql): create database if MYSQL_* env are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed Aug 18, 2024
1 parent 460a278 commit 5bde4c5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
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 mysql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "MySQL plugin for FluentCI"
edition = "2021"
license = "MIT"
name = "mysql"
version = "0.1.3"
version = "0.1.4"

[lib]
crate-type = [
Expand Down
2 changes: 1 addition & 1 deletion mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use fluentci_pdk::dag;

// ...

dag().call("https://pkg.fluentci.io/mysql@v0.1.3?wasm=1", "start", vec![])?;
dag().call("https://pkg.fluentci.io/mysql@v0.1.4?wasm=1", "start", vec![])?;
```

## 📚 Examples
Expand Down
2 changes: 1 addition & 1 deletion mysql/fluentci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ keywords = [
]
license = "MIT"
name = "mysql"
version = "0.1.3"
version = "0.1.4"
40 changes: 40 additions & 0 deletions mysql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@ pub mod helpers;
#[plugin_fn]
pub fn start(_args: String) -> FnResult<String> {
helpers::setup()?;
let user = dag().get_env("MYSQL_USER")?;
let password = dag().get_env("MYSQL_PASSWORD")?;
let database = dag().get_env("MYSQL_DATABASE")?;
let port = dag().get_env("MYSQL_PORT")?;

if user.is_empty() {
dag().set_envs(vec![("MYSQL_USER".into(), "fluentci".into())])?;
}

if password.is_empty() {
dag().set_envs(vec![("MYSQL_PASSWORD".into(), "fluentci".into())])?;
}

if database.is_empty() {
dag().set_envs(vec![("MYSQL_DATABASE".into(), "demo".into())])?;
}

let stdout = dag()
.flox()?
.with_workdir(".fluentci/mysql")?
Expand All @@ -20,6 +36,30 @@ pub fn start(_args: String) -> FnResult<String> {
])?
.wait_on(port.parse()?, None)?
.with_exec(vec!["cat", "$MYSQL_HOME/mysql.log"])?
.with_exec(vec![
"mysql",
"-u",
"root",
"--socket=$MYSQL_HOME/mysql.socket -e \"CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE;\"",
])?
.with_exec(vec![
"mysql",
"-u",
"root",
"--socket=$MYSQL_HOME/mysql.socket -e \"CREATE USER IF NOT EXISTS '$MYSQL_USER'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';\"",
])?
.with_exec(vec![
"mysql",
"-u",
"root",
"--socket=$MYSQL_HOME/mysql.socket -e \"GRANT ALL PRIVILEGES ON $MYSQL_DATABASE.* TO '$MYSQL_USER'@'localhost';\"",
])?
.with_exec(vec![
"mysql",
"-u",
"root",
"--socket=$MYSQL_HOME/mysql.socket -e \"FLUSH PRIVILEGES;\"",
])?
.with_exec(vec!["overmind", "status"])?
.stdout()?;
Ok(stdout)
Expand Down

0 comments on commit 5bde4c5

Please sign in to comment.