Skip to content

Commit afd09d2

Browse files
authored
Introduce database export and import (#292)
## Usage and product changes Add database export and database import operations. Database export saves the database information (its schema and data) on the client machine as two files at provided locations: ``` # database export <name> <schema file location> <data file location> database export my-database export/my-database.typeql export/my-database.typedb ``` Database import uses the exported files to create a new database with equivalent schema and data: ``` # database export <name> <schema file location> <data file location> database import their-database export/my-database.typeql export/my-database.typedb ``` ## Implementation Add new commands similar to other `database` interfaces. Prepare data to call the respective [typedb-driver APIs](typedb/typedb-driver#758).
1 parent fa001db commit afd09d2

File tree

6 files changed

+79
-23
lines changed

6 files changed

+79
-23
lines changed

Cargo.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ features = {}
2020

2121
[dependencies.tokio]
2222
features = ["bytes", "default", "fs", "full", "io-std", "io-util", "libc", "macros", "mio", "net", "parking_lot", "process", "rt", "rt-multi-thread", "signal", "signal-hook-registry", "socket2", "sync", "time", "tokio-macros"]
23-
version = "1.44.2"
23+
version = "1.45.0"
2424
default-features = false
2525

2626
[dependencies.rustyline]
@@ -30,7 +30,7 @@ features = {}
3030

3131
[dependencies.clap]
3232
features = ["color", "default", "derive", "error-context", "help", "std", "suggestions", "usage", "wrap_help"]
33-
version = "4.5.36"
33+
version = "4.5.38"
3434
default-features = false
3535

3636
[dependencies.glob]
@@ -46,8 +46,8 @@ features = {}
4646

4747
[dependencies.typedb-driver]
4848
features = []
49+
rev = "6db6f947bbc8f47181f81f458f1f09e1985fb514"
4950
git = "https://github.com/typedb/typedb-driver"
50-
tag = "3.2.0"
5151
default-features = false
5252

5353
[dependencies.futures]
@@ -62,7 +62,7 @@ features = {}
6262

6363
[dependencies.rpassword]
6464
features = []
65-
version = "7.3.1"
65+
version = "7.4.0"
6666
default-features = false
6767

6868
[dependencies.home]

dependencies/typedb/repositories.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ def typedb_dependencies():
88
git_repository(
99
name = "typedb_dependencies",
1010
remote = "https://github.com/typedb/typedb-dependencies",
11-
commit = "ab777bf067b1930e35146fd8e25a76a4a360aa74", # sync-marker: do not remove this comment, this is used for sync-dependencies by @typedb_dependencies
11+
commit = "4ffeaabde31c41cee271cbb563f17168f4229a93", # sync-marker: do not remove this comment, this is used for sync-dependencies by @typedb_dependencies
1212
)
1313

1414
def typedb_driver():
1515
git_repository(
1616
name = "typedb_driver",
1717
remote = "https://github.com/typedb/typedb-driver",
18-
tag = "3.2.0", # sync-marker: do not remove this comment, this is used for sync-dependencies by @typedb_driver
18+
commit = "6db6f947bbc8f47181f81f458f1f09e1985fb514", # sync-marker: do not remove this comment, this is used for sync-dependencies by @typedb_driver
1919
)
2020

2121
def typeql():

src/main.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use crate::{
2828
cli::Args,
2929
completions::{database_name_completer_fn, file_completer},
3030
operations::{
31-
database_create, database_delete, database_list, database_schema, transaction_close, transaction_commit,
32-
transaction_query, transaction_read, transaction_rollback, transaction_schema, transaction_source,
33-
transaction_write, user_create, user_delete, user_list, user_update_password,
31+
database_create, database_delete, database_export, database_import, database_list, database_schema,
32+
transaction_close, transaction_commit, transaction_query, transaction_read, transaction_rollback,
33+
transaction_schema, transaction_source, transaction_write, user_create, user_delete, user_list,
34+
user_update_password,
3435
},
3536
repl::{
3637
command::{get_word, parse_one_query, CommandInput, CommandLeaf, Subcommand},
@@ -293,6 +294,31 @@ fn entry_repl(driver: Arc<TypeDBDriver>, runtime: BackgroundRuntime) -> Repl<Con
293294
"Retrieve the TypeQL representation of a database's schema.",
294295
CommandInput::new("db", get_word, None, Some(database_name_completer_fn(driver.clone(), runtime.clone()))),
295296
database_schema,
297+
))
298+
.add(CommandLeaf::new_with_inputs(
299+
"import",
300+
"Create a database with the given name based on another previously exported database.",
301+
vec![
302+
CommandInput::new("db", get_word, None, None),
303+
CommandInput::new("schema file path", get_word, None, None),
304+
CommandInput::new("data file path", get_word, None, None),
305+
],
306+
database_import,
307+
))
308+
.add(CommandLeaf::new_with_inputs(
309+
"export",
310+
"Export a database into a schema definition and a data files.",
311+
vec![
312+
CommandInput::new(
313+
"db",
314+
get_word,
315+
None,
316+
Some(database_name_completer_fn(driver.clone(), runtime.clone())),
317+
),
318+
CommandInput::new("schema file path", get_word, None, None),
319+
CommandInput::new("data file path", get_word, None, None),
320+
],
321+
database_export,
296322
));
297323

298324
let user_commands = Subcommand::new("user")

src/operations.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ pub(crate) fn database_create(context: &mut ConsoleContext, input: &[String]) ->
5252
Ok(())
5353
}
5454

55+
pub(crate) fn database_import(context: &mut ConsoleContext, input: &[String]) -> CommandResult {
56+
let driver = context.driver.clone();
57+
let db_name = input[0].clone();
58+
let schema =
59+
read_to_string(context.convert_path(&input[1])).map_err(|err| Box::new(err) as Box<dyn Error + Send>)?;
60+
let data_file_path: PathBuf = context.convert_path(&input[2]);
61+
context
62+
.background_runtime
63+
.run(async move { driver.databases().import_from_file(db_name, schema, data_file_path).await })
64+
.map_err(|err| Box::new(err) as Box<dyn Error + Send>)?;
65+
println!("Successfully imported database.");
66+
Ok(())
67+
}
68+
69+
pub(crate) fn database_export(context: &mut ConsoleContext, input: &[String]) -> CommandResult {
70+
let driver = context.driver.clone();
71+
let db_name = input[0].clone();
72+
let schema_file_path: PathBuf = context.convert_path(&input[1]);
73+
let data_file_path: PathBuf = context.convert_path(&input[2]);
74+
context
75+
.background_runtime
76+
.run(async move {
77+
let db = driver.databases().get(db_name).await?;
78+
db.export_to_file(schema_file_path, data_file_path).await
79+
})
80+
.map_err(|err| Box::new(err) as Box<dyn Error + Send>)?;
81+
println!("Successfully exported database.");
82+
Ok(())
83+
}
84+
5585
pub(crate) fn database_delete(context: &mut ConsoleContext, input: &[String]) -> CommandResult {
5686
let driver = context.driver.clone();
5787
let db_name = input[0].clone();

tool/runner/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ features = {}
1616

1717
[dependencies.clap]
1818
features = ["color", "default", "derive", "error-context", "help", "std", "suggestions", "usage", "wrap_help"]
19-
version = "4.5.36"
19+
version = "4.5.38"
2020
default-features = false
2121

2222
[dependencies.tempdir]

0 commit comments

Comments
 (0)