Skip to content

Commit

Permalink
fix(db): make listing keys by prefix work again
Browse files Browse the repository at this point in the history
  • Loading branch information
imiric committed Apr 18, 2024
1 parent 4ea2ba4 commit 3b8373c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/app_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ func TestAppStore(t *testing.T) {
" key2 \n" +
"dev myapp/key \n"
h(assert.Equal(t, want, app.stdout.String()))

err = app.Run("ls", "app")
h(assert.NoError(t, err))
h(assert.Equal(t, "", app.stdout.String()))

err = app.Run("ls", "key")
h(assert.NoError(t, err))
h(assert.Equal(t, "key\nkey2\n", app.stdout.String()))

err = app.Run("ls", "--namespace=*", "myapp")
h(assert.NoError(t, err))
want = "NAMESPACE KEY \n" +
"dev myapp/key \n"
h(assert.Equal(t, want, app.stdout.String()))
})

t.Run("ok/rm_ls", func(t *testing.T) {
Expand Down
13 changes: 11 additions & 2 deletions db/store/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go.hackfix.me/disco/db/migrator"
"go.hackfix.me/disco/db/queries"
"go.hackfix.me/disco/db/store"
"go.hackfix.me/disco/db/types"
)

//go:embed migrations/*.sql
Expand Down Expand Up @@ -180,10 +181,18 @@ func (s *Store) List(namespace, keyPrefix string) (map[string][]string, error) {

keysPerNS := make(map[string][]string)

filter := types.NewFilter("1=1", []any{})
if keyPrefix != "" {
filter = types.NewFilter("key LIKE ? || '%'", []any{keyPrefix})
}

listNamespace := func(ns string) error {
rows, err := s.QueryContext(s.ctx, fmt.Sprintf(`SELECT key
query := fmt.Sprintf(
`SELECT key
FROM "%s"
ORDER BY key ASC`, ns))
WHERE %s
ORDER BY key ASC`, ns, filter.Where)
rows, err := s.QueryContext(s.ctx, query, filter.Args...)
if err != nil {
return err
}
Expand Down

0 comments on commit 3b8373c

Please sign in to comment.