| Index: go/database/database.go
|
| diff --git a/go/database/database.go b/go/database/database.go
|
| index 4161ccbbb1a0ae746f2cfebff77ec6aa3bc9fe68..8df1d8a632e87b4582dfd5478c72e1c002aa6211 100644
|
| --- a/go/database/database.go
|
| +++ b/go/database/database.go
|
| @@ -7,33 +7,48 @@ 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 = "test_user"
|
| + 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, 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 +61,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 +87,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 +181,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 +256,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
|
|
|