Skip to content

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

fix(202): don't use transaction for creating postgres databases (#211)

spinningbotaca73d649698
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.go
index 1900283a2a..01c0309c21 100644
--- a/plugin/db/clickhouse/clickhouse.go
+++ b/plugin/db/clickhouse/clickhouse.go
@@ -359,7 +359,7 @@ func (driver *Driver) getUserList(ctx context.Context) ([]*db.User, error) {
}
// Execute executes a SQL statement.
-func (driver *Driver) Execute(ctx context.Context, statement string) error {
+func (driver *Driver) Execute(ctx context.Context, statement string, useTransaction bool) error {
tx, err := driver.db.BeginTx(ctx, nil)
if err != nil {
return err
@@ -407,7 +407,7 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
zap.String("environment", driver.connectionCtx.EnvironmentName),
zap.String("database", driver.connectionCtx.InstanceName),
)
- if err := driver.Execute(ctx, migrationSchema); err != nil {
+ if err := driver.Execute(ctx, migrationSchema, true /* useTransaction */); err != nil {
driver.l.Error("Failed to initialize migration schema.",
zap.Error(err),
zap.String("environment", driver.connectionCtx.EnvironmentName),
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index b7bbd4398c..f33731cf4f 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -351,7 +351,7 @@ type Driver interface {
GetDbConnection(ctx context.Context, database string) (*sql.DB, error)
GetVersion(ctx context.Context) (string, error)
SyncSchema(ctx context.Context) ([]*User, []*Schema, error)
- Execute(ctx context.Context, statement string) error
+ Execute(ctx context.Context, statement string, useTransaction bool) error
// Migration related
// Check whether we need to setup migration (e.g. creating/upgrading the migration related tables)
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index 0e3339da20..20230a7f69 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -523,7 +523,7 @@ func (driver *Driver) getUserList(ctx context.Context) ([]*db.User, error) {
}
// Execute executes a SQL statement.
-func (driver *Driver) Execute(ctx context.Context, statement string) error {
+func (driver *Driver) Execute(ctx context.Context, statement string, useTransaction bool) error {
tx, err := driver.db.BeginTx(ctx, nil)
if err != nil {
return err
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index 6dcbfaa1b8..1884c7be74 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -503,7 +503,16 @@ func (driver *Driver) getUserList(ctx context.Context) ([]*db.User, error) {
}
// Execute executes a SQL statement.
-func (driver *Driver) Execute(ctx context.Context, statement string) error {
+func (driver *Driver) Execute(ctx context.Context, statement string, useTransaction bool) error {
+ // We don't use transaction for creating databases in Postgres.
+ // https://github.com/bytebase/bytebase/issues/202
+ if !useTransaction {
+ if _, err := driver.db.ExecContext(ctx, statement); err != nil {
+ return err
+ }
+ return nil
+ }
+
tx, err := driver.db.BeginTx(ctx, nil)
if err != nil {
return err
diff --git a/plugin/db/snowflake/snowflake.go b/plugin/db/snowflake/snowflake.go
index a93389b1f1..ed5e5d3137 100644
--- a/plugin/db/snowflake/snowflake.go
+++ b/plugin/db/snowflake/snowflake.go
@@ -474,7 +474,7 @@ func (driver *Driver) getUserList(ctx context.Context) ([]*db.User, error) {
}
// Execute executes a SQL statement.
-func (driver *Driver) Execute(ctx context.Context, statement string) error {
+func (driver *Driver) Execute(ctx context.Context, statement string, useTransaction bool) error {
count := 0
f := func(stmt string) error {
count++
@@ -559,7 +559,7 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
zap.String("database", driver.connectionCtx.InstanceName),
)
// Should use role SYSADMIN.
- if err := driver.Execute(ctx, migrationSchema); err != nil {
+ if err := driver.Execute(ctx, migrationSchema, true /* useTransaction */); err != nil {
driver.l.Error("Failed to initialize migration schema.",
zap.Error(err),
zap.String("environment", driver.connectionCtx.EnvironmentName),
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index a397a80dae..3b407d04da 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -376,8 +376,10 @@ func ExecuteMigration(ctx context.Context, l *zap.Logger, dbType db.Type, driver
return -1, "", err
}
}
- // MySQL executes DDL in its own transaction, so there is no need to supply a transaction.
- if err = driver.Execute(ctx, statement); err != nil {
+ // MySQL executes DDL in its own transaction, so there is no need to supply a transaction from previous migration history updates.
+ // Also, we don't use transaction for creating databases in Postgres.
+ // https://github.com/bytebase/bytebase/issues/202
+ if err = driver.Execute(ctx, statement, !m.CreateDatabase); err != nil {
return -1, "", formatError(err)
}
}