Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: golden/go/correctness_migratedb/main.go

Issue 813443002: Overhaul database package (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/golden/go/db" 13 "skia.googlesource.com/buildbot.git/golden/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 » defaultConnStr := db.GetConnectionString("root", "", "", "") 21 » // Set up flags.
22 22 » database.SetupFlags(db.PROD_DB_HOST, db.PROD_DB_PORT, database.USER_ROOT , db.PROD_DB_NAME)
23 » // flags
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."+
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 23
28 // Global init to initialize glog and parse arguments. 24 // Global init to initialize glog and parse arguments.
29 common.Init() 25 common.Init()
30 26
31 » var connectionStr = *dbConnString 27 » conf, err := database.ConfigFromFlagsAndMetadata(*local, db.MigrationSte ps())
32 28 » if err != nil {
33 » // if it contains formatting information read the password from stdin. 29 » » glog.Fatal(err)
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 } 30 }
49 vdb := database.NewVersionedDB(conf) 31 vdb := database.NewVersionedDB(conf)
50 32
51 // Get the current database version 33 // Get the current database version
52 maxDBVersion := vdb.MaxDBVersion() 34 maxDBVersion := vdb.MaxDBVersion()
53 glog.Infof("Latest database version: %d", maxDBVersion) 35 glog.Infof("Latest database version: %d", maxDBVersion)
54 36
55 dbVersion, err := vdb.DBVersion() 37 dbVersion, err := vdb.DBVersion()
56 if err != nil { 38 if err != nil {
57 glog.Fatalf("Unable to retrieve database version. Error: %s", er r) 39 glog.Fatalf("Unable to retrieve database version. Error: %s", er r)
58 } 40 }
59 glog.Infof("Current database version: %d", dbVersion) 41 glog.Infof("Current database version: %d", dbVersion)
60 42
61 if dbVersion < maxDBVersion { 43 if dbVersion < maxDBVersion {
62 glog.Infof("Migrating to version: %d", maxDBVersion) 44 glog.Infof("Migrating to version: %d", maxDBVersion)
63 err = vdb.Migrate(maxDBVersion) 45 err = vdb.Migrate(maxDBVersion)
64 if err != nil { 46 if err != nil {
65 glog.Fatalf("Unable to retrieve database version. Error: %s", err) 47 glog.Fatalf("Unable to retrieve database version. Error: %s", err)
66 } 48 }
67 } 49 }
68 50
69 glog.Infoln("Database migration finished.") 51 glog.Infoln("Database migration finished.")
70 } 52 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698