Permalink
Browse files

Update dep github.com/skeema/tengo, and update affected callsites

  • Loading branch information...
evanelias committed May 31, 2018
1 parent c12f6b8 commit b9b4e1c8c96a0967bb37d943e1fcfbbe1164ae35

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,36 @@
package main
import (
"fmt"
"sync"
"github.com/skeema/tengo"
)
var instanceCache struct {
sync.Mutex
instanceMap map[string]*tengo.Instance
}
func init() {
instanceCache.instanceMap = make(map[string]*tengo.Instance)
}
// NewInstance wraps tengo.NewInstance such that two identical requests will
// return the same *tengo.Instance. This helps reduce excessive creation of
// redundant connections.
func NewInstance(driver, dsn string) (*tengo.Instance, error) {
key := fmt.Sprintf("%s:%s", driver, dsn)
instanceCache.Lock()
defer instanceCache.Unlock()
instance, already := instanceCache.instanceMap[key]
if already {
return instance, nil
}
instance, err := tengo.NewInstance(driver, dsn)
if err != nil {
return nil, err
}
instanceCache.instanceMap[key] = instance
return instance, nil
}
@@ -96,7 +96,9 @@ func InitHandler(cfg *mybase.Config) error {
// Build list of schemas
var schemas []*tengo.Schema
if onlySchema != "" {
if !inst.HasSchema(onlySchema) {
if has, err := inst.HasSchema(onlySchema); err != nil {
return err
} else if !has {
return NewExitValue(CodeBadConfig, "Schema %s does not exist on instance %s", onlySchema, inst)
}
s, err := inst.Schema(onlySchema)
@@ -112,6 +114,12 @@ func InitHandler(cfg *mybase.Config) error {
}
}
// Look up server charset and collation, so that we know which schemas override
instCharSet, instCollation, err := inst.DefaultCharSetAndCollation()
if err != nil {
return err
}
// Figure out what needs to go in the hostDir's .skeema file.
hostOptionFile := mybase.NewFile(hostDir.Path, ".skeema")
hostOptionFile.SetOptionValue(environment, "host", inst.Host)
@@ -133,13 +141,11 @@ func InitHandler(cfg *mybase.Config) error {
// schema name is placed outside of any named section/environment since the
// default assumption is that schema names match between environments
hostOptionFile.SetOptionValue("", "schema", onlySchema)
if overridesCharSet, overridesCollation, err := schemas[0].OverridesServerCharSet(); err == nil {
if overridesCharSet {
hostOptionFile.SetOptionValue("", "default-character-set", schemas[0].CharSet)
}
if overridesCollation {
hostOptionFile.SetOptionValue("", "default-collation", schemas[0].Collation)
}
if instCharSet != schemas[0].CharSet {
hostOptionFile.SetOptionValue("", "default-character-set", schemas[0].CharSet)
}
if instCollation != schemas[0].Collation {
hostOptionFile.SetOptionValue("", "default-collation", schemas[0].Collation)
}
}
@@ -160,7 +166,7 @@ func InitHandler(cfg *mybase.Config) error {
// Iterate over the schemas. For each one, create a dir with .skeema and *.sql files
for _, s := range schemas {
if err := PopulateSchemaDir(s, hostDir, separateSchemaSubdir); err != nil {
if err := PopulateSchemaDir(s, hostDir, separateSchemaSubdir, instCharSet != s.CharSet, instCollation != s.Collation); err != nil {
return err
}
}
@@ -174,7 +180,7 @@ func InitHandler(cfg *mybase.Config) error {
// *.sql files will be put in parentDir, and it will be the caller's
// responsibility to ensure its .skeema option file exists and maps to the
// correct schema name.
func PopulateSchemaDir(s *tengo.Schema, parentDir *Dir, makeSubdir bool) error {
func PopulateSchemaDir(s *tengo.Schema, parentDir *Dir, makeSubdir, includeCharSet, includeCollation bool) error {
// Ignore any attempt to populate a dir for the temp schema
if s.Name == parentDir.Config.Get("temp-schema") {
return nil
@@ -195,13 +201,11 @@ func PopulateSchemaDir(s *tengo.Schema, parentDir *Dir, makeSubdir bool) error {
// names match between environments.
optionFile := mybase.NewFile(".skeema")
optionFile.SetOptionValue("", "schema", s.Name)
if overridesCharSet, overridesCollation, err := s.OverridesServerCharSet(); err == nil {
if overridesCharSet {
optionFile.SetOptionValue("", "default-character-set", s.CharSet)
}
if overridesCollation {
optionFile.SetOptionValue("", "default-collation", s.Collation)
}
if includeCharSet {
optionFile.SetOptionValue("", "default-character-set", s.CharSet)
}
if includeCollation {
optionFile.SetOptionValue("", "default-collation", s.Collation)
}
if schemaDir, err = parentDir.CreateSubdir(s.Name, optionFile); err != nil {
return NewExitValue(CodeCantCreate, "Unable to use directory %s for schema %s: %s", path.Join(parentDir.Path, s.Name), s.Name, err)
@@ -216,20 +220,16 @@ func PopulateSchemaDir(s *tengo.Schema, parentDir *Dir, makeSubdir bool) error {
}
log.Infof("Populating %s", schemaDir.Path)
tables, err := s.Tables()
if err != nil {
return fmt.Errorf("Cannot obtain table information for %s: %s", s.Name, err)
}
ignoreTable, err := parentDir.Config.GetRegexp("ignore-table")
if err != nil {
return err
}
for _, t := range tables {
for _, t := range s.Tables {
if ignoreTable != nil && ignoreTable.MatchString(t.Name) {
log.Warnf("Skipping table %s because ignore-table matched %s", t.Name, ignoreTable)
continue
}
createStmt := t.CreateStatement()
createStmt := t.CreateStatement
// Special handling for auto-increment tables: strip next-auto-inc value,
// unless user specifically wants to keep it in .sql file
@@ -68,8 +68,7 @@ func LintHandler(cfg *mybase.Config) error {
if err != nil {
return err
}
tables, _ := t.SchemaFromDir.Tables() // can ignore error since table list already guaranteed to be cached
for _, table := range tables {
for _, table := range t.SchemaFromDir.Tables {
if ignoreTable != nil && ignoreTable.MatchString(table.Name) {
log.Warnf("Skipping table %s because ignore-table='%s'", table.Name, ignoreTable)
continue
@@ -84,8 +83,8 @@ func LintHandler(cfg *mybase.Config) error {
for _, warning := range sf.Warnings {
log.Debug(warning)
}
if table.CreateStatement() != sf.Contents {
sf.Contents = table.CreateStatement()
if table.CreateStatement != sf.Contents {
sf.Contents = table.CreateStatement
var length int
if length, err = sf.Write(); err != nil {
return fmt.Errorf("Unable to write to %s: %s", sf.Path(), err)
@@ -59,39 +59,36 @@ func PullHandler(cfg *mybase.Config) error {
continue
}
diff, err := tengo.NewSchemaDiff(t.SchemaFromDir, t.SchemaFromInstance)
if err != nil {
return err
}
diff := tengo.NewSchemaDiff(t.SchemaFromDir, t.SchemaFromInstance)
// Handle changes in schema's default character set and/or collation by
// persisting changes to the dir's option file. Errors here are just surfaced
// as warnings.
// persisting changes to the dir's option file. File operation errors here
// are just surfaced as warnings.
if diff.SchemaDDL != "" {
instCharSet, instCollation, err := t.Instance.DefaultCharSetAndCollation()
if err != nil {
return err
}
optionFile, err := t.Dir.OptionFile()
if err != nil {
log.Warnf("Unable to update character set and/or collation for %s/.skeema: %s", t.Dir, err)
} else if optionFile == nil {
log.Warnf("Unable to update character set and/or collation for %s/.skeema: cannot read file", t.Dir)
} else {
if overridesCharSet, overridesCollation, err := t.SchemaFromInstance.OverridesServerCharSet(); err == nil {
if overridesCharSet {
optionFile.SetOptionValue("", "default-character-set", t.SchemaFromInstance.CharSet)
} else {
optionFile.UnsetOptionValue("", "default-character-set")
}
if overridesCollation {
optionFile.SetOptionValue("", "default-collation", t.SchemaFromInstance.Collation)
} else {
optionFile.UnsetOptionValue("", "default-collation")
}
if err = optionFile.Write(true); err != nil {
log.Warnf("Unable to update character set and/or collation for %s: %s", optionFile.Path(), err)
} else {
log.Infof("Wrote %s -- updated schema-level default-character-set and default-collation", optionFile.Path())
}
if instCharSet != t.SchemaFromInstance.CharSet {
optionFile.SetOptionValue("", "default-character-set", t.SchemaFromInstance.CharSet)
} else {
optionFile.UnsetOptionValue("", "default-character-set")
}
if instCollation != t.SchemaFromInstance.Collation {
optionFile.SetOptionValue("", "default-collation", t.SchemaFromInstance.Collation)
} else {
optionFile.UnsetOptionValue("", "default-collation")
}
if err = optionFile.Write(true); err != nil {
log.Warnf("Unable to update character set and/or collation for %s: %s", optionFile.Path(), err)
} else {
log.Infof("Wrote %s -- updated schema-level default-character-set and default-collation", optionFile.Path())
}
}
}
@@ -165,7 +162,7 @@ func PullHandler(cfg *mybase.Config) error {
continue
}
table := td.Table
createStmt, err := t.Instance.ShowCreateTable(t.SchemaFromInstance, table)
createStmt, err := t.Instance.ShowCreateTable(t.SchemaFromInstance.Name, table.Name)
if err != nil {
return err
}
@@ -190,7 +187,7 @@ func PullHandler(cfg *mybase.Config) error {
// updated. Handle same as AlterTable case, since created/dropped tables don't
// ever end up in UnsupportedTables since they don't do a diff operation.
for _, table := range diff.UnsupportedTables {
createStmt := table.CreateStatement()
createStmt := table.CreateStatement
if table.HasAutoIncrement() && !t.Dir.Config.GetBool("include-auto-inc") {
createStmt, _ = tengo.ParseCreateAutoInc(createStmt)
}
@@ -222,8 +219,8 @@ func PullHandler(cfg *mybase.Config) error {
for _, warning := range sf.Warnings {
log.Debug(warning)
}
if table.CreateStatement() != sf.Contents {
sf.Contents = table.CreateStatement()
if table.CreateStatement != sf.Contents {
sf.Contents = table.CreateStatement
var length int
if length, err = sf.Write(); err != nil {
return fmt.Errorf("Unable to write to %s: %s", sf.Path(), err)
@@ -261,6 +258,11 @@ func findNewSchemas(dir *Dir) error {
if err != nil {
return err
}
instCharSet, instCollation, err := instance.DefaultCharSetAndCollation()
if err != nil {
return err
}
subdirHasSchema := make(map[string]bool)
for _, subdir := range subdirs {
// We only want to evaluate subdirs that explicitly define the schema option
@@ -299,7 +301,7 @@ func findNewSchemas(dir *Dir) error {
for _, s := range schemas {
if !subdirHasSchema[s.Name] {
// use same logic from init command
if err := PopulateSchemaDir(s, dir, true); err != nil {
if err := PopulateSchemaDir(s, dir, true, instCharSet != s.CharSet, instCollation != s.Collation); err != nil {
return err
}
}
@@ -155,12 +155,9 @@ func pushWorker(sps *sharedPushState) {
log.Debug(warning)
}
diff, err := tengo.NewSchemaDiff(t.SchemaFromInstance, t.SchemaFromDir)
if err != nil {
sps.setFatalError(err)
return
}
diff := tengo.NewSchemaDiff(t.SchemaFromInstance, t.SchemaFromDir)
var targetStmtCount int
var err error
if diff.SchemaDDL != "" {
sps.syncPrintf(t.Instance, "", "%s;\n", diff.SchemaDDL)
@@ -173,7 +170,7 @@ func pushWorker(sps *sharedPushState) {
return
}
} else if strings.HasPrefix(diff.SchemaDDL, "ALTER DATABASE") {
err = t.Instance.AlterSchema(t.SchemaFromInstance, t.SchemaFromDir.CharSet, t.SchemaFromDir.Collation)
err := t.Instance.AlterSchema(t.SchemaFromInstance.Name, t.SchemaFromDir.CharSet, t.SchemaFromDir.Collation)
if err != nil {
sps.setFatalError(fmt.Errorf("Unable to alter defaults for schema %s on %s: %s", t.SchemaFromInstance.Name, t.Instance, err))
return
@@ -205,9 +205,9 @@ func (ddl *DDLStatement) setErr(err error) {
// the target. If the table has no rows, this method always returns a size of 0,
// even though information_schema normally indicates at least 16kb in this case.
func (ddl *DDLStatement) getTableSize(target *Target, table *tengo.Table) (int64, error) {
hasRows, err := target.Instance.TableHasRows(target.SchemaFromInstance, table)
hasRows, err := target.Instance.TableHasRows(target.SchemaFromInstance.Name, table.Name)
if !hasRows || err != nil {
return 0, err
}
return target.Instance.TableSize(target.SchemaFromInstance, table)
return target.Instance.TableSize(target.SchemaFromInstance.Name, table.Name)
}
@@ -39,10 +39,7 @@ func (s *SkeemaIntegrationSuite) TestNewDDLStatement(t *testing.T) {
break
}
}
sd, err := tengo.NewSchemaDiff(target.SchemaFromInstance, target.SchemaFromDir)
if err != nil {
t.Fatalf("Unexpected error from tengo.NewSchemaDiff: %s", err)
}
sd := tengo.NewSchemaDiff(target.SchemaFromInstance, target.SchemaFromDir)
if len(sd.TableDiffs) != 4 {
// modifications in ddlstatement.sql should have yielded 4 diffs: one drop
// table, one create table, and two alter tables (one to a table with rows
Oops, something went wrong.

0 comments on commit b9b4e1c

Please sign in to comment.