mirror of
https://codeberg.org/icewind/haze.git
synced 2026-06-03 17:14:08 +02:00
allow running db queries on sharded db
This commit is contained in:
parent
54682a06b6
commit
89280d9371
3 changed files with 73 additions and 11 deletions
51
src/args.rs
51
src/args.rs
|
|
@ -35,6 +35,7 @@ pub enum HazeArgs {
|
|||
filter: Option<String>,
|
||||
root: bool,
|
||||
command: Vec<String>,
|
||||
index: Option<String>,
|
||||
},
|
||||
/// Remove all non-pinned instances
|
||||
Clean,
|
||||
|
|
@ -193,11 +194,23 @@ impl HazeArgs {
|
|||
} else {
|
||||
false
|
||||
};
|
||||
let index = if let Some(next) = args.peek() {
|
||||
let all = next.as_ref() == "all";
|
||||
let is_number = next.as_ref().chars().all(char::is_numeric);
|
||||
if all || is_number {
|
||||
args.next().map(Into::into)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let command = args.map(S::into).collect();
|
||||
Ok(HazeArgs::Db {
|
||||
filter,
|
||||
root,
|
||||
command,
|
||||
index,
|
||||
})
|
||||
}
|
||||
HazeCommand::Clean => Ok(HazeArgs::Clean),
|
||||
|
|
@ -357,7 +370,8 @@ fn test_arg_parse() {
|
|||
HazeArgs::Db {
|
||||
filter: Some("asdasd".to_string()),
|
||||
root: false,
|
||||
command: Vec::new()
|
||||
command: Vec::new(),
|
||||
index: None,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
|
|
@ -365,7 +379,8 @@ fn test_arg_parse() {
|
|||
HazeArgs::Db {
|
||||
filter: Some("asdasd".to_string()),
|
||||
root: true,
|
||||
command: Vec::new()
|
||||
command: Vec::new(),
|
||||
index: None,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
|
|
@ -373,7 +388,8 @@ fn test_arg_parse() {
|
|||
HazeArgs::Db {
|
||||
filter: Some("asdasd".to_string()),
|
||||
root: false,
|
||||
command: vec!["select".to_string(), "1".to_string()]
|
||||
command: vec!["select".to_string(), "1".to_string()],
|
||||
index: None,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
|
|
@ -385,7 +401,34 @@ fn test_arg_parse() {
|
|||
HazeArgs::Db {
|
||||
filter: Some("asdasd".to_string()),
|
||||
root: true,
|
||||
command: vec!["select 1".to_string()]
|
||||
command: vec!["select 1".to_string()],
|
||||
index: None,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
HazeArgs::parse(
|
||||
&[],
|
||||
vec!["haze", "asdasd", "db", "root", "1", "select 1"].into_iter()
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Db {
|
||||
filter: Some("asdasd".to_string()),
|
||||
root: true,
|
||||
command: vec!["select 1".to_string()],
|
||||
index: Some("1".into()),
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
HazeArgs::parse(
|
||||
&[],
|
||||
vec!["haze", "asdasd", "db", "all", "select 1"].into_iter()
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Db {
|
||||
filter: Some("asdasd".to_string()),
|
||||
root: false,
|
||||
command: vec!["select 1".to_string()],
|
||||
index: Some("all".into()),
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
|
|
|
|||
|
|
@ -271,7 +271,13 @@ impl Database {
|
|||
cloud_id: &str,
|
||||
root: bool,
|
||||
command: &[String],
|
||||
index: Option<&str>,
|
||||
) -> Result<ExitCode> {
|
||||
let container_id = if let Some(index) = index {
|
||||
format!("{cloud_id}-db-{index}")
|
||||
} else {
|
||||
format!("{cloud_id}-db")
|
||||
};
|
||||
let command = command.join(" ");
|
||||
if command.is_empty() {
|
||||
match self.family() {
|
||||
|
|
@ -288,7 +294,7 @@ impl Database {
|
|||
DatabaseFamily::MariaDB | DatabaseFamily::Mysql => {
|
||||
exec_tty(
|
||||
docker,
|
||||
format!("{}-db", cloud_id),
|
||||
container_id,
|
||||
"mysql",
|
||||
vec![
|
||||
"mysql",
|
||||
|
|
@ -304,7 +310,7 @@ impl Database {
|
|||
DatabaseFamily::Postgres => {
|
||||
exec_tty(
|
||||
docker,
|
||||
format!("{}-db", cloud_id),
|
||||
container_id,
|
||||
"root",
|
||||
vec!["psql", "haze", "haze"],
|
||||
vec!["PGPASSWORD=haze"],
|
||||
|
|
@ -314,7 +320,7 @@ impl Database {
|
|||
DatabaseFamily::Oracle => {
|
||||
exec_tty(
|
||||
docker,
|
||||
format!("{}-db", cloud_id),
|
||||
container_id,
|
||||
"root",
|
||||
vec!["sqlplus", "system/haze"],
|
||||
Vec::<String>::default(),
|
||||
|
|
@ -339,7 +345,7 @@ impl Database {
|
|||
DatabaseFamily::MariaDB | DatabaseFamily::Mysql => {
|
||||
exec(
|
||||
docker,
|
||||
format!("{}-db", cloud_id),
|
||||
container_id,
|
||||
"mysql",
|
||||
vec![
|
||||
"mysql",
|
||||
|
|
@ -358,7 +364,7 @@ impl Database {
|
|||
DatabaseFamily::Postgres => {
|
||||
exec(
|
||||
docker,
|
||||
format!("{}-db", cloud_id),
|
||||
container_id,
|
||||
"root",
|
||||
vec!["psql", "haze", "haze", "-c", &command],
|
||||
vec!["PGPASSWORD=haze"],
|
||||
|
|
@ -369,7 +375,7 @@ impl Database {
|
|||
DatabaseFamily::Oracle => {
|
||||
exec(
|
||||
docker,
|
||||
format!("{}-db", cloud_id),
|
||||
container_id,
|
||||
"root",
|
||||
vec!["sqlplus", "system/haze"],
|
||||
Vec::<String>::default(),
|
||||
|
|
|
|||
15
src/main.rs
15
src/main.rs
|
|
@ -166,9 +166,22 @@ async fn main() -> Result<ExitCode> {
|
|||
filter,
|
||||
root,
|
||||
command,
|
||||
index,
|
||||
} => {
|
||||
let cloud = Cloud::get_by_filter(&docker, filter, &config).await?;
|
||||
cloud.db().exec(&docker, &cloud.id, root, &command).await?;
|
||||
if Some("all") == index.as_deref() {
|
||||
for index in ["1", "2", "3", "4"] {
|
||||
cloud
|
||||
.db()
|
||||
.exec(&docker, &cloud.id, root, &command, Some(index))
|
||||
.await?;
|
||||
}
|
||||
} else {
|
||||
cloud
|
||||
.db()
|
||||
.exec(&docker, &cloud.id, root, &command, index.as_deref())
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
HazeArgs::Open { filter } => {
|
||||
let cloud = Cloud::get_by_filter(&docker, filter, &config).await?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue