Add test for server type and version detection (#612)

This commit is contained in:
Dan Sosedoff
2022-12-08 13:33:38 -06:00
committed by GitHub
parent 1754274d46
commit 0008842a68
3 changed files with 81 additions and 24 deletions

View File

@@ -12,6 +12,14 @@ var (
// Comment regular expressions
reSlashComment = regexp.MustCompile(`(?m)/\*.+\*/`)
reDashComment = regexp.MustCompile(`(?m)--.+`)
// Postgres version signature
postgresSignature = regexp.MustCompile(`(?i)postgresql ([\d\.]+)\s?`)
postgresType = "PostgreSQL"
// Cockroach version signature
cockroachSignature = regexp.MustCompile(`(?i)cockroachdb ccl v([\d\.]+)\s?`)
cockroachType = "CockroachDB"
)
// Get short version from the string
@@ -24,6 +32,24 @@ func getMajorMinorVersion(str string) string {
return strings.Join(chunks[0:2], ".")
}
func detectServerTypeAndVersion(version string) (bool, string, string) {
version = strings.TrimSpace(version)
// Detect postgresql
matches := postgresSignature.FindAllStringSubmatch(version, 1)
if len(matches) > 0 {
return true, postgresType, matches[0][1]
}
// Detect cockroachdb
matches = cockroachSignature.FindAllStringSubmatch(version, 1)
if len(matches) > 0 {
return true, cockroachType, matches[0][1]
}
return false, "", ""
}
// containsRestrictedKeywords returns true if given keyword is not allowed in read-only mode
func containsRestrictedKeywords(str string) bool {
str = reSlashComment.ReplaceAllString(str, "")