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