Skip to content

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

feat: update `sheet` api structure (#964)

STEVENb4e12fdec7a2
diff --git a/api/repository.go b/api/repository.go
index ebeb17e8ed..e88161c53c 100644
--- a/api/repository.go
+++ b/api/repository.go
@@ -28,6 +28,7 @@ type RepositoryRaw struct {
BaseDirectory string
FilePathTemplate string
SchemaPathTemplate string
+ SheetPathTemplate string
ExternalID string
ExternalWebhookID string
WebhookURLHost string
@@ -59,6 +60,7 @@ func (raw *RepositoryRaw) ToRepository() *Repository {
BaseDirectory: raw.BaseDirectory,
FilePathTemplate: raw.FilePathTemplate,
SchemaPathTemplate: raw.SchemaPathTemplate,
+ SheetPathTemplate: raw.SheetPathTemplate,
ExternalID: raw.ExternalID,
ExternalWebhookID: raw.ExternalWebhookID,
WebhookURLHost: raw.WebhookURLHost,
@@ -99,6 +101,8 @@ type Repository struct {
// The file path template for storing the latest schema auto-generated by Bytebase after migration.
// If empty, then Bytebase won't auto generate it.
SchemaPathTemplate string `jsonapi:"attr,schemaPathTemplate"`
+ // The file path template for matching the sql files for sheet.
+ SheetPathTemplate string `jsonapi:"attr,sheetPathTemplate"`
ExternalID string `jsonapi:"attr,externalId"`
ExternalWebhookID string
WebhookURLHost string
@@ -128,6 +132,7 @@ type RepositoryCreate struct {
BaseDirectory string `jsonapi:"attr,baseDirectory"`
FilePathTemplate string `jsonapi:"attr,filePathTemplate"`
SchemaPathTemplate string `jsonapi:"attr,schemaPathTemplate"`
+ SheetPathTemplate string `jsonapi:"attr,sheetPathTemplate"`
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
@@ -174,6 +179,7 @@ type RepositoryPatch struct {
BaseDirectory *string `jsonapi:"attr,baseDirectory"`
FilePathTemplate *string `jsonapi:"attr,filePathTemplate"`
SchemaPathTemplate *string `jsonapi:"attr,schemaPathTemplate"`
+ SheetPathTemplate *string `jsonapi:"attr,sheetPathTemplate"`
AccessToken *string
ExpiresTs *int64
RefreshToken *string
diff --git a/api/sheet.go b/api/sheet.go
index fa7b57e445..974162ed14 100644
--- a/api/sheet.go
+++ b/api/sheet.go
@@ -29,6 +29,58 @@ func (v SheetVisibility) String() string {
return ""
}
+// SheetSource is the type of sheet origin source.
+type SheetSource string
+
+const (
+ // SheetFromBytebase is the sheet created by Bytebase. e.g. SQL Editor.
+ SheetFromBytebase SheetSource = "BYTEBASE"
+ // SheetFromGitLabSelfHost is the sheet synced from self host GitLab.
+ SheetFromGitLabSelfHost SheetSource = "GITLAB_SELF_HOST"
+ // SheetFromGitHubCom is the sheet synced from github.com.
+ SheetFromGitHubCom SheetSource = "GITHUB_COM"
+)
+
+func (v SheetSource) String() string {
+ switch v {
+ case SheetFromBytebase:
+ return "BYTEBASE"
+ case SheetFromGitLabSelfHost:
+ return "GITLAB_SELF_HOST"
+ case SheetFromGitHubCom:
+ return "GITHUB_COM"
+ }
+ // Default sheet source is BYTEBASE.
+ return "BYTEBASE"
+}
+
+// SheetType is the type of sheet.
+type SheetType string
+
+const (
+ // SheetForSQL is the sheet that used for saving SQL statements.
+ SheetForSQL SheetType = "SQL"
+)
+
+func (v SheetType) String() string {
+ switch v {
+ case SheetForSQL:
+ return "SQL"
+ }
+ // Default sheet type is SQL.
+ return "SQL"
+}
+
+// SheetVCSPayload is the additional data payload of the VCS sheet.
+type SheetVCSPayload struct {
+ FileName string `json:"fileName"`
+ Path string `json:"path"`
+ Size int64 `json:"size"`
+ Author string `json:"author"`
+ LastCommitID string `json:"lastCommitId"`
+ LastSyncTs int64 `json:"lastSyncTs"`
+}
+
// SheetRaw is the store model for an Sheet.
// Fields have exactly the same meanings as Sheet.
type SheetRaw struct {
@@ -51,6 +103,10 @@ type SheetRaw struct {
Name string
Statement string
Visibility SheetVisibility
+ Source SheetSource
+ Type SheetType
+ // Payload is in the json string format of SheetVCSPayload.
+ Payload string
}
// ToSheet creates an instance of Sheet based on the SheetRaw.
@@ -76,6 +132,9 @@ func (raw *SheetRaw) ToSheet() *Sheet {
Name: raw.Name,
Statement: raw.Statement,
Visibility: raw.Visibility,
+ Source: raw.Source,
+ Type: raw.Type,
+ Payload: raw.Payload,
}
}
@@ -104,6 +163,9 @@ type Sheet struct {
Name string `jsonapi:"attr,name"`
Statement string `jsonapi:"attr,statement"`
Visibility SheetVisibility `jsonapi:"attr,visibility"`
+ Source SheetSource `jsonapi:"attr,source"`
+ Type SheetType `jsonapi:"attr,type"`
+ Payload string `jsonapi:"attr,payload"`
}
// SheetCreate is the API message for creating a sheet.
@@ -120,6 +182,9 @@ type SheetCreate struct {
Name string `jsonapi:"attr,name"`
Statement string `jsonapi:"attr,statement"`
Visibility SheetVisibility `jsonapi:"attr,visibility"`
+ Source SheetSource
+ Type SheetType
+ Payload string `jsonapi:"attr,payload"`
}
// SheetPatch is the API message for patching a sheet.
@@ -138,6 +203,7 @@ type SheetPatch struct {
Name *string `jsonapi:"attr,name"`
Statement *string `jsonapi:"attr,statement"`
Visibility *string `jsonapi:"attr,visibility"`
+ Payload *string `jsonapi:"attr,payload"`
}
// SheetFind is the API message for finding sheets.
@@ -155,6 +221,9 @@ type SheetFind struct {
// Domain fields
Visibility *SheetVisibility
+ Source *SheetSource
+ Type *SheetType
+ Payload *string
}
func (find *SheetFind) String() string {
diff --git a/store/repository.go b/store/repository.go
index 75c4c0dfd5..5f8d8eb6f8 100644
--- a/store/repository.go
+++ b/store/repository.go
@@ -153,6 +153,7 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *sql.Tx, cr
base_directory,
file_path_template,
schema_path_template,
+ sheet_path_template,
external_id,
external_webhook_id,
webhook_url_host,
@@ -162,8 +163,8 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *sql.Tx, cr
expires_ts,
refresh_token
)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)
- 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, schema_path_template, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)
+ 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, schema_path_template, sheet_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,
@@ -176,6 +177,7 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *sql.Tx, cr
create.BaseDirectory,
create.FilePathTemplate,
create.SchemaPathTemplate,
+ create.SheetPathTemplate,
create.ExternalID,
create.ExternalWebhookID,
create.WebhookURLHost,
@@ -208,6 +210,7 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *sql.Tx, cr
&repository.BaseDirectory,
&repository.FilePathTemplate,
&repository.SchemaPathTemplate,
+ &repository.SheetPathTemplate,
&repository.ExternalID,
&repository.ExternalWebhookID,
&repository.WebhookURLHost,
@@ -255,6 +258,7 @@ func findRepositoryList(ctx context.Context, tx *sql.Tx, find *api.RepositoryFin
base_directory,
file_path_template,
schema_path_template,
+ sheet_path_template,
external_id,
external_webhook_id,
webhook_url_host,
@@ -291,6 +295,7 @@ func findRepositoryList(ctx context.Context, tx *sql.Tx, find *api.RepositoryFin
&repository.BaseDirectory,
&repository.FilePathTemplate,
&repository.SchemaPathTemplate,
+ &repository.SheetPathTemplate,
&repository.ExternalID,
&repository.ExternalWebhookID,
&repository.WebhookURLHost,
@@ -328,6 +333,9 @@ func patchRepository(ctx context.Context, tx *sql.Tx, patch *api.RepositoryPatch
if v := patch.SchemaPathTemplate; v != nil {
set, args = append(set, fmt.Sprintf("schema_path_template = $%d", len(args)+1)), append(args, *v)
}
+ if v := patch.SheetPathTemplate; v != nil {
+ set, args = append(set, fmt.Sprintf("sheet_path_template = $%d", len(args)+1)), append(args, *v)
+ }
if v := patch.AccessToken; v != nil {
set, args = append(set, fmt.Sprintf("access_token = $%d", len(args)+1)), append(args, *v)
}
@@ -345,7 +353,7 @@ func patchRepository(ctx context.Context, tx *sql.Tx, patch *api.RepositoryPatch
UPDATE repository
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
- 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, schema_path_template, 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, schema_path_template, sheet_path_template, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
`, len(args)),
args...,
)
@@ -371,6 +379,7 @@ func patchRepository(ctx context.Context, tx *sql.Tx, patch *api.RepositoryPatch
&repository.BaseDirectory,
&repository.FilePathTemplate,
&repository.SchemaPathTemplate,
+ &repository.SheetPathTemplate,
&repository.ExternalID,
&repository.ExternalWebhookID,
&repository.WebhookURLHost,
diff --git a/store/sheet.go b/store/sheet.go
index bb0f753cde..d1a5ac1af6 100644
--- a/store/sheet.go
+++ b/store/sheet.go
@@ -126,6 +126,7 @@ func (s *SheetService) DeleteSheet(ctx context.Context, delete *api.SheetDelete)
// createSheet creates a new sheet.
func createSheet(ctx context.Context, tx *sql.Tx, create *api.SheetCreate) (*api.SheetRaw, error) {
+ // TODO(Steven): remove the default value when developing the service logic for sheet.
row, err := tx.QueryContext(ctx, `
INSERT INTO sheet (
creator_id,
@@ -134,10 +135,13 @@ func createSheet(ctx context.Context, tx *sql.Tx, create *api.SheetCreate) (*api
database_id,
name,
statement,
- visibility
+ visibility,
+ source,
+ type,
+ payload
)
- VALUES ($1, $2, $3, $4, $5, $6, $7)
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, statement, visibility
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, statement, visibility, source, type, payload
`,
create.CreatorID,
create.CreatorID,
@@ -146,6 +150,9 @@ func createSheet(ctx context.Context, tx *sql.Tx, create *api.SheetCreate) (*api
create.Name,
create.Statement,
create.Visibility,
+ "BYTEBASE",
+ "SQL",
+ "{}",
)
if err != nil {
@@ -167,6 +174,9 @@ func createSheet(ctx context.Context, tx *sql.Tx, create *api.SheetCreate) (*api
&sheetRaw.Name,
&sheetRaw.Statement,
&sheetRaw.Visibility,
+ &sheetRaw.Source,
+ &sheetRaw.Type,
+ &sheetRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
@@ -191,6 +201,9 @@ func patchSheet(ctx context.Context, tx *sql.Tx, patch *api.SheetPatch) (*api.Sh
if v := patch.Visibility; v != nil {
set, args = append(set, fmt.Sprintf("visibility = $%d", len(args)+1)), append(args, *v)
}
+ if v := patch.Payload; v != nil {
+ set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, *v)
+ }
args = append(args, patch.ID)
@@ -198,7 +211,7 @@ func patchSheet(ctx context.Context, tx *sql.Tx, patch *api.SheetPatch) (*api.Sh
UPDATE sheet
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, statement, visibility
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, statement, visibility, source, type, payload
`, len(args)),
args...,
)
@@ -222,6 +235,9 @@ func patchSheet(ctx context.Context, tx *sql.Tx, patch *api.SheetPatch) (*api.Sh
&sheetRaw.Name,
&sheetRaw.Statement,
&sheetRaw.Visibility,
+ &sheetRaw.Source,
+ &sheetRaw.Type,
+ &sheetRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
@@ -262,6 +278,12 @@ func findSheetList(ctx context.Context, tx *sql.Tx, find *api.SheetFind) ([]*api
if v := find.Visibility; v != nil {
where, args = append(where, fmt.Sprintf("visibility = $%d", len(args)+1)), append(args, *v)
}
+ if v := find.Source; v != nil {
+ where, args = append(where, fmt.Sprintf("source = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.Type; v != nil {
+ where, args = append(where, fmt.Sprintf("type = $%d", len(args)+1)), append(args, *v)
+ }
rows, err := tx.QueryContext(ctx, `
SELECT
@@ -274,7 +296,10 @@ func findSheetList(ctx context.Context, tx *sql.Tx, find *api.SheetFind) ([]*api
database_id,
name,
statement,
- visibility
+ visibility,
+ source,
+ type,
+ payload
FROM sheet
WHERE `+strings.Join(where, " AND "),
args...,
@@ -299,6 +324,9 @@ func findSheetList(ctx context.Context, tx *sql.Tx, find *api.SheetFind) ([]*api
&sheetRaw.Name,
&sheetRaw.Statement,
&sheetRaw.Visibility,
+ &sheetRaw.Source,
+ &sheetRaw.Type,
+ &sheetRaw.Payload,
); err != nil {
return nil, FormatError(err)
}