Skip to content

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

Move mysql/pg driver to its own package

天舟e7ef9a498666
diff --git a/bin/bb/main.go b/bin/bb/main.go
index f90b8a0036..e224dc0ea5 100644
--- a/bin/bb/main.go
+++ b/bin/bb/main.go
@@ -5,6 +5,11 @@ import (
"os"
"github.com/bytebase/bytebase/bin/bb/cmd"
+
+ // Register mysql driver
+ _ "github.com/bytebase/bytebase/plugin/db/mysql"
+ // Register postgres driver
+ _ "github.com/bytebase/bytebase/plugin/db/pg"
)
func main() {
diff --git a/bin/server/main.go b/bin/server/main.go
index eab6cbab52..89b3531884 100644
--- a/bin/server/main.go
+++ b/bin/server/main.go
@@ -5,6 +5,9 @@ import (
"time"
"github.com/bytebase/bytebase/bin/server/cmd"
+
+ // Register mysql driver
+ _ "github.com/bytebase/bytebase/plugin/db/mysql"
)
func main() {
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index aa3a4189f4..bf9590ad91 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -265,7 +265,7 @@ type ConnectionContext struct {
}
type Driver interface {
- open(config ConnectionConfig, ctx ConnectionContext) (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
@@ -292,7 +292,7 @@ type Driver interface {
// 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.
-func register(dbType Type, f DriverFunc) {
+func Register(dbType Type, f DriverFunc) {
driversMu.Lock()
defer driversMu.Unlock()
if f == nil {
@@ -313,7 +313,7 @@ func Open(dbType Type, driverConfig DriverConfig, connectionConfig ConnectionCon
return nil, fmt.Errorf("db: unknown driver %v", dbType)
}
- driver, err := f(driverConfig).open(connectionConfig, ctx)
+ driver, err := f(driverConfig).Open(connectionConfig, ctx)
if err != nil {
return nil, err
}
diff --git a/plugin/db/mysql.go b/plugin/db/mysql/mysql.go
similarity index 96%
rename from plugin/db/mysql.go
rename to plugin/db/mysql/mysql.go
index c4a485bbe1..68bf1b7d59 100644
--- a/plugin/db/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -1,4 +1,4 @@
-package db
+package mysql
import (
"bufio"
@@ -10,6 +10,7 @@ import (
"strings"
"time"
+ "github.com/bytebase/bytebase/plugin/db"
"github.com/go-sql-driver/mysql"
"go.uber.org/zap"
)
@@ -25,27 +26,27 @@ var (
"sys": true,
}
- _ Driver = (*MySQLDriver)(nil)
+ _ db.Driver = (*MySQLDriver)(nil)
)
func init() {
- register(MySQL, newMySQLDriver)
+ db.Register(db.MySQL, newMySQLDriver)
}
type MySQLDriver struct {
l *zap.Logger
- connectionCtx ConnectionContext
+ connectionCtx db.ConnectionContext
db *sql.DB
}
-func newMySQLDriver(config DriverConfig) Driver {
+func newMySQLDriver(config db.DriverConfig) db.Driver {
return &MySQLDriver{
l: config.Logger,
}
}
-func (driver *MySQLDriver) open(config ConnectionConfig, ctx ConnectionContext) (Driver, error) {
+func (driver *MySQLDriver) Open(config db.ConnectionConfig, ctx db.ConnectionContext) (db.Driver, error) {
protocol := "tcp"
if strings.HasPrefix(config.Host, "/") {
protocol = "unix"
@@ -97,7 +98,7 @@ func (driver *MySQLDriver) Ping(ctx context.Context) error {
return driver.db.PingContext(ctx)
}
-func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSchema, error) {
+func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSchema, error) {
// Query MySQL version
query := "SELECT VERSION()"
versionRow, err := driver.db.QueryContext(ctx, query)
@@ -131,7 +132,7 @@ func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSche
FROM mysql.user
WHERE user NOT LIKE 'mysql.%'
`
- userList := make([]*DBUser, 0)
+ userList := make([]*db.DBUser, 0)
userRows, err := driver.db.QueryContext(ctx, query)
if err != nil {
@@ -171,7 +172,7 @@ func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSche
grantList = append(grantList, grant)
}
- userList = append(userList, &DBUser{
+ userList = append(userList, &db.DBUser{
Name: name,
Grant: strings.Join(grantList, "\n"),
})
@@ -216,13 +217,13 @@ func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSche
defer indexRows.Close()
// dbName/tableName -> indexList map
- indexMap := make(map[string][]DBIndex)
+ indexMap := make(map[string][]db.DBIndex)
for indexRows.Next() {
var dbName string
var tableName string
var columnName sql.NullString
var expression sql.NullString
- var index DBIndex
+ var index db.DBIndex
if err := indexRows.Scan(
&dbName,
&tableName,
@@ -249,7 +250,7 @@ func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSche
if ok {
indexMap[key] = append(indexList, index)
} else {
- list := make([]DBIndex, 0)
+ list := make([]db.DBIndex, 0)
indexMap[key] = append(list, index)
}
}
@@ -277,13 +278,13 @@ func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSche
defer columnRows.Close()
// dbName/tableName -> columnList map
- columnMap := make(map[string][]DBColumn)
+ columnMap := make(map[string][]db.DBColumn)
for columnRows.Next() {
var dbName string
var tableName string
var nullable string
var defaultStr sql.NullString
- var column DBColumn
+ var column db.DBColumn
if err := columnRows.Scan(
&dbName,
&tableName,
@@ -308,7 +309,7 @@ func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSche
if ok {
columnMap[key] = append(tableList, column)
} else {
- list := make([]DBColumn, 0)
+ list := make([]db.DBColumn, 0)
columnMap[key] = append(list, column)
}
}
@@ -339,10 +340,10 @@ func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSche
defer tableRows.Close()
// dbName -> tableList map
- tableMap := make(map[string][]DBTable)
+ tableMap := make(map[string][]db.DBTable)
for tableRows.Next() {
var dbName string
- var table DBTable
+ var table db.DBTable
if err := tableRows.Scan(
&dbName,
&table.Name,
@@ -369,7 +370,7 @@ func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSche
if ok {
tableMap[dbName] = append(tableList, table)
} else {
- list := make([]DBTable, 0)
+ list := make([]db.DBTable, 0)
tableMap[dbName] = append(list, table)
}
}
@@ -389,9 +390,9 @@ func (driver *MySQLDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSche
}
defer rows.Close()
- schemaList := make([]*DBSchema, 0)
+ schemaList := make([]*db.DBSchema, 0)
for rows.Next() {
- var schema DBSchema
+ var schema db.DBSchema
if err := rows.Scan(
&schema.Name,
&schema.CharacterSet,
@@ -471,7 +472,7 @@ func (driver *MySQLDriver) SetupMigrationIfNeeded(ctx context.Context) error {
return nil
}
-func (driver *MySQLDriver) ExecuteMigration(ctx context.Context, m *MigrationInfo, statement string) error {
+func (driver *MySQLDriver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) error {
tx, err := driver.db.BeginTx(ctx, nil)
if err != nil {
return err
@@ -501,7 +502,7 @@ func (driver *MySQLDriver) ExecuteMigration(ctx context.Context, m *MigrationInf
// If the migration engine is VCS and type is not baseline and is not branch, then we can only proceed if there is existing baseline
// This check is also wrapped in transaction to avoid edge case where two baselinings are running concurrently.
- if m.Engine == VCS && m.Type != Baseline && m.Type != Branch {
+ if m.Engine == db.VCS && m.Type != db.Baseline && m.Type != db.Branch {
hasBaseline, err := findBaseline(ctx, tx, m.Namespace)
if err != nil {
return err
@@ -513,7 +514,7 @@ func (driver *MySQLDriver) ExecuteMigration(ctx context.Context, m *MigrationInf
}
// VCS based SQL migration requires existing baselining
- requireBaseline := m.Engine == VCS && m.Type == Migrate
+ requireBaseline := m.Engine == db.VCS && m.Type == db.Migrate
sequence, err := findNextSequence(ctx, tx, m.Namespace, requireBaseline)
if err != nil {
return err
@@ -575,7 +576,7 @@ func (driver *MySQLDriver) ExecuteMigration(ctx context.Context, m *MigrationInf
return nil
}
-func (driver *MySQLDriver) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
+func (driver *MySQLDriver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
tx, err := driver.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
@@ -618,9 +619,9 @@ func (driver *MySQLDriver) FindMigrationHistoryList(ctx context.Context, find *M
defer rows.Close()
// Iterate over result set and deserialize rows into list.
- list := make([]*MigrationHistory, 0)
+ list := make([]*db.MigrationHistory, 0)
for rows.Next() {
- var history MigrationHistory
+ var history db.MigrationHistory
if err := rows.Scan(
&history.ID,
&history.Creator,
@@ -675,7 +676,7 @@ func findBaseline(ctx context.Context, tx *sql.Tx, namespace string) (bool, erro
return true, nil
}
-func checkDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, engine MigrationEngine, version string) (bool, error) {
+func checkDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, engine db.MigrationEngine, version string) (bool, error) {
query := `
SELECT 1 FROM bytebase.migration_history WHERE namespace = ? AND ` + "`engine` = ? AND version = ?" + `
`
@@ -695,7 +696,7 @@ func checkDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, en
return false, nil
}
-func checkOutofOrderVersion(ctx context.Context, tx *sql.Tx, namespace string, engine MigrationEngine, version string) (*string, error) {
+func checkOutofOrderVersion(ctx context.Context, tx *sql.Tx, namespace string, engine db.MigrationEngine, version string) (*string, error) {
query := `
SELECT MIN(version) FROM bytebase.migration_history WHERE namespace = ? AND ` + "`engine` = ? AND STRCMP(?, version) = -1" + `
`
diff --git a/plugin/db/mysql_migration_schema.sql b/plugin/db/mysql/mysql_migration_schema.sql
similarity index 100%
rename from plugin/db/mysql_migration_schema.sql
rename to plugin/db/mysql/mysql_migration_schema.sql
diff --git a/plugin/db/pg.go b/plugin/db/pg/pg.go
similarity index 76%
rename from plugin/db/pg.go
rename to plugin/db/pg/pg.go
index 11cc5ad38c..926ec2514a 100644
--- a/plugin/db/pg.go
+++ b/plugin/db/pg/pg.go
@@ -1,4 +1,4 @@
-package db
+package pg
import (
"bufio"
@@ -8,32 +8,33 @@ import (
"fmt"
"os"
+ "github.com/bytebase/bytebase/plugin/db"
_ "github.com/lib/pq"
"go.uber.org/zap"
)
var (
- _ Driver = (*PostgresDriver)(nil)
+ _ db.Driver = (*PostgresDriver)(nil)
)
func init() {
- register(Postgres, newPostgresDriver)
+ db.Register(db.Postgres, newPostgresDriver)
}
type PostgresDriver struct {
l *zap.Logger
- connectionCtx ConnectionContext
+ connectionCtx db.ConnectionContext
db *sql.DB
}
-func newPostgresDriver(config DriverConfig) Driver {
+func newPostgresDriver(config db.DriverConfig) db.Driver {
return &PostgresDriver{
l: config.Logger,
}
}
-func (driver *PostgresDriver) open(config ConnectionConfig, ctx ConnectionContext) (Driver, error) {
+func (driver *PostgresDriver) Open(config db.ConnectionConfig, ctx db.ConnectionContext) (db.Driver, error) {
return nil, fmt.Errorf("not implemented")
}
@@ -45,7 +46,7 @@ func (driver *PostgresDriver) Ping(ctx context.Context) error {
return driver.db.PingContext(ctx)
}
-func (driver *PostgresDriver) SyncSchema(ctx context.Context) ([]*DBUser, []*DBSchema, error) {
+func (driver *PostgresDriver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSchema, error) {
return nil, nil, fmt.Errorf("not implemented")
}
@@ -69,11 +70,11 @@ func (driver *PostgresDriver) SetupMigrationIfNeeded(ctx context.Context) error
return fmt.Errorf("not implemented")
}
-func (driver *PostgresDriver) ExecuteMigration(ctx context.Context, m *MigrationInfo, statement string) error {
+func (driver *PostgresDriver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) error {
return fmt.Errorf("not implemented")
}
-func (driver *PostgresDriver) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
+func (driver *PostgresDriver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
return nil, fmt.Errorf("not implemented")
}