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