2016-03-24 14:11:56 +00:00
/ *
Copyright 2016 GitHub Inc .
See https : //github.com/github/gh-osc/blob/master/LICENSE
* /
2016-03-21 14:57:01 +00:00
package main
import (
"flag"
"fmt"
"os"
2016-04-04 10:27:51 +00:00
"github.com/github/gh-osc/go/base"
"github.com/github/gh-osc/go/logic"
2016-03-21 14:57:01 +00:00
"github.com/outbrain/golib/log"
)
// main is the application's entry point. It will either spawn a CLI or HTTP itnerfaces.
func main ( ) {
2016-04-04 10:27:51 +00:00
migrationContext := base . GetMigrationContext ( )
2016-03-30 13:43:40 +00:00
// mysqlBasedir := flag.String("mysql-basedir", "", "the --basedir config for MySQL (auto-detected if not given)")
// mysqlDatadir := flag.String("mysql-datadir", "", "the --datadir config for MySQL (auto-detected if not given)")
2016-03-23 11:40:17 +00:00
internalExperiment := flag . Bool ( "internal-experiment" , false , "issue an internal experiment" )
binlogFile := flag . String ( "binlog-file" , "" , "Name of binary log file" )
2016-03-30 13:43:40 +00:00
2016-04-04 13:29:02 +00:00
flag . StringVar ( & migrationContext . InspectorConnectionConfig . Key . Hostname , "host" , "127.0.0.1" , "MySQL hostname (preferably a replica, not the master)" )
flag . IntVar ( & migrationContext . InspectorConnectionConfig . Key . Port , "port" , 3306 , "MySQL port (preferably a replica, not the master)" )
flag . StringVar ( & migrationContext . InspectorConnectionConfig . User , "user" , "root" , "MySQL user" )
flag . StringVar ( & migrationContext . InspectorConnectionConfig . Password , "password" , "" , "MySQL password" )
2016-03-30 13:43:40 +00:00
2016-04-04 10:27:51 +00:00
flag . StringVar ( & migrationContext . DatabaseName , "database" , "" , "database name (mandatory)" )
flag . StringVar ( & migrationContext . OriginalTableName , "table" , "" , "table name (mandatory)" )
flag . StringVar ( & migrationContext . AlterStatement , "alter" , "" , "alter statement (mandatory)" )
flag . BoolVar ( & migrationContext . CountTableRows , "exact-rowcount" , false , "actually count table rows as opposed to estimate them (results in more accurate progress estimation)" )
2016-04-04 13:29:02 +00:00
flag . BoolVar ( & migrationContext . AllowedRunningOnMaster , "allow-on-master" , false , "allow this migration to run directly on master. Preferably it would run on a replica" )
2016-04-04 10:27:51 +00:00
2016-04-04 16:19:46 +00:00
flag . IntVar ( & migrationContext . ChunkSize , "chunk-size" , 1000 , "amount of rows to handle in each iteration" )
2016-03-21 14:57:01 +00:00
quiet := flag . Bool ( "quiet" , false , "quiet" )
verbose := flag . Bool ( "verbose" , false , "verbose" )
debug := flag . Bool ( "debug" , false , "debug mode (very verbose)" )
stack := flag . Bool ( "stack" , false , "add stack trace upon error" )
help := flag . Bool ( "help" , false , "Display usage" )
flag . Parse ( )
if * help {
fmt . Fprintf ( os . Stderr , "Usage of gh-osc:\n" )
flag . PrintDefaults ( )
return
}
log . SetLevel ( log . ERROR )
if * verbose {
log . SetLevel ( log . INFO )
}
if * debug {
log . SetLevel ( log . DEBUG )
}
if * stack {
log . SetPrintStackTrace ( * stack )
}
if * quiet {
// Override!!
log . SetLevel ( log . ERROR )
}
2016-04-04 10:27:51 +00:00
if migrationContext . DatabaseName == "" {
log . Fatalf ( "--database must be provided and database name must not be empty" )
}
if migrationContext . OriginalTableName == "" {
log . Fatalf ( "--table must be provided and table name must not be empty" )
}
if migrationContext . AlterStatement == "" {
log . Fatalf ( "--alter must be provided and statement must not be empty" )
}
2016-03-21 14:57:01 +00:00
log . Info ( "starting gh-osc" )
2016-03-23 11:40:17 +00:00
if * internalExperiment {
2016-04-07 13:57:12 +00:00
log . Debug ( "starting experiment with %+v" , * binlogFile )
2016-03-30 13:43:40 +00:00
//binlogReader = binlog.NewMySQLBinlogReader(*mysqlBasedir, *mysqlDatadir)
2016-04-07 13:57:12 +00:00
// binlogReader, err := binlog.NewGoMySQLReader(migrationContext.InspectorConnectionConfig)
// if err != nil {
// log.Fatale(err)
// }
// if err := binlogReader.ConnectBinlogStreamer(mysql.BinlogCoordinates{LogFile: *binlogFile, LogPos: 0}); err != nil {
// log.Fatale(err)
// }
// binlogReader.StreamEvents(func() bool { return false })
// return
2016-04-04 10:27:51 +00:00
}
2016-04-04 13:29:02 +00:00
migrator := logic . NewMigrator ( )
2016-04-04 10:27:51 +00:00
err := migrator . Migrate ( )
if err != nil {
log . Fatale ( err )
2016-03-23 11:40:17 +00:00
}
2016-04-04 10:27:51 +00:00
log . Info ( "Done" )
2016-03-21 14:57:01 +00:00
}