Skip to content

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

feat: ping Oracle database (#5036)

Danny Xue5f696f9b73a
diff --git a/backend/plugin/db/oracle/dump.go b/backend/plugin/db/oracle/dump.go
index 2e54685c4d..2a881e5390 100644
--- a/backend/plugin/db/oracle/dump.go
+++ b/backend/plugin/db/oracle/dump.go
@@ -1,4 +1,4 @@
-package snowflake
+package oracle
import (
"context"
diff --git a/backend/plugin/db/oracle/migrate.go b/backend/plugin/db/oracle/migrate.go
index 3c60461fc4..c72d6c98d8 100644
--- a/backend/plugin/db/oracle/migrate.go
+++ b/backend/plugin/db/oracle/migrate.go
@@ -1,4 +1,4 @@
-package snowflake
+package oracle
import (
"context"
@@ -11,13 +11,11 @@ import (
// Migration related.
// NeedsSetupMigration checks whether we need to setup migration (e.g. creating/upgrading the migration related tables).
-// No need because redis uses bytebase metaDB InstanceChangeHistory.
func (*Driver) NeedsSetupMigration(context.Context) (bool, error) {
return false, nil
}
// SetupMigrationIfNeeded create or upgrade migration related tables.
-// No need for redis because it uses bytebase metaDB InstanceChangeHistory.
func (*Driver) SetupMigrationIfNeeded(context.Context) error {
return nil
}
@@ -27,7 +25,7 @@ func (*Driver) SetupMigrationIfNeeded(context.Context) error {
// Returns the created migration history id and the updated schema on success.
func (d *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (migrationHistoryID string, updatedSchema string, resErr error) {
if m.CreateDatabase {
- return "", "", errors.New("redis: creating databases is not supported")
+ return "", "", errors.New("creating databases is not supported")
}
if _, err := d.Execute(ctx, statement, m.CreateDatabase); err != nil {
return "", "", util.FormatError(err)
@@ -37,5 +35,5 @@ func (d *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, stat
// FindMigrationHistoryList finds the migration history list and return most recent item first.
func (*Driver) FindMigrationHistoryList(context.Context, *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return nil, errors.New("redis: not supported")
+ return nil, errors.New("unsupported")
}
diff --git a/backend/plugin/db/oracle/oracle.go b/backend/plugin/db/oracle/oracle.go
index 9bcbda79a9..20eb856069 100644
--- a/backend/plugin/db/oracle/oracle.go
+++ b/backend/plugin/db/oracle/oracle.go
@@ -1,9 +1,14 @@
-// Package snowflake is the plugin for Snowflake driver.
-package snowflake
+// Package oracle is the plugin for Oracle driver.
+package oracle
import (
"context"
"database/sql"
+ "strconv"
+
+ // Import go-ora Oracle driver.
+ "github.com/pkg/errors"
+ go_ora "github.com/sijms/go-ora/v2"
"github.com/bytebase/bytebase/backend/plugin/db"
)
@@ -18,6 +23,7 @@ func init() {
// Driver is the Oracle driver.
type Driver struct {
+ db *sql.DB
}
func newDriver(db.DriverConfig) db.Driver {
@@ -25,21 +31,32 @@ func newDriver(db.DriverConfig) db.Driver {
}
// Open opens a Snowflake driver.
-func (*Driver) Open(_ context.Context, _ db.Type, _ db.ConnectionConfig, _ db.ConnectionContext) (db.Driver, error) {
- // TODO(d): implement it.
- return nil, nil
+func (driver *Driver) Open(_ context.Context, _ db.Type, config db.ConnectionConfig, _ db.ConnectionContext) (db.Driver, error) {
+ port, err := strconv.Atoi(config.Port)
+ if err != nil {
+ return nil, errors.Errorf("invalid port %q", config.Port)
+ }
+ options := make(map[string]string)
+ if config.SID != "" {
+ options["SID"] = config.SID
+ }
+ dsn := go_ora.BuildUrl(config.Host, port, config.ServiceName, config.Username, config.Password, options)
+ db, err := sql.Open("oracle", dsn)
+ if err != nil {
+ return nil, err
+ }
+ driver.db = db
+ return driver, nil
}
// Close closes the driver.
-func (*Driver) Close(_ context.Context) error {
- // TODO(d): implement it.
- return nil
+func (driver *Driver) Close(_ context.Context) error {
+ return driver.db.Close()
}
// Ping pings the database.
-func (*Driver) Ping(_ context.Context) error {
- // TODO(d): implement it.
- return nil
+func (driver *Driver) Ping(ctx context.Context) error {
+ return driver.db.PingContext(ctx)
}
// GetType returns the database type.
@@ -49,8 +66,7 @@ func (*Driver) GetType() db.Type {
// GetDBConnection gets a database connection.
func (*Driver) GetDBConnection(_ context.Context, _ string) (*sql.DB, error) {
- // TODO(d): implement it.
- return nil, nil
+ return nil, errors.Errorf("GetDBConnection is unsupported for Oracle")
}
// Execute executes a SQL statement and returns the affected rows.
diff --git a/backend/plugin/db/oracle/role.go b/backend/plugin/db/oracle/role.go
index 897d3446d8..3f6518d292 100644
--- a/backend/plugin/db/oracle/role.go
+++ b/backend/plugin/db/oracle/role.go
@@ -1,4 +1,4 @@
-package snowflake
+package oracle
import (
"context"
diff --git a/backend/plugin/db/oracle/sync.go b/backend/plugin/db/oracle/sync.go
index 4bda5f7827..3e0f16babd 100644
--- a/backend/plugin/db/oracle/sync.go
+++ b/backend/plugin/db/oracle/sync.go
@@ -1,4 +1,4 @@
-package snowflake
+package oracle
import (
"context"
@@ -10,11 +10,13 @@ import (
// SyncInstance syncs the instance.
func (*Driver) SyncInstance(_ context.Context) (*db.InstanceMetadata, error) {
// TODO(d): implement it.
- return nil, nil
+ return &db.InstanceMetadata{}, nil
}
// SyncDBSchema syncs a single database schema.
func (*Driver) SyncDBSchema(_ context.Context, _ string) (*storepb.DatabaseMetadata, error) {
// TODO(d): implement it.
- return nil, nil
+ return &storepb.DatabaseMetadata{
+ Name: "orac",
+ }, nil
}
diff --git a/backend/server/server.go b/backend/server/server.go
index 547d7019bf..5ad222af14 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -89,6 +89,8 @@ import (
_ "github.com/bytebase/bytebase/backend/plugin/db/spanner"
// Register redis driver.
_ "github.com/bytebase/bytebase/backend/plugin/db/redis"
+ // Register oracle driver.
+ _ "github.com/bytebase/bytebase/backend/plugin/db/oracle"
// Register pingcap parser driver.
_ "github.com/pingcap/tidb/types/parser_driver"
diff --git a/frontend/src/components/InstanceForm/OracleSIDAndServiceNameInput.vue b/frontend/src/components/InstanceForm/OracleSIDAndServiceNameInput.vue
index e78997eca8..d1b1d0e4f2 100644
--- a/frontend/src/components/InstanceForm/OracleSIDAndServiceNameInput.vue
+++ b/frontend/src/components/InstanceForm/OracleSIDAndServiceNameInput.vue
@@ -7,10 +7,10 @@
tabindex="-1"
type="radio"
class="text-accent disabled:text-accent-disabled focus:ring-accent"
- value="SID"
+ value="ServiceName"
:disabled="!allowEdit"
/>
- <span class="label">SID</span>
+ <span class="label">ServiceName</span>
</label>
<label class="radio">
<input
@@ -18,21 +18,13 @@
tabindex="-1"
type="radio"
class="text-accent disabled:text-accent-disabled focus:ring-accent"
- value="ServiceName"
+ value="SID"
:disabled="!allowEdit"
/>
- <span class="label">ServiceName</span>
+ <span class="label">SID</span>
</label>
</div>
<div>
- <input
- v-if="state.mode === 'SID'"
- :value="state.sid"
- type="text"
- class="textfield w-full mt-1"
- placeholder="SID"
- @input="update(($event.target as HTMLInputElement).value, 'SID')"
- />
<input
v-if="state.mode === 'ServiceName'"
:value="state.serviceName"
@@ -43,6 +35,14 @@
update(($event.target as HTMLInputElement).value, 'ServiceName')
"
/>
+ <input
+ v-if="state.mode === 'SID'"
+ :value="state.sid"
+ type="text"
+ class="textfield w-full mt-1"
+ placeholder="SID"
+ @input="update(($event.target as HTMLInputElement).value, 'SID')"
+ />
</div>
</div>
</template>
@@ -50,7 +50,7 @@
<script lang="ts" setup>
import { reactive, watch } from "vue";
-type Mode = "SID" | "ServiceName";
+type Mode = "ServiceName" | "SID";
type LocalState = {
mode: Mode;
@@ -70,8 +70,8 @@ const emit = defineEmits<{
}>();
const guessModeFromProps = (): Mode => {
- if (props.serviceName) return "ServiceName";
- return "SID";
+ if (props.sid) return "SID";
+ return "ServiceName";
};
const state = reactive<LocalState>({
diff --git a/go.mod b/go.mod
index f59fd8f683..b2a036f4b9 100644
--- a/go.mod
+++ b/go.mod
@@ -40,6 +40,7 @@ require (
github.com/redis/go-redis/v9 v9.0.2
github.com/segmentio/analytics-go v3.1.0+incompatible
github.com/shopspring/decimal v1.3.1
+ github.com/sijms/go-ora/v2 v2.5.33
github.com/snowflakedb/gosnowflake v1.6.14
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
diff --git a/go.sum b/go.sum
index 6c9b8ba007..9bd5679e8d 100644
--- a/go.sum
+++ b/go.sum
@@ -859,6 +859,8 @@ github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBf
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07/go.mod h1:yFdBgwXP24JziuRl2NMUahT7nGLNOKi1SIiFxMttVD4=
github.com/siddontang/go-log v0.0.0-20190221022429-1e957dd83bed h1:KMgQoLJGCq1IoZpLZE3AIffh9veYWoVlsvA4ib55TMM=
github.com/siddontang/go-log v0.0.0-20190221022429-1e957dd83bed/go.mod h1:yFdBgwXP24JziuRl2NMUahT7nGLNOKi1SIiFxMttVD4=
+github.com/sijms/go-ora/v2 v2.5.33 h1:ICRRyLmmFK92TPcgzKvgq8K/lhR5A2A+UB3pUL68Agg=
+github.com/sijms/go-ora/v2 v2.5.33/go.mod h1:EHxlY6x7y9HAsdfumurRfTd+v8NrEOTR3Xl4FWlH6xk=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=