Skip to content

Commit 7108b05

Browse files
committed
support schema in table triggers
1 parent d84f8c3 commit 7108b05

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

hook.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"log/slog"
8+
"strings"
89
"sync"
910
"time"
1011

@@ -243,7 +244,7 @@ func (h *Hook[T]) processPayload(ctx context.Context, rawPayload json.RawMessage
243244

244245
func (h *Hook[T]) createNotificationFunction(ctx context.Context) error {
245246
query := fmt.Sprintf(`
246-
CREATE OR REPLACE FUNCTION pghook.notify()
247+
CREATE OR REPLACE FUNCTION pghook.%[1]s_notify()
247248
RETURNS trigger AS $$
248249
DECLARE
249250
payload jsonb;
@@ -269,7 +270,7 @@ func (h *Hook[T]) createNotificationFunction(ctx context.Context) error {
269270
);
270271
END IF;
271272
272-
PERFORM pgmq.send('%s', payload);
273+
PERFORM pgmq.send('%[1]s', payload);
273274
274275
RETURN NULL;
275276
END;
@@ -283,13 +284,16 @@ func (h *Hook[T]) createNotificationFunction(ctx context.Context) error {
283284
}
284285

285286
func (h *Hook[T]) createTableTrigger(ctx context.Context, table string) error {
287+
name := fmt.Sprintf("pghook_%s", strings.ReplaceAll(table, ".", "_"))
288+
286289
query := fmt.Sprintf(`
287-
CREATE OR REPLACE TRIGGER pghook_%s AFTER INSERT OR UPDATE OR DELETE ON %s
288-
FOR EACH ROW EXECUTE PROCEDURE pghook.notify();
289-
`, table, table)
290+
CREATE OR REPLACE TRIGGER %s AFTER INSERT OR UPDATE OR DELETE ON %s
291+
FOR EACH ROW EXECUTE PROCEDURE pghook.%s_notify();
292+
`, name, table, h.config.eventQueueName)
293+
290294
if _, err := h.querier.Exec(ctx, query); err != nil {
291295
return fmt.Errorf("create trigger failed: %w", err)
292296
}
293-
h.config.logger.Debug("created trigger", "name", fmt.Sprintf("pghook_%s", table), "table", table)
297+
h.config.logger.Debug("created trigger", "name", name, "table", table)
294298
return nil
295299
}

0 commit comments

Comments
 (0)