-
Notifications
You must be signed in to change notification settings - Fork 0
/
quoting.go
55 lines (49 loc) · 1.25 KB
/
quoting.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
package pgxschema
import (
"hash/crc32"
"strings"
"unicode"
)
const postgresAdvisoryLockSalt = 542384964
// QuotedTableName returns the string value of the name of the migration
// tracking table after it has been quoted for Postgres
//
func QuotedTableName(schemaName, tableName string) string {
if schemaName == "" {
return QuotedIdent(tableName)
}
return QuotedIdent(schemaName) + "." + QuotedIdent(tableName)
}
// QuotedIdent transforms the provided string into a valid, quoted Postgres
// identifier. This
func QuotedIdent(ident string) string {
if ident == "" {
return ""
}
var sb strings.Builder
sb.WriteRune('"')
for _, r := range ident {
switch {
case unicode.IsSpace(r):
// Skip spaces
continue
case r == '"':
// Escape double-quotes with repeated double-quotes
sb.WriteString(`""`)
case r == ';':
// Ignore the command termination character
continue
default:
sb.WriteRune(r)
}
}
sb.WriteRune('"')
return sb.String()
}
// LockIdentifierForTable computes a hash of the migrations table's name which
// can be used as a unique name for the Postgres advisory lock
//
func LockIdentifierForTable(tableName string) int64 {
sum := crc32.ChecksumIEEE([]byte(tableName))
return int64(sum) * postgresAdvisoryLockSalt
}