Skip to content

Commit 405c83e

Browse files
committed
fix: create trigger clean_expired_cache_trigger
1 parent 20f1248 commit 405c83e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

storage/database.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,17 @@ func NewDatabaseStorage(dsn string) (*DatabaseStorage, error) {
5151

5252
// Create a trigger to call the function after insert or update
5353
_, err = pool.Exec(ctx, `
54-
CREATE TRIGGER clean_expired_cache_trigger
55-
AFTER INSERT OR UPDATE ON cache
56-
FOR EACH ROW
57-
EXECUTE FUNCTION clean_expired_cache();
54+
DO $$
55+
BEGIN
56+
IF NOT EXISTS (
57+
SELECT 1 FROM pg_trigger WHERE tgname = 'clean_expired_cache_trigger'
58+
) THEN
59+
CREATE TRIGGER clean_expired_cache_trigger
60+
AFTER INSERT OR UPDATE ON cache
61+
FOR EACH ROW
62+
EXECUTE FUNCTION clean_expired_cache();
63+
END IF;
64+
END $$;
5865
`)
5966
if err != nil {
6067
pool.Close()

0 commit comments

Comments
 (0)