OLD | NEW |
1 package db | 1 package db |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 | 5 |
6 "github.com/golang/glog" | 6 "github.com/golang/glog" |
7 "skia.googlesource.com/buildbot.git/go/database" | 7 "skia.googlesource.com/buildbot.git/go/database" |
8 "skia.googlesource.com/buildbot.git/go/metadata" | 8 "skia.googlesource.com/buildbot.git/go/metadata" |
9 ) | 9 ) |
10 | 10 |
11 const ( | 11 const ( |
12 // Key of the password for the readwrite user. | 12 // Key of the password for the readwrite user. |
13 METADATA_KEY = "readwrite" | 13 METADATA_KEY = "readwrite" |
14 ) | 14 ) |
15 | 15 |
16 // The migration steps for this DB. | 16 // MigrationSteps returns the migration (up and down) for the database. |
17 func MigrationSteps() []database.MigrationStep { | 17 func MigrationSteps() []database.MigrationStep { |
18 return migrationSteps | 18 return migrationSteps |
19 } | 19 } |
20 | 20 |
21 // Returns the DB connection string for running in production where a | 21 // GetConfig returns a DatabaseConfig instance for running in production if a |
22 // metadata server is available. If 'local' is true it will always return | 22 // metadata server is available. If 'local' is true it will always |
23 // "" (empty string). When used with Init() this will cause it to use a | 23 // set the MySQL connection string to "" and thus use a local SQLite database |
24 // local SQLite database. If it's not local and the meta data server is | 24 // when used with database.NewVersionedDB. |
25 // unreachable it will terminate. | 25 func GetConfig(mySQLConnStr string, sqlitePath string, local bool) *database.Dat
abaseConfig { |
26 func GetDatabase(mySQLConnStr string, sqlitePath string, local bool) *database.D
atabaseConfig { | |
27 useMySQLConnStr := mySQLConnStr | 26 useMySQLConnStr := mySQLConnStr |
28 | 27 |
29 // We are in the production environment, so we look up the password. | 28 // We are in the production environment, so we look up the password. |
30 if !local { | 29 if !local { |
31 // First, get the password from the metadata server. | 30 // First, get the password from the metadata server. |
32 // See https://developers.google.com/compute/docs/metadata#custo
m. | 31 // See https://developers.google.com/compute/docs/metadata#custo
m. |
33 password, err := metadata.Get(METADATA_KEY) | 32 password, err := metadata.Get(METADATA_KEY) |
34 if err != nil { | 33 if err != nil { |
35 glog.Fatalf("Failed to find metadata. Use 'local' flag w
hen running locally.") | 34 glog.Fatalf("Failed to find metadata. Use 'local' flag w
hen running locally.") |
36 } | 35 } |
37 useMySQLConnStr = fmt.Sprintf(mySQLConnStr, password) | 36 useMySQLConnStr = fmt.Sprintf(mySQLConnStr, password) |
38 } | 37 } |
39 | 38 |
40 return &database.DatabaseConfig{ | 39 return &database.DatabaseConfig{ |
41 MySQLString: useMySQLConnStr, | 40 MySQLString: useMySQLConnStr, |
42 SQLiteFilePath: sqlitePath, | 41 SQLiteFilePath: sqlitePath, |
43 MigrationSteps: migrationSteps, | 42 MigrationSteps: migrationSteps, |
44 } | 43 } |
45 } | 44 } |
46 | 45 |
47 // Define the migration steps. | 46 // migrationSteps define the steps it takes to migrate the db between versions. |
48 // Note: Only add to this list, once a step has landed in version control it | 47 // Note: Only add to this list, once a step has landed in version control it |
49 // must not be changed. | 48 // must not be changed. |
50 var migrationSteps = []database.MigrationStep{ | 49 var migrationSteps = []database.MigrationStep{ |
51 // version 1 | 50 // version 1 |
52 { | 51 { |
53 MySQLUp: []string{ | 52 MySQLUp: []string{ |
54 `CREATE TABLE expectations ( | 53 `CREATE TABLE expectations ( |
55 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, | 54 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, |
56 userid TEXT NOT NULL, | 55 userid TEXT NOT NULL, |
57 ts BIGINT NOT NULL, | 56 ts BIGINT NOT NULL, |
(...skipping 18 matching lines...) Expand all Loading... |
76 | 75 |
77 // Use this is a template for more migration steps. | 76 // Use this is a template for more migration steps. |
78 // version x | 77 // version x |
79 // { | 78 // { |
80 // MySQLUp: , | 79 // MySQLUp: , |
81 // MySQLDown: , | 80 // MySQLDown: , |
82 // SQLiteUp: , | 81 // SQLiteUp: , |
83 // SQLiteDown: , | 82 // SQLiteDown: , |
84 // }, | 83 // }, |
85 } | 84 } |
OLD | NEW |