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

Side by Side Diff: golden/go/db/db.go

Issue 813443002: Overhaul database package (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Comments/readme cleanup 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 unified diff | Download patch
OLDNEW
1 package db 1 package db
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "strings"
6 5
7 "github.com/golang/glog"
8 "skia.googlesource.com/buildbot.git/go/database" 6 "skia.googlesource.com/buildbot.git/go/database"
9 "skia.googlesource.com/buildbot.git/go/metadata"
10 ) 7 )
11 8
12 const ( 9 const (
13 // Key of the password for the readwrite user. 10 // Key of the password for the readwrite user.
14 METADATA_KEY = "readwrite" 11 METADATA_KEY = "readwrite"
15 12
16 // Detfault database parameters. 13 // Detfault database parameters.
17 DEFAULT_DB_HOST = "173.194.104.24" 14 DEFAULT_DB_HOST = "173.194.104.24"
18 DEFAULT_DB_PORT = "3306" 15 DEFAULT_DB_PORT = "3306"
19 DEFAULT_DB_NAME = "skiacorrectness" 16 DEFAULT_DB_NAME = "skiacorrectness"
20 17
21 // Template to generate the MySQL database connection string. 18 // Template to generate the MySQL database connection string.
22 // And 3306 is the default port for MySQL. 19 // And 3306 is the default port for MySQL.
23 DB_CONN_TMPL = "%s:%s@tcp(%s:%s)/%s?parseTime=true" 20 DB_CONN_TMPL = "%s:%s@tcp(%s:%s)/%s?parseTime=true"
24 ) 21 )
25 22
26 // MigrationSteps returns the migration (up and down) for the database. 23 // MigrationSteps returns the migration (up and down) for the database.
27 func MigrationSteps() []database.MigrationStep { 24 func MigrationSteps() []database.MigrationStep {
28 return migrationSteps 25 return migrationSteps
29 } 26 }
30 27
31 // GetConfig returns a DatabaseConfig instance for running in production if a 28 // DatabaseConfig returns a DatabaseConfig instance. If mySQLConnStr is
32 // metadata server is available. If 'local' is true it will always 29 // provided, it overrides all other settings. Otherwise, if 'local' is true
33 // set the MySQL connection string to "" and thus use a local SQLite database 30 // a config for a local MySQL database is returned. If 'local' is false, a
34 // when used with database.NewVersionedDB. 31 // config appropriate for running in production is returned.
35 func GetConfig(mySQLConnStr string, sqlitePath string, local bool) *database.Dat abaseConfig { 32 func DatabaseConfig(mySQLConnStr string, local bool) (*database.DatabaseConfig, error) {
36 » useMySQLConnStr := mySQLConnStr 33 » if mySQLConnStr != "" {
37 34 » » return database.ResolveCustomMySQLString(mySQLConnStr, migration Steps)
38 » // We are in the production environment, so we look up the password.
39 » if !local {
40 » » // First, get the password from the metadata server.
41 » » // See https://developers.google.com/compute/docs/metadata#custo m.
42 » » password, err := metadata.Get(METADATA_KEY)
43 » » if err != nil {
44 » » » glog.Fatalf("Failed to find metadata. Use 'local' flag w hen running locally.")
45 » » }
46 » » useMySQLConnStr = fmt.Sprintf(mySQLConnStr, password)
47 } 35 }
48 36
49 » // If there is still a placeholder in the connection string, we 37 » if local {
50 » // set it to empty, so that the the local SQLite database kicks in. 38 » » return database.LocalDatabaseConfig(DEFAULT_DB_NAME, migrationSt eps), nil
51 » if strings.Contains(useMySQLConnStr, "%s") { 39 » } else {
52 » » useMySQLConnStr = "" 40 » » return database.ProdDatabaseConfig(database.AddrFromHostPort(DEF AULT_DB_HOST, DEFAULT_DB_PORT), DEFAULT_DB_NAME, migrationSteps), nil
53 » }
54
55 » return &database.DatabaseConfig{
56 » » MySQLString: useMySQLConnStr,
57 » » SQLiteFilePath: sqlitePath,
58 » » MigrationSteps: migrationSteps,
59 } 41 }
60 } 42 }
61 43
62 // GetConnectionString returns a MySQL connection string with the given 44 // GetConnectionString returns a MySQL connection string with the given
63 // parameters replace in the template. Only userName has to be provided. 45 // parameters replace in the template. Only userName has to be provided.
64 // If host, port or dbName are empty the default (production) values will 46 // If host, port or dbName are empty the default (production) values will
65 // be used. 47 // be used.
66 func GetConnectionString(userName, host, port, dbName string) string { 48 func GetConnectionString(userName, host, port, dbName string) string {
67 useHost, usePort, useDBName := host, port, dbName 49 useHost, usePort, useDBName := host, port, dbName
68 if useHost == "" { 50 if useHost == "" {
(...skipping 21 matching lines...) Expand all
90 `CREATE TABLE expectations ( 72 `CREATE TABLE expectations (
91 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 73 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
92 userid TEXT NOT NULL, 74 userid TEXT NOT NULL,
93 ts BIGINT NOT NULL, 75 ts BIGINT NOT NULL,
94 expectations MEDIUMTEXT NOT NULL 76 expectations MEDIUMTEXT NOT NULL
95 )`, 77 )`,
96 }, 78 },
97 MySQLDown: []string{ 79 MySQLDown: []string{
98 `DROP TABLE expectations`, 80 `DROP TABLE expectations`,
99 }, 81 },
100 SQLiteUp: []string{
101 `CREATE TABLE expectations (
102 id INTEGER NOT NULL PRIMARY KEY A UTOINCREMENT,
103 userid TEXT NOT NULL,
104 ts BIGINT NOT NULL,
105 expectations MEDIUXMTEXT NOT NULL
106 )`,
107 },
108 SQLiteDown: []string{
109 `DROP TABLE expectations`,
110 },
111 }, 82 },
112 83
113 // Use this is a template for more migration steps. 84 // Use this is a template for more migration steps.
114 // version x 85 // version x
115 // { 86 // {
116 // MySQLUp: , 87 // MySQLUp: ,
117 // MySQLDown: , 88 // MySQLDown: ,
118 // SQLiteUp: ,
119 // SQLiteDown: ,
120 // }, 89 // },
121 } 90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698