Skip to content

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

Add file_path_template to allow user to customize the migration file path pattern

天舟8067d1332e27
diff --git a/api/repository.go b/api/repository.go
index e10b894b0a..f6a265da61 100644
--- a/api/repository.go
+++ b/api/repository.go
@@ -26,8 +26,9 @@ type Repository struct {
Name string `jsonapi:"attr,name"`
FullPath string `jsonapi:"attr,fullPath"`
WebURL string `jsonapi:"attr,webURL"`
- BaseDirectory string `jsonapi:"attr,baseDirectory"`
BranchFilter string `jsonapi:"attr,branchFilter"`
+ BaseDirectory string `jsonapi:"attr,baseDirectory"`
+ FilePathTemplate string `jsonapi:"attr,filePathTemplate"`
ExternalId string `jsonapi:"attr,externalId"`
ExternalWebhookId string
WebhookURLHost string
@@ -49,12 +50,13 @@ type RepositoryCreate struct {
ProjectId int `jsonapi:"attr,projectId"`
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- FullPath string `jsonapi:"attr,fullPath"`
- WebURL string `jsonapi:"attr,webURL"`
- BaseDirectory string `jsonapi:"attr,baseDirectory"`
- BranchFilter string `jsonapi:"attr,branchFilter"`
- ExternalId string `jsonapi:"attr,externalId"`
+ Name string `jsonapi:"attr,name"`
+ FullPath string `jsonapi:"attr,fullPath"`
+ WebURL string `jsonapi:"attr,webURL"`
+ BranchFilter string `jsonapi:"attr,branchFilter"`
+ BaseDirectory string `jsonapi:"attr,baseDirectory"`
+ FilePathTemplate string `jsonapi:"attr,filePathTemplate"`
+ ExternalId string `jsonapi:"attr,externalId"`
// Token belonged by the user linking the project to the VCS repository. We store this token together
// with the refresh token in the new repository record so we can use it to call VCS API on
// behalf of that user to perform tasks like webhook CRUD later.
@@ -94,8 +96,9 @@ type RepositoryPatch struct {
UpdaterId int
// Domain specific fields
- BaseDirectory *string `jsonapi:"attr,baseDirectory"`
- BranchFilter *string `jsonapi:"attr,branchFilter"`
+ BranchFilter *string `jsonapi:"attr,branchFilter"`
+ BaseDirectory *string `jsonapi:"attr,baseDirectory"`
+ FilePathTemplate *string `jsonapi:"attr,filePathTemplate"`
}
type RepositoryDelete struct {
diff --git a/frontend/src/components/RepositoryForm.vue b/frontend/src/components/RepositoryForm.vue
index 17413b2006..f0d5fe265b 100644
--- a/frontend/src/components/RepositoryForm.vue
+++ b/frontend/src/components/RepositoryForm.vue
@@ -39,7 +39,7 @@
<div>
<div class="textlabel">Branch <span class="text-red-600">*</span></div>
<div class="mt-1 textinfolabel">
- The branch where Bytebase observes the SQL file (.sql) change.
+ The branch where Bytebase observes the file change.
</div>
<input
id="branch"
@@ -57,9 +57,7 @@
<div>
<div class="textlabel">Base directory</div>
<div class="mt-1 textinfolabel">
- The directory and all its sub-directories that Bytebase will observe the
- SQL file (.sql) change. If empty, then Bytebase will observe the entire
- repository.
+ The root directory where Bytebase observes the file change.
</div>
<input
id="basedirectory"
@@ -70,6 +68,21 @@
v-model="repositoryConfig.baseDirectory"
/>
</div>
+ <div>
+ <div class="textlabel">File path template</div>
+ <div class="mt-1 textinfolabel">
+ Bytebase only observes the file path name matching the template pattern
+ relative to the base directory.
+ </div>
+ <input
+ id="filepathtemplate"
+ name="filepathtemplate"
+ type="text"
+ class="textfield mt-2 w-full"
+ :disabled="!allowEdit"
+ v-model="repositoryConfig.filePathTemplate"
+ />
+ </div>
</div>
</template>
diff --git a/frontend/src/components/RepositoryPanel.vue b/frontend/src/components/RepositoryPanel.vue
index b84493855b..73d03cfd61 100644
--- a/frontend/src/components/RepositoryPanel.vue
+++ b/frontend/src/components/RepositoryPanel.vue
@@ -7,10 +7,13 @@
<a class="normal-link" :href="repository.webURL" target="_blank">{{
repository.fullPath
}}</a
- >. To make schema changes, a developer would create a migration script under
- base directory
+ >. To make schema changes, a developer would create a migration script
+ matching file path pattern
<span class="font-medium text-main"
>/{{ state.repositoryConfig.baseDirectory }}</span
+ >
+ <span class="font-medium text-main"
+ >/{{ state.repositoryConfig.filePathTemplate }}</span
>. After the script is review approved and merged into the
<template v-if="state.repositoryConfig.branchFilter"
>branch matching pattern
@@ -94,6 +97,7 @@ export default {
repositoryConfig: {
baseDirectory: props.repository.baseDirectory,
branchFilter: props.repository.branchFilter,
+ filePathTemplate: props.repository.filePathTemplate,
},
});
@@ -103,6 +107,7 @@ export default {
state.repositoryConfig = {
baseDirectory: cur.baseDirectory,
branchFilter: cur.branchFilter,
+ filePathTemplate: cur.filePathTemplate,
};
}
);
@@ -119,9 +124,11 @@ export default {
const allowUpdate = computed(() => {
return (
!isEmpty(state.repositoryConfig.branchFilter) &&
- (props.repository.baseDirectory !=
- state.repositoryConfig.baseDirectory ||
- props.repository.branchFilter != state.repositoryConfig.branchFilter)
+ (props.repository.branchFilter != state.repositoryConfig.branchFilter ||
+ props.repository.baseDirectory !=
+ state.repositoryConfig.baseDirectory ||
+ props.repository.filePathTemplate !=
+ state.repositoryConfig.filePathTemplate)
);
});
@@ -139,15 +146,22 @@ export default {
const doUpdate = () => {
const repositoryPatch: RepositoryPatch = {};
+ if (
+ props.repository.branchFilter != state.repositoryConfig.branchFilter
+ ) {
+ repositoryPatch.branchFilter = state.repositoryConfig.branchFilter;
+ }
if (
props.repository.baseDirectory != state.repositoryConfig.baseDirectory
) {
repositoryPatch.baseDirectory = state.repositoryConfig.baseDirectory;
}
if (
- props.repository.branchFilter != state.repositoryConfig.branchFilter
+ props.repository.filePathTemplate !=
+ state.repositoryConfig.filePathTemplate
) {
- repositoryPatch.branchFilter = state.repositoryConfig.branchFilter;
+ repositoryPatch.filePathTemplate =
+ state.repositoryConfig.filePathTemplate;
}
store
.dispatch("repository/updateRepositoryByProjectId", {
diff --git a/frontend/src/components/RepositorySetupWizard.vue b/frontend/src/components/RepositorySetupWizard.vue
index 0b9c6a383b..9980d9219b 100644
--- a/frontend/src/components/RepositorySetupWizard.vue
+++ b/frontend/src/components/RepositorySetupWizard.vue
@@ -36,7 +36,6 @@ import { computed, PropType } from "@vue/runtime-core";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import isEmpty from "lodash-es/isEmpty";
-import { v4 as uuidv4 } from "uuid";
import { BBStepTabItem } from "../bbkit/types";
import RepositoryVCSProviderPanel from "./RepositoryVCSProviderPanel.vue";
import RepositorySelectionPanel from "./RepositorySelectionPanel.vue";
@@ -48,7 +47,11 @@ import {
unknown,
VCS,
} from "../types";
-import { projectSlug, randomString } from "../utils";
+import { projectSlug } from "../utils";
+
+// Default file path template is to organize migration files from different environments under separate directories.
+const DEFAULT_FILE_PATH_TEMPLATE =
+ "{{envname}}/{{version}}__{{dbname}}__{{type}}__{{description}}.sql";
const CHOOSE_PROVIDER_STEP = 0;
const CHOOSE_REPOSITORY_STEP = 1;
@@ -105,6 +108,7 @@ export default {
repositoryConfig: {
baseDirectory: "",
branchFilter: "",
+ filePathTemplate: DEFAULT_FILE_PATH_TEMPLATE,
},
},
currentStep: CHOOSE_PROVIDER_STEP,
diff --git a/frontend/src/types/common.ts b/frontend/src/types/common.ts
index b4719bbe43..71843eff7e 100644
--- a/frontend/src/types/common.ts
+++ b/frontend/src/types/common.ts
@@ -374,6 +374,7 @@ export const unknown = (
webURL: "",
baseDirectory: "",
branchFilter: "",
+ filePathTemplate: "",
externalId: UNKNOWN_ID.toString(),
};
@@ -681,6 +682,7 @@ export const empty = (
webURL: "",
baseDirectory: "",
branchFilter: "",
+ filePathTemplate: "",
externalId: EMPTY_ID.toString(),
};
diff --git a/frontend/src/types/repository.ts b/frontend/src/types/repository.ts
index 40ec410b28..843d84973c 100644
--- a/frontend/src/types/repository.ts
+++ b/frontend/src/types/repository.ts
@@ -1,5 +1,5 @@
import isEmpty from "lodash-es/isEmpty";
-import { RepositoryId, VCSId, ProjectId } from "./id";
+import { ProjectId, RepositoryId, VCSId } from "./id";
import { Principal } from "./principal";
import { Project } from "./project";
import { VCS } from "./vcs";
@@ -26,6 +26,7 @@ export type Repository = {
webURL: string;
baseDirectory: string;
branchFilter: string;
+ filePathTemplate: string;
// e.g. In GitLab, this is the corresponding project id.
externalId: string;
};
@@ -50,11 +51,13 @@ export type RepositoryCreate = {
export type RepositoryPatch = {
baseDirectory?: string;
branchFilter?: string;
+ filePathTemplate?: string;
};
export type RepositoryConfig = {
baseDirectory: string;
branchFilter: string;
+ filePathTemplate: string;
};
export type ExternalRepositoryInfo = {
diff --git a/store/migration/10001__init_schema.sql b/store/migration/10001__init_schema.sql
index dc0ab3da0a..89cd1c4f03 100644
--- a/store/migration/10001__init_schema.sql
+++ b/store/migration/10001__init_schema.sql
@@ -1059,11 +1059,13 @@ CREATE TABLE repository (
-- Web url from the corresponding VCS provider.
-- For GitLab, this is the project web url. e.g. https://gitlab.example.com/group1/project-1
web_url TEXT NOT NULL,
- -- Base working directory we are interested.
- base_directory TEXT NOT NULL DEFAULT '',
-- Branch we are interested.
-- For GitLab, this corresponds to webhook's push_events_branch_filter. Wildcard is supported
branch_filter TEXT NOT NULL CHECK (trim(branch_filter) != ''),
+ -- Base working directory we are interested.
+ base_directory TEXT NOT NULL DEFAULT '',
+ -- The file path template for matching the commited migration script.
+ file_path_template TEXT NOT NULL,
-- Repository id from the corresponding VCS provider.
-- For GitLab, this is the project id. e.g. 123
external_id TEXT NOT NULL,
diff --git a/store/repository.go b/store/repository.go
index 2a22be6fb4..c1e79ecf85 100644
--- a/store/repository.go
+++ b/store/repository.go
@@ -149,8 +149,9 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
name,
full_path,
web_url,
- base_directory,
branch_filter,
+ base_directory,
+ file_path_template,
external_id,
external_webhook_id,
webhook_url_host,
@@ -160,8 +161,8 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
expires_ts,
refresh_token
)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, branch_filter, base_directory, file_path_template, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
`,
create.CreatorId,
create.CreatorId,
@@ -170,8 +171,9 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
create.Name,
create.FullPath,
create.WebURL,
- create.BaseDirectory,
create.BranchFilter,
+ create.BaseDirectory,
+ create.FilePathTemplate,
create.ExternalId,
create.ExternalWebhookId,
create.WebhookURLHost,
@@ -200,8 +202,9 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
&repository.Name,
&repository.FullPath,
&repository.WebURL,
- &repository.BaseDirectory,
&repository.BranchFilter,
+ &repository.BaseDirectory,
+ &repository.FilePathTemplate,
&repository.ExternalId,
&repository.ExternalWebhookId,
&repository.WebhookURLHost,
@@ -245,8 +248,9 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
name,
full_path,
web_url,
- base_directory,
branch_filter,
+ base_directory,
+ file_path_template,
external_id,
external_webhook_id,
webhook_url_host,
@@ -279,8 +283,9 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
&repository.Name,
&repository.FullPath,
&repository.WebURL,
- &repository.BaseDirectory,
&repository.BranchFilter,
+ &repository.BaseDirectory,
+ &repository.FilePathTemplate,
&repository.ExternalId,
&repository.ExternalWebhookId,
&repository.WebhookURLHost,
@@ -306,11 +311,14 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
func patchRepository(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*api.Repository, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = ?"}, []interface{}{patch.UpdaterId}
+ if v := patch.BranchFilter; v != nil {
+ set, args = append(set, "branch_filter = ?"), append(args, *v)
+ }
if v := patch.BaseDirectory; v != nil {
set, args = append(set, "base_directory = ?"), append(args, *v)
}
- if v := patch.BranchFilter; v != nil {
- set, args = append(set, "branch_filter = ?"), append(args, *v)
+ if v := patch.FilePathTemplate; v != nil {
+ set, args = append(set, "file_path_template = ?"), append(args, *v)
}
args = append(args, patch.ID)
@@ -320,7 +328,7 @@ func patchRepository(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*
UPDATE repository
SET `+strings.Join(set, ", ")+`
WHERE id = ?
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, branch_filter, base_directory, file_path_template, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
`,
args...,
)
@@ -342,8 +350,9 @@ func patchRepository(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*
&repository.Name,
&repository.FullPath,
&repository.WebURL,
- &repository.BaseDirectory,
&repository.BranchFilter,
+ &repository.BaseDirectory,
+ &repository.FilePathTemplate,
&repository.ExternalId,
&repository.ExternalWebhookId,
&repository.WebhookURLHost,