OLD | NEW |
(Empty) | |
| 1 package main |
| 2 |
| 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 |
| 5 // it is not entered via the command line. |
| 6 |
| 7 import ( |
| 8 "bufio" |
| 9 "flag" |
| 10 "fmt" |
| 11 "os" |
| 12 "strings" |
| 13 |
| 14 "github.com/golang/glog" |
| 15 "skia.googlesource.com/buildbot.git/perf/go/db" |
| 16 ) |
| 17 |
| 18 // flags |
| 19 var ( |
| 20 dbConnString = flag.String("db_conn_string", "root:%s@tcp(173.194.104.24
:3306)/skia?parseTime=true", "\n\tDatabase string to open connect to the MySQL d
atabase. "+ |
| 21 "\n\tNeeds to follow the format of the golang-mysql driver (http
s://github.com/go-sql-driver/mysql."+ |
| 22 "\n\tIf the string contains %s the user will be prompted to ente
r a password which will then be used for subtitution.") |
| 23 ) |
| 24 |
| 25 func main() { |
| 26 flag.Parse() |
| 27 |
| 28 var connectionStr = *dbConnString |
| 29 |
| 30 // if it contains formatting information read the password from stdin. |
| 31 if strings.Contains(connectionStr, "%s") { |
| 32 glog.Infof("Using connection string: %s", connectionStr) |
| 33 reader := bufio.NewReader(os.Stdin) |
| 34 fmt.Print("Enter password for MySQL: ") |
| 35 password, err := reader.ReadString('\n') |
| 36 if err != nil { |
| 37 glog.Fatalf("Unable to read password. Error: %s", err.Er
ror()) |
| 38 } |
| 39 connectionStr = fmt.Sprintf(connectionStr, strings.TrimRight(pas
sword, "\n")) |
| 40 } |
| 41 |
| 42 // Initialize the database. |
| 43 db.Init(connectionStr) |
| 44 |
| 45 // Get the current database version |
| 46 maxDBVersion := db.MaxDBVersion() |
| 47 glog.Infof("Latest database version: %d", maxDBVersion) |
| 48 |
| 49 dbVersion, err := db.DBVersion() |
| 50 if err != nil { |
| 51 glog.Fatalf("Unable to retrieve database version. Error: %s", er
r.Error()) |
| 52 } |
| 53 glog.Infof("Current database version: %d", dbVersion) |
| 54 |
| 55 if dbVersion < maxDBVersion { |
| 56 glog.Infof("Migrating to version: %d", maxDBVersion) |
| 57 err = db.Migrate(maxDBVersion) |
| 58 if err != nil { |
| 59 glog.Fatalf("Unable to retrieve database version. Error:
%s", err.Error()) |
| 60 } |
| 61 } |
| 62 |
| 63 glog.Infoln("Database migration finished.") |
| 64 } |
OLD | NEW |