This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
/
helpers.go
187 lines (160 loc) · 4.08 KB
/
helpers.go
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
package pgslice
import (
"fmt"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"github.com/urfave/cli"
"os"
"regexp"
"strings"
"time"
)
func CreateTable(name string) Table {
schema := "public"
if strings.Contains(name, ".") {
parts := strings.SplitN(name, ".", 2)
schema = parts[0]
name = parts[1]
}
return Table{Schema: schema, Name: name}
}
func Connection(ctx *cli.Context) (*sqlx.DB, error) {
url := ctx.String("url")
if url == "" {
url = os.Getenv("PGSLICE_URL")
}
return sqlx.Connect("postgres", url)
}
func ServerVersionNum(db *sqlx.DB) int {
var num int
db.Get(&num, "SHOW server_version_num")
return num
}
func QuoteTable(table Table) string {
return strings.Join([]string{QuoteIdent(table.Schema), QuoteIdent(table.Name)}, ".")
}
func QuoteColumns(key []string) string {
quotedKey := make([]string, len(key))
for i, k := range key {
quotedKey[i] = QuoteIdent(k)
}
return strings.Join(quotedKey, ", ")
}
func QuoteIdent(column string) string {
return pq.QuoteIdentifier(column)
}
func Contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func RoundDate(t time.Time, period string) time.Time {
if period == "day" {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC)
} else if period == "month" {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC)
}
return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, time.UTC)
}
func NameFormat(period string) string {
if period == "day" {
return "20060102"
} else if period == "month" {
return "200601"
}
return "2006"
}
func SQLDate(time time.Time, cast string, addCast bool) string {
strFmt := "2006-01-02"
if cast == "timestamptz" {
strFmt = "2016-01-02 15:04:05 UTC"
}
str := fmt.Sprintf("'%s'", time.Format(strFmt))
if addCast {
return fmt.Sprintf("%s::%s", str, cast)
}
return str
}
func AdvanceDate(date time.Time, period string, count int) time.Time {
if period == "day" {
return date.AddDate(0, 0, count)
} else if period == "month" {
return date.AddDate(0, count, 0)
} else {
return date.AddDate(count, 0, 0)
}
}
func QuoteNoSchema(table Table) string {
return QuoteIdent(table.Name)
}
func Abort(message string) error {
return cli.NewExitError(message, 1)
}
func RunQueries(db *sqlx.DB, queries []string, ctx *cli.Context) error {
queries = append([]string{"BEGIN;"}, queries...)
queries = append(queries, "COMMIT;")
return RunQueriesWithoutTransaction(db, queries, ctx)
}
func LogSQL(s string) {
fmt.Println(s)
}
func RunQueriesWithoutTransaction(db *sqlx.DB, queries []string, ctx *cli.Context) error {
for _, query := range queries {
err := RunQuery(db, query, ctx)
if err != nil {
return err
}
}
return nil
}
func RunQuery(db *sqlx.DB, query string, ctx *cli.Context) error {
LogSQL(query)
LogSQL("")
if !ctx.Bool("dry-run") {
_, err := db.Exec(query)
if err != nil {
return err
}
}
return nil
}
func MakeIndexDef(def string, table Table) string {
re1 := regexp.MustCompile(` ON \S+ USING `)
re2 := regexp.MustCompile(` INDEX .+ ON `)
def = re1.ReplaceAllString(def, fmt.Sprintf(" ON %s USING ", QuoteTable(table)))
def = re2.ReplaceAllString(def, " INDEX ON ")
return def + ";"
}
func MakeFkDef(def string, table Table) string {
return "ALTER TABLE " + QuoteTable(table) + " ADD " + def + ";"
}
func PartitionDate(partition Table, nameFormat string) time.Time {
parts := strings.Split(partition.Name, "_")
day, _ := time.Parse(nameFormat, parts[len(parts)-1])
return day
}
func FetchSettings(db *sqlx.DB, originalTable Table, table Table) (string, string, string, bool) {
var field string
var period string
var cast string
triggerName := originalTable.TriggerName()
triggerComment := table.FetchTrigger(db, triggerName)
comment := triggerComment
if comment == "" {
comment = table.FetchComment(db)
}
if comment != "" {
parts := make([]string, 3)
for i, part := range strings.Split(comment, ",") {
parts[i] = strings.Split(part, ":")[1]
}
field = parts[0]
period = parts[1]
cast = parts[2]
}
declarative := triggerComment == ""
return period, field, cast, declarative
}