| 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 » // Name of the database in production. |
| 15 » METADATA_KEY = "readwrite" | 11 » PROD_DB = "skia" |
| 16 | 12 |
| 17 // Path where the SQLite database is stored when running locally. | |
| 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: | 13 // The IP address of the database is found here: |
| 22 // https://console.developers.google.com/project/31977622648/sql/inst
ances/skiaperf/overview | 14 // https://console.developers.google.com/project/31977622648/sql/inst
ances/skiaperf/overview |
| 23 // And 3306 is the default port for MySQL. | 15 // And 3306 is the default port for MySQL. |
| 24 » DB_CONN_TMPL = "%s:%s@tcp(173.194.104.24:3306)/skia?parseTime=true" | 16 » PROD_DB_HOST = "tcp(173.194.104.24:3306)" |
| 25 | |
| 26 » // Username of the read/write user. | |
| 27 » RW_USER = "readwrite" | |
| 28 ) | 17 ) |
| 29 | 18 |
| 30 var ( | 19 var ( |
| 31 DB *sql.DB = nil | 20 DB *sql.DB = nil |
| 32 ) | 21 ) |
| 33 | 22 |
| 34 // Setup the database to be shared across the app. | 23 // Setup the database to be shared across the app. |
| 35 func Init(conf *database.DatabaseConfig) { | 24 func Init(conf *database.DatabaseConfig) { |
| 36 vdb := database.NewVersionedDB(conf) | 25 vdb := database.NewVersionedDB(conf) |
| 37 DB = vdb.DB | 26 DB = vdb.DB |
| 38 } | 27 } |
| 39 | 28 |
| 40 func MigrationSteps() []database.MigrationStep { | 29 func MigrationSteps() []database.MigrationStep { |
| 41 return migrationSteps | 30 return migrationSteps |
| 42 } | 31 } |
| 43 | 32 |
| 44 // Returns the DB connection string for running in production where a | 33 // Returns the DB connection string for running in production where a |
| 45 // metadata server is available. If 'local' is true it will always return | 34 // 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 | 35 // "" (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 | 36 // local MySQL test database. If it's not local and the meta data server is |
| 48 // unreachable it will terminate. | 37 // unreachable it will terminate. |
| 49 func ProdDatabaseConfig(local bool) *database.DatabaseConfig { | 38 func DatabaseConfig(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. | 39 // We are in the production environment, so we look up the parameters. |
| 54 if !local { | 40 if !local { |
| 55 » » // First, get the password from the metadata server. | 41 » » return database.ProdDatabaseConfig(PROD_DB_HOST, PROD_DB, migrat
ionSteps) |
| 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 } | 42 } |
| 63 | 43 |
| 64 » return &database.DatabaseConfig{ | 44 » return database.LocalDatabaseConfig(PROD_DB, migrationSteps) |
| 65 » » MySQLString: mysqlStr, | |
| 66 » » SQLiteFilePath: sqlitePath, | |
| 67 » » MigrationSteps: migrationSteps, | |
| 68 » } | |
| 69 } | 45 } |
| 70 | 46 |
| 71 // Define the migration steps. | 47 // Define the migration steps. |
| 72 // Note: Only add to this list, once a step has landed in version control it | 48 // Note: Only add to this list, once a step has landed in version control it |
| 73 // must not be changed. | 49 // must not be changed. |
| 74 var migrationSteps = []database.MigrationStep{ | 50 var migrationSteps = []database.MigrationStep{ |
| 75 // version 1 | 51 // version 1 |
| 76 { | 52 { |
| 77 MySQLUp: []string{ | 53 MySQLUp: []string{ |
| 78 `CREATE TABLE IF NOT EXISTS shortcuts ( | 54 `CREATE TABLE IF NOT EXISTS shortcuts ( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 90 message TEXT NOT NULL | 66 message TEXT NOT NULL |
| 91 )`, | 67 )`, |
| 92 | 68 |
| 93 `CREATE TABLE IF NOT EXISTS tries ( | 69 `CREATE TABLE IF NOT EXISTS tries ( |
| 94 issue VARCHAR(255) NOT NULL PRIMARY KEY, | 70 issue VARCHAR(255) NOT NULL PRIMARY KEY, |
| 95 lastUpdated BIGINT NOT NULL, | 71 lastUpdated BIGINT NOT NULL, |
| 96 results LONGTEXT NOT NULL | 72 results LONGTEXT NOT NULL |
| 97 )`, | 73 )`, |
| 98 }, | 74 }, |
| 99 MySQLDown: []string{}, | 75 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 }, | 76 }, |
| 126 // version 2 | 77 // version 2 |
| 127 { | 78 { |
| 128 MySQLUp: []string{ | 79 MySQLUp: []string{ |
| 129 `CREATE TABLE IF NOT EXISTS activitylog ( | 80 `CREATE TABLE IF NOT EXISTS activitylog ( |
| 130 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, | 81 id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY, |
| 131 timestamp BIGINT NOT NULL, | 82 timestamp BIGINT NOT NULL, |
| 132 userid TEXT NOT NULL, | 83 userid TEXT NOT NULL, |
| 133 action TEXT NOT NULL, | 84 action TEXT NOT NULL, |
| 134 url TEXT | 85 url TEXT |
| 135 )`, | 86 )`, |
| 136 }, | 87 }, |
| 137 MySQLDown: []string{}, | 88 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 }, | 89 }, |
| 151 | 90 |
| 152 // Use this is a template for more migration steps. | 91 // Use this is a template for more migration steps. |
| 153 // version x | 92 // version x |
| 154 // { | 93 // { |
| 155 // MySQLUp: , | 94 // MySQLUp: , |
| 156 // MySQLDown: , | 95 // MySQLDown: , |
| 157 // SQLiteUp: , | |
| 158 // SQLiteDown: , | |
| 159 // }, | 96 // }, |
| 160 } | 97 } |
| OLD | NEW |