Combine action add/edit with on conflict replace

This commit is contained in:
Tim Van Baak 2025-02-12 09:49:27 -08:00
parent 7c8b4ee3a3
commit 7361fd4600
6 changed files with 12 additions and 19 deletions

View File

@ -27,6 +27,8 @@ func init() {
actionAddCmd.MarkFlagRequired("action")
}
// TODO: This is a duplicate of `action edit`, the action CLI should be simplified
func actionAdd(source string, action string, argv []string) {
if source == "" {
log.Fatal("error: --source is empty")
@ -40,7 +42,7 @@ func actionAdd(source string, action string, argv []string) {
db := openAndMigrateDb()
err := core.AddAction(db, source, action, argv)
err := core.SetAction(db, source, action, argv)
if err != nil {
log.Fatalf("error: failed to add action: %v", err)
}

View File

@ -40,7 +40,7 @@ func actionEdit(source string, action string, argv []string) {
db := openAndMigrateDb()
err := core.UpdateAction(db, source, action, argv)
err := core.SetAction(db, source, action, argv)
if err != nil {
log.Fatalf("error: failed to update action: %v", err)
}

View File

@ -16,7 +16,7 @@ func (a *argList) Scan(value interface{}) error {
return json.Unmarshal([]byte(value.(string)), a)
}
func AddAction(db DB, source string, name string, argv []string) error {
func SetAction(db DB, source string, name string, argv []string) error {
_, err := db.Exec(`
insert into actions (source, name, argv)
values (?, ?, jsonb(?))
@ -24,15 +24,6 @@ func AddAction(db DB, source string, name string, argv []string) error {
return err
}
func UpdateAction(db DB, source string, name string, argv []string) error {
_, err := db.Exec(`
update actions
set argv = jsonb(?)
where source = ? and name = ?
`, argList(argv), source, name)
return err
}
func GetActionsForSource(db DB, source string) ([]string, error) {
rows, err := db.Query(`
select name

View File

@ -7,7 +7,7 @@ import (
func TestActionCreate(t *testing.T) {
db := EphemeralDb(t)
if err := AddAction(db, "test", "hello", []string{"echo", "hello"}); err == nil {
if err := SetAction(db, "test", "hello", []string{"echo", "hello"}); err == nil {
t.Fatal("Action created for nonexistent source")
}
@ -15,13 +15,13 @@ func TestActionCreate(t *testing.T) {
t.Fatal(err)
}
if err := AddAction(db, "test", "hello", []string{"echo", "hello"}); err != nil {
if err := SetAction(db, "test", "hello", []string{"echo", "hello"}); err != nil {
t.Fatal(err)
}
if err := AddAction(db, "test", "goodbye", []string{"exit", "1"}); err != nil {
if err := SetAction(db, "test", "goodbye", []string{"exit", "1"}); err != nil {
t.Fatal(err)
}
if err := UpdateAction(db, "test", "goodbye", []string{"echo", "goodbye"}); err != nil {
if err := SetAction(db, "test", "goodbye", []string{"echo", "goodbye"}); err != nil {
t.Fatal(err)
}

View File

@ -141,7 +141,7 @@ func TestOnCreateAction(t *testing.T) {
if err := AddSource(db, "test"); err != nil {
t.Fatal(err)
}
if err := AddAction(db, "test", "on_create", []string{"true"}); err != nil {
if err := SetAction(db, "test", "on_create", []string{"true"}); err != nil {
t.Fatal(err)
}
@ -164,7 +164,7 @@ func TestOnCreateAction(t *testing.T) {
onCreate := func(argv []string) {
t.Helper()
if err := UpdateAction(db, "test", "on_create", argv); err != nil {
if err := SetAction(db, "test", "on_create", argv); err != nil {
t.Fatal(err)
}
}

View File

@ -7,7 +7,7 @@ create table actions(
source text not null,
name text not null,
argv blob not null,
primary key (source, name),
unique (source, name) on conflict replace,
foreign key (source) references sources (name) on delete cascade
) strict;
create table envs(