Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: bytebase action version (#16142)
Danny Xubd9947ee3e5a
diff --git a/action/file.go b/action/file.goindex a6cebe5355..4ce8f596bf 100644--- a/action/file.go+++ b/action/file.go@@ -10,8 +10,6 @@ import (v1pb "github.com/bytebase/bytebase/proto/generated-go/v1")-var versionReg = regexp.MustCompile(`^\d+`)-func getReleaseFiles(pattern string) ([]*v1pb.Release_File, error) {matches, err := filepath.Glob(pattern)if err != nil {@@ -33,7 +31,7 @@ func getReleaseFiles(pattern string) ([]*v1pb.Release_File, error) {t = v1pb.Release_File_DDL_GHOST}- version := versionReg.FindString(base)+ version := extractVersion(base)if version == "" {slog.Warn("version not found. ignore the file", "file", m)continue@@ -50,3 +48,16 @@ func getReleaseFiles(pattern string) ([]*v1pb.Release_File, error) {return files, nil}++var versionReg = regexp.MustCompile(`^[vV]?(\d+(\.\d+)*)`)++// extractVersion extracts version from a string and removes the optional "v" or "V" prefix+func extractVersion(s string) string {+ matches := versionReg.FindStringSubmatch(s)+ if len(matches) < 2 {+ return ""+ }++ // Return the first capture group which contains just the version numbers+ return matches[1]+}diff --git a/action/file_test.go b/action/file_test.gonew file mode 100644index 0000000000..70eebe1c79--- /dev/null+++ b/action/file_test.go@@ -0,0 +1,98 @@+package main++import (+ "testing"++ "github.com/stretchr/testify/require"+)++func TestExtractVersion(t *testing.T) {+ tests := []struct {+ name string+ input string+ expected string+ }{+ {+ name: "simple version",+ input: "3_desc.sql",+ expected: "3",+ },+ {+ name: "version with minor",+ input: "3.1_desc.sql",+ expected: "3.1",+ },+ {+ name: "semantic version",+ input: "3.1.1_desc.sql",+ expected: "3.1.1",+ },+ {+ name: "with v prefix",+ input: "v3.1.1_desc.sql",+ expected: "3.1.1",+ },+ {+ name: "with V prefix",+ input: "V3.1.1_desc.sql",+ expected: "3.1.1",+ },+ {+ name: "with additional text",+ input: "3.1.1_description_with_more_text.sql",+ expected: "3.1.1",+ },+ {+ name: "with pre-release version",+ input: "3.1.1-beta_desc.sql",+ expected: "3.1.1",+ },+ {+ name: "with v prefix and pre-release version",+ input: "v3.1.1-alpha_desc.sql",+ expected: "3.1.1",+ },+ {+ name: "timestamp version",+ input: "202101130001_desc.sql",+ expected: "202101130001",+ },+ {+ name: "timestamp version with v prefix",+ input: "v202101130001_desc.sql",+ expected: "202101130001",+ },+ {+ name: "timestamp version with V prefix",+ input: "V202101130001_desc.sql",+ expected: "202101130001",+ },+ {+ name: "timestamp version with additional text",+ input: "202101130001_migration_description.sql",+ expected: "202101130001",+ },+ {+ name: "no version",+ input: "no_version_here.sql",+ expected: "",+ },+ {+ name: "empty string",+ input: "",+ expected: "",+ },+ {+ name: "just v",+ input: "v_desc.sql",+ expected: "",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ result := extractVersion(tt.input)+ require.Equal(t, tt.expected, result)+ })+ }+}