Skip to content

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

snowflake driver: add skeleton driver.

spinningbotd5ed321cd920
diff --git a/plugin/db/snowflake/snowflake.go b/plugin/db/snowflake/snowflake.go
new file mode 100644
index 0000000000..596d0d2005
--- /dev/null
+++ b/plugin/db/snowflake/snowflake.go
@@ -0,0 +1,177 @@
+package snowflake
+
+import (
+ "bufio"
+ "context"
+ "database/sql"
+ _ "embed"
+ "io"
+
+ "github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/db/util"
+ "go.uber.org/zap"
+)
+
+//go:embed snowflake_migration_schema.sql
+var migrationSchema string
+
+var (
+ systemSchemas = map[string]bool{
+ "information_schema": true,
+ }
+
+ _ db.Driver = (*Driver)(nil)
+)
+
+func init() {
+ db.Register(db.Snowflake, newDriver)
+}
+
+type Driver struct {
+ l *zap.Logger
+ connectionCtx db.ConnectionContext
+ dbType db.Type
+
+ db *sql.DB
+}
+
+func newDriver(config db.DriverConfig) db.Driver {
+ return &Driver{
+ l: config.Logger,
+ }
+}
+
+func (driver *Driver) Open(ctx context.Context, dbType db.Type, config db.ConnectionConfig, connCtx db.ConnectionContext) (db.Driver, error) {
+ // TODO(spinningbot): implement it.
+ return nil, nil
+}
+
+func (driver *Driver) Close(ctx context.Context) error {
+ return driver.db.Close()
+}
+
+func (driver *Driver) Ping(ctx context.Context) error {
+ return driver.db.PingContext(ctx)
+}
+
+func (driver *Driver) GetDbConnection(ctx context.Context, database string) (*sql.DB, error) {
+ return driver.db, nil
+}
+
+func (driver *Driver) GetVersion(ctx context.Context) (string, error) {
+ query := "SELECT CURRENT_VERSION()"
+ versionRow, err := driver.db.QueryContext(ctx, query)
+ if err != nil {
+ return "", util.FormatErrorWithQuery(err, query)
+ }
+ defer versionRow.Close()
+
+ var version string
+ versionRow.Next()
+ if err := versionRow.Scan(&version); err != nil {
+ return "", err
+ }
+ return version, nil
+}
+
+func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSchema, error) {
+ // TODO(spinningbot): implement it.
+ return nil, nil, nil
+}
+
+func (driver *Driver) getUserList(ctx context.Context) ([]*db.DBUser, error) {
+ // TODO(spinningbot): implement it.
+ return nil, nil
+}
+
+func (driver *Driver) Execute(ctx context.Context, statement string) error {
+ tx, err := driver.db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ defer tx.Rollback()
+
+ _, err = tx.ExecContext(ctx, statement)
+
+ if err := tx.Commit(); err != nil {
+ return err
+ }
+
+ return err
+}
+
+// Migration related
+func (driver *Driver) NeedsSetupMigration(ctx context.Context) (bool, error) {
+ // TODO(spinningbot): implement it.
+ return false, nil
+}
+
+func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
+ setup, err := driver.NeedsSetupMigration(ctx)
+ if err != nil {
+ return nil
+ }
+
+ if setup {
+ 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.db.ExecContext(ctx, migrationSchema); err != nil {
+ driver.l.Error("Failed to initialize migration schema.",
+ zap.Error(err),
+ zap.String("environment", driver.connectionCtx.EnvironmentName),
+ zap.String("database", driver.connectionCtx.InstanceName),
+ )
+ return util.FormatErrorWithQuery(err, migrationSchema)
+ }
+ driver.l.Info("Successfully created migration schema.",
+ zap.String("environment", driver.connectionCtx.EnvironmentName),
+ zap.String("database", driver.connectionCtx.InstanceName),
+ )
+ }
+
+ return nil
+}
+
+// ExecuteMigration will execute the migration for MySQL.
+func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (int64, string, error) {
+ // TODO(spinningbot): implement it.
+ return int64(0), "", nil
+}
+
+func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
+ baseQuery := `
+ SELECT
+ id,
+ created_by,
+ created_ts,
+ updated_by,
+ updated_ts,
+ release_version,
+ namespace,
+ sequence,
+ engine,
+ type,
+ status,
+ version,
+ description,
+ statement,
+ schema,
+ schema_prev,
+ execution_duration,
+ issue_id,
+ payload
+ FROM bytebase.public.migration_history `
+ return util.FindMigrationHistoryList(ctx, db.MySQL, driver, find, baseQuery)
+}
+
+func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {
+ // TODO(spinningbot): implement it.
+ return nil
+}
+
+func (driver *Driver) Restore(ctx context.Context, sc *bufio.Scanner) (err error) {
+ // TODO(spinningbot): implement it.
+ return nil
+}
diff --git a/plugin/db/snowflake/snowflake_migration_schema.sql b/plugin/db/snowflake/snowflake_migration_schema.sql
new file mode 100644
index 0000000000..5e5ec307a7
--- /dev/null
+++ b/plugin/db/snowflake/snowflake_migration_schema.sql
@@ -0,0 +1,41 @@
+-- This is the bytebase schema to track migration info for Snowflake
+-- Create a database called bytebase
+CREATE DATABASE bytebase;
+
+-- Create migration_history table
+-- Note, we don't create trigger to update created_ts and updated_ts because that may causes error:
+-- ERROR 1419 (HY000): You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
+CREATE TABLE bytebase.public.migration_history (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ created_by TEXT NOT NULL,
+ created_ts BIGINT NOT NULL,
+ updated_by TEXT NOT NULL,
+ updated_ts BIGINT NOT NULL,
+ -- Record the client version creating this migration history. For Bytebase, we use its binary release version. Different Bytebase release might
+ -- record different history info and thie field helps to handle such situation properly. Moreover, it helps debugging.
+ release_version TEXT NOT NULL,
+ -- Allows granular tracking of migration history (e.g If an application manages schemas for a multi-tenant service and each tenant has its own schema, that application can use namespace to record the tenant name to track the per-tenant schema migration)
+ -- Since bytebase also manages different application databases from an instance, it leverages this field to track each database migration history.
+ namespace TEXT NOT NULL,
+ -- Used to detect out of order migration together with 'namespace' and 'version' column.
+ sequence INTEGER NOT NULL,
+ -- We call it engine because maybe we could load history from other migration tool.
+ engine TEXT NOT NULL,
+ type TEXT NOT NULL,
+ -- Snowflake runs DDL in its own transaction, so we can't record DDL and migration_history into a single transaction.
+ -- Thus, we create a "PENDING" record before applying the DDL and update that record to "DONE" after applying the DDL.
+ status TEXT NOT NULL,
+ -- Record the migration version.
+ version TEXT NOT NULL,
+ description TEXT NOT NULL,
+ -- Record the migration statement
+ statement TEXT NOT NULL,
+ -- Record the schema after migration
+ schema TEXT NOT NULL,
+ -- Record the schema before migration. Though we could also fetch it from the previous migration history, it would complicate fetching logic.
+ -- Besides, by storing the schema_prev, we can perform consistency check to see if the migration history has any gaps.
+ schema_prev TEXT NOT NULL,
+ execution_duration INTEGER NOT NULL,
+ issue_id TEXT NOT NULL,
+ payload TEXT NOT NULL
+);