From 1f477c8eb14567a9628bd7782fa1fadc75313b17 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 7 Jan 2025 10:28:26 -0600 Subject: [PATCH] fix `stor reset` when there are foreign keys (#14772) # Description This PR fixes a problem with `stor reset`. That problem was that it called drop_all_tables which just iterated through the tables and dropped them one by one. This works as long as there are no foreign keys or if the tables are dropped in the "right" order. It doesn't work in most cases since you have to know what order to drop tables in. So, this PR turns off foreign key constraints, then drops all the tables, then turns the foreign key constraints back on, which seems to work well... so far. :) # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-command/src/stor/reset.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/stor/reset.rs b/crates/nu-command/src/stor/reset.rs index dc68a83188..381f2faf63 100644 --- a/crates/nu-command/src/stor/reset.rs +++ b/crates/nu-command/src/stor/reset.rs @@ -49,9 +49,25 @@ impl Command for StorReset { )); if let Ok(conn) = db.open_connection() { + conn.execute("PRAGMA foreign_keys = OFF", []) + .map_err(|err| ShellError::GenericError { + error: "Failed to turn off foreign_key protections for reset".into(), + msg: err.to_string(), + span: Some(Span::test_data()), + help: None, + inner: vec![], + })?; db.drop_all_tables(&conn) .map_err(|err| ShellError::GenericError { - error: "Failed to open SQLite connection in memory from reset".into(), + error: "Failed to drop all tables in memory from reset".into(), + msg: err.to_string(), + span: Some(Span::test_data()), + help: None, + inner: vec![], + })?; + conn.execute("PRAGMA foreign_keys = ON", []) + .map_err(|err| ShellError::GenericError { + error: "Failed to turn on foreign_key protections for reset".into(), msg: err.to_string(), span: Some(Span::test_data()), help: None,