Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: using for-loop to handle the continuous single asterisk case (#5169)

h3n4l799735e1a613
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index c47d9d2ac9..64d46634ba 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -203,7 +203,16 @@ func ParseMigrationInfo(filePath, filePathTemplate string, allowOmitDatabaseName
// Escape "." characters to match literals instead of using it as a wildcard.
filePathRegex := strings.ReplaceAll(filePathTemplate, `.`, `\.`)
- filePathRegex = strings.ReplaceAll(filePathRegex, `/*/`, `/[^/]+/`)
+ // We need using for-loop to handle the continuous "*" case.
+ // For example, if filePathTemplate is "foo/*/*/bar", then we need to convert it to "foo/[^/]+/[^/]+/bar",
+ // but strings.ReplaceAll(filePathRegex, `/*/`, `/[^/]+/`) will only replace the first "*" to "/[^/]+/"(i.e "foo/[^/]+/*/bar")
+ tempFilePathRegex := strings.ReplaceAll(filePathRegex, `/*/`, `/[^/]+/`)
+ for tempFilePathRegex != filePathRegex {
+ filePathRegex = tempFilePathRegex
+ tempFilePathRegex = strings.ReplaceAll(filePathRegex, `/*/`, `/[^/]+/`)
+ }
+
+ // After the previous for-loop, filePathRegex will not include any "/*/" anymore, so we can safely replace all ** to .*.
filePathRegex = strings.ReplaceAll(filePathRegex, `**`, `.*`)
for _, placeholder := range placeholderList {
diff --git a/backend/tests/schema_update_test.go b/backend/tests/schema_update_test.go
index 5f6549bd62..267e23b455 100644
--- a/backend/tests/schema_update_test.go
+++ b/backend/tests/schema_update_test.go
@@ -1139,6 +1139,32 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
},
newWebhookPushEvent: defaultNewWebhookPushEvent,
},
+ {
+ name: "continuousSingleAsterisk",
+ vcsProviderCreator: fake.NewGitLab,
+ vcsType: vcs.GitLab,
+ baseDirectory: "bbtest",
+ envName: "wildcard",
+ filePathTemplate: "{{ENV_ID}}/*/*/{{DB_NAME}}##{{VERSION}}##{{TYPE}}##{{DESCRIPTION}}.sql",
+ commitNewFileNames: []string{
+ // The second single asterisk represents empty folder.
+ fmt.Sprintf("%s/%s/foo/bar/%s##ver1##migrate##create_table_t1.sql", baseDirectory, "wildcard", dbName),
+ // Any singleAsterisk cannot match zero directory.
+ fmt.Sprintf("%s/%s/foo/%s##ver2##data##insert_data.sql", baseDirectory, "wildcard", dbName),
+ fmt.Sprintf("%s/%s/%s##ver3##migrate##create_table_t3.sql", baseDirectory, "wildcard", dbName),
+ },
+ commitNewFileContents: []string{
+ "CREATE TABLE t1 (id INT);",
+ "INSERT INTO t1 VALUES (1);",
+ "CREATE TABLE t3 (id INT);",
+ },
+ expect: []bool{
+ true,
+ false,
+ false,
+ },
+ newWebhookPushEvent: defaultNewWebhookPushEvent,
+ },
{
name: "doubleAsterisks",
vcsProviderCreator: fake.NewGitLab,