OLD | NEW |
1 package database | 1 package database |
2 | 2 |
3 import ( | 3 import ( |
4 "database/sql" | 4 "database/sql" |
5 "fmt" | 5 "fmt" |
6 "time" | 6 "time" |
7 | 7 |
8 _ "github.com/go-sql-driver/mysql" | 8 _ "github.com/go-sql-driver/mysql" |
9 "github.com/golang/glog" | 9 "github.com/golang/glog" |
10 _ "github.com/mattn/go-sqlite3" | |
11 "skia.googlesource.com/buildbot.git/go/util" | 10 "skia.googlesource.com/buildbot.git/go/util" |
12 ) | 11 ) |
13 | 12 |
| 13 const ( |
| 14 // Template for DB connection strings. |
| 15 DB_CONN_TMPL = "%s:%s@%s/%s?parseTime=true" |
| 16 |
| 17 // Local test database. |
| 18 TEST_DATABASE = "sk_testing" |
| 19 TEST_DB_HOST = "" |
| 20 TEST_USER_ROOT = "test_root" |
| 21 TEST_USER_RW = "test_rw" |
| 22 TEST_PASSWORD = "" |
| 23 ) |
| 24 |
14 // Config information to create a database connection. | 25 // Config information to create a database connection. |
15 type DatabaseConfig struct { | 26 type DatabaseConfig struct { |
16 MySQLString string | 27 MySQLString string |
17 SQLiteFilePath string | |
18 MigrationSteps []MigrationStep | 28 MigrationSteps []MigrationStep |
19 } | 29 } |
20 | 30 |
| 31 func NewDatabaseConfig(user, password, host, database string, m []MigrationStep)
*DatabaseConfig { |
| 32 return &DatabaseConfig{ |
| 33 MySQLString: fmt.Sprintf(DB_CONN_TMPL, user, password, host,
database), |
| 34 MigrationSteps: m, |
| 35 } |
| 36 } |
| 37 |
| 38 func LocalTestDatabaseConfig(m []MigrationStep) *DatabaseConfig { |
| 39 return NewDatabaseConfig(TEST_USER_RW, TEST_PASSWORD, TEST_DB_HOST, TEST
_DATABASE, m) |
| 40 } |
| 41 |
| 42 func LocalTestRootDatabaseConfig(m []MigrationStep) *DatabaseConfig { |
| 43 return NewDatabaseConfig(TEST_USER_ROOT, TEST_PASSWORD, TEST_DB_HOST, TE
ST_DATABASE, m) |
| 44 } |
| 45 |
21 // Single step to migrated from one database version to the next and back. | 46 // Single step to migrated from one database version to the next and back. |
22 type MigrationStep struct { | 47 type MigrationStep struct { |
23 » MySQLUp []string | 48 » MySQLUp []string |
24 » MySQLDown []string | 49 » MySQLDown []string |
25 » SQLiteUp []string | |
26 » SQLiteDown []string | |
27 } | 50 } |
28 | 51 |
29 // Database handle to send queries to the underlying database. | 52 // Database handle to send queries to the underlying database. |
30 type VersionedDB struct { | 53 type VersionedDB struct { |
31 » // Database intance that is either backed by SQLite or MySQl. | 54 » // Database intance that is backed by MySQL. |
32 DB *sql.DB | 55 DB *sql.DB |
33 | 56 |
34 // Keeps track if we are connected to MySQL or SQLite | |
35 IsMySQL bool | |
36 | |
37 // List of migration steps for this database. | 57 // List of migration steps for this database. |
38 migrationSteps []MigrationStep | 58 migrationSteps []MigrationStep |
39 } | 59 } |
40 | 60 |
41 // Init must be called once before DB is used. | 61 // Init must be called once before DB is used. |
42 // | 62 // |
43 // Since it used glog, make sure it is also called after flag.Parse is called. | 63 // Since it used glog, make sure it is also called after flag.Parse is called. |
44 func NewVersionedDB(conf *DatabaseConfig) *VersionedDB { | 64 func NewVersionedDB(conf *DatabaseConfig) *VersionedDB { |
45 // If there is a connection string then connect to the MySQL server. | 65 // If there is a connection string then connect to the MySQL server. |
46 // This is for testing only. In production we get the relevant informati
on | 66 // This is for testing only. In production we get the relevant informati
on |
47 // from the metadata server. | 67 // from the metadata server. |
48 var err error | 68 var err error |
49 var isMySQL = true | |
50 var DB *sql.DB = nil | 69 var DB *sql.DB = nil |
51 | 70 |
52 » if conf.MySQLString != "" { | 71 » glog.Infoln("Opening SQL database.") |
53 » » glog.Infoln("Opening SQL database.") | 72 » if DB, err = sql.Open("mysql", conf.MySQLString); err == nil { |
54 » » if DB, err = sql.Open("mysql", conf.MySQLString); err == nil { | 73 » » glog.Infoln("Sending Ping.") |
55 » » » glog.Infoln("Sending Ping.") | 74 » » err = DB.Ping() |
56 » » » err = DB.Ping() | 75 » } |
57 » » } | |
58 | 76 |
59 » » if err != nil { | 77 » if err != nil { |
60 » » » glog.Fatalln("Failed to open connection to SQL server:",
err) | 78 » » glog.Fatalln("Failed to open connection to SQL server:", err) |
61 » » } | |
62 » } else { | |
63 » » // Open a local SQLite database instead. | |
64 » » glog.Infof("Opening local sqlite database at: %s", conf.SQLiteFi
lePath) | |
65 » » // Fallback to sqlite for local use. | |
66 » » DB, err = sql.Open("sqlite3", conf.SQLiteFilePath) | |
67 » » if err != nil { | |
68 » » » glog.Fatalln("Failed to open:", err) | |
69 » » } | |
70 » » isMySQL = false | |
71 } | 79 } |
72 | 80 |
73 result := &VersionedDB{ | 81 result := &VersionedDB{ |
74 DB: DB, | 82 DB: DB, |
75 IsMySQL: isMySQL, | |
76 migrationSteps: conf.MigrationSteps, | 83 migrationSteps: conf.MigrationSteps, |
77 } | 84 } |
78 | 85 |
79 // Make sure the migration table exists. | 86 // Make sure the migration table exists. |
80 if err := result.checkVersionTable(); err != nil { | 87 if err := result.checkVersionTable(); err != nil { |
81 // We are using panic() instead of Fataln() to be able to trap t
his | 88 // We are using panic() instead of Fataln() to be able to trap t
his |
82 // in tests and make sure it fails when no version table exists. | 89 // in tests and make sure it fails when no version table exists. |
83 glog.Errorln("Unable to create version table.") | 90 glog.Errorln("Unable to create version table.") |
84 panic("Attempt to create version table returned: " + err.Error()
) | 91 panic("Attempt to create version table returned: " + err.Error()
) |
85 } | 92 } |
86 glog.Infoln("Version table OK.") | 93 glog.Infoln("Version table OK.") |
87 | 94 |
88 // Migrate to the latest version if we are using SQLite, so we don't hav
e | |
89 // to run the *_migratdb command for a local database. | |
90 if !result.IsMySQL { | |
91 result.Migrate(result.MaxDBVersion()) | |
92 } | |
93 | |
94 // Ping the database occasionally to keep the connection fresh. | 95 // Ping the database occasionally to keep the connection fresh. |
95 go func() { | 96 go func() { |
96 c := time.Tick(1 * time.Minute) | 97 c := time.Tick(1 * time.Minute) |
97 for _ = range c { | 98 for _ = range c { |
98 if err := result.DB.Ping(); err != nil { | 99 if err := result.DB.Ping(); err != nil { |
99 glog.Warningln("Database failed to respond:", er
r) | 100 glog.Warningln("Database failed to respond:", er
r) |
100 } | 101 } |
101 glog.Infof("db: Successful ping") | 102 glog.Infof("db: Successful ping") |
102 } | 103 } |
103 }() | 104 }() |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 return version, err | 179 return version, err |
179 } | 180 } |
180 | 181 |
181 // Returns the highest version currently available. | 182 // Returns the highest version currently available. |
182 func (vdb *VersionedDB) MaxDBVersion() int { | 183 func (vdb *VersionedDB) MaxDBVersion() int { |
183 return len(vdb.migrationSteps) | 184 return len(vdb.migrationSteps) |
184 } | 185 } |
185 | 186 |
186 // Returns an error if the version table does not exist. | 187 // Returns an error if the version table does not exist. |
187 func (vdb *VersionedDB) checkVersionTable() error { | 188 func (vdb *VersionedDB) checkVersionTable() error { |
188 » // Check if the table exists in MySQL or SQLite. | 189 » // Check if the table exists in MySQL. |
189 stmt := "SHOW TABLES LIKE 'sk_db_version'" | 190 stmt := "SHOW TABLES LIKE 'sk_db_version'" |
190 if !vdb.IsMySQL { | |
191 stmt = "SELECT name FROM sqlite_master WHERE type='table' AND na
me='sk_db_version';" | |
192 } | |
193 | 191 |
194 var temp string | 192 var temp string |
195 err := vdb.DB.QueryRow(stmt).Scan(&temp) | 193 err := vdb.DB.QueryRow(stmt).Scan(&temp) |
196 if err != nil { | 194 if err != nil { |
197 // See if we can create the version table. | 195 // See if we can create the version table. |
198 return vdb.ensureVersionTable() | 196 return vdb.ensureVersionTable() |
199 } | 197 } |
200 | 198 |
201 return nil | 199 return nil |
202 } | 200 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 if inc < 0 { | 254 if inc < 0 { |
257 idx = currentVersion - 1 | 255 idx = currentVersion - 1 |
258 } | 256 } |
259 delta := util.AbsInt(targetVersion - currentVersion) | 257 delta := util.AbsInt(targetVersion - currentVersion) |
260 result := make([][]string, 0, delta) | 258 result := make([][]string, 0, delta) |
261 | 259 |
262 for i := 0; i < delta; i++ { | 260 for i := 0; i < delta; i++ { |
263 var temp []string | 261 var temp []string |
264 switch { | 262 switch { |
265 // using mysqlp | 263 // using mysqlp |
266 » » case (inc > 0) && vdb.IsMySQL: | 264 » » case (inc > 0): |
267 temp = vdb.migrationSteps[idx].MySQLUp | 265 temp = vdb.migrationSteps[idx].MySQLUp |
268 » » case (inc < 0) && vdb.IsMySQL: | 266 » » case (inc < 0): |
269 temp = vdb.migrationSteps[idx].MySQLDown | 267 temp = vdb.migrationSteps[idx].MySQLDown |
270 // using sqlite | |
271 case (inc > 0): | |
272 temp = vdb.migrationSteps[idx].SQLiteUp | |
273 case (inc < 0): | |
274 temp = vdb.migrationSteps[idx].SQLiteDown | |
275 } | 268 } |
276 result = append(result, temp) | 269 result = append(result, temp) |
277 idx += inc | 270 idx += inc |
278 } | 271 } |
279 return result | 272 return result |
280 } | 273 } |
OLD | NEW |