-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_reader.go
115 lines (95 loc) · 3.09 KB
/
migration_reader.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
package pgutil
import (
"fmt"
"regexp"
"strings"
)
type Definition struct {
ID int
Name string
UpQuery Q
DownQuery Q
IndexMetadata *IndexMetadata
}
type IndexMetadata struct {
TableName string
IndexName string
}
type MigrationReader interface {
ReadAll() ([]RawDefinition, error)
}
type MigrationReaderFunc func() ([]RawDefinition, error)
func (f MigrationReaderFunc) ReadAll() ([]RawDefinition, error) {
return f()
}
type RawDefinition struct {
ID int
Name string
RawUpQuery string
RawDownQuery string
}
var (
keyword = func(pattern string) string { return phrase(pattern) }
phrase = func(patterns ...string) string { return strings.Join(patterns, `\s+`) + `\s+` }
opt = func(pattern string) string { return `(?:` + pattern + `)?` }
capturedIdentifierPattern = `([a-zA-Z0-9$_]+|"(?:[^"]+)")`
createIndexConcurrentlyPatternHead = strings.Join([]string{
keyword(`CREATE`),
opt(keyword(`UNIQUE`)),
keyword(`INDEX`),
opt(keyword(`CONCURRENTLY`)),
opt(phrase(`IF`, `NOT`, `EXISTS`)),
capturedIdentifierPattern, // capture index name
`\s+`,
keyword(`ON`),
opt(keyword(`ONLY`)),
capturedIdentifierPattern, // capture table name
}, ``)
createIndexConcurrentlyPattern = regexp.MustCompile(createIndexConcurrentlyPatternHead)
createIndexConcurrentlyPatternAll = regexp.MustCompile(createIndexConcurrentlyPatternHead + "[^;]+;")
)
func ReadMigrations(reader MigrationReader) (definitions []Definition, _ error) {
rawDefinitions, err := reader.ReadAll()
if err != nil {
return nil, err
}
ids := map[int]struct{}{}
for _, rawDefinition := range rawDefinitions {
if _, ok := ids[rawDefinition.ID]; ok {
return nil, fmt.Errorf("duplicate migration identifier %d", rawDefinition.ID)
}
ids[rawDefinition.ID] = struct{}{}
var indexMetadata *IndexMetadata
prunedUp := removeComments(rawDefinition.RawUpQuery)
prunedDown := removeComments(rawDefinition.RawDownQuery)
if matches := createIndexConcurrentlyPattern.FindStringSubmatch(prunedUp); len(matches) > 0 {
if strings.TrimSpace(createIndexConcurrentlyPatternAll.ReplaceAllString(prunedUp, "")) != "" {
return nil, fmt.Errorf(`"create index concurrently" is not the only statement in the up migration`)
}
indexMetadata = &IndexMetadata{
TableName: matches[2],
IndexName: matches[1],
}
}
if len(createIndexConcurrentlyPattern.FindAllString(prunedDown, 1)) > 0 {
return nil, fmt.Errorf(`"create index concurrently" is not allowed in down migrations`)
}
definitions = append(definitions, Definition{
ID: rawDefinition.ID,
Name: rawDefinition.Name,
UpQuery: RawQuery(rawDefinition.RawUpQuery),
DownQuery: RawQuery(rawDefinition.RawDownQuery),
IndexMetadata: indexMetadata,
})
}
return definitions, nil
}
func removeComments(query string) string {
var filtered []string
for _, line := range strings.Split(query, "\n") {
if line := strings.TrimSpace(strings.Split(line, "--")[0]); line != "" {
filtered = append(filtered, line)
}
}
return strings.TrimSpace(strings.Join(filtered, "\n"))
}