| OLD | NEW |
| 1 package main | 1 package main |
| 2 | 2 |
| 3 // Executes database migrations to the latest target version. In production this | 3 // Executes database migrations to the latest target version. In production this |
| 4 // requires the root password for MySQL. The user will be prompted for that so | 4 // requires the root password for MySQL. The user will be prompted for that so |
| 5 // it is not entered via the command line. | 5 // it is not entered via the command line. |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bufio" | 8 "bufio" |
| 9 "flag" | 9 "flag" |
| 10 "fmt" | 10 "fmt" |
| 11 "os" | 11 "os" |
| 12 "strings" | 12 "strings" |
| 13 | 13 |
| 14 "github.com/golang/glog" | 14 "github.com/golang/glog" |
| 15 "skia.googlesource.com/buildbot.git/go/buildbot" |
| 15 "skia.googlesource.com/buildbot.git/go/common" | 16 "skia.googlesource.com/buildbot.git/go/common" |
| 16 "skia.googlesource.com/buildbot.git/go/database" | 17 "skia.googlesource.com/buildbot.git/go/database" |
| 17 "skia.googlesource.com/buildbot.git/golden/go/db" | |
| 18 ) | 18 ) |
| 19 | 19 |
| 20 func main() { | 20 func main() { |
| 21 » defaultConnStr := db.GetConnectionString("root", "", "", "") | 21 » defaultConnStr := fmt.Sprintf(database.DB_CONN_TMPL, buildbot.PROD_USER,
"%s", buildbot.PROD_DB_HOST, buildbot.PROD_DATABASE) |
| 22 | 22 |
| 23 // flags | 23 // flags |
| 24 dbConnString := flag.String("db_conn_string", defaultConnStr, "\n\tDatab
ase string to open connect to the MySQL database. "+ | 24 dbConnString := flag.String("db_conn_string", defaultConnStr, "\n\tDatab
ase string to open connect to the MySQL database. "+ |
| 25 "\n\tNeeds to follow the format of the golang-mysql driver (http
s://github.com/go-sql-driver/mysql."+ | 25 "\n\tNeeds to follow the format of the golang-mysql driver (http
s://github.com/go-sql-driver/mysql."+ |
| 26 "\n\tIf the string contains %s the user will be prompted to ente
r a password which will then be used for subtitution.") | 26 "\n\tIf the string contains %s the user will be prompted to ente
r a password which will then be used for subtitution.") |
| 27 | 27 |
| 28 // Global init to initialize glog and parse arguments. | 28 // Global init to initialize glog and parse arguments. |
| 29 common.Init() | 29 common.Init() |
| 30 | 30 |
| 31 var connectionStr = *dbConnString | 31 var connectionStr = *dbConnString |
| 32 | 32 |
| 33 // if it contains formatting information read the password from stdin. | 33 // if it contains formatting information read the password from stdin. |
| 34 if strings.Contains(connectionStr, "%s") { | 34 if strings.Contains(connectionStr, "%s") { |
| 35 glog.Infof("Using connection string: %s", connectionStr) | 35 glog.Infof("Using connection string: %s", connectionStr) |
| 36 reader := bufio.NewReader(os.Stdin) | 36 reader := bufio.NewReader(os.Stdin) |
| 37 fmt.Print("Enter password for MySQL: ") | 37 fmt.Print("Enter password for MySQL: ") |
| 38 password, err := reader.ReadString('\n') | 38 password, err := reader.ReadString('\n') |
| 39 if err != nil { | 39 if err != nil { |
| 40 glog.Fatalf("Unable to read password. Error: %s", err) | 40 glog.Fatalf("Unable to read password. Error: %s", err) |
| 41 } | 41 } |
| 42 connectionStr = fmt.Sprintf(connectionStr, strings.TrimRight(pas
sword, "\n")) | 42 connectionStr = fmt.Sprintf(connectionStr, strings.TrimRight(pas
sword, "\n")) |
| 43 } | 43 } |
| 44 | 44 |
| 45 conf := &database.DatabaseConfig{ | 45 conf := &database.DatabaseConfig{ |
| 46 MySQLString: connectionStr, | 46 MySQLString: connectionStr, |
| 47 » » MigrationSteps: db.MigrationSteps(), | 47 » » MigrationSteps: buildbot.MigrationSteps(), |
| 48 } | 48 } |
| 49 vdb := database.NewVersionedDB(conf) | 49 vdb := database.NewVersionedDB(conf) |
| 50 | 50 |
| 51 // Get the current database version | 51 // Get the current database version |
| 52 maxDBVersion := vdb.MaxDBVersion() | 52 maxDBVersion := vdb.MaxDBVersion() |
| 53 glog.Infof("Latest database version: %d", maxDBVersion) | 53 glog.Infof("Latest database version: %d", maxDBVersion) |
| 54 | 54 |
| 55 dbVersion, err := vdb.DBVersion() | 55 dbVersion, err := vdb.DBVersion() |
| 56 if err != nil { | 56 if err != nil { |
| 57 glog.Fatalf("Unable to retrieve database version. Error: %s", er
r) | 57 glog.Fatalf("Unable to retrieve database version. Error: %s", er
r) |
| 58 } | 58 } |
| 59 glog.Infof("Current database version: %d", dbVersion) | 59 glog.Infof("Current database version: %d", dbVersion) |
| 60 | 60 |
| 61 if dbVersion < maxDBVersion { | 61 if dbVersion < maxDBVersion { |
| 62 glog.Infof("Migrating to version: %d", maxDBVersion) | 62 glog.Infof("Migrating to version: %d", maxDBVersion) |
| 63 err = vdb.Migrate(maxDBVersion) | 63 err = vdb.Migrate(maxDBVersion) |
| 64 if err != nil { | 64 if err != nil { |
| 65 glog.Fatalf("Unable to retrieve database version. Error:
%s", err) | 65 glog.Fatalf("Unable to retrieve database version. Error:
%s", err) |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 glog.Infoln("Database migration finished.") | 69 glog.Infoln("Database migration finished.") |
| 70 } | 70 } |
| OLD | NEW |