Skip to content

Commit

Permalink
fix: update existing cmd on conflict
Browse files Browse the repository at this point in the history
When adding a new command, and the command in fact already exists, thus,
it's an update, replace the content of the command instead of failing.
  • Loading branch information
dnaka91 committed Nov 4, 2024
1 parent b4313a1 commit 8701604
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion queries/custom_cmds/add.sql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
INSERT INTO custom_commands (source, name, content) VALUES (?, ?, ?);
INSERT INTO custom_commands (source, name, content) VALUES (?, ?, ?)
ON CONFLICT (source, name) DO UPDATE SET content = excluded.content;
15 changes: 15 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,19 @@ mod tests {

assert!(state.list_custom_commands().unwrap().is_empty());
}

#[test]
fn overwrite_command() {
let state = State::in_memory().unwrap();

state
.add_custom_command(Source::Discord, "test", "one")
.unwrap();
state
.add_custom_command(Source::Discord, "test", "two")
.unwrap();

let cmd = state.get_custom_command(Source::Discord, "test").unwrap();
assert_eq!(Some("two"), cmd.as_deref());
}
}

0 comments on commit 8701604

Please sign in to comment.