2016-04-04 10:27:51 +00:00
/ *
Copyright 2016 GitHub Inc .
See https : //github.com/github/gh-osc/blob/master/LICENSE
* /
package logic
import (
2016-04-04 13:29:02 +00:00
"fmt"
"github.com/github/gh-osc/go/base"
"github.com/outbrain/golib/log"
2016-04-04 10:27:51 +00:00
)
// Migrator is the main schema migration flow manager.
type Migrator struct {
inspector * Inspector
2016-04-04 13:29:02 +00:00
applier * Applier
migrationContext * base . MigrationContext
2016-04-04 10:27:51 +00:00
}
2016-04-04 13:29:02 +00:00
func NewMigrator ( ) * Migrator {
2016-04-04 10:27:51 +00:00
return & Migrator {
2016-04-04 13:29:02 +00:00
migrationContext : base . GetMigrationContext ( ) ,
2016-04-04 10:27:51 +00:00
}
}
2016-04-04 13:29:02 +00:00
func ( this * Migrator ) Migrate ( ) ( err error ) {
this . inspector = NewInspector ( )
2016-04-04 10:27:51 +00:00
if err := this . inspector . InitDBConnections ( ) ; err != nil {
return err
}
2016-04-04 13:29:02 +00:00
if this . migrationContext . MasterConnectionConfig , err = this . inspector . getMasterConnectionConfig ( ) ; err != nil {
return err
}
if this . migrationContext . IsRunningOnMaster ( ) && ! this . migrationContext . AllowedRunningOnMaster {
return fmt . Errorf ( "It seems like this migration attempt to run directly on master. Preferably it would be executed on a replica (and this reduces load from the master). To proceed please provide --allow-on-master" )
}
log . Infof ( "Master found to be %+v" , this . migrationContext . MasterConnectionConfig . Key )
2016-04-04 16:19:46 +00:00
uniqueKeys , err := this . inspector . InspectTables ( )
if err != nil {
2016-04-04 10:27:51 +00:00
return err
}
2016-04-04 13:29:02 +00:00
this . applier = NewApplier ( )
if err := this . applier . InitDBConnections ( ) ; err != nil {
return err
}
2016-04-04 16:19:46 +00:00
// if err := this.applier.CreateGhostTable(); err != nil {
// log.Errorf("Unable to create ghost table, see further error details. Perhaps a previous migration failed without dropping the table? Bailing out")
// return err
// }
// if err := this.applier.AlterGhost(); err != nil {
// log.Errorf("Unable to ALTER ghost table, see further error details. Bailing out")
// return err
// }
if err := this . applier . ReadMigrationRangeValues ( uniqueKeys [ 0 ] ) ; err != nil {
2016-04-04 13:29:02 +00:00
return err
}
2016-04-04 16:19:46 +00:00
if err := this . applier . IterateTable ( uniqueKeys [ 0 ] ) ; err != nil {
2016-04-04 13:29:02 +00:00
return err
}
2016-04-04 10:27:51 +00:00
return nil
}