Debian Codegraph @

user/manager_postgres.go

raw
1 package user
2
3 import (
4 "fmt"
5 "strings"
6
7 "heckel.io/ntfy/v2/db"
8 "heckel.io/ntfy/v2/db/schema"
9 )
10
11 // PostgreSQL queries
12 const (
13 // User queries
14 postgresSelectUsersQuery = `
15 SELECT u.id, u.user_name, u.pass, u.role, u.prefs, u.sync_topic, u.provisioned, u.stats_messages, u.stats_emails, u.stats_calls, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_interval, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, u.deleted, t.id, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.calls_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_monthly_price_id, t.stripe_yearly_price_id
16 FROM "user" u
17 LEFT JOIN tier t on t.id = u.tier_id
18 ORDER BY
19 CASE u.role
20 WHEN 'admin' THEN 1
21 WHEN 'anonymous' THEN 3
22 ELSE 2
23 END, u.user_name
24 `
25 postgresSelectUserByIDQuery = `
26 SELECT u.id, u.user_name, u.pass, u.role, u.prefs, u.sync_topic, u.provisioned, u.stats_messages, u.stats_emails, u.stats_calls, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_interval, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, u.deleted, t.id, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.calls_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_monthly_price_id, t.stripe_yearly_price_id
27 FROM "user" u
28 LEFT JOIN tier t on t.id = u.tier_id
29 WHERE u.id = $1
30 `
31 postgresSelectUserByNameQuery = `
32 SELECT u.id, u.user_name, u.pass, u.role, u.prefs, u.sync_topic, u.provisioned, u.stats_messages, u.stats_emails, u.stats_calls, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_interval, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, u.deleted, t.id, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.calls_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_monthly_price_id, t.stripe_yearly_price_id
33 FROM "user" u
34 LEFT JOIN tier t on t.id = u.tier_id
35 WHERE user_name = $1
36 `
37 postgresSelectUserByNameOrPrimaryEmailQuery = `
38 SELECT u.id, u.user_name, u.pass, u.role, u.prefs, u.sync_topic, u.provisioned, u.stats_messages, u.stats_emails, u.stats_calls, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_interval, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, u.deleted, t.id, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.calls_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_monthly_price_id, t.stripe_yearly_price_id
39 FROM "user" u
40 LEFT JOIN tier t on t.id = u.tier_id
41 WHERE u.user_name = $1
42 OR u.id = (SELECT user_id FROM user_email WHERE email = $2 AND is_primary)
43 ORDER BY CASE WHEN u.user_name = $3 THEN 0 ELSE 1 END
44 LIMIT 1
45 `
46 postgresSelectUserByTokenQuery = `
47 SELECT u.id, u.user_name, u.pass, u.role, u.prefs, u.sync_topic, u.provisioned, u.stats_messages, u.stats_emails, u.stats_calls, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_interval, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, u.deleted, t.id, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.calls_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_monthly_price_id, t.stripe_yearly_price_id
48 FROM "user" u
49 JOIN user_token tk on u.id = tk.user_id
50 LEFT JOIN tier t on t.id = u.tier_id
51 WHERE tk.token = $1 AND (tk.expires = 0 OR tk.expires >= $2)
52 `
53 postgresSelectUserByStripeCustomerIDQuery = `
54 SELECT u.id, u.user_name, u.pass, u.role, u.prefs, u.sync_topic, u.provisioned, u.stats_messages, u.stats_emails, u.stats_calls, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_interval, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, u.deleted, t.id, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.calls_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_monthly_price_id, t.stripe_yearly_price_id
55 FROM "user" u
56 LEFT JOIN tier t on t.id = u.tier_id
57 WHERE u.stripe_customer_id = $1
58 `
59 postgresSelectUsernamesQuery = `
60 SELECT user_name
61 FROM "user"
62 ORDER BY
63 CASE role
64 WHEN 'admin' THEN 1
65 WHEN 'anonymous' THEN 3
66 ELSE 2
67 END, user_name
68 `
69 postgresSelectUserCountQuery = `SELECT COUNT(*) FROM "user"`
70 postgresSelectUserIDFromUsernameQuery = `SELECT id FROM "user" WHERE user_name = $1`
71 postgresInsertUserQuery = `INSERT INTO "user" (id, user_name, pass, role, sync_topic, provisioned, created) VALUES ($1, $2, $3, $4, $5, $6, $7)`
72 postgresUpdateUserPassQuery = `UPDATE "user" SET pass = $1 WHERE user_name = $2`
73 postgresUpdateUserRoleQuery = `UPDATE "user" SET role = $1 WHERE user_name = $2`
74 postgresUpdateUserProvisionedQuery = `UPDATE "user" SET provisioned = $1 WHERE user_name = $2`
75 postgresUpdateUserPrefsQuery = `UPDATE "user" SET prefs = $1 WHERE id = $2`
76 postgresUpdateUserStatsQuery = `UPDATE "user" SET stats_messages = $1, stats_emails = $2, stats_calls = $3 WHERE id = $4`
77 postgresUpdateUserStatsResetAllQuery = `UPDATE "user" SET stats_messages = 0, stats_emails = 0, stats_calls = 0`
78 postgresUpdateUserTierQuery = `UPDATE "user" SET tier_id = (SELECT id FROM tier WHERE code = $1) WHERE user_name = $2`
79 postgresUpdateUserDeletedQuery = `UPDATE "user" SET deleted = $1 WHERE id = $2`
80 postgresDeleteUserQuery = `DELETE FROM "user" WHERE user_name = $1`
81 postgresDeleteUserTierQuery = `UPDATE "user" SET tier_id = null WHERE user_name = $1`
82 postgresDeleteUsersMarkedQuery = `DELETE FROM "user" WHERE deleted < $1`
83 postgresDeleteUsersProvisionedQuery = `DELETE FROM "user" WHERE provisioned = true`
84
85 // Access queries
86 postgresSelectTopicPermsQuery = `
87 SELECT read, write
88 FROM user_access a
89 JOIN "user" u ON u.id = a.user_id
90 WHERE (u.user_name = $1 OR u.user_name = $2) AND $3 LIKE a.topic ESCAPE '\'
91 ORDER BY u.user_name DESC, LENGTH(a.topic) DESC, CASE WHEN a.write THEN 1 ELSE 0 END DESC
92 `
93 postgresSelectAccessCacheAllQuery = `
94 SELECT u.user_name, a.topic, a.read, a.write
95 FROM user_access a
96 JOIN "user" u ON u.id = a.user_id
97 `
98 postgresSelectUserAllAccessQuery = `
99 SELECT user_id, topic, read, write, provisioned
100 FROM user_access
101 ORDER BY LENGTH(topic) DESC, CASE WHEN write THEN 1 ELSE 0 END DESC, CASE WHEN read THEN 1 ELSE 0 END DESC, topic
102 `
103 postgresSelectUserAccessQuery = `
104 SELECT topic, read, write, provisioned
105 FROM user_access
106 WHERE user_id = (SELECT id FROM "user" WHERE user_name = $1)
107 ORDER BY LENGTH(topic) DESC, CASE WHEN write THEN 1 ELSE 0 END DESC, CASE WHEN read THEN 1 ELSE 0 END DESC, topic
108 `
109 postgresSelectUserReservationsQuery = `
110 SELECT a_user.topic, a_user.read, a_user.write, a_everyone.read AS everyone_read, a_everyone.write AS everyone_write
111 FROM user_access a_user
112 LEFT JOIN user_access a_everyone ON a_user.topic = a_everyone.topic AND a_everyone.user_id = (SELECT id FROM "user" WHERE user_name = $1)
113 WHERE a_user.user_id = a_user.owner_user_id
114 AND a_user.owner_user_id = (SELECT id FROM "user" WHERE user_name = $2)
115 ORDER BY a_user.topic
116 `
117 postgresSelectUserReservationsCountQuery = `
118 SELECT COUNT(*)
119 FROM user_access
120 WHERE user_id = owner_user_id
121 AND owner_user_id = (SELECT id FROM "user" WHERE user_name = $1)
122 `
123 postgresSelectUserReservationsOwnerQuery = `
124 SELECT owner_user_id
125 FROM user_access
126 WHERE topic = $1
127 AND user_id = owner_user_id
128 `
129 postgresSelectUserHasReservationQuery = `
130 SELECT COUNT(*)
131 FROM user_access
132 WHERE user_id = owner_user_id
133 AND owner_user_id = (SELECT id FROM "user" WHERE user_name = $1)
134 AND topic = $2
135 `
136 postgresSelectOtherAccessCountQuery = `
137 SELECT COUNT(*)
138 FROM user_access
139 WHERE (topic = $1 OR $2 LIKE topic ESCAPE '\')
140 AND (owner_user_id IS NULL OR owner_user_id != (SELECT id FROM "user" WHERE user_name = $3))
141 `
142 postgresUpsertUserAccessQuery = `
143 INSERT INTO user_access (user_id, topic, read, write, owner_user_id, provisioned)
144 VALUES (
145 (SELECT id FROM "user" WHERE user_name = $1),
146 $2,
147 $3,
148 $4,
149 CASE WHEN $5 = '' THEN NULL ELSE (SELECT id FROM "user" WHERE user_name = $6) END,
150 $7
151 )
152 ON CONFLICT (user_id, topic)
153 DO UPDATE SET read=excluded.read, write=excluded.write, owner_user_id=excluded.owner_user_id, provisioned=excluded.provisioned
154 `
155 postgresDeleteUserAccessQuery = `
156 DELETE FROM user_access
157 WHERE user_id = (SELECT id FROM "user" WHERE user_name = $1)
158 OR owner_user_id = (SELECT id FROM "user" WHERE user_name = $2)
159 `
160 postgresDeleteUserAccessProvisionedQuery = `DELETE FROM user_access WHERE provisioned = true`
161 postgresDeleteTopicAccessQuery = `
162 DELETE FROM user_access
163 WHERE (user_id = (SELECT id FROM "user" WHERE user_name = $1) OR owner_user_id = (SELECT id FROM "user" WHERE user_name = $2))
164 AND topic = $3
165 `
166 postgresDeleteAllAccessQuery = `DELETE FROM user_access`
167
168 // Token queries
169 postgresSelectTokenQuery = `SELECT token, label, last_access, last_origin, expires, provisioned FROM user_token WHERE user_id = $1 AND token = $2`
170 postgresSelectTokensQuery = `SELECT token, label, last_access, last_origin, expires, provisioned FROM user_token WHERE user_id = $1`
171 postgresSelectTokenCountQuery = `SELECT COUNT(*) FROM user_token WHERE user_id = $1`
172 postgresSelectAllProvisionedTokensQuery = `SELECT token, label, last_access, last_origin, expires, provisioned FROM user_token WHERE provisioned = true`
173 postgresUpsertTokenQuery = `
174 INSERT INTO user_token (user_id, token, label, last_access, last_origin, expires, provisioned)
175 VALUES ($1, $2, $3, $4, $5, $6, $7)
176 ON CONFLICT (user_id, token)
177 DO UPDATE SET label = excluded.label, expires = excluded.expires, provisioned = excluded.provisioned
178 `
179 postgresUpdateTokenQuery = `UPDATE user_token SET label = $1, expires = $2 WHERE user_id = $3 AND token = $4`
180 postgresUpdateTokenLastAccessQuery = `UPDATE user_token SET last_access = $1, last_origin = $2 WHERE token = $3`
181 postgresDeleteTokenQuery = `DELETE FROM user_token WHERE user_id = $1 AND token = $2`
182 postgresDeleteProvisionedTokenQuery = `DELETE FROM user_token WHERE token = $1`
183 postgresDeleteAllProvisionedTokensQuery = `DELETE FROM user_token WHERE provisioned = true`
184 postgresDeleteAllTokenQuery = `DELETE FROM user_token WHERE user_id = $1`
185 postgresDeleteExpiredTokensQuery = `DELETE FROM user_token WHERE expires > 0 AND expires < $1`
186 postgresDeleteExcessTokensQuery = `
187 DELETE FROM user_token
188 WHERE user_id = $1
189 AND (user_id, token) NOT IN (
190 SELECT user_id, token
191 FROM user_token
192 WHERE user_id = $2
193 ORDER BY expires DESC
194 LIMIT $3
195 )
196 `
197
198 // Tier queries
199 postgresInsertTierQuery = `
200 INSERT INTO tier (id, code, name, messages_limit, messages_expiry_duration, emails_limit, calls_limit, reservations_limit, attachment_file_size_limit, attachment_total_size_limit, attachment_expiry_duration, attachment_bandwidth_limit, stripe_monthly_price_id, stripe_yearly_price_id)
201 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
202 `
203 postgresUpdateTierQuery = `
204 UPDATE tier
205 SET name = $1, messages_limit = $2, messages_expiry_duration = $3, emails_limit = $4, calls_limit = $5, reservations_limit = $6, attachment_file_size_limit = $7, attachment_total_size_limit = $8, attachment_expiry_duration = $9, attachment_bandwidth_limit = $10, stripe_monthly_price_id = $11, stripe_yearly_price_id = $12
206 WHERE code = $13
207 `
208 postgresSelectTiersQuery = `
209 SELECT id, code, name, messages_limit, messages_expiry_duration, emails_limit, calls_limit, reservations_limit, attachment_file_size_limit, attachment_total_size_limit, attachment_expiry_duration, attachment_bandwidth_limit, stripe_monthly_price_id, stripe_yearly_price_id
210 FROM tier
211 `
212 postgresSelectTierByCodeQuery = `
213 SELECT id, code, name, messages_limit, messages_expiry_duration, emails_limit, calls_limit, reservations_limit, attachment_file_size_limit, attachment_total_size_limit, attachment_expiry_duration, attachment_bandwidth_limit, stripe_monthly_price_id, stripe_yearly_price_id
214 FROM tier
215 WHERE code = $1
216 `
217 postgresSelectTierByPriceIDQuery = `
218 SELECT id, code, name, messages_limit, messages_expiry_duration, emails_limit, calls_limit, reservations_limit, attachment_file_size_limit, attachment_total_size_limit, attachment_expiry_duration, attachment_bandwidth_limit, stripe_monthly_price_id, stripe_yearly_price_id
219 FROM tier
220 WHERE (stripe_monthly_price_id = $1 OR stripe_yearly_price_id = $2)
221 `
222 postgresDeleteTierQuery = `DELETE FROM tier WHERE code = $1`
223
224 // Phone queries
225 postgresSelectPhoneNumbersQuery = `SELECT phone_number FROM user_phone WHERE user_id = $1`
226 postgresInsertPhoneNumberQuery = `INSERT INTO user_phone (user_id, phone_number) VALUES ($1, $2)`
227 postgresDeletePhoneNumberQuery = `DELETE FROM user_phone WHERE user_id = $1 AND phone_number = $2`
228
229 // Email queries
230 postgresSelectEmailsQuery = `SELECT email, is_primary FROM user_email WHERE user_id = $1 ORDER BY email`
231 postgresInsertEmailQuery = `INSERT INTO user_email (user_id, email) VALUES ($1, $2)`
232 postgresInsertEmailIgnoreQuery = `INSERT INTO user_email (user_id, email) VALUES ($1, $2) ON CONFLICT (user_id, email) DO NOTHING`
233 postgresDeleteEmailQuery = `DELETE FROM user_email WHERE user_id = $1 AND email = $2`
234 postgresSelectPrimaryEmailQuery = `SELECT email FROM user_email WHERE user_id = $1 AND is_primary`
235 postgresSelectUserIDByPrimaryQuery = `SELECT user_id FROM user_email WHERE email = $1 AND is_primary`
236 postgresUpdateEmailSetPrimaryQuery = `UPDATE user_email SET is_primary = TRUE WHERE user_id = $1 AND email = $2`
237 postgresUpdateEmailClearPrimaryQuery = `UPDATE user_email SET is_primary = FALSE WHERE user_id = $1 AND is_primary`
238
239 // Magic link queries (email verification + password reset)
240 postgresInsertMagicLinkQuery = `INSERT INTO user_magic_link (token_hash, kind, user_id, email, expires, created) VALUES ($1, $2, $3, $4, $5, $6)`
241 postgresSelectMagicLinkByHashQuery = `SELECT token_hash, kind, user_id, email, expires, created FROM user_magic_link WHERE token_hash = $1`
242 postgresDeleteMagicLinkByHashQuery = `DELETE FROM user_magic_link WHERE token_hash = $1`
243 postgresDeleteVerifyScopeQuery = `DELETE FROM user_magic_link WHERE kind = $1 AND user_id = $2 AND email = $3`
244 postgresDeleteResetScopeQuery = `DELETE FROM user_magic_link WHERE kind = $1 AND user_id = $2`
245 postgresSelectPendingEmailsQuery = `SELECT email FROM user_magic_link WHERE kind = $1 AND user_id = $2 ORDER BY email`
246 postgresDeleteExpiredMagicLinksQuery = `DELETE FROM user_magic_link WHERE expires < $1`
247
248 // Billing queries
249 postgresUpdateBillingQuery = `
250 UPDATE "user"
251 SET stripe_customer_id = $1, stripe_subscription_id = $2, stripe_subscription_status = $3, stripe_subscription_interval = $4, stripe_subscription_paid_until = $5, stripe_subscription_cancel_at = $6
252 WHERE user_name = $7
253 `
254 )
255
256 // postgresSelectAccessCacheUsersQuery builds the per-users cache-load query
257 // with a "$1, $2, ..." IN clause sized for n usernames.
258 func postgresSelectAccessCacheUsersQuery(n int) string {
259 var sb strings.Builder
260 sb.WriteString(`SELECT u.user_name, a.topic, a.read, a.write FROM user_access a JOIN "user" u ON u.id = a.user_id WHERE u.user_name IN (`)
261 for i := 0; i < n; i++ {
262 if i > 0 {
263 sb.WriteString(",")
264 }
265 fmt.Fprintf(&sb, "$%d", i+1)
266 }
267 sb.WriteString(")")
268 return sb.String()
269 }
270
271 // NewPostgresManager creates a new Manager backed by a PostgreSQL database using an existing connection pool.
272 var postgresQueries = queries{
273 selectUserByID: postgresSelectUserByIDQuery,
274 selectUserByName: postgresSelectUserByNameQuery,
275 selectUserByNameOrPrimaryEmail: postgresSelectUserByNameOrPrimaryEmailQuery,
276 selectUserByToken: postgresSelectUserByTokenQuery,
277 selectUserByStripeCustomerID: postgresSelectUserByStripeCustomerIDQuery,
278 selectUsernames: postgresSelectUsernamesQuery,
279 selectUsers: postgresSelectUsersQuery,
280 selectUserCount: postgresSelectUserCountQuery,
281 selectUserIDFromUsername: postgresSelectUserIDFromUsernameQuery,
282 insertUser: postgresInsertUserQuery,
283 updateUserPass: postgresUpdateUserPassQuery,
284 updateUserRole: postgresUpdateUserRoleQuery,
285 updateUserProvisioned: postgresUpdateUserProvisionedQuery,
286 updateUserPrefs: postgresUpdateUserPrefsQuery,
287 updateUserStats: postgresUpdateUserStatsQuery,
288 updateUserStatsResetAll: postgresUpdateUserStatsResetAllQuery,
289 updateUserTier: postgresUpdateUserTierQuery,
290 updateUserDeleted: postgresUpdateUserDeletedQuery,
291 deleteUser: postgresDeleteUserQuery,
292 deleteUserTier: postgresDeleteUserTierQuery,
293 deleteUsersMarked: postgresDeleteUsersMarkedQuery,
294 deleteUsersProvisioned: postgresDeleteUsersProvisionedQuery,
295 selectTopicPerms: postgresSelectTopicPermsQuery,
296 selectAccessCacheAll: postgresSelectAccessCacheAllQuery,
297 selectAccessCacheUsers: postgresSelectAccessCacheUsersQuery,
298 selectUserAllAccess: postgresSelectUserAllAccessQuery,
299 selectUserAccess: postgresSelectUserAccessQuery,
300 selectUserReservations: postgresSelectUserReservationsQuery,
301 selectUserReservationsCount: postgresSelectUserReservationsCountQuery,
302 selectUserReservationsOwner: postgresSelectUserReservationsOwnerQuery,
303 selectUserHasReservation: postgresSelectUserHasReservationQuery,
304 selectOtherAccessCount: postgresSelectOtherAccessCountQuery,
305 upsertUserAccess: postgresUpsertUserAccessQuery,
306 deleteUserAccess: postgresDeleteUserAccessQuery,
307 deleteUserAccessProvisioned: postgresDeleteUserAccessProvisionedQuery,
308 deleteTopicAccess: postgresDeleteTopicAccessQuery,
309 deleteAllAccess: postgresDeleteAllAccessQuery,
310 selectToken: postgresSelectTokenQuery,
311 selectTokens: postgresSelectTokensQuery,
312 selectTokenCount: postgresSelectTokenCountQuery,
313 selectAllProvisionedTokens: postgresSelectAllProvisionedTokensQuery,
314 upsertToken: postgresUpsertTokenQuery,
315 updateToken: postgresUpdateTokenQuery,
316 updateTokenLastAccess: postgresUpdateTokenLastAccessQuery,
317 deleteToken: postgresDeleteTokenQuery,
318 deleteProvisionedToken: postgresDeleteProvisionedTokenQuery,
319 deleteAllProvisionedTokens: postgresDeleteAllProvisionedTokensQuery,
320 deleteAllToken: postgresDeleteAllTokenQuery,
321 deleteExpiredTokens: postgresDeleteExpiredTokensQuery,
322 deleteExcessTokens: postgresDeleteExcessTokensQuery,
323 insertTier: postgresInsertTierQuery,
324 selectTiers: postgresSelectTiersQuery,
325 selectTierByCode: postgresSelectTierByCodeQuery,
326 selectTierByPriceID: postgresSelectTierByPriceIDQuery,
327 updateTier: postgresUpdateTierQuery,
328 deleteTier: postgresDeleteTierQuery,
329 selectPhoneNumbers: postgresSelectPhoneNumbersQuery,
330 insertPhoneNumber: postgresInsertPhoneNumberQuery,
331 deletePhoneNumber: postgresDeletePhoneNumberQuery,
332 selectEmails: postgresSelectEmailsQuery,
333 insertEmail: postgresInsertEmailQuery,
334 insertEmailIgnore: postgresInsertEmailIgnoreQuery,
335 deleteEmail: postgresDeleteEmailQuery,
336 selectPrimaryEmail: postgresSelectPrimaryEmailQuery,
337 selectUserIDByPrimary: postgresSelectUserIDByPrimaryQuery,
338 updateEmailSetPrimary: postgresUpdateEmailSetPrimaryQuery,
339 updateEmailClearPrimary: postgresUpdateEmailClearPrimaryQuery,
340 insertMagicLink: postgresInsertMagicLinkQuery,
341 selectMagicLinkByHash: postgresSelectMagicLinkByHashQuery,
342 deleteMagicLinkByHash: postgresDeleteMagicLinkByHashQuery,
343 deleteMagicLinkEmailVerify: postgresDeleteVerifyScopeQuery,
344 deleteMagicLinkResetPassword: postgresDeleteResetScopeQuery,
345 selectPendingEmails: postgresSelectPendingEmailsQuery,
346 deleteExpiredMagicLinks: postgresDeleteExpiredMagicLinksQuery,
347 updateBilling: postgresUpdateBillingQuery,
348 }
349
350 // NewPostgresManager creates a new Manager backed by a PostgreSQL database
351 func NewPostgresManager(d *db.DB, config *Config) (*Manager, error) {
352 if err := schema.Migrate(d.Primary(), schema.Postgres, schemaStore, postgresCurrentSchemaVersion, postgresCreateTables, postgresMigrations); err != nil {
353 return nil, err
354 }
355 return newManager(d, postgresQueries, config)
356 }