Skip to content

Commit

Permalink
Performance improvements after refactors in previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evanelias committed Jun 1, 2018
1 parent b9b4e1c commit 32618e4
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 9 additions & 17 deletions cmd_init.go
Expand Up @@ -94,24 +94,16 @@ func InitHandler(cfg *mybase.Config) error {
} }


// Build list of schemas // Build list of schemas
var schemas []*tengo.Schema schemaNameFilter := []string{}
if onlySchema != "" { if onlySchema != "" {
if has, err := inst.HasSchema(onlySchema); err != nil { schemaNameFilter = []string{onlySchema}
return err }
} else if !has { schemas, err := inst.Schemas(schemaNameFilter...)
return NewExitValue(CodeBadConfig, "Schema %s does not exist on instance %s", onlySchema, inst) if err != nil {
} return NewExitValue(CodeFatalError, "Cannot examine schemas on %s: %s", inst, err)
s, err := inst.Schema(onlySchema) }
if err != nil { if onlySchema != "" && len(schemas) == 0 {
return NewExitValue(CodeFatalError, "Cannot examine schema %s: %s", onlySchema, err) return NewExitValue(CodeBadConfig, "Schema %s does not exist on instance %s", onlySchema, inst)
}
schemas = []*tengo.Schema{s}
} else {
var err error
schemas, err = inst.Schemas()
if err != nil {
return NewExitValue(CodeFatalError, "Cannot examine schemas on %s: %s", inst, err)
}
} }


// Look up server charset and collation, so that we know which schemas override // Look up server charset and collation, so that we know which schemas override
Expand Down
10 changes: 7 additions & 3 deletions cmd_pull.go
Expand Up @@ -294,12 +294,16 @@ func findNewSchemas(dir *Dir) error {
} else if inst == nil { } else if inst == nil {
return fmt.Errorf("Unable to obtain instance for %s", dir) return fmt.Errorf("Unable to obtain instance for %s", dir)
} }
schemas, err := inst.Schemas() schemaNames, err := inst.SchemaNames()
if err != nil { if err != nil {
return err return err
} }
for _, s := range schemas { for _, name := range schemaNames {
if !subdirHasSchema[s.Name] { if !subdirHasSchema[name] {
s, err := inst.Schema(name)
if err != nil {
return err
}
// use same logic from init command // use same logic from init command
if err := PopulateSchemaDir(s, dir, true, instCharSet != s.CharSet, instCollation != s.Collation); err != nil { if err := PopulateSchemaDir(s, dir, true, instCharSet != s.CharSet, instCollation != s.Collation); err != nil {
return err return err
Expand Down
2 changes: 1 addition & 1 deletion cmd_push.go
Expand Up @@ -164,7 +164,7 @@ func pushWorker(sps *sharedPushState) {
targetStmtCount++ targetStmtCount++
if !sps.dryRun { if !sps.dryRun {
if strings.HasPrefix(diff.SchemaDDL, "CREATE DATABASE") && t.SchemaFromInstance == nil { if strings.HasPrefix(diff.SchemaDDL, "CREATE DATABASE") && t.SchemaFromInstance == nil {
t.SchemaFromInstance, err = t.Instance.CreateSchema(schemaName, t.SchemaFromDir.CharSet, t.SchemaFromDir.Collation) _, err = t.Instance.CreateSchema(schemaName, t.SchemaFromDir.CharSet, t.SchemaFromDir.Collation)
if err != nil { if err != nil {
sps.setFatalError(fmt.Errorf("Error creating schema %s on %s: %s", schemaName, t.Instance, err)) sps.setFatalError(fmt.Errorf("Error creating schema %s on %s: %s", schemaName, t.Instance, err))
return return
Expand Down
6 changes: 1 addition & 5 deletions dir.go
Expand Up @@ -274,14 +274,10 @@ func (dir *Dir) SchemaNames(instance *tengo.Instance) ([]string, error) {


if schemaValue == "*" { if schemaValue == "*" {
// This automatically already filters out information_schema, performance_schema, sys, test, mysql // This automatically already filters out information_schema, performance_schema, sys, test, mysql
schemasByName, err := instance.SchemasByName() schemaNames, err := instance.SchemaNames()
if err != nil { if err != nil {
return nil, err return nil, err
} }
schemaNames := make([]string, 0, len(schemasByName))
for name := range schemasByName {
schemaNames = append(schemaNames, name)
}
// Schema name list must be sorted so that generateTargetsForDir with // Schema name list must be sorted so that generateTargetsForDir with
// firstOnly==true consistently grabs the alphabetically first schema // firstOnly==true consistently grabs the alphabetically first schema
sort.Strings(schemaNames) sort.Strings(schemaNames)
Expand Down
7 changes: 3 additions & 4 deletions skeema_test.go
Expand Up @@ -290,19 +290,18 @@ func (s *SkeemaIntegrationSuite) objectExists(schemaName, tableName, columnName
} }
if tableName == "" && columnName == "" { if tableName == "" && columnName == "" {
phrase = fmt.Sprintf("schema %s", schemaName) phrase = fmt.Sprintf("schema %s", schemaName)
has, err := s.d.HasSchema(schemaName)
return has, phrase, err
} else if columnName == "" { } else if columnName == "" {
phrase = fmt.Sprintf("table %s.%s", schemaName, tableName) phrase = fmt.Sprintf("table %s.%s", schemaName, tableName)
} else { } else {
phrase = fmt.Sprintf("column %s.%s.%s", schemaName, tableName, columnName) phrase = fmt.Sprintf("column %s.%s.%s", schemaName, tableName, columnName)
} }


schema, err := s.d.Schema(schemaName) schema, err := s.d.Schema(schemaName)
if tableName == "" && columnName == "" { if err != nil {
return schema != nil, phrase, err
} else if err != nil {
return false, phrase, fmt.Errorf("Unable to obtain %s: %s", phrase, err) return false, phrase, fmt.Errorf("Unable to obtain %s: %s", phrase, err)
} }

table := schema.Table(tableName) table := schema.Table(tableName)
if columnName == "" { if columnName == "" {
return table != nil, phrase, err return table != nil, phrase, err
Expand Down
15 changes: 11 additions & 4 deletions target.go
Expand Up @@ -146,14 +146,15 @@ func generateTargetsForDir(dir *Dir, targetsByInstance TargetGroupMap, firstOnly
targetsByInstance.AddInstanceError(inst, dir, err) targetsByInstance.AddInstanceError(inst, dir, err)
continue continue
} }
schemasByName, err := inst.SchemasByName() if len(schemaNames) > 1 && firstOnly {
schemaNames = schemaNames[0:1]
}

schemasByName, err := inst.SchemasByName(schemaNames...)
if err != nil { if err != nil {
targetsByInstance.AddInstanceError(inst, dir, err) targetsByInstance.AddInstanceError(inst, dir, err)
continue continue
} }
if len(schemaNames) > 1 && firstOnly {
schemaNames = schemaNames[0:1]
}
for _, schemaName := range schemaNames { for _, schemaName := range schemaNames {
// Copy the template into a new Target. Using inst, set its Instance and // Copy the template into a new Target. Using inst, set its Instance and
// SchemaFromInstance accordingly. Set its SchemaFromDir to a copy of the // SchemaFromInstance accordingly. Set its SchemaFromDir to a copy of the
Expand Down Expand Up @@ -213,6 +214,12 @@ func generateTargetsForDir(dir *Dir, targetsByInstance TargetGroupMap, firstOnly
// bring a table from the version in SchemaFromInstance to the version in // bring a table from the version in SchemaFromInstance to the version in
// SchemaFromDir. // SchemaFromDir.
func (t *Target) verifyDiff(diff *tengo.SchemaDiff) (err error) { func (t *Target) verifyDiff(diff *tengo.SchemaDiff) (err error) {
// If the schema is being newly created on the instance, we know there are
// no alters and therefore nothing to verify
if t.SchemaFromInstance == nil {
return nil
}

// Populate the temp schema with a copy of the tables from SchemaFromInstance, // Populate the temp schema with a copy of the tables from SchemaFromInstance,
// the "before" state of the tables // the "before" state of the tables
tempSchemaName := t.Dir.Config.Get("temp-schema") tempSchemaName := t.Dir.Config.Get("temp-schema")
Expand Down
93 changes: 54 additions & 39 deletions vendor/github.com/skeema/tengo/instance.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 32618e4

Please sign in to comment.