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