Skip to content

Conversation

nronas
Copy link
Contributor

@nronas nronas commented Feb 23, 2021

Implements: #503

Adds a new -tz cmd option on create that allows specifying the timezone on which non-sequential migration files will be generated with.

@nronas
Copy link
Contributor Author

nronas commented Feb 23, 2021

/cc @dhui

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@@ -180,7 +183,12 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n", createUsage, gotoU
log.fatal("error: -ext flag must be specified")
}

if err := createCmd(*dirPtr, startTime, *formatPtr, name, *extPtr, seq, seqDigits, true); err != nil {
timezone, err := time.LoadLocation(*timezoneName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid loading TZ data if the named tz is the default. That way, missing tz data won't break the CLI if a tz isn't specified.

Copy link
Contributor Author

@nronas nronas Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhui on another thought, I cannot see how this will break, since the default location will be UTC even if you do not specify the timezone. Essentially we will do time.LoadLocation("UTC")

With timezone

go run ./cmd/migrate/... create -tz Europe/Athens -ext postgres hello
/Users/nronas/projects/migrate/20210223204225_hello.up.postgres
/Users/nronas/projects/migrate/20210223204225_hello.down.postgres

Without timezone

go run ./cmd/migrate/... create -ext postgres hello_no_tz
/Users/nronas/projects/migrate/20210223184235_hello_no_tz.up.postgres
/Users/nronas/projects/migrate/20210223184235_hello_no_tz.down.postgres

With unknown timezone

go run ./cmd/migrate/... create -tz Asgard -ext postgres hello_invalid_tz
unknown time zone Asgard
exit status 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is when the tz database is missing, not when the -tz option is not specified or invalid/incorrect.

From the time.LoadLocation() docs:

The time zone database needed by LoadLocation may not be present on all systems, especially non-Unix systems. LoadLocation looks in the directory or uncompressed zip file named by the ZONEINFO environment variable, if any, then looks in known installation locations on Unix systems, and finally looks in $GOROOT/lib/time/zoneinfo.zip.

Copy link
Contributor Author

@nronas nronas Feb 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I completely misinterpreted what you were referring to. I think we should still be fine since zoneinfo does a look upon an IANA file only if the location name is neither 'UTC' nor 'Local' https://github.com/golang/go/blob/master/src/time/zoneinfo.go#L630-L635

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging deeper and discovering that the tz db isn't loaded if the location UTC or Local are specified! Looks like this behavior has been around since at least Go 1.4 and seems unlikely to change in the future, so I think the current approach is fine. The fix is easy enough to make should this break in the future.

@nronas nronas requested a review from dhui February 24, 2021 15:15
@dhui dhui merged commit 7dc03e3 into golang-migrate:master Feb 24, 2021
@coveralls
Copy link

coveralls commented Feb 24, 2021

Pull Request Test Coverage Report for Build 145

  • 0 of 6 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.05%) to 56.115%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/cli/main.go 0 6 0.0%
Totals Coverage Status
Change from base Build 130: -0.05%
Covered Lines: 3120
Relevant Lines: 5560

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants