Skip to content

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

tests(tenant): add integration test for empty database name teamplte (#2940)

Joe Chen50ad2eb0e105
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 74decdc81c..e876f89ac5 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -252,7 +252,7 @@ const placeholderRegexp = `[^\\/?%*:|"<>]+`
// If filePath matches, then it will derive MigrationInfo from the filePath.
// Both filePath and filePathTemplate are the full file path (including the base directory) of the repository.
// It returns (nil, nil) if it doesn't look like a migration file path.
-func ParseMigrationInfo(filePath, filePathTemplate string) (*MigrationInfo, error) {
+func ParseMigrationInfo(filePath, filePathTemplate string, allowOmitDatabaseName bool) (*MigrationInfo, error) {
placeholderList := []string{
"ENV_NAME",
"VERSION",
@@ -317,7 +317,7 @@ func ParseMigrationInfo(filePath, filePathTemplate string) (*MigrationInfo, erro
if mi.Version == "" {
return nil, errors.Errorf("file path %q does not contain {{VERSION}}, configured file path template %q", filePath, filePathTemplate)
}
- if mi.Namespace == "" {
+ if mi.Namespace == "" && !allowOmitDatabaseName {
return nil, errors.Errorf("file path %q does not contain {{DB_NAME}}, configured file path template %q", filePath, filePathTemplate)
}
diff --git a/plugin/db/driver_test.go b/plugin/db/driver_test.go
index 554d4e5a4a..fc36c952b9 100644
--- a/plugin/db/driver_test.go
+++ b/plugin/db/driver_test.go
@@ -10,10 +10,11 @@ import (
func TestParseMigrationInfo(t *testing.T) {
type test struct {
- filePath string
- filePathTemplate string
- want *MigrationInfo
- wantErr string
+ filePath string
+ filePathTemplate string
+ allowOmitDatabaseName bool
+ want *MigrationInfo
+ wantErr string
}
tests := []test{
@@ -209,10 +210,29 @@ func TestParseMigrationInfo(t *testing.T) {
want: nil,
wantErr: "",
},
+
+ {
+ filePath: "001foo##ddl",
+ filePathTemplate: "{{VERSION}}##{{TYPE}}",
+ want: nil,
+ wantErr: "does not contain {{DB_NAME}}",
+ },
+ {
+ filePath: "001foo##ddl",
+ filePathTemplate: "{{VERSION}}##{{TYPE}}",
+ allowOmitDatabaseName: true,
+ want: &MigrationInfo{
+ Version: "001foo",
+ Source: VCS,
+ Type: Migrate,
+ Description: "Create schema migration",
+ },
+ wantErr: "",
+ },
}
for _, tc := range tests {
t.Run(tc.filePath, func(t *testing.T) {
- mi, err := ParseMigrationInfo(tc.filePath, tc.filePathTemplate)
+ mi, err := ParseMigrationInfo(tc.filePath, tc.filePathTemplate, tc.allowOmitDatabaseName)
if tc.wantErr != "" {
got := fmt.Sprintf("%v", err)
require.Contains(t, got, tc.wantErr)
@@ -222,6 +242,7 @@ func TestParseMigrationInfo(t *testing.T) {
if tc.want == nil {
require.Nil(t, mi)
} else {
+ require.NotNil(t, mi)
require.Equal(t, *tc.want, *mi)
}
})
diff --git a/server/webhook.go b/server/webhook.go
index c733b690e9..af64329095 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -322,8 +322,9 @@ func getFileInfo(fileItem vcs.DistinctFileItem, repositoryList []*api.Repository
continue
}
+ allowOmitDatabaseName := repository.Project.TenantMode == api.TenantModeTenant && repository.Project.DBNameTemplate == ""
// NOTE: We do not want to use filepath.Join here because we always need "/" as the path separator.
- mi, err := db.ParseMigrationInfo(fileItem.FileName, path.Join(repository.BaseDirectory, repository.FilePathTemplate))
+ mi, err := db.ParseMigrationInfo(fileItem.FileName, path.Join(repository.BaseDirectory, repository.FilePathTemplate), allowOmitDatabaseName)
if err != nil {
log.Error("Failed to parse migration file info",
zap.Int("project", repository.ProjectID),
diff --git a/tests/tenant_test.go b/tests/tenant_test.go
index f7c5ea6ed5..7dafb0a4f7 100644
--- a/tests/tenant_test.go
+++ b/tests/tenant_test.go
@@ -977,3 +977,275 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
})
}
}
+
+// TestTenantVCSDatabaseNameTemplate_Empty tests the behavior when a tenant
+// project has empty database name template where a single commit file should
+// match all databases in the project, and create migration issues for all of
+// them.
+func TestTenantVCSDatabaseNameTemplate_Empty(t *testing.T) {
+ tests := []struct {
+ name string
+ vcsProviderCreator fake.VCSProviderCreator
+ vcsType vcs.Type
+ externalID string
+ repositoryFullPath string
+ newWebhookPushEvent func(gitFile string) interface{}
+ }{
+ {
+ name: "GitLab",
+ vcsProviderCreator: fake.NewGitLab,
+ vcsType: vcs.GitLabSelfHost,
+ externalID: "121",
+ repositoryFullPath: "test/schemaUpdate",
+ newWebhookPushEvent: func(gitFile string) interface{} {
+ return gitlab.WebhookPushEvent{
+ ObjectKind: gitlab.WebhookPush,
+ Ref: "refs/heads/feature/foo",
+ Project: gitlab.WebhookProject{
+ ID: 121,
+ },
+ CommitList: []gitlab.WebhookCommit{
+ {
+ Timestamp: "2021-01-13T13:14:00Z",
+ AddedList: []string{gitFile},
+ },
+ },
+ }
+ },
+ },
+ {
+ name: "GitHub",
+ vcsProviderCreator: fake.NewGitHub,
+ vcsType: vcs.GitHubCom,
+ externalID: "octocat/Hello-World",
+ repositoryFullPath: "octocat/Hello-World",
+ newWebhookPushEvent: func(gitFile string) interface{} {
+ return github.WebhookPushEvent{
+ Ref: "refs/heads/feature/foo",
+ Repository: github.WebhookRepository{
+ ID: 211,
+ FullName: "octocat/Hello-World",
+ HTMLURL: "https://github.com/octocat/Hello-World",
+ },
+ Sender: github.WebhookSender{
+ Login: "fake_github_author",
+ },
+ Commits: []github.WebhookCommit{
+ {
+ ID: "fake_github_commit_id",
+ Distinct: true,
+ Message: "Fake GitHub commit message",
+ Timestamp: time.Now(),
+ URL: "https://api.github.com/octocat/Hello-World/commits/fake_github_commit_id",
+ Author: github.WebhookCommitAuthor{
+ Name: "fake_github_author",
+ Email: "fake_github_author@localhost",
+ },
+ Added: []string{gitFile},
+ },
+ },
+ }
+ },
+ },
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ t.Parallel()
+
+ a := require.New(t)
+ ctx := context.Background()
+ ctl := &controller{}
+ err := ctl.StartServer(ctx, t.TempDir(), fake.NewGitHub, getTestPort(t.Name()))
+ a.NoError(err)
+ defer func() {
+ _ = ctl.Close(ctx)
+ }()
+
+ err = ctl.Login()
+ a.NoError(err)
+ err = ctl.setLicense()
+ a.NoError(err)
+
+ // Create a VCS.
+ vcs, err := ctl.createVCS(
+ api.VCSCreate{
+ Name: t.Name(),
+ Type: test.vcsType,
+ InstanceURL: ctl.vcsURL,
+ APIURL: ctl.vcsProvider.APIURL(ctl.vcsURL),
+ ApplicationID: "testApplicationID",
+ Secret: "testApplicationSecret",
+ },
+ )
+ a.NoError(err)
+
+ // Create a tenant project with empty database name template.
+ project, err := ctl.createProject(
+ api.ProjectCreate{
+ Name: "Test VCS Project",
+ Key: "TestTenantVCSDatabaseNameTemplate_Empty",
+ TenantMode: api.TenantModeTenant,
+ },
+ )
+ a.NoError(err)
+
+ // Create a repository.
+ ctl.vcsProvider.CreateRepository(test.externalID)
+ _, err = ctl.createRepository(
+ api.RepositoryCreate{
+ VCSID: vcs.ID,
+ ProjectID: project.ID,
+ Name: "Test Repository",
+ FullPath: test.repositoryFullPath,
+ WebURL: fmt.Sprintf("%s/%s", ctl.vcsURL, test.repositoryFullPath),
+ BranchFilter: "feature/foo",
+ BaseDirectory: baseDirectory,
+ FilePathTemplate: "{{VERSION}}##{{TYPE}}##{{DESCRIPTION}}.sql",
+ SchemaPathTemplate: ".{{DB_NAME}}##LATEST.sql",
+ ExternalID: test.externalID,
+ AccessToken: "accessToken1",
+ RefreshToken: "refreshToken1",
+ },
+ )
+ a.NoError(err)
+
+ // Provision instances.
+ instanceRootDir := t.TempDir()
+
+ const stagingTenantNumber = 2 // We need more than one tenant to test wildcard
+ var stagingInstanceDirs []string
+ for i := 0; i < stagingTenantNumber; i++ {
+ instanceDir, err := ctl.provisionSQLiteInstance(instanceRootDir, fmt.Sprintf("%s-%d", stagingInstanceName, i))
+ a.NoError(err)
+ stagingInstanceDirs = append(stagingInstanceDirs, instanceDir)
+ }
+ environments, err := ctl.getEnvironments()
+ a.NoError(err)
+ stagingEnvironment, err := findEnvironment(environments, "Staging")
+ a.NoError(err)
+
+ // Add the provisioned instances.
+ var stagingInstances []*api.Instance
+ for i, stagingInstanceDir := range stagingInstanceDirs {
+ instance, err := ctl.addInstance(
+ api.InstanceCreate{
+ EnvironmentID: stagingEnvironment.ID,
+ Name: fmt.Sprintf("%s-%d", stagingInstanceName, i),
+ Engine: db.SQLite,
+ Host: stagingInstanceDir,
+ },
+ )
+ a.NoError(err)
+ stagingInstances = append(stagingInstances, instance)
+ }
+
+ // Create deployment configuration.
+ _, err = ctl.upsertDeploymentConfig(
+ api.DeploymentConfigUpsert{
+ ProjectID: project.ID,
+ },
+ api.DeploymentSchedule{
+ Deployments: []*api.Deployment{
+ {
+ Name: "Staging stage",
+ Spec: &api.DeploymentSpec{
+ Selector: &api.LabelSelector{
+ MatchExpressions: []*api.LabelSelectorRequirement{
+ {
+ Key: api.EnvironmentKeyName,
+ Operator: api.InOperatorType,
+ Values: []string{"Staging"},
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ )
+ a.NoError(err)
+
+ // Create issues that create databases.
+ const baseDatabaseName = "TestTenantVCSDatabaseNameTemplate_Empty"
+ for i, stagingInstance := range stagingInstances {
+ tenant := fmt.Sprintf("tenant%d", i)
+ databaseName := baseDatabaseName + "_" + tenant
+ err := ctl.createDatabase(project, stagingInstance, databaseName, "", nil /* labelMap */)
+ a.NoError(err)
+ }
+
+ // Getting databases for each environment.
+ databases, err := ctl.getDatabases(
+ api.DatabaseFind{
+ ProjectID: &project.ID,
+ },
+ )
+ a.NoError(err)
+
+ var stagingDatabases []*api.Database
+ for _, stagingInstance := range stagingInstances {
+ for _, database := range databases {
+ if database.Instance.ID == stagingInstance.ID {
+ stagingDatabases = append(stagingDatabases, database)
+ break
+ }
+ }
+ }
+ a.Equal(stagingTenantNumber, len(stagingDatabases))
+
+ // Simulate Git commits for schema update.
+ gitFile := baseDirectory + "/ver1##migrate##create_a_test_table.sql"
+ err = ctl.vcsProvider.AddFiles(test.externalID, map[string]string{gitFile: migrationStatement})
+ a.NoError(err)
+
+ payload, err := json.Marshal(test.newWebhookPushEvent(gitFile))
+ a.NoError(err)
+ err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)
+ a.NoError(err)
+
+ // Get schema update issues.
+ openStatus := []api.IssueStatus{api.IssueOpen}
+ issues, err := ctl.getIssues(
+ api.IssueFind{
+ ProjectID: &project.ID,
+ StatusList: openStatus,
+ },
+ )
+ a.NoError(err)
+ a.Len(issues, 1)
+ issue := issues[0]
+ status, err := ctl.waitIssuePipeline(issue.ID)
+ a.NoError(err)
+ a.Equal(api.TaskDone, status)
+
+ // Query schema.
+ for i, stagingInstance := range stagingInstances {
+ tenant := fmt.Sprintf("tenant%d", i)
+ databaseName := baseDatabaseName + "_" + tenant
+ result, err := ctl.query(stagingInstance, databaseName, bookTableQuery)
+ a.NoError(err)
+ a.Equal(bookSchemaSQLResult, result)
+ }
+
+ // Query migration history
+ hm := map[string]bool{}
+ for i, instance := range stagingInstances {
+ tenant := fmt.Sprintf("tenant%d", i)
+ databaseName := baseDatabaseName + "_" + tenant
+ histories, err := ctl.getInstanceMigrationHistory(
+ db.MigrationHistoryFind{
+ ID: &instance.ID,
+ Database: &databaseName,
+ },
+ )
+ a.NoError(err)
+ a.Len(histories, 2)
+ a.Equal(histories[0].Version, "ver1")
+ a.NotEqual(histories[1].Version, "")
+ hm[histories[0].Version] = true
+ }
+
+ a.Len(hm, 1)
+ })
+ }
+}
diff --git a/tests/tests.go b/tests/tests.go
index af29e3d83a..976acbbd68 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -115,8 +115,8 @@ type controller struct {
}
func getTestPort(testName string) int {
- // We allocates 4 ports for each of the integration test, who probably would start
- // the Bytebase server, Postgres, MySQL and GitLab.
+ // We allocate 4 ports for each of the integration test, who probably would start
+ // the Bytebase server, Postgres, MySQL and code host (GitLab or GitHub).
tests := []string{
"TestServiceRestart",
"TestSchemaAndDataUpdate",
@@ -136,6 +136,8 @@ func getTestPort(testName string) int {
"TestGhostSchemaUpdate",
"TestTenantVCSDatabaseNameTemplate/GitLab",
"TestTenantVCSDatabaseNameTemplate/GitHub",
+ "TestTenantVCSDatabaseNameTemplate_Empty/GitLab",
+ "TestTenantVCSDatabaseNameTemplate_Empty/GitHub",
"TestBootWithExternalPg",
"TestSheetVCS/GitLab",
"TestSheetVCS/GitHub",