OLD | NEW |
1 package db | 1 package db |
2 | 2 |
3 import ( | 3 import ( |
4 "database/sql" | 4 "database/sql" |
5 | 5 |
6 "fmt" | |
7 | |
8 "github.com/golang/glog" | |
9 "skia.googlesource.com/buildbot.git/go/database" | 6 "skia.googlesource.com/buildbot.git/go/database" |
10 "skia.googlesource.com/buildbot.git/go/metadata" | |
11 ) | 7 ) |
12 | 8 |
13 const ( | 9 const ( |
14 » // Key of the password for the readwrite user. | 10 » // Default database parameters. |
15 » METADATA_KEY = "readwrite" | 11 » PROD_DB_HOST = "173.194.104.24" |
16 | 12 » PROD_DB_PORT = 3306 |
17 » // Path where the SQLite database is stored when running locally. | 13 » PROD_DB_NAME = "skia" |
18 » SQLITE_DB_PATH = "./perf.db" | |
19 | |
20 » // Template to generate the database connection string in production. | |
21 » // The IP address of the database is found here: | |
22 » // https://console.developers.google.com/project/31977622648/sql/inst
ances/skiaperf/overview | |
23 » // And 3306 is the default port for MySQL. | |
24 » DB_CONN_TMPL = "%s:%s@tcp(173.194.104.24:3306)/skia?parseTime=true" | |
25 | |
26 » // Username of the read/write user. | |
27 » RW_USER = "readwrite" | |
28 ) | 14 ) |
29 | 15 |
30 var ( | 16 var ( |
31 DB *sql.DB = nil | 17 DB *sql.DB = nil |
32 ) | 18 ) |
33 | 19 |
34 // Setup the database to be shared across the app. | 20 // Setup the database to be shared across the app. |
35 func Init(conf *database.DatabaseConfig) { | 21 func Init(conf *database.DatabaseConfig) { |
36 vdb := database.NewVersionedDB(conf) | 22 vdb := database.NewVersionedDB(conf) |
37 DB = vdb.DB | 23 DB = vdb.DB |
38 } | 24 } |
39 | 25 |
| 26 // MigrationSteps returns the migration (up and down) for the database. |
40 func MigrationSteps() []database.MigrationStep { | 27 func MigrationSteps() []database.MigrationStep { |
41 return migrationSteps | 28 return migrationSteps |
42 } | 29 } |
43 | 30 |
44 // Returns the DB connection string for running in production where a | |
45 // metadata server is available. If 'local' is true it will always return | |
46 // "" (empty string). When used with Init() this will cause it to use a | |
47 // local SQLite database. If it's not local and the meta data server is | |
48 // unreachable it will terminate. | |
49 func ProdDatabaseConfig(local bool) *database.DatabaseConfig { | |
50 mysqlStr := "" | |
51 sqlitePath := SQLITE_DB_PATH | |
52 | |
53 // We are in the production environment, so we look up the parameters. | |
54 if !local { | |
55 // First, get the password from the metadata server. | |
56 // See https://developers.google.com/compute/docs/metadata#custo
m. | |
57 password, err := metadata.Get(METADATA_KEY) | |
58 if err != nil { | |
59 glog.Fatalf("Failed to find metadata. Use 'local' flag w
hen running locally.") | |
60 } | |
61 mysqlStr, sqlitePath = fmt.Sprintf(DB_CONN_TMPL, RW_USER, passwo
rd), "" | |
62 } | |
63 | |
64 return &database.DatabaseConfig{ | |
65 MySQLString: mysqlStr, | |
66 SQLiteFilePath: sqlitePath, | |
67 MigrationSteps: migrationSteps, | |
68 } | |
69 } | |
70 | |
71 // Define the migration steps. | 31 // Define the migration steps. |
72 // Note: Only add to this list, once a step has landed in version control it | 32 // Note: Only add to this list, once a step has landed in version control it |
73 // must not be changed. | 33 // must not be changed. |
74 var migrationSteps = []database.MigrationStep{ | 34 var migrationSteps = []database.MigrationStep{ |
75 // version 1 | 35 // version 1 |
76 { | 36 { |
77 MySQLUp: []string{ | 37 MySQLUp: []string{ |
78 `CREATE TABLE IF NOT EXISTS shortcuts ( | 38 `CREATE TABLE IF NOT EXISTS shortcuts ( |
79 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, | 39 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, |
80 traces MEDIUMTEXT NOT NULL | 40 traces MEDIUMTEXT NOT NULL |
81 )`, | 41 )`, |
82 | 42 |
83 `CREATE TABLE IF NOT EXISTS clusters ( | 43 `CREATE TABLE IF NOT EXISTS clusters ( |
84 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, | 44 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, |
85 ts BIGINT NOT NULL, | 45 ts BIGINT NOT NULL, |
86 hash TEXT NOT NULL, | 46 hash TEXT NOT NULL, |
87 regression FLOAT NOT NULL, | 47 regression FLOAT NOT NULL, |
88 cluster MEDIUMTEXT NOT NULL, | 48 cluster MEDIUMTEXT NOT NULL, |
89 status TEXT NOT NULL, | 49 status TEXT NOT NULL, |
90 message TEXT NOT NULL | 50 message TEXT NOT NULL |
91 )`, | 51 )`, |
92 | 52 |
93 `CREATE TABLE IF NOT EXISTS tries ( | 53 `CREATE TABLE IF NOT EXISTS tries ( |
94 issue VARCHAR(255) NOT NULL PRIMARY KEY, | 54 issue VARCHAR(255) NOT NULL PRIMARY KEY, |
95 lastUpdated BIGINT NOT NULL, | 55 lastUpdated BIGINT NOT NULL, |
96 results LONGTEXT NOT NULL | 56 results LONGTEXT NOT NULL |
97 )`, | 57 )`, |
98 }, | 58 }, |
99 MySQLDown: []string{}, | 59 MySQLDown: []string{}, |
100 SQLiteUp: []string{ | |
101 `CREATE TABLE clusters ( | |
102 id INTEGER NOT NULL PRIMARY KEY AUT
OINCREMENT, | |
103 ts TIMESTAMP NOT NULL, | |
104 hash TEXT NOT NULL, | |
105 regression FLOAT NOT NULL, | |
106 cluster MEDIUMTEXT NOT NULL, | |
107 status TEXT NOT NULL, | |
108 message TEXT NOT NULL | |
109 )`, | |
110 `CREATE TABLE shortcuts ( | |
111 id INTEGER NOT NULL PRIMARY KEY AUTOINC
REMENT, | |
112 traces MEDIUMTEXT NOT NULL | |
113 )`, | |
114 `CREATE TABLE tries ( | |
115 issue VARCHAR(255) NOT NULL PRIMARY KEY, | |
116 lastUpdated TIMESTAMP NOT NULL, | |
117 results MEDIUMTEXT NOT NULL | |
118 )`, | |
119 }, | |
120 SQLiteDown: []string{ | |
121 `DROP TABLE IF EXISTS clusters`, | |
122 `DROP TABLE IF EXISTS shortcuts`, | |
123 `DROP TABLE IF EXISTS tries`, | |
124 }, | |
125 }, | 60 }, |
126 // version 2 | 61 // version 2 |
127 { | 62 { |
128 MySQLUp: []string{ | 63 MySQLUp: []string{ |
129 `CREATE TABLE IF NOT EXISTS activitylog ( | 64 `CREATE TABLE IF NOT EXISTS activitylog ( |
130 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, | 65 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, |
131 timestamp BIGINT NOT NULL, | 66 timestamp BIGINT NOT NULL, |
132 userid TEXT NOT NULL, | 67 userid TEXT NOT NULL, |
133 action TEXT NOT NULL, | 68 action TEXT NOT NULL, |
134 url TEXT | 69 url TEXT |
135 )`, | 70 )`, |
136 }, | 71 }, |
137 MySQLDown: []string{}, | 72 MySQLDown: []string{}, |
138 SQLiteUp: []string{ | |
139 `CREATE TABLE activitylog ( | |
140 id INTEGER NOT NULL PRIMARY KEY AUT
OINCREMENT, | |
141 timestamp TIMESTAMP NOT NULL, | |
142 userid TEXT NOT NULL, | |
143 action TEXT NOT NULL, | |
144 url TEXT | |
145 )`, | |
146 }, | |
147 SQLiteDown: []string{ | |
148 `DROP TABLE IF EXISTS activitylog`, | |
149 }, | |
150 }, | 73 }, |
151 | 74 |
152 // Use this is a template for more migration steps. | 75 // Use this is a template for more migration steps. |
153 // version x | 76 // version x |
154 // { | 77 // { |
155 // MySQLUp: , | 78 // MySQLUp: , |
156 // MySQLDown: , | 79 // MySQLDown: , |
157 // SQLiteUp: , | |
158 // SQLiteDown: , | |
159 // }, | 80 // }, |
160 } | 81 } |
OLD | NEW |