Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit Precision of Time values written to Migrations table #256

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
Expand All @@ -42,6 +48,7 @@ require (
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.15.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
25 changes: 23 additions & 2 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type MigrationSet struct {
IgnoreUnknown bool
// DisableCreateTable disable the creation of the migration table
DisableCreateTable bool
// Limits precision of time values inserted into the database to 6 milliseconds
LimitTimePrecision bool
}

var migSet = MigrationSet{}
Expand Down Expand Up @@ -111,6 +113,11 @@ func SetSchema(name string) {
}
}

// Resets schema to empty
func ResetSchema() {
migSet.SchemaName = ""
}

// SetDisableCreateTable sets the boolean to disable the creation of the migration table
func SetDisableCreateTable(disable bool) {
migSet.DisableCreateTable = disable
Expand All @@ -124,6 +131,12 @@ func SetIgnoreUnknown(v bool) {
migSet.IgnoreUnknown = v
}

// LimitTimePrecision limits the precision of time values inserted into the database
// to be no greater than 6 milliseconds
func LimitTimePrecision(v bool) {
migSet.LimitTimePrecision = v
}

type Migration struct {
Id string
Up []string
Expand Down Expand Up @@ -563,9 +576,13 @@ func (MigrationSet) applyMigrations(ctx context.Context, dir MigrationDirection,

switch dir {
case Up:
appliedAt := time.Now()
if migSet.LimitTimePrecision {
appliedAt = appliedAt.Round(6 * time.Millisecond)
}
err = executor.Insert(&MigrationRecord{
Id: migration.Id,
AppliedAt: time.Now(),
AppliedAt: appliedAt,
})
if err != nil {
if trans, ok := executor.(*gorp.Transaction); ok {
Expand Down Expand Up @@ -743,9 +760,13 @@ func SkipMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirecti
}
}

appliedAt := time.Now()
if migSet.LimitTimePrecision {
appliedAt = appliedAt.Round(6 * time.Millisecond)
}
err = executor.Insert(&MigrationRecord{
Id: migration.Id,
AppliedAt: time.Now(),
AppliedAt: appliedAt,
})
if err != nil {
if trans, ok := executor.(*gorp.Transaction); ok {
Expand Down
8 changes: 8 additions & 0 deletions migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (s *SqliteMigrateSuite) TestMigrateIncremental(c *C) {
Migrations: sqliteMigrations[:1],
}

LimitTimePrecision(true)
defer func() {
LimitTimePrecision(false)
}()
// Executes one migration
n, err := Exec(s.Db, "sqlite3", migrations, Up)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -384,6 +388,10 @@ func (s *SqliteMigrateSuite) TestSkipMigration(c *C) {
},
},
}
LimitTimePrecision(true)
defer func() {
LimitTimePrecision(false)
}()
n, err := SkipMax(s.Db, "sqlite3", migrations, Up, 0)
// there should be no errors
c.Assert(err, IsNil)
Expand Down
25 changes: 14 additions & 11 deletions sql-migrate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ func ConfigFlags(f *flag.FlagSet) {
}

type Environment struct {
Dialect string `yaml:"dialect"`
DataSource string `yaml:"datasource"`
Dir string `yaml:"dir"`
TableName string `yaml:"table"`
SchemaName string `yaml:"schema"`
IgnoreUnknown bool `yaml:"ignoreunknown"`
Dialect string `yaml:"dialect"`
DataSource string `yaml:"datasource"`
Dir string `yaml:"dir"`
TableName string `yaml:"table"`
SchemaName string `yaml:"schema"`
IgnoreUnknown bool `yaml:"ignoreunknown"`
LimitTimePrecision bool `yaml:"limitprecision"`
}

func ReadConfig() (map[string]*Environment, error) {
Expand All @@ -66,15 +67,15 @@ func GetEnvironment() (*Environment, error) {

env := config[ConfigEnvironment]
if env == nil {
return nil, errors.New("No environment: " + ConfigEnvironment)
return nil, errors.New("no environment: " + ConfigEnvironment)
}

if env.Dialect == "" {
return nil, errors.New("No dialect specified")
return nil, errors.New("no dialect specified")
}

if env.DataSource == "" {
return nil, errors.New("No data source specified")
return nil, errors.New("no data source specified")
}
env.DataSource = os.ExpandEnv(env.DataSource)

Expand All @@ -92,19 +93,21 @@ func GetEnvironment() (*Environment, error) {

migrate.SetIgnoreUnknown(env.IgnoreUnknown)

migrate.LimitTimePrecision(env.LimitTimePrecision)

return env, nil
}

func GetConnection(env *Environment) (*sql.DB, string, error) {
db, err := sql.Open(env.Dialect, env.DataSource)
if err != nil {
return nil, "", fmt.Errorf("Cannot connect to database: %w", err)
return nil, "", fmt.Errorf("cannot connect to database: %w", err)
}

// Make sure we only accept dialects that were compiled in.
_, exists := dialects[env.Dialect]
if !exists {
return nil, "", fmt.Errorf("Unsupported dialect: %s", env.Dialect)
return nil, "", fmt.Errorf("unsupported dialect: %s", env.Dialect)
}

return db, env.Dialect, nil
Expand Down
Loading