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

Support go embed #164

Closed
wants to merge 5 commits into from
Closed

Support go embed #164

wants to merge 5 commits into from

Conversation

alovak
Copy link
Contributor

@alovak alovak commented May 10, 2021

The goal of this PR is to switch Moov's projects from pkger to go:embed for default config and migrations.

Why?

  • pkger is an external dependency that currently adds external steps in dev setup, compilation, etc. it creates unnecessary friction
  • pkger raises some unnecessary questions like: should we store pkged.go in repository or no? (the answer is no, but in some repositories we still have it)
  • starting from v 1.16 golang supports file embedding

Some context:

  • golang-migrate/migrate added support for io.FS and embed, but then reverted it and soon will get it back (in short, go 1.15 had some issues with go:embed and go mod tidy, it was fixed, but golang-migrate team decided to wait for a little and git time to users to switch to fixed go version) here are more details: the feat: embed, io/fs support golang-migrate/migrate#471
  • until that issue solved, there is a separate driver for go io.fs which we are using in the PR

Concerns / Questions

  • Should we still support pkger for previous projects? Or I can remove it completely?

@codecov-commenter
Copy link

Codecov Report

Merging #164 (3954d44) into master (49502f2) will decrease coverage by 1.06%.
The diff coverage is 45.16%.

❗ Current head 3954d44 differs from pull request most recent head 2565f41. Consider uploading reports for the commit 2565f41 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #164      +/-   ##
==========================================
- Coverage   79.76%   78.69%   -1.07%     
==========================================
  Files          25       25              
  Lines         771      798      +27     
==========================================
+ Hits          615      628      +13     
- Misses         93      104      +11     
- Partials       63       66       +3     
Impacted Files Coverage Δ
database/database.go 39.28% <0.00%> (ø)
config/config.go 52.38% <45.45%> (+0.86%) ⬆️
database/migrator.go 54.68% <47.36%> (-4.01%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 49502f2...2565f41. Read the comment docs.

@alovak alovak requested review from vxio and InfernoJJ May 10, 2021 09:48
@alovak alovak marked this pull request as ready for review May 10, 2021 11:14
@alovak alovak requested a review from adamdecaf as a code owner May 10, 2021 11:14
@@ -17,7 +17,7 @@ import (

var migrationMutex sync.Mutex

func RunMigrations(logger log.Logger, config DatabaseConfig) error {
func RunMigrations(logger log.Logger, config DatabaseConfig, migrationsSource source.Driver) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we want to keep base package compatible with old versions of the service, I can create RunMigrationsWithSource to support pkger migrations as well.

@@ -45,12 +47,29 @@ func (s *Service) LoadFile(file string, config interface{}) error {

f, err := pkger.Open(file)
if err != nil {
return logger.LogErrorf("pkger unable to load %s: %w", file, err).Err()
return fmt.Errorf("pkger unable to load %s: %w", file, err)
Copy link
Member

Choose a reason for hiding this comment

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

Use our logger.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It duplicates information in logs. I changed it as I saw the output and was duplicating the same errors...

@@ -25,7 +27,7 @@ func NewService(logger log.Logger) Service {

func (s *Service) Load(config interface{}) error {
if err := s.LoadFile(pkger.Include("/configs/config.default.yml"), config); err != nil {
return err
s.logger.LogErrorf("loading default config file using pkger: %w", err)
Copy link
Member

Choose a reason for hiding this comment

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

Not returning the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeap. My idea here, if we want to keep pkger ... for backward compatibility?, just to ignore the error. And log it instead. I would prefer to get rid of pkger then we don't need it.

Copy link
Member

Choose a reason for hiding this comment

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

If its going embedded completely we don't need it??? because the next compile should bring in the default. If it doesn't work that way we can't use the embed tooling yet then and have to re-do how we do configs.

Copy link
Contributor Author

@alovak alovak May 11, 2021

Choose a reason for hiding this comment

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

We have to re-do how we do configs as we now have to pass file (or its content) outside of the config package.

Copy link
Member

Choose a reason for hiding this comment

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

It sounds like moovfinancial needs a config package to address the needs of those services, so moov-io/base might not be the best location for these changes.

@@ -17,6 +18,7 @@ type ConfigModel struct {
Default string
App string
Secret string
Custom string
Copy link
Member

Choose a reason for hiding this comment

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

Whats this?

Copy link
Contributor Author

@alovak alovak May 10, 2021

Choose a reason for hiding this comment

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

Just to test that we read a custom field from a custom file.

Copy link
Member

Choose a reason for hiding this comment

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

The App is the custom file?

@@ -42,7 +46,18 @@ func RunMigrations(logger log.Logger, config DatabaseConfig) error {
return logger.Fatal().LogErrorf("Error running migration: %w", err).Err()
}

ver, _, err := m.Version()
Copy link
Member

Choose a reason for hiding this comment

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

Why was this added in? m.Up() brings it upto the latest version

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on current output I had no idea what was the version before migration and what's the version after. It was not clear for me if migrations run at all. That's why I decided to add some information to migrations.

@@ -37,3 +39,20 @@ func Test_Load(t *testing.T) {
require.Equal(t, "app", cfg.Config.App)
require.Equal(t, "keep secret!", cfg.Config.Secret)
}

//go:embed testdata/*.yml
var configs embed.FS
Copy link
Member

Choose a reason for hiding this comment

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

This PR only adds embed to our test file so far?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeap. embed works for the package directory or subdirectories. Here I'm using this for tests.

templater has separate packages configs and migrations which embed files and pass them inside the config or migration.

@adamdecaf
Copy link
Member

LGTM overall, but we need to release this as v0.19.0 with a note in the changelog about the breaking change.

https://github.com/moov-io/base/blob/master/CHANGELOG.md

@adamdecaf
Copy link
Member

Replaced by #302

@adamdecaf adamdecaf closed this May 31, 2023
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.

5 participants