mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 18:57:44 +02:00
Fully qualify the sqlite path for into sqlite
(#16349)
- related #16258 # Description In #16258 we had some trouble getting tests to work properly. After some investigation with @WindSoilder we figured out that `Table::new` inside `into_sqlite.rs` did not respect the `$env.PWD`. The underlying `open_sqlite_db` and in that `Connection::open` respects the current working directory. That one is updated via `cd` but not necessarily in tests (and we should not try that). In this PR I join the `$env.PWD` with the path passed to `into sqlite`. This PR also adds a test for the auto conversion from #16258. # User-Facing Changes Should be none, some edge cases might be fixed now.
This commit is contained in:
@ -1,9 +1,12 @@
|
||||
use crate::database::values::sqlite::{open_sqlite_db, values_to_sql};
|
||||
use crate::{
|
||||
MEMORY_DB,
|
||||
database::values::sqlite::{open_sqlite_db, values_to_sql},
|
||||
};
|
||||
use nu_engine::command_prelude::*;
|
||||
|
||||
use itertools::Itertools;
|
||||
use nu_protocol::Signals;
|
||||
use std::path::Path;
|
||||
use std::{borrow::Cow, path::Path};
|
||||
|
||||
pub const DEFAULT_TABLE_NAME: &str = "main";
|
||||
|
||||
@ -100,15 +103,25 @@ impl Table {
|
||||
pub fn new(
|
||||
db_path: &Spanned<String>,
|
||||
table_name: Option<Spanned<String>>,
|
||||
engine_state: &EngineState,
|
||||
stack: &Stack,
|
||||
) -> Result<Self, nu_protocol::ShellError> {
|
||||
let table_name = if let Some(table_name) = table_name {
|
||||
table_name.item
|
||||
} else {
|
||||
DEFAULT_TABLE_NAME.to_string()
|
||||
let table_name = table_name
|
||||
.map(|table_name| table_name.item)
|
||||
.unwrap_or_else(|| DEFAULT_TABLE_NAME.to_string());
|
||||
|
||||
let span = db_path.span;
|
||||
let db_path: Cow<'_, Path> = match db_path.item.as_str() {
|
||||
MEMORY_DB => Cow::Borrowed(Path::new(&db_path.item)),
|
||||
item => engine_state
|
||||
.cwd(Some(stack))?
|
||||
.join(item)
|
||||
.to_std_path_buf()
|
||||
.into(),
|
||||
};
|
||||
|
||||
// create the sqlite database table
|
||||
let conn = open_sqlite_db(Path::new(&db_path.item), db_path.span)?;
|
||||
let conn = open_sqlite_db(&db_path, span)?;
|
||||
|
||||
Ok(Self { conn, table_name })
|
||||
}
|
||||
@ -193,7 +206,7 @@ fn operate(
|
||||
let span = call.head;
|
||||
let file_name: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||
let table_name: Option<Spanned<String>> = call.get_flag(engine_state, stack, "table-name")?;
|
||||
let table = Table::new(&file_name, table_name)?;
|
||||
let table = Table::new(&file_name, table_name, engine_state, stack)?;
|
||||
Ok(action(engine_state, input, table, span, engine_state.signals())?.into_pipeline_data())
|
||||
}
|
||||
|
||||
|
@ -495,3 +495,16 @@ fn insert_test_rows(dirs: &Dirs, nu_table: &str, sql_query: Option<&str>, expect
|
||||
|
||||
assert_eq!(expected, actual_rows);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_auto_conversion() {
|
||||
Playground::setup("sqlite json auto conversion", |_, playground| {
|
||||
let raw = "{a_record:{foo:bar,baz:quux},a_list:[1,2,3],a_table:[[a,b];[0,1],[2,3]]}";
|
||||
nu!(cwd: playground.cwd(), "{} | into sqlite filename.db -t my_table", raw);
|
||||
let outcome = nu!(
|
||||
cwd: playground.cwd(),
|
||||
"open filename.db | get my_table.0 | to nuon --raw"
|
||||
);
|
||||
assert_eq!(outcome.out, raw);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user