-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcustom_integration_guild_counts.go
More file actions
63 lines (52 loc) · 1.7 KB
/
Copy pathcustom_integration_guild_counts.go
File metadata and controls
63 lines (52 loc) · 1.7 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
package database
import (
"context"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
)
type CustomIntegrationGuildCountsView struct {
*pgxpool.Pool
}
func newCustomIntegrationGuildCountsView(db *pgxpool.Pool) *CustomIntegrationGuildCountsView {
return &CustomIntegrationGuildCountsView{
db,
}
}
func (v CustomIntegrationGuildCountsView) Schema() string {
s := v.schema("custom_integration_guild_counts")
for _, indexSchema := range v.indexes("custom_integration_guild_counts") {
s += "\n"
s += indexSchema
}
return s
}
func (v CustomIntegrationGuildCountsView) schema(tableName string) string {
return fmt.Sprintf(`
CREATE MATERIALIZED VIEW IF NOT EXISTS %[1]s
AS
SELECT integration_id, COUNT(*) AS COUNT
FROM custom_integration_guilds
GROUP BY integration_id
WITH DATA;
`, tableName)
}
func (v CustomIntegrationGuildCountsView) indexes(tableName string) []string {
return []string{
fmt.Sprintf("CREATE UNIQUE INDEX IF NOT EXISTS %[1]s_integration_id_key ON %[1]s(integration_id);", tableName),
}
}
func (v *CustomIntegrationGuildCountsView) Refresh(ctx context.Context) error {
statements := slice(v.schema("custom_integration_guild_counts_new"))
statements = append(statements, v.indexes("custom_integration_guild_counts_new")...)
statements = append(statements,
"DROP MATERIALIZED VIEW IF EXISTS custom_integration_guild_counts;",
"ALTER MATERIALIZED VIEW custom_integration_guild_counts_new RENAME TO custom_integration_guild_counts;",
"ALTER INDEX custom_integration_guild_counts_new_integration_id_key RENAME TO custom_integration_guild_counts_integration_id_key;",
)
tx, err := transact(ctx, v.Pool, statements...)
if err != nil {
return err
}
defer tx.Rollback(ctx)
return tx.Commit(ctx)
}