Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: cleanup unused code (#8606)
Danny Xue619168bcadb
diff --git a/backend/plugin/db/util/driverutil.go b/backend/plugin/db/util/driverutil.goindex 07dd08a0ce..40eb8185e3 100644--- a/backend/plugin/db/util/driverutil.go+++ b/backend/plugin/db/util/driverutil.go@@ -13,7 +13,6 @@ import ("strings""time"- "github.com/blang/semver/v4""github.com/pkg/errors""google.golang.org/protobuf/types/known/durationpb""google.golang.org/protobuf/types/known/structpb"@@ -448,59 +447,6 @@ func getMySQLStatementWithResultLimit(stmt string, limit int) string {return fmt.Sprintf("SELECT * FROM (%s) result LIMIT %d;", stmt, limit)}-// NonSemanticPrefix is the prefix for non-semantic version.-const NonSemanticPrefix = "0000.0000.0000-"--// ToStoredVersion converts semantic or non-semantic version to stored version format.-// Non-semantic version will have additional "0000.0000.0000-" prefix.-// Semantic version will add zero padding to MAJOR, MINOR, PATCH version with a timestamp suffix.-func ToStoredVersion(useSemanticVersion bool, version, semanticVersionSuffix string) (string, error) {- if !useSemanticVersion {- return fmt.Sprintf("%s%s", NonSemanticPrefix, version), nil- }- v, err := semver.Make(version)- if err != nil {- return "", err- }- major, minor, patch := fmt.Sprintf("%d", v.Major), fmt.Sprintf("%d", v.Minor), fmt.Sprintf("%d", v.Patch)- if len(major) > 4 || len(minor) > 4 || len(patch) > 4 {- return "", errors.Errorf("invalid version %q, major, minor, patch version should be < 10000", version)- }- return fmt.Sprintf("%04s.%04s.%04s-%s", major, minor, patch, semanticVersionSuffix), nil-}--// FromStoredVersion converts stored version to semantic or non-semantic version.-func FromStoredVersion(storedVersion string) (bool, string, string, error) {- if strings.HasPrefix(storedVersion, NonSemanticPrefix) {- return false, strings.TrimPrefix(storedVersion, NonSemanticPrefix), "", nil- }- idx := strings.Index(storedVersion, "-")- if idx < 0 {- return false, "", "", errors.Errorf("invalid stored version %q, version should contain '-'", storedVersion)- }- prefix, suffix := storedVersion[:idx], storedVersion[idx+1:]- parts := strings.Split(prefix, ".")- if len(parts) != 3 {- return false, "", "", errors.Errorf("invalid stored version %q, version prefix %q should be in semantic version format", storedVersion, prefix)- }- major, err := strconv.Atoi(parts[0])- if err != nil {- return false, "", "", errors.Errorf("invalid stored version %q, version prefix %q should be in semantic version format", storedVersion, prefix)- }- minor, err := strconv.Atoi(parts[1])- if err != nil {- return false, "", "", errors.Errorf("invalid stored version %q, version prefix %q should be in semantic version format", storedVersion, prefix)- }- patch, err := strconv.Atoi(parts[2])- if err != nil {- return false, "", "", errors.Errorf("invalid stored version %q, version prefix %q should be in semantic version format", storedVersion, prefix)- }- if major >= 10000 || minor >= 10000 || patch >= 10000 {- return false, "", "", errors.Errorf("invalid stored version %q, major, minor, patch version of %q should be < 10000", storedVersion, prefix)- }- return true, fmt.Sprintf("%d.%d.%d", major, minor, patch), suffix, nil-}-// IsAffectedRowsStatement returns true if the statement will return the number of affected rows.func IsAffectedRowsStatement(stmt string) bool {affectedRowsStatementPrefix := []string{"INSERT ", "UPDATE ", "DELETE "}diff --git a/backend/plugin/db/util/driverutil_test.go b/backend/plugin/db/util/driverutil_test.goindex 5e7f130ab4..661064e85a 100644--- a/backend/plugin/db/util/driverutil_test.go+++ b/backend/plugin/db/util/driverutil_test.go@@ -10,69 +10,6 @@ import ("github.com/stretchr/testify/require")-func TestToStoredVersion(t *testing.T) {- type test struct {- useSemanticVersion bool- version string- semanticVersionSuffix string- want string- wantErr string- }- tests := []test{- {false, "hello", "", "0000.0000.0000-hello", ""},- {false, "hello", "world", "0000.0000.0000-hello", ""},- {true, "hello", "world", "", "No Major.Minor.Patch elements found"},- {true, "v1.2.3", "world", "", "Invalid character(s) found in major number"},- {true, "1.10000.3", "world", "", "major, minor, patch version should be < 10000"},- {true, "1.2.3", "world", "0001.0002.0003-world", ""},- {true, "2021.1.13", "world", "2021.0001.0013-world", ""},- }- for _, tc := range tests {- got, err := ToStoredVersion(tc.useSemanticVersion, tc.version, tc.semanticVersionSuffix)- if tc.wantErr != "" {- require.Contains(t, err.Error(), tc.wantErr)- continue- }- require.NoError(t, err)- require.Equal(t, tc.want, got)- }-}--func TestFromStoredVersion(t *testing.T) {- type test struct {- storedVersion string- wantUseSemanticVersion bool- wantVersion string- wantSemanticVersionSuffix string- wantErr string- }- tests := []test{- {"0000.0000.0000-hello", false, "hello", "", ""},- {"0001.0001.0000-hello", true, "1.1.0", "hello", ""},- {"2021.0001.0013-world", true, "2021.1.13", "world", ""},- {"2021.0001.0013-hello-world", true, "2021.1.13", "hello-world", ""},- {"2021.0001.0013", false, "", "", "should contain '-'"},- {"2021.0001.0013.1234-hello", false, "", "", "should be in semantic version"},- {"2021.0001-hello", false, "", "", "should be in semantic version"},- {"2a21.0001.0013-hello", false, "", "", "should be in semantic version"},- {"10000.0001.0000-hello", false, "", "", "should be < 10000"},- {"", false, "", "", "should contain '-'"},- {"hello", false, "", "", "should contain '-'"},- {"1.2.3", false, "", "", "should contain '-'"},- }- for _, tc := range tests {- gotUseSemanticVersion, gotVersion, gotSemanticVersionSuffix, err := FromStoredVersion(tc.storedVersion)- if tc.wantErr != "" {- require.Contains(t, err.Error(), tc.wantErr)- continue- }- require.NoError(t, err)- require.Equal(t, tc.wantUseSemanticVersion, gotUseSemanticVersion)- require.Equal(t, tc.wantVersion, gotVersion)- require.Equal(t, tc.wantSemanticVersionSuffix, gotSemanticVersionSuffix)- }-}-func TestGetStatementWithResultLimit(t *testing.T) {tests := []struct {sqlStatement stringdiff --git a/backend/store/model/change_history_test.go b/backend/store/model/change_history_test.gonew file mode 100644index 0000000000..346842e3fa--- /dev/null+++ b/backend/store/model/change_history_test.go@@ -0,0 +1,71 @@+package model++import (+ "testing"++ "github.com/stretchr/testify/require"+)++func TestToStoredVersion(t *testing.T) {+ type test struct {+ useSemanticVersion bool+ version string+ semanticVersionSuffix string+ want string+ wantErr string+ }+ tests := []test{+ {false, "hello", "", "0000.0000.0000-hello", ""},+ {false, "hello", "world", "0000.0000.0000-hello", ""},+ {true, "hello", "world", "", "No Major.Minor.Patch elements found"},+ {true, "v1.2.3", "world", "", "Invalid character(s) found in major number"},+ {true, "1.10000.3", "world", "", "major, minor, patch version should be < 10000"},+ {true, "1.2.3", "world", "0001.0002.0003-world", ""},+ {true, "2021.1.13", "world", "2021.0001.0013-world", ""},+ }+ for _, tc := range tests {+ version := Version{Semantic: tc.useSemanticVersion, Version: tc.version, Suffix: tc.semanticVersionSuffix}+ got, err := version.Marshal()+ if tc.wantErr != "" {+ require.Contains(t, err.Error(), tc.wantErr)+ continue+ }+ require.NoError(t, err)+ require.Equal(t, tc.want, got)+ }+}++func TestFromStoredVersion(t *testing.T) {+ type test struct {+ storedVersion string+ wantUseSemanticVersion bool+ wantVersion string+ wantSemanticVersionSuffix string+ wantErr string+ }+ tests := []test{+ {"0000.0000.0000-hello", false, "hello", "", ""},+ {"0001.0001.0000-hello", true, "1.1.0", "hello", ""},+ {"2021.0001.0013-world", true, "2021.1.13", "world", ""},+ {"2021.0001.0013-hello-world", true, "2021.1.13", "hello-world", ""},+ {"2021.0001.0013", false, "", "", "should contain '-'"},+ {"2021.0001.0013.1234-hello", false, "", "", "should be in semantic version"},+ {"2021.0001-hello", false, "", "", "should be in semantic version"},+ {"2a21.0001.0013-hello", false, "", "", "should be in semantic version"},+ {"10000.0001.0000-hello", false, "", "", "should be < 10000"},+ {"", false, "", "", ""},+ {"hello", false, "", "", "should contain '-'"},+ {"1.2.3", false, "", "", "should contain '-'"},+ }+ for _, tc := range tests {+ version, err := NewVersion(tc.storedVersion)+ if tc.wantErr != "" {+ require.Contains(t, err.Error(), tc.wantErr)+ continue+ }+ require.NoError(t, err)+ require.Equal(t, tc.wantUseSemanticVersion, version.Semantic)+ require.Equal(t, tc.wantVersion, version.Version)+ require.Equal(t, tc.wantSemanticVersionSuffix, version.Suffix)+ }+}