Skip to content

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

Create all database and admin data source when creating the instance on the server

天舟d34745e29b5c
diff --git a/api/dataSource.go b/api/dataSource.go
index b151a3088d..2072c36d3b 100644
--- a/api/dataSource.go
+++ b/api/dataSource.go
@@ -2,9 +2,14 @@ package api
import (
"context"
+ "database/sql"
"encoding/json"
)
+const (
+ ADMIN_DATA_SOURCE_NAME = "Admin data source"
+)
+
type DataSourceType string
const (
@@ -95,6 +100,8 @@ type DataSourcePatch struct {
type DataSourceService interface {
CreateDataSource(ctx context.Context, create *DataSourceCreate) (*DataSource, error)
+ // This is specifically used to create the admin data source when creating the instance.
+ CreateDataSourceTx(ctx context.Context, tx *sql.Tx, create *DataSourceCreate) (*DataSource, error)
FindDataSourceList(ctx context.Context, find *DataSourceFind) ([]*DataSource, error)
FindDataSource(ctx context.Context, find *DataSourceFind) (*DataSource, error)
PatchDataSource(ctx context.Context, patch *DataSourcePatch) (*DataSource, error)
diff --git a/api/database.go b/api/database.go
index fa2fdd1927..3f3c8373dd 100644
--- a/api/database.go
+++ b/api/database.go
@@ -2,10 +2,15 @@ package api
import (
"context"
+ "database/sql"
"encoding/json"
)
-const ALL_DATABASE_NAME = "*"
+const (
+ ALL_DATABASE_NAME = "*"
+ DEFAULT_CHARACTER_SET_NAME = "utf8mb4"
+ DEFAULT_COLLATION_NAME = "utf8mb4_0900_ai_ci"
+)
type SyncStatus string
@@ -107,6 +112,8 @@ type DatabasePatch struct {
type DatabaseService interface {
CreateDatabase(ctx context.Context, create *DatabaseCreate) (*Database, error)
+ // This is specifically used to create the * database when creating the instance.
+ CreateDatabaseTx(ctx context.Context, tx *sql.Tx, create *DatabaseCreate) (*Database, error)
FindDatabaseList(ctx context.Context, find *DatabaseFind) ([]*Database, error)
FindDatabase(ctx context.Context, find *DatabaseFind) (*Database, error)
PatchDatabase(ctx context.Context, patch *DatabasePatch) (*Database, error)
diff --git a/api/instance.go b/api/instance.go
index 818e327ef3..95194e5a1d 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -111,6 +111,7 @@ type InstanceMigration struct {
}
type InstanceService interface {
+ // CreateInstance should also create the * database and the admin data source.
CreateInstance(ctx context.Context, create *InstanceCreate) (*Instance, error)
FindInstanceList(ctx context.Context, find *InstanceFind) ([]*Instance, error)
FindInstance(ctx context.Context, find *InstanceFind) (*Instance, error)
diff --git a/api/project.go b/api/project.go
index cc09cbd63a..128dea89ab 100644
--- a/api/project.go
+++ b/api/project.go
@@ -95,5 +95,5 @@ type ProjectService interface {
FindProject(ctx context.Context, find *ProjectFind) (*Project, error)
PatchProject(ctx context.Context, patch *ProjectPatch) (*Project, error)
// This is specifically used to update the ProjectWorkflowType when linking/unlinking the repository.
- PatchProjectWithTx(ctx context.Context, tx *sql.Tx, patch *ProjectPatch) (*Project, error)
+ PatchProjectTx(ctx context.Context, tx *sql.Tx, patch *ProjectPatch) (*Project, error)
}
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index 04bb028e55..72506e07d3 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -160,10 +160,10 @@ func (m *main) Run() error {
server.ProjectService = store.NewProjectService(m.l, db)
server.ProjectMemberService = store.NewProjectMemberService(m.l, db)
server.EnvironmentService = store.NewEnvironmentService(m.l, db)
- server.InstanceService = store.NewInstanceService(m.l, db)
+ server.DataSourceService = store.NewDataSourceService(m.l, db)
server.DatabaseService = store.NewDatabaseService(m.l, db)
+ server.InstanceService = store.NewInstanceService(m.l, db, server.DatabaseService, server.DataSourceService)
server.TableService = store.NewTableService(m.l, db)
- server.DataSourceService = store.NewDataSourceService(m.l, db)
server.IssueService = store.NewIssueService(m.l, db)
server.PipelineService = store.NewPipelineService(m.l, db)
server.StageService = store.NewStageService(m.l, db)
diff --git a/store/dataSource.go b/store/dataSource.go
index c9e1231462..fdb8f909c4 100644
--- a/store/dataSource.go
+++ b/store/dataSource.go
@@ -2,6 +2,7 @@ package store
import (
"context"
+ "database/sql"
"fmt"
"strings"
@@ -33,7 +34,7 @@ func (s *DataSourceService) CreateDataSource(ctx context.Context, create *api.Da
}
defer tx.Rollback()
- dataSource, err := s.createDataSource(ctx, tx, create)
+ dataSource, err := s.createDataSource(ctx, tx.Tx, create)
if err != nil {
return nil, err
}
@@ -45,6 +46,10 @@ func (s *DataSourceService) CreateDataSource(ctx context.Context, create *api.Da
return dataSource, nil
}
+func (s *DataSourceService) CreateDataSourceTx(ctx context.Context, tx *sql.Tx, create *api.DataSourceCreate) (*api.DataSource, error) {
+ return s.createDataSource(ctx, tx, create)
+}
+
// FindDataSourceList retrieves a list of data sources based on find.
func (s *DataSourceService) FindDataSourceList(ctx context.Context, find *api.DataSourceFind) ([]*api.DataSource, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -104,7 +109,7 @@ func (s *DataSourceService) PatchDataSource(ctx context.Context, patch *api.Data
}
// createDataSource creates a new dataSource.
-func (s *DataSourceService) createDataSource(ctx context.Context, tx *Tx, create *api.DataSourceCreate) (*api.DataSource, error) {
+func (s *DataSourceService) createDataSource(ctx context.Context, tx *sql.Tx, create *api.DataSourceCreate) (*api.DataSource, error) {
// Insert row into dataSource.
row, err := tx.QueryContext(ctx, `
INSERT INTO data_source (
diff --git a/store/database.go b/store/database.go
index 5a7254118f..a1b666f5ef 100644
--- a/store/database.go
+++ b/store/database.go
@@ -2,6 +2,7 @@ package store
import (
"context"
+ "database/sql"
"fmt"
"strings"
@@ -33,7 +34,7 @@ func (s *DatabaseService) CreateDatabase(ctx context.Context, create *api.Databa
}
defer tx.Rollback()
- database, err := s.createDatabase(ctx, tx, create)
+ database, err := s.createDatabase(ctx, tx.Tx, create)
if err != nil {
return nil, err
}
@@ -45,6 +46,10 @@ func (s *DatabaseService) CreateDatabase(ctx context.Context, create *api.Databa
return database, nil
}
+func (s *DatabaseService) CreateDatabaseTx(ctx context.Context, tx *sql.Tx, create *api.DatabaseCreate) (*api.Database, error) {
+ return s.createDatabase(ctx, tx, create)
+}
+
// FindDatabaseList retrieves a list of databases based on find.
func (s *DatabaseService) FindDatabaseList(ctx context.Context, find *api.DatabaseFind) ([]*api.Database, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -104,7 +109,7 @@ func (s *DatabaseService) PatchDatabase(ctx context.Context, patch *api.Database
}
// createDatabase creates a new database.
-func (s *DatabaseService) createDatabase(ctx context.Context, tx *Tx, create *api.DatabaseCreate) (*api.Database, error) {
+func (s *DatabaseService) createDatabase(ctx context.Context, tx *sql.Tx, create *api.DatabaseCreate) (*api.Database, error) {
// Insert row into database.
row, err := tx.QueryContext(ctx, `
INSERT INTO db (
diff --git a/store/instance.go b/store/instance.go
index f84f581c61..4484f46bc1 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -18,11 +18,14 @@ var (
type InstanceService struct {
l *zap.Logger
db *DB
+
+ databaseService api.DatabaseService
+ dataSourceService api.DataSourceService
}
// NewInstanceService returns a new instance of InstanceService.
-func NewInstanceService(logger *zap.Logger, db *DB) *InstanceService {
- return &InstanceService{l: logger, db: db}
+func NewInstanceService(logger *zap.Logger, db *DB, databaseService api.DatabaseService, dataSourceService api.DataSourceService) *InstanceService {
+ return &InstanceService{l: logger, db: db, databaseService: databaseService, dataSourceService: dataSourceService}
}
// CreateInstance creates a new instance.
@@ -38,6 +41,35 @@ func (s *InstanceService) CreateInstance(ctx context.Context, create *api.Instan
return nil, err
}
+ // Create * database
+ databaseCreate := &api.DatabaseCreate{
+ CreatorId: create.CreatorId,
+ ProjectId: api.DEFAULT_PROJECT_ID,
+ InstanceId: instance.ID,
+ Name: api.ALL_DATABASE_NAME,
+ CharacterSet: api.DEFAULT_CHARACTER_SET_NAME,
+ Collation: api.DEFAULT_COLLATION_NAME,
+ }
+ allDatabase, err := s.databaseService.CreateDatabaseTx(ctx, tx.Tx, databaseCreate)
+ if err != nil {
+ return nil, err
+ }
+
+ // Create admin data source
+ adminDataSourceCreate := &api.DataSourceCreate{
+ CreatorId: create.CreatorId,
+ InstanceId: instance.ID,
+ DatabaseId: allDatabase.ID,
+ Name: api.ADMIN_DATA_SOURCE_NAME,
+ Type: api.Admin,
+ Username: create.Username,
+ Password: create.Password,
+ }
+ _, err = s.dataSourceService.CreateDataSourceTx(ctx, tx.Tx, adminDataSourceCreate)
+ if err != nil {
+ return nil, err
+ }
+
if err := tx.Commit(); err != nil {
return nil, FormatError(err)
}
diff --git a/store/project.go b/store/project.go
index 2e4b7d8ca1..c6d9da4713 100644
--- a/store/project.go
+++ b/store/project.go
@@ -104,9 +104,9 @@ func (s *ProjectService) PatchProject(ctx context.Context, patch *api.ProjectPat
return project, nil
}
-// PatchProjectWithTx updates an existing project by ID.
+// PatchProjectTx updates an existing project by ID.
// Returns ENOTFOUND if project does not exist.
-func (s *ProjectService) PatchProjectWithTx(ctx context.Context, tx *sql.Tx, patch *api.ProjectPatch) (*api.Project, error) {
+func (s *ProjectService) PatchProjectTx(ctx context.Context, tx *sql.Tx, patch *api.ProjectPatch) (*api.Project, error) {
return patchProject(ctx, tx, patch)
}
diff --git a/store/repository.go b/store/repository.go
index 7c3c8fd452..de8a305d8d 100644
--- a/store/repository.go
+++ b/store/repository.go
@@ -19,12 +19,12 @@ type RepositoryService struct {
l *zap.Logger
db *DB
- ProjectService api.ProjectService
+ projectService api.ProjectService
}
// NewRepositoryService returns a new instance of RepositoryService.
func NewRepositoryService(logger *zap.Logger, db *DB, projectService api.ProjectService) *RepositoryService {
- return &RepositoryService{l: logger, db: db, ProjectService: projectService}
+ return &RepositoryService{l: logger, db: db, projectService: projectService}
}
// CreateRepository creates a new repository.
@@ -135,7 +135,7 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
UpdaterId: create.CreatorId,
WorkflowType: &workflowType,
}
- if _, err := s.ProjectService.PatchProjectWithTx(ctx, tx.Tx, &projectPatch); err != nil {
+ if _, err := s.projectService.PatchProjectTx(ctx, tx.Tx, &projectPatch); err != nil {
return nil, err
}
@@ -347,7 +347,7 @@ func (s *RepositoryService) deleteRepository(ctx context.Context, tx *Tx, delete
UpdaterId: delete.DeleterId,
WorkflowType: &workflowType,
}
- if _, err := s.ProjectService.PatchProjectWithTx(ctx, tx.Tx, &projectPatch); err != nil {
+ if _, err := s.projectService.PatchProjectTx(ctx, tx.Tx, &projectPatch); err != nil {
return err
}