Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sqlite): add sqlite database type (#414)
spinningbotf2fdfa131fd0
diff --git a/bin/server/main.go b/bin/server/main.goindex 8f12430fb7..6cae0347c8 100644--- a/bin/server/main.go+++ b/bin/server/main.go@@ -19,6 +19,8 @@ import (// Register snowflake driver._ "github.com/bytebase/bytebase/plugin/db/snowflake"+ // Register sqlite driver.+ _ "github.com/bytebase/bytebase/plugin/db/sqlite"// Register pingcap parser driver._ "github.com/pingcap/tidb/types/parser_driver"diff --git a/plugin/db/driver.go b/plugin/db/driver.goindex f52102d02c..82de823d58 100644--- a/plugin/db/driver.go+++ b/plugin/db/driver.go@@ -26,6 +26,8 @@ const (Postgres Type = "POSTGRES"// Snowflake is the database type for SNOWFLAKE.Snowflake Type = "SNOWFLAKE"+ // SQLite is the database type for SQLite.+ SQLite Type = "SQLITE"// TiDB is the database type for TIDB.TiDB Type = "TIDB")diff --git a/plugin/db/mysql/mysql_migration_schema.sql b/plugin/db/mysql/mysql_migration_schema.sqlindex 9f40099448..9938c29a16 100644--- a/plugin/db/mysql/mysql_migration_schema.sql+++ b/plugin/db/mysql/mysql_migration_schema.sql@@ -49,4 +49,4 @@ CREATE UNIQUE INDEX bytebase_idx_unique_migration_history_namespace_engine_versiCREATE INDEX bytebase_idx_migration_history_namespace_engine_type ON bytebase.migration_history(namespace(256), engine(256), type(256));-CREATE INDEX bytebase_idx_migration_history_namespace_created ON bytebase.migration_history(namespace(256), `created_ts`);+CREATE INDEX bytebase_idx_migration_history_namespace_created ON bytebase.migration_history(namespace(256), created_ts);diff --git a/plugin/db/sqlite/sqlite.go b/plugin/db/sqlite/sqlite.gonew file mode 100644index 0000000000..1f087cf00d--- /dev/null+++ b/plugin/db/sqlite/sqlite.go@@ -0,0 +1,154 @@+package sqlite++import (+ "bufio"+ "context"+ "database/sql"+ "io"++ // embed will embeds the migration schema.+ _ "embed"++ "github.com/bytebase/bytebase/plugin/db"+ "github.com/bytebase/bytebase/plugin/db/util"+ "go.uber.org/zap"+)++//go:embed sqlite_migration_schema.sql+var migrationSchema string++var (+ _ db.Driver = (*Driver)(nil)+)++func init() {+ db.Register(db.SQLite, newDriver)+}++// Driver is the SQLite driver.+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,+ }+}++// Open opens a SQLite driver.+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+}++// Close closes the driver.+func (driver *Driver) Close(ctx context.Context) error {+ return driver.db.Close()+}++// Ping pings the database.+func (driver *Driver) Ping(ctx context.Context) error {+ return driver.db.PingContext(ctx)+}++// GetDbConnection gets a database connection.+func (driver *Driver) GetDbConnection(ctx context.Context, database string) (*sql.DB, error) {+ return driver.db, nil+}++// GetVersion gets the version.+func (driver *Driver) GetVersion(ctx context.Context) (string, error) {+ // TODO(spinningbot): implement it.+ return "", nil+}++// SyncSchema synces the schema.+func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.User, []*db.Schema, error) {+ // TODO(spinningbot): implement it.+ return nil, nil, nil+}++// Execute executes a SQL statement.+func (driver *Driver) Execute(ctx context.Context, statement string, useTransaction bool) error {+ tx, err := driver.db.BeginTx(ctx, nil)+ if err != nil {+ return err+ }+ defer tx.Rollback()++ _, err = tx.ExecContext(ctx, statement)++ if err == nil {+ if err := tx.Commit(); err != nil {+ return err+ }+ }++ return err+}++// Query queries a SQL statement.+func (driver *Driver) Query(ctx context.Context, statement string, limit int) ([]interface{}, error) {+ return util.Query(ctx, driver.l, driver.db, statement, limit)+}++// NeedsSetupMigration returns whether it needs to setup migration.+func (driver *Driver) NeedsSetupMigration(ctx context.Context) (bool, error) {+ // TODO(spinningbot): implement it.+ return false, nil+}++// SetupMigrationIfNeeded sets up migration if needed.+func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {+ // TODO(spinningbot): implement it.+ return nil+}++// ExecuteMigration will execute the migration.+func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (int64, string, error) {+ // TODO(spinningbot): implement it.+ return 0, "", nil+}++// FindMigrationHistoryList finds the migration history.+func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {+ // TODO(spinningbot): implement it.+ return nil, nil+}++// Dump dumps the database.+func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {+ // TODO(spinningbot): implement it.+ return nil+}++// Restore restores a database.+func (driver *Driver) Restore(ctx context.Context, sc *bufio.Scanner) (err error) {+ txn, err := driver.db.BeginTx(ctx, nil)+ if err != nil {+ return err+ }+ defer txn.Rollback()++ f := func(stmt string) error {+ if _, err := txn.Exec(stmt); err != nil {+ return err+ }+ return nil+ }++ if err := util.ApplyMultiStatements(sc, f); err != nil {+ return err+ }++ if err := txn.Commit(); err != nil {+ return err+ }++ return nil+}diff --git a/plugin/db/sqlite/sqlite_migration_schema.sql b/plugin/db/sqlite/sqlite_migration_schema.sqlnew file mode 100644index 0000000000..e20d4890bd--- /dev/null+++ b/plugin/db/sqlite/sqlite_migration_schema.sql@@ -0,0 +1,45 @@+-- Create migration_history table+CREATE TABLE bytebase_migration_history (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ created_by TEXT NOT NULL,+ created_ts INTEGER NOT NULL,+ updated_by TEXT NOT NULL,+ updated_ts INTEGER 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 UNSIGNED NOT NULL,+ -- We call it engine because maybe we could load history from other migration tool.+ -- Current allowed values are UI, VCS.+ engine TEXT NOT NULL,+ -- Current allowed values are BASELINE, MIGRATE, BRANCH, DATA.+ type TEXT NOT NULL,+ -- Current allowed values are PENDING, DONE, FAILED.+ -- 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 MEDIUMTEXT 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 MEDIUMTEXT NOT NULL,+ execution_duration_ns INTEGER NOT NULL,+ issue_id TEXT NOT NULL,+ payload TEXT NOT NULL+);++CREATE UNIQUE INDEX bytebase_idx_unique_migration_history_namespace_sequence ON bytebase_migration_history (namespace, sequence);++CREATE UNIQUE INDEX bytebase_idx_unique_migration_history_namespace_engine_version ON bytebase_migration_history (namespace, engine, version);++CREATE INDEX bytebase_idx_migration_history_namespace_engine_type ON bytebase_migration_history(namespace, engine, type);++CREATE INDEX bytebase_idx_migration_history_namespace_created ON bytebase_migration_history(namespace, created_ts);