Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Unified Diff: go/database/database.go

Issue 813443002: Overhaul database package (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Add MySQLTestDatabase for shared test usage Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: go/database/database.go
diff --git a/go/database/database.go b/go/database/database.go
index 4161ccbbb1a0ae746f2cfebff77ec6aa3bc9fe68..187c08e7f05564cedd247d83465d749d976d4f4a 100644
--- a/go/database/database.go
+++ b/go/database/database.go
@@ -7,33 +7,53 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/golang/glog"
- _ "github.com/mattn/go-sqlite3"
"skia.googlesource.com/buildbot.git/go/util"
)
+const (
+ // Template for DB connection strings.
+ DB_CONN_TMPL = "%s:%s@%s/%s?parseTime=true"
+
+ // Local test database.
+ TEST_DATABASE = "sk_testing"
+ TEST_DB_HOST = ""
+ TEST_USER_ROOT = "test_root"
+ TEST_USER_RW = "test_rw"
+ TEST_PASSWORD = ""
+)
+
// Config information to create a database connection.
type DatabaseConfig struct {
MySQLString string
- SQLiteFilePath string
MigrationSteps []MigrationStep
}
+func NewDatabaseConfig(user, password, host, database string, m []MigrationStep) *DatabaseConfig {
+ return &DatabaseConfig{
+ MySQLString: fmt.Sprintf(DB_CONN_TMPL, user, password, host, database),
+ MigrationSteps: m,
+ }
+}
+
+func LocalTestDatabaseConfig(m []MigrationStep) *DatabaseConfig {
+ return NewDatabaseConfig(TEST_USER_RW, TEST_PASSWORD, TEST_DB_HOST, TEST_DATABASE, m)
+}
+
+func LocalTestRootDatabaseConfig(m []MigrationStep) *DatabaseConfig {
+ return NewDatabaseConfig(TEST_USER_ROOT, TEST_PASSWORD, TEST_DB_HOST, TEST_DATABASE, m)
+}
+
// Single step to migrated from one database version to the next and back.
type MigrationStep struct {
- MySQLUp []string
- MySQLDown []string
- SQLiteUp []string
- SQLiteDown []string
+ MySQLUp []string
+ MySQLDown []string
}
// Database handle to send queries to the underlying database.
type VersionedDB struct {
- // Database intance that is either backed by SQLite or MySQl.
+ // Database intance that is backed by MySQL.
DB *sql.DB
- // Keeps track if we are connected to MySQL or SQLite
- IsMySQL bool
-
// List of migration steps for this database.
migrationSteps []MigrationStep
}
@@ -46,33 +66,20 @@ func NewVersionedDB(conf *DatabaseConfig) *VersionedDB {
// This is for testing only. In production we get the relevant information
// from the metadata server.
var err error
- var isMySQL = true
var DB *sql.DB = nil
- if conf.MySQLString != "" {
- glog.Infoln("Opening SQL database.")
- if DB, err = sql.Open("mysql", conf.MySQLString); err == nil {
- glog.Infoln("Sending Ping.")
- err = DB.Ping()
- }
+ glog.Infoln("Opening SQL database.")
+ if DB, err = sql.Open("mysql", conf.MySQLString); err == nil {
+ glog.Infoln("Sending Ping.")
+ err = DB.Ping()
+ }
- if err != nil {
- glog.Fatalln("Failed to open connection to SQL server:", err)
- }
- } else {
- // Open a local SQLite database instead.
- glog.Infof("Opening local sqlite database at: %s", conf.SQLiteFilePath)
- // Fallback to sqlite for local use.
- DB, err = sql.Open("sqlite3", conf.SQLiteFilePath)
- if err != nil {
- glog.Fatalln("Failed to open:", err)
- }
- isMySQL = false
+ if err != nil {
+ glog.Fatalln("Failed to open connection to SQL server:", err)
}
result := &VersionedDB{
DB: DB,
- IsMySQL: isMySQL,
migrationSteps: conf.MigrationSteps,
}
@@ -85,12 +92,6 @@ func NewVersionedDB(conf *DatabaseConfig) *VersionedDB {
}
glog.Infoln("Version table OK.")
- // Migrate to the latest version if we are using SQLite, so we don't have
- // to run the *_migratdb command for a local database.
- if !result.IsMySQL {
- result.Migrate(result.MaxDBVersion())
- }
-
// Ping the database occasionally to keep the connection fresh.
go func() {
c := time.Tick(1 * time.Minute)
@@ -185,11 +186,8 @@ func (vdb *VersionedDB) MaxDBVersion() int {
// Returns an error if the version table does not exist.
func (vdb *VersionedDB) checkVersionTable() error {
- // Check if the table exists in MySQL or SQLite.
+ // Check if the table exists in MySQL.
stmt := "SHOW TABLES LIKE 'sk_db_version'"
- if !vdb.IsMySQL {
- stmt = "SELECT name FROM sqlite_master WHERE type='table' AND name='sk_db_version';"
- }
var temp string
err := vdb.DB.QueryRow(stmt).Scan(&temp)
@@ -263,15 +261,10 @@ func (vdb *VersionedDB) getMigrations(currentVersion int, targetVersion int) [][
var temp []string
switch {
// using mysqlp
- case (inc > 0) && vdb.IsMySQL:
- temp = vdb.migrationSteps[idx].MySQLUp
- case (inc < 0) && vdb.IsMySQL:
- temp = vdb.migrationSteps[idx].MySQLDown
- // using sqlite
case (inc > 0):
- temp = vdb.migrationSteps[idx].SQLiteUp
+ temp = vdb.migrationSteps[idx].MySQLUp
case (inc < 0):
- temp = vdb.migrationSteps[idx].SQLiteDown
+ temp = vdb.migrationSteps[idx].MySQLDown
}
result = append(result, temp)
idx += inc

Powered by Google App Engine
This is Rietveld 408576698