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

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: Rebase Created 5 years, 11 months 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 "skia.googlesource.com/buildbot.git/go/database"
4 » "fmt"
5 » "strings"
6
7 » "github.com/golang/glog"
8 » "skia.googlesource.com/buildbot.git/go/database"
9 » "skia.googlesource.com/buildbot.git/go/metadata"
10 )
11 4
12 const ( 5 const (
13 » // Key of the password for the readwrite user. 6 » // Default database parameters.
14 » METADATA_KEY = "readwrite" 7 » PROD_DB_HOST = "173.194.104.24"
15 8 » PROD_DB_PORT = 3306
16 » // Detfault database parameters. 9 » PROD_DB_NAME = "skiacorrectness"
17 » DEFAULT_DB_HOST = "173.194.104.24"
18 » DEFAULT_DB_PORT = "3306"
19 » DEFAULT_DB_NAME = "skiacorrectness"
20
21 » // Template to generate the MySQL database connection string.
22 » // And 3306 is the default port for MySQL.
23 » DB_CONN_TMPL = "%s:%s@tcp(%s:%s)/%s?parseTime=true"
24 ) 10 )
25 11
26 // MigrationSteps returns the migration (up and down) for the database. 12 // MigrationSteps returns the migration (up and down) for the database.
27 func MigrationSteps() []database.MigrationStep { 13 func MigrationSteps() []database.MigrationStep {
28 return migrationSteps 14 return migrationSteps
29 } 15 }
30 16
31 // GetConfig returns a DatabaseConfig instance for running in production if a
32 // metadata server is available. If 'local' is true it will always
33 // set the MySQL connection string to "" and thus use a local SQLite database
34 // when used with database.NewVersionedDB.
35 func GetConfig(mySQLConnStr string, sqlitePath string, local bool) *database.Dat abaseConfig {
36 useMySQLConnStr := mySQLConnStr
37
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 }
48
49 // If there is still a placeholder in the connection string, we
50 // set it to empty, so that the the local SQLite database kicks in.
51 if strings.Contains(useMySQLConnStr, "%s") {
52 useMySQLConnStr = ""
53 }
54
55 return &database.DatabaseConfig{
56 MySQLString: useMySQLConnStr,
57 SQLiteFilePath: sqlitePath,
58 MigrationSteps: migrationSteps,
59 }
60 }
61
62 // GetConnectionString returns a MySQL connection string with the given
63 // parameters replace in the template. Only userName has to be provided.
64 // If host, port or dbName are empty the default (production) values will
65 // be used.
66 func GetConnectionString(userName, host, port, dbName string) string {
67 useHost, usePort, useDBName := host, port, dbName
68 if useHost == "" {
69 useHost = DEFAULT_DB_HOST
70 }
71
72 if usePort == "" {
73 usePort = DEFAULT_DB_PORT
74 }
75
76 if useDBName == "" {
77 useDBName = DEFAULT_DB_NAME
78 }
79
80 return fmt.Sprintf(DB_CONN_TMPL, userName, "%s", useHost, usePort, useDB Name)
81 }
82
83 // migrationSteps define the steps it takes to migrate the db between versions. 17 // migrationSteps define the steps it takes to migrate the db between versions.
84 // Note: Only add to this list, once a step has landed in version control it 18 // Note: Only add to this list, once a step has landed in version control it
85 // must not be changed. 19 // must not be changed.
86 var migrationSteps = []database.MigrationStep{ 20 var migrationSteps = []database.MigrationStep{
87 // version 1 21 // version 1
88 { 22 {
89 MySQLUp: []string{ 23 MySQLUp: []string{
90 `CREATE TABLE expectations ( 24 `CREATE TABLE expectations (
91 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 25 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
92 userid TEXT NOT NULL, 26 userid TEXT NOT NULL,
93 ts BIGINT NOT NULL, 27 ts BIGINT NOT NULL,
94 expectations MEDIUMTEXT NOT NULL 28 expectations MEDIUMTEXT NOT NULL
95 )`, 29 )`,
96 }, 30 },
97 MySQLDown: []string{ 31 MySQLDown: []string{
98 `DROP TABLE expectations`, 32 `DROP TABLE expectations`,
99 }, 33 },
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 }, 34 },
112 35
113 // Use this is a template for more migration steps. 36 // Use this is a template for more migration steps.
114 // version x 37 // version x
115 // { 38 // {
116 // MySQLUp: , 39 // MySQLUp: ,
117 // MySQLDown: , 40 // MySQLDown: ,
118 // SQLiteUp: ,
119 // SQLiteDown: ,
120 // }, 41 // },
121 } 42 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698