OLD | NEW |
1 package db | 1 package db |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "strings" | 5 "strings" |
6 | 6 |
7 "github.com/golang/glog" | 7 "github.com/golang/glog" |
8 "skia.googlesource.com/buildbot.git/go/database" | 8 "skia.googlesource.com/buildbot.git/go/database" |
9 "skia.googlesource.com/buildbot.git/go/metadata" | 9 "skia.googlesource.com/buildbot.git/go/metadata" |
10 ) | 10 ) |
(...skipping 12 matching lines...) Expand all Loading... |
23 DB_CONN_TMPL = "%s:%s@tcp(%s:%s)/%s?parseTime=true" | 23 DB_CONN_TMPL = "%s:%s@tcp(%s:%s)/%s?parseTime=true" |
24 ) | 24 ) |
25 | 25 |
26 // MigrationSteps returns the migration (up and down) for the database. | 26 // MigrationSteps returns the migration (up and down) for the database. |
27 func MigrationSteps() []database.MigrationStep { | 27 func MigrationSteps() []database.MigrationStep { |
28 return migrationSteps | 28 return migrationSteps |
29 } | 29 } |
30 | 30 |
31 // GetConfig returns a DatabaseConfig instance for running in production if a | 31 // GetConfig returns a DatabaseConfig instance for running in production if a |
32 // metadata server is available. If 'local' is true it will always | 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 | 33 // set the MySQL connection string to "" and thus use a local MySQL test |
34 // when used with database.NewVersionedDB. | 34 // database when used with database.NewVersionedDB. |
35 func GetConfig(mySQLConnStr string, sqlitePath string, local bool) *database.Dat
abaseConfig { | 35 func GetConfig(mySQLConnStr string, local bool) *database.DatabaseConfig { |
36 useMySQLConnStr := mySQLConnStr | 36 useMySQLConnStr := mySQLConnStr |
37 | 37 |
38 // We are in the production environment, so we look up the password. | 38 // We are in the production environment, so we look up the password. |
39 if !local { | 39 if !local { |
40 // First, get the password from the metadata server. | 40 // First, get the password from the metadata server. |
41 // See https://developers.google.com/compute/docs/metadata#custo
m. | 41 // See https://developers.google.com/compute/docs/metadata#custo
m. |
42 password, err := metadata.Get(METADATA_KEY) | 42 password, err := metadata.Get(METADATA_KEY) |
43 if err != nil { | 43 if err != nil { |
44 glog.Fatalf("Failed to find metadata. Use 'local' flag w
hen running locally.") | 44 glog.Fatalf("Failed to find metadata. Use 'local' flag w
hen running locally.") |
45 } | 45 } |
46 useMySQLConnStr = fmt.Sprintf(mySQLConnStr, password) | 46 useMySQLConnStr = fmt.Sprintf(mySQLConnStr, password) |
47 } | 47 } |
48 | 48 |
49 // If there is still a placeholder in the connection string, we | 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. | 50 » // set it to empty, so that the the local MySQL test database kicks in. |
51 if strings.Contains(useMySQLConnStr, "%s") { | 51 if strings.Contains(useMySQLConnStr, "%s") { |
52 useMySQLConnStr = "" | 52 useMySQLConnStr = "" |
53 } | 53 } |
54 | 54 |
55 » return &database.DatabaseConfig{ | 55 » if useMySQLConnStr != "" { |
56 » » MySQLString: useMySQLConnStr, | 56 » » return &database.DatabaseConfig{ |
57 » » SQLiteFilePath: sqlitePath, | 57 » » » MySQLString: useMySQLConnStr, |
58 » » MigrationSteps: migrationSteps, | 58 » » » MigrationSteps: migrationSteps, |
| 59 » » } |
| 60 » } else { |
| 61 » » return database.LocalTestDatabaseConfig(migrationSteps) |
59 } | 62 } |
60 } | 63 } |
61 | 64 |
62 // GetConnectionString returns a MySQL connection string with the given | 65 // GetConnectionString returns a MySQL connection string with the given |
63 // parameters replace in the template. Only userName has to be provided. | 66 // parameters replace in the template. Only userName has to be provided. |
64 // If host, port or dbName are empty the default (production) values will | 67 // If host, port or dbName are empty the default (production) values will |
65 // be used. | 68 // be used. |
66 func GetConnectionString(userName, host, port, dbName string) string { | 69 func GetConnectionString(userName, host, port, dbName string) string { |
67 useHost, usePort, useDBName := host, port, dbName | 70 useHost, usePort, useDBName := host, port, dbName |
68 if useHost == "" { | 71 if useHost == "" { |
(...skipping 21 matching lines...) Expand all Loading... |
90 `CREATE TABLE expectations ( | 93 `CREATE TABLE expectations ( |
91 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, | 94 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, |
92 userid TEXT NOT NULL, | 95 userid TEXT NOT NULL, |
93 ts BIGINT NOT NULL, | 96 ts BIGINT NOT NULL, |
94 expectations MEDIUMTEXT NOT NULL | 97 expectations MEDIUMTEXT NOT NULL |
95 )`, | 98 )`, |
96 }, | 99 }, |
97 MySQLDown: []string{ | 100 MySQLDown: []string{ |
98 `DROP TABLE expectations`, | 101 `DROP TABLE expectations`, |
99 }, | 102 }, |
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 }, | 103 }, |
112 | 104 |
113 // Use this is a template for more migration steps. | 105 // Use this is a template for more migration steps. |
114 // version x | 106 // version x |
115 // { | 107 // { |
116 // MySQLUp: , | 108 // MySQLUp: , |
117 // MySQLDown: , | 109 // MySQLDown: , |
118 // SQLiteUp: , | |
119 // SQLiteDown: , | |
120 // }, | 110 // }, |
121 } | 111 } |
OLD | NEW |