Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/backend/database_backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::database_schema::{
CURRENT_SCHEMA_VERSION,
};
use super::database_tables::{
full_backup_table_is_entirely_local, is_portable_backup_setting_key,
is_required_full_backup_table, portable_backup_row, FULL_BACKUP_TABLES,
};
use super::database_values::{json_value_to_sql, sqlite_value_to_json};
Expand Down Expand Up @@ -302,15 +303,16 @@ pub(crate) fn insert_portable_full_backup_rows(
parsed: &ParsedFullBackup,
) -> InventoryResult<()> {
for table in FULL_BACKUP_TABLES {
if full_backup_table_is_entirely_local(table) {
continue;
}
let Some(rows) = parsed.tables.get(table) else {
continue;
};
let allowed_columns = table_columns(conn, table)?;

for (row_index, row) in rows.iter().enumerate() {
if !full_backup_table_is_entirely_local(table)
&& !row.keys().any(|column| allowed_columns.contains(column))
{
if !row.keys().any(|column| allowed_columns.contains(column)) {
return Err(unrecognized_backup_row_error(table, row_index));
}

Expand Down Expand Up @@ -354,13 +356,6 @@ fn insert_portable_backup_row(
Ok(())
}

fn full_backup_table_is_entirely_local(table: &str) -> bool {
matches!(
table,
"trusted_lan_pairings" | "trusted_lan_paired_browsers" | "sync_queue"
)
}

fn unrecognized_backup_row_error(table: &str, row_index: usize) -> InventoryError {
InventoryError::Db(format!(
"Backup row {} for `{table}` contains no recognized importable columns",
Expand Down Expand Up @@ -432,13 +427,25 @@ pub(crate) fn export_full_backup_content(conn: &rusqlite::Connection) -> Invento
}

fn export_table_rows(conn: &rusqlite::Connection, table: &str) -> InventoryResult<Vec<Value>> {
if full_backup_table_is_entirely_local(table) {
return Ok(Vec::new());
}
let query = format!("SELECT * FROM {table}");
let mut stmt = conn.prepare(&query)?;
let columns: Vec<String> = stmt.column_names().map(str::to_owned).collect();

let mut rows = stmt.query([])?;
let mut output = Vec::new();
while let Some(row) = rows.next()? {
if table == "settings"
&& !row
.get_ref("key")?
.as_str()
.ok()
.is_some_and(is_portable_backup_setting_key)
{
continue;
}
let mut object = Map::new();
for (index, column_name) in columns.iter().enumerate() {
let value = row.get_ref(index)?;
Expand Down Expand Up @@ -485,3 +492,20 @@ mod document_error_tests {
}
}
}

#[cfg(test)]
mod local_backup_tests {
use super::*;

#[test]
fn full_backup_skips_local_tables_without_querying_them() {
let db = rusqlite::Connection::open_in_memory().unwrap();
for table in [
"trusted_lan_pairings",
"trusted_lan_paired_browsers",
"sync_queue",
] {
assert!(export_table_rows(&db, table).unwrap().is_empty());
}
}
}
25 changes: 18 additions & 7 deletions src/backend/database_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,15 @@ pub(crate) fn portable_backup_row(
mut row: Map<String, Value>,
) -> Option<Map<String, Value>> {
match table {
"trusted_lan_pairings" | "trusted_lan_paired_browsers" | "sync_queue" => return None,
"settings" if !is_portable_backup_setting_row(&row) => return None,
table if full_backup_table_is_entirely_local(table) => return None,
"settings"
if !row
.get("key")
.and_then(Value::as_str)
.is_some_and(is_portable_backup_setting_key) =>
{
return None
}
"printers" => {
// These legacy columns contain device-local connection material and
// must never travel with a portable library backup.
Expand All @@ -89,12 +96,16 @@ pub(crate) fn portable_backup_row(
Some(row)
}

fn is_portable_backup_setting_row(row: &Map<String, Value>) -> bool {
let Some(key) = row.get("key").and_then(Value::as_str).map(str::trim) else {
return false;
};
pub(crate) fn full_backup_table_is_entirely_local(table: &str) -> bool {
matches!(
table,
"trusted_lan_pairings" | "trusted_lan_paired_browsers" | "sync_queue"
)
}

pub(crate) fn is_portable_backup_setting_key(key: &str) -> bool {
matches!(
key,
key.trim(),
"active_printer_id"
| "default_purchase_currency"
| "filament_price_standards_json"
Expand Down
Loading