From 292d669164a4d1cb848a446aa5baa2c35c5d58f3 Mon Sep 17 00:00:00 2001 From: Craig Pastro Date: Thu, 19 Oct 2023 09:49:13 -0700 Subject: [PATCH] Add CreateUnloggedQueue (#27) --- pgmq.go | 12 ++++++++++++ pgmq_test.go | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pgmq.go b/pgmq.go index 1fb42a8..36e1a72 100644 --- a/pgmq.go +++ b/pgmq.go @@ -91,6 +91,18 @@ func (p *PGMQ) CreateQueue(ctx context.Context, queue string) error { return nil } +// CreateUnloggedQueue creates a new unlogged queue, which uses an unlogged +// table under the hood. This sets up the queue's tables, indexes, and +// metadata. +func (p *PGMQ) CreateUnloggedQueue(ctx context.Context, queue string) error { + _, err := p.db.Exec(ctx, "SELECT pgmq.create_unlogged($1)", queue) + if err != nil { + return wrapPostgresError(err) + } + + return nil +} + // DropQueue deletes the given queue. It deletes the queue's tables, indices, // and metadata. It will return an error if the queue does not exist. func (p *PGMQ) DropQueue(ctx context.Context, queue string) error { diff --git a/pgmq_test.go b/pgmq_test.go index 94dbcc8..2b42792 100644 --- a/pgmq_test.go +++ b/pgmq_test.go @@ -69,7 +69,7 @@ func TestMain(m *testing.M) { os.Exit(code) } -func TestDropQueue(t *testing.T) { +func TestCreateAndDropQueue(t *testing.T) { ctx := context.Background() queue := t.Name() @@ -91,6 +91,23 @@ func TestDropQueueWhichDoesNotExist(t *testing.T) { require.Error(t, err) } +func TestCreateUnloggedAndDropQueue(t *testing.T) { + ctx := context.Background() + queue := t.Name() + + err := q.CreateUnloggedQueue(ctx, queue) + require.NoError(t, err) + + _, err = q.Send(ctx, queue, testMsg1) + require.NoError(t, err) + + err = q.DropQueue(ctx, queue) + require.NoError(t, err) + + _, err = q.Send(ctx, queue, testMsg1) + require.Error(t, err) +} + func TestSend(t *testing.T) { ctx := context.Background() queue := t.Name()