Skip to content

Commit 3d7ca1e

Browse files
Malcolm Greavesmalcolmgreaves
authored andcommitted
wip
1 parent 3202b70 commit 3d7ca1e

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

oxen-rust/src/cli/src/cmd/workspace/db_import.rs

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clap::{Arg, Command};
66
use liboxen::config::UserConfig;
77
use liboxen::error::OxenError;
88
use liboxen::model::db_import::DbImportConfig;
9-
use liboxen::model::{LocalRepository, NewCommitBody};
9+
use liboxen::model::{Commit, LocalRepository, NewCommitBody};
1010
use liboxen::repositories;
1111

1212
use crate::cmd::RunCmd;
@@ -27,34 +27,40 @@ impl RunCmd for WorkspaceDbImportCmd {
2727
.arg(
2828
Arg::new("connection-url")
2929
.long("connection-url")
30+
.short("u")
3031
.required(true)
3132
.help("Database connection URL (e.g. postgres://user@host/db, sqlite:///path/to/db)"),
3233
)
3334
.arg(
3435
Arg::new("password")
3536
.long("password")
37+
.short("p")
3638
.help("Database password (if not included in the connection URL)"),
3739
)
3840
.arg(
3941
Arg::new("query")
4042
.long("query")
43+
.short("q")
4144
.required(true)
4245
.help("SQL SELECT query to execute"),
4346
)
4447
.arg(
4548
Arg::new("output")
4649
.long("output")
50+
.short("o")
4751
.required(true)
4852
.help("Output path in the repository (e.g. data/users.csv)"),
4953
)
5054
.arg(
5155
Arg::new("format")
5256
.long("format")
57+
.short("f")
5358
.help("Output format: csv, parquet, tsv, jsonl (default: inferred from output extension)"),
5459
)
5560
.arg(
5661
Arg::new("batch-size")
5762
.long("batch-size")
63+
.short("bs")
5864
.value_parser(clap::value_parser!(usize))
5965
.help("Number of rows per batch (default: 10000)"),
6066
)
@@ -68,27 +74,53 @@ impl RunCmd for WorkspaceDbImportCmd {
6874
Arg::new("message")
6975
.long("message")
7076
.short('m')
71-
.help("Commit message"),
77+
.help("Commit message. A commit message is always included: default is automatically generated."),
7278
)
7379
}
7480

7581
async fn run(&self, args: &clap::ArgMatches) -> Result<(), OxenError> {
82+
let (config, commit_body) = parse_args(args)?;
83+
84+
println!(
85+
"Importing from {} database into {}",
86+
config.db_type,
87+
output_path.display()
88+
);
89+
90+
let commit = repositories::workspaces::db_import::import_db(
91+
&repo,
92+
&branch_name,
93+
&config,
94+
&commit_body,
95+
)
96+
.await?;
97+
98+
println!(
99+
"Successfully imported and committed as {} on branch {}",
100+
commit.id, branch_name
101+
);
102+
103+
Ok(())
104+
}
105+
106+
107+
async fn parse_args(args: &clap::ArgMatches) -> Result<(DbImportConfig, NewCommitBody), OxenError> {
76108
let repo = LocalRepository::from_current_dir()?;
77109
check_repo_migration_needed(&repo)?;
78110

79111
let connection_url = args
80112
.get_one::<String>("connection-url")
81-
.ok_or_else(|| OxenError::basic_str("--connection-url is required"))?;
113+
.ok_or_else(|| OxenError::basic_str("--connection-url (-u) is required"))?;
82114

83115
let password = args.get_one::<String>("password").cloned();
84116

85117
let query = args
86118
.get_one::<String>("query")
87-
.ok_or_else(|| OxenError::basic_str("--query is required"))?;
119+
.ok_or_else(|| OxenError::basic_str("--query (-q) is required"))?;
88120

89121
let output = args
90122
.get_one::<String>("output")
91-
.ok_or_else(|| OxenError::basic_str("--output is required"))?;
123+
.ok_or_else(|| OxenError::basic_str("--output (-o) is required"))?;
92124
let output_path = PathBuf::from(output);
93125

94126
let format = if let Some(fmt) = args.get_one::<String>("format") {
@@ -115,16 +147,6 @@ impl RunCmd for WorkspaceDbImportCmd {
115147
},
116148
};
117149

118-
let config = DbImportConfig::new(
119-
connection_url,
120-
password,
121-
query,
122-
&output_path,
123-
&format,
124-
batch_size,
125-
)?;
126-
127-
let cfg = UserConfig::get()?;
128150
let commit_message = args
129151
.get_one::<String>("message")
130152
.cloned()
@@ -136,31 +158,23 @@ impl RunCmd for WorkspaceDbImportCmd {
136158
)
137159
});
138160

161+
let config = DbImportConfig::new(
162+
connection_url,
163+
password,
164+
query,
165+
&output_path,
166+
&format,
167+
batch_size,
168+
)?;
169+
170+
let cfg = UserConfig::get()?;
171+
139172
let commit_body = NewCommitBody {
140173
message: commit_message,
141174
author: cfg.name,
142175
email: cfg.email,
143176
};
144177

145-
println!(
146-
"Importing from {} database into {}...",
147-
config.db_type,
148-
output_path.display()
149-
);
150-
151-
let commit = repositories::workspaces::db_import::import_db(
152-
&repo,
153-
&branch_name,
154-
&config,
155-
&commit_body,
156-
)
157-
.await?;
158-
159-
println!(
160-
"Successfully imported and committed as {} on branch {}",
161-
commit.id, branch_name
162-
);
163-
164-
Ok(())
178+
Ok((config, commit_body))
165179
}
166180
}

0 commit comments

Comments
 (0)