Skip to content

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

Provide a connectionCtx to the driver code to assist logging

天舟e3a92283bf3f
diff --git a/db/driver.go b/db/driver.go
index 1ca8c992d7..4e85a94f59 100644
--- a/db/driver.go
+++ b/db/driver.go
@@ -219,8 +219,22 @@ type MigrationHistoryFind struct {
Limit *int
}
+type ConnectionConfig struct {
+ Host string
+ Port string
+ Username string
+ Password string
+ Database string
+}
+
+// Context not used for establishing the db connection, but is useful for logging.
+type ConnectionContext struct {
+ EnvironmentName string
+ InstanceName string
+}
+
type Driver interface {
- open(config ConnectionConfig) (Driver, error)
+ open(config ConnectionConfig, ctx ConnectionContext) (Driver, error)
// Remember to call Close to avoid connection leak
Close(ctx context.Context) error
Ping(ctx context.Context) error
@@ -238,14 +252,6 @@ type Driver interface {
FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error)
}
-type ConnectionConfig struct {
- Host string
- Port string
- Username string
- Password string
- Database string
-}
-
// Register makes a database driver available by the provided type.
// If Register is called twice with the same name or if driver is nil,
// it panics.
@@ -262,7 +268,7 @@ func register(dbType Type, f DriverFunc) {
}
// Open opens a database specified by its database driver type and connection config
-func Open(dbType Type, driverConfig DriverConfig, connectionConfig ConnectionConfig) (Driver, error) {
+func Open(dbType Type, driverConfig DriverConfig, connectionConfig ConnectionConfig, ctx ConnectionContext) (Driver, error) {
driversMu.RLock()
f, ok := drivers[dbType]
driversMu.RUnlock()
@@ -270,7 +276,7 @@ func Open(dbType Type, driverConfig DriverConfig, connectionConfig ConnectionCon
return nil, fmt.Errorf("db: unknown driver %v", dbType)
}
- driver, err := f(driverConfig).open(connectionConfig)
+ driver, err := f(driverConfig).open(connectionConfig, ctx)
if err != nil {
return nil, err
}
diff --git a/db/mysql.go b/db/mysql.go
index f2edf23e0a..b1cee60be1 100644
--- a/db/mysql.go
+++ b/db/mysql.go
@@ -24,7 +24,8 @@ func init() {
}
type MySQLDriver struct {
- l *zap.Logger
+ l *zap.Logger
+ connectionCtx ConnectionContext
db *sql.DB
}
@@ -35,7 +36,7 @@ func newDriver(config DriverConfig) Driver {
}
}
-func (driver *MySQLDriver) open(config ConnectionConfig) (Driver, error) {
+func (driver *MySQLDriver) open(config ConnectionConfig, ctx ConnectionContext) (Driver, error) {
protocol := "tcp"
if strings.HasPrefix(config.Host, "/") {
protocol = "unix"
@@ -49,12 +50,17 @@ func (driver *MySQLDriver) open(config ConnectionConfig) (Driver, error) {
}
dsn := fmt.Sprintf("%s:%s@%s(%s:%s)/%s?%s", config.Username, config.Password, protocol, config.Host, port, config.Database, strings.Join(params, "&"))
- driver.l.Debug("Opening MySQL driver", zap.String("dsn", dsn))
+ driver.l.Debug("Opening MySQL driver",
+ zap.String("dsn", dsn),
+ zap.String("environment", ctx.EnvironmentName),
+ zap.String("database", ctx.InstanceName),
+ )
db, err := sql.Open("mysql", dsn)
if err != nil {
panic(err)
}
driver.db = db
+ driver.connectionCtx = ctx
return driver, nil
}
@@ -418,12 +424,22 @@ func (driver *MySQLDriver) SetupMigrationIfNeeded(ctx context.Context) error {
}
if setup {
- driver.l.Info("Bytebase migration schema not found, creating schema...")
+ driver.l.Info("Bytebase migration schema not found, creating schema...",
+ zap.String("environment", driver.connectionCtx.EnvironmentName),
+ zap.String("database", driver.connectionCtx.InstanceName),
+ )
if err := driver.Execute(ctx, migrationSchema); err != nil {
- driver.l.Error("Failed to initialize migration schema.", zap.Error(err))
+ driver.l.Error("Failed to initialize migration schema.",
+ zap.Error(err),
+ zap.String("environment", driver.connectionCtx.EnvironmentName),
+ zap.String("database", driver.connectionCtx.InstanceName),
+ )
return formatErrorWithQuery(err, migrationSchema)
}
- driver.l.Info("Successfully created migration schema.")
+ driver.l.Info("Successfully created migration schema.",
+ zap.String("environment", driver.connectionCtx.EnvironmentName),
+ zap.String("database", driver.connectionCtx.InstanceName),
+ )
}
return nil
diff --git a/server/instance.go b/server/instance.go
index 03df7c0f13..dbf51f27d7 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -38,12 +38,20 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
// Try creating the "bytebase" db in the added instance if needed.
// Since we allow user to add new instance upfront even providing the incorrect username/password,
// thus it's OK if it fails. Frontend will surface relavant info suggesting the "bytebase" db hasn't created yet.
- db, err := db.Open(instance.Engine, db.DriverConfig{Logger: s.l}, db.ConnectionConfig{
- Username: instance.Username,
- Password: instance.Password,
- Host: instance.Host,
- Port: instance.Port,
- })
+ db, err := db.Open(
+ instance.Engine,
+ db.DriverConfig{Logger: s.l},
+ db.ConnectionConfig{
+ Username: instance.Username,
+ Password: instance.Password,
+ Host: instance.Host,
+ Port: instance.Port,
+ },
+ db.ConnectionContext{
+ EnvironmentName: instance.Environment.Name,
+ InstanceName: instance.Name,
+ },
+ )
if err == nil {
defer db.Close(context.Background())
db.SetupMigrationIfNeeded(context.Background())
@@ -210,12 +218,20 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
}
resultSet := &api.SqlResultSet{}
- db, err := db.Open(instance.Engine, db.DriverConfig{Logger: s.l}, db.ConnectionConfig{
- Username: instance.Username,
- Password: instance.Password,
- Host: instance.Host,
- Port: instance.Port,
- })
+ db, err := db.Open(
+ instance.Engine,
+ db.DriverConfig{Logger: s.l},
+ db.ConnectionConfig{
+ Username: instance.Username,
+ Password: instance.Password,
+ Host: instance.Host,
+ Port: instance.Port,
+ },
+ db.ConnectionContext{
+ EnvironmentName: instance.Environment.Name,
+ InstanceName: instance.Name,
+ },
+ )
if err != nil {
resultSet.Error = err.Error()
} else {
@@ -247,12 +263,20 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
}
instanceMigration := &api.InstanceMigration{}
- db, err := db.Open(instance.Engine, db.DriverConfig{Logger: s.l}, db.ConnectionConfig{
- Username: instance.Username,
- Password: instance.Password,
- Host: instance.Host,
- Port: instance.Port,
- })
+ db, err := db.Open(
+ instance.Engine,
+ db.DriverConfig{Logger: s.l},
+ db.ConnectionConfig{
+ Username: instance.Username,
+ Password: instance.Password,
+ Host: instance.Host,
+ Port: instance.Port,
+ },
+ db.ConnectionContext{
+ EnvironmentName: instance.Environment.Name,
+ InstanceName: instance.Name,
+ },
+ )
if err != nil {
instanceMigration.Status = api.InstanceMigrationSchemaUnknown
instanceMigration.Error = err.Error()
@@ -304,12 +328,20 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
}
historyList := []*api.MigrationHistory{}
- driver, err := db.Open(instance.Engine, db.DriverConfig{Logger: s.l}, db.ConnectionConfig{
- Username: instance.Username,
- Password: instance.Password,
- Host: instance.Host,
- Port: instance.Port,
- })
+ driver, err := db.Open(
+ instance.Engine,
+ db.DriverConfig{Logger: s.l},
+ db.ConnectionConfig{
+ Username: instance.Username,
+ Password: instance.Password,
+ Host: instance.Host,
+ Port: instance.Port,
+ },
+ db.ConnectionContext{
+ EnvironmentName: instance.Environment.Name,
+ InstanceName: instance.Name,
+ },
+ )
if err == nil {
defer driver.Close(context.Background())
list, err := driver.FindMigrationHistoryList(context.Background(), find)
diff --git a/server/schema_syncer.go b/server/schema_syncer.go
index 282088421f..8e51b6845a 100644
--- a/server/schema_syncer.go
+++ b/server/schema_syncer.go
@@ -49,7 +49,7 @@ func (s *SchemaSyncer) Run() error {
}
for _, instance := range list {
- if err := s.server.ComposeInstanceAdminDataSource(context.Background(), instance); err != nil {
+ if err := s.server.ComposeInstanceRelationship(context.Background(), instance); err != nil {
s.l.Error("Failed to sync instance",
zap.Int("id", instance.ID),
zap.String("name", instance.Name),
diff --git a/server/sql.go b/server/sql.go
index 56b3750905..5c1f917a1e 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -33,12 +33,17 @@ func (s *Server) registerSqlRoutes(g *echo.Group) {
password = adminPassword
}
- db, err := db.Open(connectionInfo.DBType, db.DriverConfig{Logger: s.l}, db.ConnectionConfig{
- Username: connectionInfo.Username,
- Password: password,
- Host: connectionInfo.Host,
- Port: connectionInfo.Port,
- })
+ db, err := db.Open(
+ connectionInfo.DBType,
+ db.DriverConfig{Logger: s.l},
+ db.ConnectionConfig{
+ Username: connectionInfo.Username,
+ Password: password,
+ Host: connectionInfo.Host,
+ Port: connectionInfo.Port,
+ },
+ db.ConnectionContext{},
+ )
resultSet := &api.SqlResultSet{}
if err != nil {
@@ -92,12 +97,20 @@ func (s *Server) registerSqlRoutes(g *echo.Group) {
func (s *Server) SyncSchema(instance *api.Instance) (rs *api.SqlResultSet) {
resultSet := &api.SqlResultSet{}
err := func() error {
- driver, err := db.Open(db.Mysql, db.DriverConfig{Logger: s.l}, db.ConnectionConfig{
- Username: instance.Username,
- Password: instance.Password,
- Host: instance.Host,
- Port: instance.Port,
- })
+ driver, err := db.Open(
+ db.Mysql,
+ db.DriverConfig{Logger: s.l},
+ db.ConnectionConfig{
+ Username: instance.Username,
+ Password: instance.Password,
+ Host: instance.Host,
+ Port: instance.Port,
+ },
+ db.ConnectionContext{
+ EnvironmentName: instance.Environment.Name,
+ InstanceName: instance.Name,
+ },
+ )
if err != nil {
return fmt.Errorf("failed to connect instance: %v with user: %v. Error %w", instance.Name, instance.Username, err)
}
diff --git a/server/task_executor_database_create.go b/server/task_executor_database_create.go
index 0ef7405015..255fe4106b 100644
--- a/server/task_executor_database_create.go
+++ b/server/task_executor_database_create.go
@@ -43,12 +43,20 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser
}
instance := task.Instance
- driver, err := db.Open(instance.Engine, db.DriverConfig{Logger: exec.l}, db.ConnectionConfig{
- Username: instance.Username,
- Password: instance.Password,
- Host: instance.Host,
- Port: instance.Port,
- })
+ driver, err := db.Open(
+ instance.Engine,
+ db.DriverConfig{Logger: exec.l},
+ db.ConnectionConfig{
+ Username: instance.Username,
+ Password: instance.Password,
+ Host: instance.Host,
+ Port: instance.Port,
+ },
+ db.ConnectionContext{
+ EnvironmentName: instance.Environment.Name,
+ InstanceName: instance.Name,
+ },
+ )
if err != nil {
return true, "", fmt.Errorf("failed to connect instance: %v with user: %v. %w", instance.Name, instance.Username, err)
}
diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.go
index 36050f8bdb..be2686af92 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -111,13 +111,21 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
}
instance := task.Instance
- driver, err := db.Open(instance.Engine, db.DriverConfig{Logger: exec.l}, db.ConnectionConfig{
- Username: instance.Username,
- Password: instance.Password,
- Host: instance.Host,
- Port: instance.Port,
- Database: databaseName,
- })
+ driver, err := db.Open(
+ instance.Engine,
+ db.DriverConfig{Logger: exec.l},
+ db.ConnectionConfig{
+ Username: instance.Username,
+ Password: instance.Password,
+ Host: instance.Host,
+ Port: instance.Port,
+ Database: databaseName,
+ },
+ db.ConnectionContext{
+ EnvironmentName: instance.Environment.Name,
+ InstanceName: instance.Name,
+ },
+ )
if err != nil {
return true, "", fmt.Errorf("failed to connect instance: %v with user: %v. %w", instance.Name, instance.Username, err)
}