-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.go
More file actions
357 lines (342 loc) · 14.9 KB
/
database.go
File metadata and controls
357 lines (342 loc) · 14.9 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package database
import (
"context"
"time"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
const defaultTransactionTimeout = time.Second * 3
type Database struct {
pool *pgxpool.Pool
ActiveLanguage *ActiveLanguage
ArchiveChannel *ArchiveChannel
AuditLog *AuditLogTable
ArchiveMessages *ArchiveMessages
AutoClose *AutoCloseTable
AutoCloseExclude *AutoCloseExclude
Blacklist *Blacklist
BotStaff *BotStaff
CategoryUpdateQueue *CategoryUpdateQueue
ChannelCategory *ChannelCategory
ClaimSettings *ClaimSettingsTable
CloseConfirmation *CloseConfirmation
CloseReason *CloseMetadataTable
CloseRequest *CloseRequestTable
CustomIntegrations *CustomIntegrationTable
CustomIntegrationGuildCounts *CustomIntegrationGuildCountsView
CustomIntegrationGuilds *CustomIntegrationGuildsTable
CustomIntegrationHeaders *CustomIntegrationHeadersTable
CustomIntegrationPlaceholders *CustomIntegrationPlaceholdersTable
CustomIntegrationSecretValues *CustomIntegrationSecretValuesTable
CustomIntegrationSecrets *CustomIntegrationSecretsTable
CustomColours *CustomColours
DashboardUsers *DashboardUsersTable
ArchiveDmMessages *ArchiveDmMessages
DiscordEntitlements *DiscordEntitlements
DiscordStoreSkus *DiscordStoreSkus
EmbedFields *EmbedFieldsTable
Embeds *EmbedsTable
Entitlements *Entitlements
ExitSurveyResponses *ExitSurveyResponses
Experiment *ExperimentTable
FeedbackEnabled *FeedbackEnabled
FirstResponseTime *FirstResponseTime
FormInput *FormInputTable
FormInputOption *FormInputOptionTable
Forms *FormsTable
FormInputApiConfig *FormInputApiConfigTable
FormInputApiHeaders *FormInputApiHeaderTable
GdprLogs *GDPRLogsTable
GlobalBlacklist *GlobalBlacklist
GuildLeaveTime *GuildLeaveTime
GuildMetadata *GuildMetadataTable
ImportLogs *ImportLogsTable
ImportMappingTable *ImportMappingTable
LegacyPremiumEntitlementGuilds *LegacyPremiumEntitlementGuilds
LegacyPremiumEntitlements *LegacyPremiumEntitlements
MultiPanels *MultiPanelTable
MultiPanelTargets *MultiPanelTargets
MultiServerSkus *MultiServerSkus
NamingScheme *TicketNamingScheme
OnCall *OnCall
Panel *PanelTable
PanelAccessControlRules *PanelAccessControlRules
PanelRoleMentions *PanelRoleMentions
PanelSupportHours *PanelSupportHoursTable
PanelSupportHoursSettings *PanelSupportHoursSettingsTable
PanelTeams *PanelTeamsTable
PanelTicketPermissions *PanelTicketPermissionsTable
PanelUserMention *PanelUserMention
PanelHereMention *PanelHereMention
Participants *ParticipantTable
PatreonEntitlements *PatreonEntitlements
Permissions *Permissions
PremiumGuilds *PremiumGuilds
PremiumKeys *PremiumKeys
RoleBlacklist *RoleBlacklist
RolePermissions *RolePermissions
ServerBlacklist *ServerBlacklist
ServiceRatings *ServiceRatings
Settings *SettingsTable
StaffOverride *StaffOverride
SubscriptionSkus *SubscriptionSkus
SupportTeam *SupportTeamTable
SupportTeamMembers *SupportTeamMembersTable
SupportTeamPermissions *SupportTeamPermissionsTable
SupportTeamRoles *SupportTeamRolesTable
Tag *TagsTable
TicketClaims *TicketClaims
TicketLastMessage *TicketLastMessageTable
TicketLimit *TicketLimit
TicketMembers *TicketMembers
TicketPermissions *TicketPermissionsTable
Tickets *TicketTable
UsedKeys *UsedKeys
UsersCanClose *UsersCanClose
UserGuilds *UserGuildsTable
VoteCredits *VoteCredits
Votes *Votes
Webhooks *WebhookTable
WelcomeMessages *WelcomeMessages
TicketLabels *TicketLabelsTable
TicketLabelAssignments *TicketLabelAssignmentsTable
Whitelabel *WhitelabelBotTable
WhitelabelErrors *WhitelabelErrors
WhitelabelGuilds *WhitelabelGuilds
WhitelabelStatuses *WhitelabelStatuses
WhitelabelUsers *WhitelabelUsers
}
func NewDatabase(pool *pgxpool.Pool) *Database {
db := &Database{
pool: pool,
ActiveLanguage: newActiveLanguage(pool),
ArchiveChannel: newArchiveChannel(pool),
AuditLog: newAuditLogTable(pool),
ArchiveMessages: newArchiveMessages(pool),
AutoClose: newAutoCloseTable(pool),
AutoCloseExclude: newAutoCloseExclude(pool),
Blacklist: newBlacklist(pool),
BotStaff: newBotStaff(pool),
CategoryUpdateQueue: newCategoryUpdateQueueTable(pool),
ChannelCategory: newChannelCategory(pool),
ClaimSettings: newClaimSettingsTable(pool),
CloseConfirmation: newCloseConfirmation(pool),
CloseReason: newCloseReasonTable(pool),
CloseRequest: newCloseRequestTable(pool),
CustomIntegrations: newCustomIntegrationTable(pool),
CustomIntegrationGuildCounts: newCustomIntegrationGuildCountsView(pool),
CustomIntegrationGuilds: newCustomIntegrationGuildsTable(pool),
CustomIntegrationHeaders: newCustomIntegrationHeadersTable(pool),
CustomIntegrationPlaceholders: newCustomIntegrationPlaceholdersTable(pool),
CustomIntegrationSecretValues: newCustomIntegrationSecretValuesTable(pool),
CustomIntegrationSecrets: newCustomIntegrationSecretsTable(pool),
CustomColours: newCustomColours(pool),
DashboardUsers: newDashboardUsersTable(pool),
ArchiveDmMessages: newArchiveDmMessages(pool),
DiscordEntitlements: newDiscordEntitlementsTable(pool),
DiscordStoreSkus: newDiscordStoreSkusTable(pool),
EmbedFields: newEmbedFieldsTable(pool),
Embeds: newEmbedsTable(pool),
Entitlements: newEntitlementsTable(pool),
ExitSurveyResponses: newExitSurveyResponses(pool),
Experiment: newExperimentTable(pool),
FeedbackEnabled: newFeedbackEnabled(pool),
FirstResponseTime: newFirstResponseTime(pool),
FormInput: newFormInputTable(pool),
Forms: newFormsTable(pool),
FormInputApiConfig: newFormInputApiConfigTable(pool),
FormInputApiHeaders: newFormInputApiHeaderTable(pool),
FormInputOption: newFormInputOptionTable(pool),
GdprLogs: newGDPRLogs(pool),
GlobalBlacklist: newGlobalBlacklist(pool),
GuildLeaveTime: newGuildLeaveTime(pool),
GuildMetadata: newGuildMetadataTable(pool),
ImportLogs: newImportLogs(pool),
ImportMappingTable: newImportMapping(pool),
LegacyPremiumEntitlementGuilds: newLegacyPremiumEntitlementGuildsTable(pool),
LegacyPremiumEntitlements: newLegacyPremiumEntitlement(pool),
MultiPanels: newMultiMultiPanelTable(pool),
MultiPanelTargets: newMultiPanelTargets(pool),
MultiServerSkus: newMultiServerSkusTable(pool),
NamingScheme: newTicketNamingScheme(pool),
OnCall: newOnCall(pool),
Panel: newPanelTable(pool),
PanelAccessControlRules: newPanelAccessControlRules(pool),
PanelRoleMentions: newPanelRoleMentions(pool),
PanelSupportHours: newPanelSupportHoursTable(pool),
PanelSupportHoursSettings: newPanelSupportHoursSettingsTable(pool),
PanelTeams: newPanelTeamsTable(pool),
PanelTicketPermissions: newPanelTicketPermissionsTable(pool),
PanelUserMention: newPanelUserMention(pool),
PanelHereMention: newPanelHereMention(pool),
Participants: newParticipantTable(pool),
PatreonEntitlements: newPatreonEntitlements(pool),
Permissions: newPermissions(pool),
PremiumGuilds: newPremiumGuilds(pool),
PremiumKeys: newPremiumKeys(pool),
RoleBlacklist: newRoleBlacklist(pool),
RolePermissions: newRolePermissions(pool),
ServerBlacklist: newServerBlacklist(pool),
ServiceRatings: newServiceRatings(pool),
Settings: newSettingsTable(pool),
StaffOverride: newStaffOverride(pool),
SubscriptionSkus: newSubscriptionSkusTable(pool),
SupportTeam: newSupportTeamTable(pool),
SupportTeamMembers: newSupportTeamMembersTable(pool),
SupportTeamPermissions: newSupportTeamPermissionsTable(pool),
SupportTeamRoles: newSupportTeamRolesTable(pool),
Tag: newTag(pool),
TicketClaims: newTicketClaims(pool),
TicketLastMessage: newTicketLastMessageTable(pool),
TicketLimit: newTicketLimit(pool),
TicketMembers: newTicketMembers(pool),
TicketPermissions: newTicketPermissionsTable(pool),
Tickets: newTicketTable(pool),
UsedKeys: newUsedKeys(pool),
UsersCanClose: newUsersCanClose(pool),
UserGuilds: newUserGuildsTable(pool),
VoteCredits: newVoteCreditsTable(pool),
Votes: newVotes(pool),
Webhooks: newWebhookTable(pool),
WelcomeMessages: newWelcomeMessages(pool),
TicketLabels: newTicketLabelsTable(pool),
TicketLabelAssignments: newTicketLabelAssignmentsTable(pool),
Whitelabel: newWhitelabelBotTable(pool),
WhitelabelErrors: newWhitelabelErrors(pool),
WhitelabelGuilds: newWhitelabelGuilds(pool),
WhitelabelStatuses: newWhitelabelStatuses(pool),
WhitelabelUsers: newWhitelabelUsers(pool),
}
return db
}
func (d *Database) BeginTx(ctx context.Context) (pgx.Tx, error) {
return d.pool.Begin(ctx)
}
func (d *Database) WithTx(ctx context.Context, f func(tx pgx.Tx) error) error {
tx, err := d.pool.Begin(ctx)
if err != nil {
return err
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), defaultTransactionTimeout)
defer cancel()
tx.Rollback(ctx)
}()
if err := f(tx); err != nil {
return err
}
return tx.Commit(ctx)
}
func (d *Database) CreateTables(ctx context.Context, pool *pgxpool.Pool) {
mustCreate(ctx, pool,
d.ActiveLanguage,
d.ArchiveChannel,
d.AutoClose,
d.Blacklist,
d.BotStaff,
d.ChannelCategory,
d.ClaimSettings,
d.CloseConfirmation,
d.CustomIntegrations,
d.CustomIntegrationGuilds,
d.CustomIntegrationGuildCounts,
d.CustomIntegrationHeaders,
d.CustomIntegrationPlaceholders,
d.CustomIntegrationSecrets,
d.CustomIntegrationSecretValues,
d.CustomColours,
d.DashboardUsers,
d.Embeds,
d.EmbedFields, // depends on embeds
d.Entitlements,
d.Experiment,
d.DiscordEntitlements, // depends on entitlements
d.DiscordStoreSkus, // depends on skus
d.SubscriptionSkus, // depends on skus
d.FeedbackEnabled,
d.Forms,
d.FormInput, // depends on forms
d.FormInputOption, // depends on form inputs
d.FormInputApiConfig, // depends on form inputs
d.FormInputApiHeaders, // depends on form input api config
d.GdprLogs,
d.GlobalBlacklist,
d.GuildLeaveTime,
d.GuildMetadata,
d.ImportLogs,
d.ImportMappingTable,
d.LegacyPremiumEntitlements,
d.LegacyPremiumEntitlementGuilds,
d.MultiPanels,
d.MultiServerSkus,
d.NamingScheme,
d.OnCall,
d.Panel,
d.PanelTicketPermissions, // must be created after panels table
d.PanelAccessControlRules, // must be created after panels table
d.MultiPanelTargets, // must be created after panels table
d.PanelRoleMentions,
d.PanelSupportHours, // must be created after panels table
d.PanelSupportHoursSettings, // must be created after panels table
d.PanelUserMention,
d.PanelHereMention,
d.PatreonEntitlements,
d.Permissions,
d.PremiumGuilds,
d.PremiumKeys,
d.RoleBlacklist,
d.RolePermissions,
d.ServerBlacklist,
d.Settings,
d.StaffOverride,
d.SupportTeam,
d.SupportTeamMembers,
d.SupportTeamRoles,
d.SupportTeamPermissions, // must be created after support_team table
d.PanelTeams, // Must be created after panels & support teams tables
d.Tag,
d.TicketLimit,
d.TicketPermissions,
d.Tickets, // Must be created before members table
d.TicketLastMessage, // Must be created after Tickets table
d.Participants, // Must be created after Tickets table
d.AutoCloseExclude, // Must be created after Tickets table
d.CloseReason, // Must be created after Tickets table
d.CloseRequest, // Must be created after Tickets table
d.ServiceRatings, // Must be created after Tickets table
d.ExitSurveyResponses, // Must be created after Tickets table
d.ArchiveMessages, // Must be created after Tickets table
d.ArchiveDmMessages, // Must be created after Tickets table
d.CategoryUpdateQueue, // Must be created after Tickets table
d.TicketLabels, // Must be created after Tickets table
d.TicketLabelAssignments, // Must be created after Tickets and TicketLabels tables
d.FirstResponseTime,
d.TicketMembers,
d.TicketClaims,
d.UsedKeys,
d.UsersCanClose,
d.UserGuilds,
d.VoteCredits,
d.Votes,
d.Webhooks,
d.WelcomeMessages,
d.Whitelabel,
d.WhitelabelErrors,
d.WhitelabelGuilds,
d.WhitelabelStatuses,
d.WhitelabelUsers,
d.AuditLog,
)
}
func (d *Database) Views() []View {
return []View{
d.CustomIntegrationGuildCounts,
}
}
func mustCreate(ctx context.Context, pool *pgxpool.Pool, tables ...Table) {
for _, table := range tables {
if _, err := pool.Exec(ctx, table.Schema()); err != nil {
panic(err)
}
}
}