Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(db): handle edge cases for `ParseMigrationInfo` (#2229)
Joe Chen4133627a1c2d
diff --git a/plugin/db/driver.go b/plugin/db/driver.goindex a4eb2508e6..fac263e28d 100644--- a/plugin/db/driver.go+++ b/plugin/db/driver.go@@ -250,7 +250,9 @@ func ParseMigrationInfo(filePath string, filePathTemplate string) (*MigrationInf"TYPE","DESCRIPTION",}- filePathRegex := filePathTemplate++ // Escape "." characters to match literals instead of using it as a wildcard.+ filePathRegex := strings.ReplaceAll(filePathTemplate, ".", `\.`)for _, placeholder := range placeholderList {filePathRegex = strings.ReplaceAll(filePathRegex, fmt.Sprintf("{{%s}}", placeholder), fmt.Sprintf("(?P<%s>[a-zA-Z0-9+-=/_#?!$. ]+)", placeholder))}diff --git a/plugin/db/driver_test.go b/plugin/db/driver_test.goindex 0b6982a069..9d49c8099b 100644--- a/plugin/db/driver_test.go+++ b/plugin/db/driver_test.go@@ -1,6 +1,7 @@package dbimport (+ "fmt""testing""github.com/stretchr/testify/require"@@ -180,6 +181,22 @@ func TestParseMigrationInfo(t *testing.T) {},wantErr: "",},+ {+ filePath: ".db__20220811.sql",+ filePathTemplate: ".{{DB_NAME}}__{{VERSION}}.sql",+ want: MigrationInfo{+ Version: "20220811",+ Namespace: "db",+ Database: "db",+ Environment: "",+ Source: VCS,+ Type: Migrate,+ Description: "Create db schema migration",+ Creator: "",+ },+ wantErr: "",+ },+{filePath: "db",filePathTemplate: "{{DB_NAME}}__{{VERSION}}",@@ -194,15 +211,31 @@ func TestParseMigrationInfo(t *testing.T) {},wantErr: "does not match file path template",},+ { // Make sure the "." is escaped and should only match literals not as a wildcard+ filePath: ".db__20220811.sql",+ filePathTemplate: "A{{DB_NAME}}__{{VERSION}}Asql",+ want: MigrationInfo{+ Version: "",+ Namespace: "",+ Database: "",+ Environment: "",+ Type: "",+ Description: "",+ Creator: "",+ },+ wantErr: "does not match file path template",+ },}-for _, tc := range tests {- mi, err := ParseMigrationInfo(tc.filePath, tc.filePathTemplate)- if tc.wantErr != "" {- require.Contains(t, err.Error(), tc.wantErr)- continue- }- require.NoError(t, err)- require.Equal(t, tc.want, *mi)+ t.Run(tc.filePath, func(t *testing.T) {+ mi, err := ParseMigrationInfo(tc.filePath, tc.filePathTemplate)+ if tc.wantErr != "" {+ got := fmt.Sprintf("%v", err)+ require.Contains(t, got, tc.wantErr)+ return+ }+ require.NoError(t, err)+ require.Equal(t, tc.want, *mi)+ })}}diff --git a/server/webhook.go b/server/webhook.goindex c0e1d2d06c..1bdedc321b 100644--- a/server/webhook.go+++ b/server/webhook.go@@ -10,7 +10,7 @@ import ("fmt""io""net/http"- "path/filepath"+ "path""regexp""strconv""strings"@@ -260,11 +260,11 @@ func validateGitHubWebhookSignature256(signature, key string, body []byte) (bool// we need to filter the commit list to prevent creating a duplicated issue. GitLab has a limitation to distinguish// whether the commit is a merge commit (https://gitlab.com/gitlab-org/gitlab/-/issues/30914), so we need to dedup// ourselves. Below is the filtering algorithm:-// 1. If we observe the same migration file multiple times, then we should use the latest migration file. This does not matter-// for change-based migration since a developer would always create different migration file with incremental names, while it-// will be important for the state-based migration, since the file name is always the same and we need to use the latest snapshot.-// 2. Maintain the relative commit order between different migration files. If migration file A happens before migration file B,-// then we should create an issue for migration file A first.+// 1. If we observe the same migration file multiple times, then we should use the latest migration file. This does not matter+// for change-based migration since a developer would always create different migration file with incremental names, while it+// will be important for the state-based migration, since the file name is always the same and we need to use the latest snapshot.+// 2. Maintain the relative commit order between different migration files. If migration file A happens before migration file B,+// then we should create an issue for migration file A first.type distinctFileItem struct {createdTime time.Timecommit gitlab.WebhookCommit@@ -469,7 +469,8 @@ func (s *Server) createIssueFromPushEvent(ctx context.Context, repo *api.Reposit}}- mi, err := db.ParseMigrationInfo(fileEscaped, filepath.Join(repo.BaseDirectory, repo.FilePathTemplate))+ // NOTE: We do not want to use filepath.Join here because we always need "/" as the path separator.+ mi, err := db.ParseMigrationInfo(fileEscaped, path.Join(repo.BaseDirectory, repo.FilePathTemplate))if err != nil {createIgnoredFileActivity(err)return "", false, nil