Skip to content

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

feat(test): make sqlite create database to work (#510)

Danny Xu8a51622ee455
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 4fcd38b4aa..bc0575c205 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -484,6 +484,8 @@ func (p *QueryParams) QueryString() string {
return mysqlQuery(p.Names)
case Postgres:
return pgQuery(p.Names)
+ case SQLite:
+ return mysqlQuery(p.Names)
}
return ""
}
diff --git a/plugin/db/sqlite/sqlite.go b/plugin/db/sqlite/sqlite.go
index 5ee0dc62bf..d5287053f3 100644
--- a/plugin/db/sqlite/sqlite.go
+++ b/plugin/db/sqlite/sqlite.go
@@ -157,6 +157,23 @@ func (driver *Driver) hasBytebaseDatabase() (bool, error) {
// Execute executes a SQL statement.
func (driver *Driver) Execute(ctx context.Context, statement string, useTransaction bool) error {
+ // This is a fake CREATA DATABASE statement. Engine driver will recognize it and establish a connect to create the database.
+ if strings.HasPrefix(statement, "CREATE DATABASE ") {
+ parts := strings.Split(statement, `'`)
+ if len(parts) != 3 {
+ return fmt.Errorf("invalid statement %q", statement)
+ }
+ db, err := driver.GetDbConnection(ctx, parts[1])
+ if err != nil {
+ return err
+ }
+ // We need to query to persist the database file.
+ if _, err := db.Query("SELECT 1;"); err != nil {
+ return err
+ }
+ return nil
+ }
+
tx, err := driver.db.BeginTx(ctx, nil)
if err != nil {
return err
@@ -288,7 +305,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
InsertHistoryQuery: insertHistoryQuery,
UpdateHistoryAsDoneQuery: updateHistoryAsDoneQuery,
UpdateHistoryAsFailedQuery: updateHistoryAsFailedQuery,
- TablePrefix: "",
+ TablePrefix: "bytebase_",
}
return util.ExecuteMigration(ctx, driver.l, db.SQLite, driver, m, statement, args)
}
diff --git a/server/issue.go b/server/issue.go
index eaef8222cb..b9e8b91e1d 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -990,6 +990,9 @@ func getDatabaseNameAndStatement(dbType db.Type, databaseName, characterSet, col
if schema != "" {
stmt = fmt.Sprintf("%s\nUSE DATABASE %s;\n%s", stmt, databaseName, schema)
}
+ case db.SQLite:
+ // This is a fake CREATA DATABASE statement since a single SQLite file represents a database. Engine driver will recognize it and establish a connection to create the sqlite file representing the database.
+ stmt = fmt.Sprintf("CREATE DATABASE '%s';", databaseName)
}
return databaseName, stmt