-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
166 lines (148 loc) · 4.56 KB
/
schema.sql
File metadata and controls
166 lines (148 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
CREATE SCHEMA IF NOT EXISTS {{.Schema}};
CREATE TABLE IF NOT EXISTS {{.Schema}}.pkg_outbox (
id VARCHAR(255) PRIMARY KEY,
destination VARCHAR(255),
type VARCHAR(255),
payload JSONB,
status VARCHAR(50),
route VARCHAR,
delivery_attempts INTEGER NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
sent_at TIMESTAMPTZ,
next_delivery TIMESTAMPTZ,
options jsonb
);
CREATE TABLE IF NOT EXISTS {{.Schema}}.pkg_outbox_dead_letter (
id VARCHAR(255) PRIMARY KEY,
destination VARCHAR(255),
type VARCHAR(255),
payload JSONB,
status VARCHAR(50),
route VARCHAR,
delivery_attempts INTEGER NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
sent_at TIMESTAMPTZ,
next_delivery TIMESTAMPTZ,
options jsonb
);
-- Create a function to notify of new inserts into the outbox table
CREATE OR REPLACE FUNCTION {{.Schema}}.notify_outbox_change()
RETURNS trigger AS $$
BEGIN
PERFORM pg_notify('outbox_changes', NEW.id::text);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create a trigger that will invoke the notify_outbox_change function
-- after a new row is inserted into the outbox table
DO $$
BEGIN
-- Check if the trigger already exists
IF NOT EXISTS (
SELECT 1 FROM pg_trigger
WHERE NOT tgisinternal -- Exclude internally created triggers
AND tgname = 'outbox_insert_trigger'
AND tgrelid = '{{.Schema}}.pkg_outbox'::regclass
) THEN
-- Trigger does not exist, so create it
CREATE TRIGGER outbox_insert_trigger
AFTER INSERT ON {{.Schema}}.pkg_outbox
FOR EACH ROW EXECUTE FUNCTION {{.Schema}}.notify_outbox_change();
ELSE
-- Optional: output a notice if the trigger already exists
RAISE NOTICE 'Trigger already exists';
END IF;
END$$;
-- Add route column
DO $$
BEGIN
-- Check if the column does not exist in the table
IF NOT EXISTS (
SELECT FROM information_schema.columns
WHERE table_schema = '{{.Schema}}'
AND table_name = 'pkg_outbox'
AND column_name = 'route'
) THEN
-- Perform the ALTER TABLE operation
ALTER TABLE {{.Schema}}.pkg_outbox ADD COLUMN route VARCHAR;
ELSE
RAISE NOTICE 'Column already exists';
END IF;
END$$;
DO $$
BEGIN
-- Check if the column does not exist in the table
IF NOT EXISTS (
SELECT FROM information_schema.columns
WHERE table_schema = '{{.Schema}}'
AND table_name = 'pkg_outbox_dead_letter'
AND column_name = 'route'
) THEN
-- Perform the ALTER TABLE operation
ALTER TABLE {{.Schema}}.pkg_outbox_dead_letter ADD COLUMN route VARCHAR;
ELSE
RAISE NOTICE 'Column already exists dead letter';
END IF;
END$$;
-- Add next delivery column
DO $$
BEGIN
-- Check if the column does not exist in the table
IF NOT EXISTS (
SELECT FROM information_schema.columns
WHERE table_schema = '{{.Schema}}'
AND table_name = 'pkg_outbox'
AND column_name = 'next_delivery'
) THEN
-- Perform the ALTER TABLE operation
ALTER TABLE {{.Schema}}.pkg_outbox ADD COLUMN next_delivery TIMESTAMPTZ;
ELSE
RAISE NOTICE 'Column next_delivery already exists';
END IF;
END$$;
DO $$
BEGIN
-- Check if the column does not exist in the table
IF NOT EXISTS (
SELECT FROM information_schema.columns
WHERE table_schema = '{{.Schema}}'
AND table_name = 'pkg_outbox_dead_letter'
AND column_name = 'next_delivery'
) THEN
-- Perform the ALTER TABLE operation
ALTER TABLE {{.Schema}}.pkg_outbox_dead_letter ADD COLUMN next_delivery TIMESTAMPTZ;
ELSE
RAISE NOTICE 'Column next_delivery already exists dead letter';
END IF;
END$$;
-- Add options column
DO $$
BEGIN
-- Check if the column does not exist in the table
IF NOT EXISTS (
SELECT FROM information_schema.columns
WHERE table_schema = '{{.Schema}}'
AND table_name = 'pkg_outbox'
AND column_name = 'options'
) THEN
-- Perform the ALTER TABLE operation
ALTER TABLE {{.Schema}}.pkg_outbox ADD COLUMN options VARCHAR;
ELSE
RAISE NOTICE 'options column already exists';
END IF;
END$$;
DO $$
BEGIN
-- Check if the column does not exist in the table
IF NOT EXISTS (
SELECT FROM information_schema.columns
WHERE table_schema = '{{.Schema}}'
AND table_name = 'pkg_outbox_dead_letter'
AND column_name = 'options'
) THEN
-- Perform the ALTER TABLE operation
ALTER TABLE {{.Schema}}.pkg_outbox_dead_letter ADD COLUMN options VARCHAR;
ELSE
RAISE NOTICE 'options column already exists dead letter';
END IF;
END$$;