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:
Piepmatz
2025-08-05 22:02:33 +02:00
committed by GitHub
parent fcdc7f3d83
commit 61a89c1834
2 changed files with 34 additions and 8 deletions

View File

@ -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())
}

View File

@ -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);
});
}