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

Allow accepting env vars for config #128

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

quintenpalmer
Copy link
Contributor

Our company has a use case to provide the environment information through the shell environment instead of from a dbconfig.yml file. We have augmented the sql-migrate binary's capability to check for a dbconfig.yml file before trying to read it in, and if there is no file present, it will be optimistic and try to read from the shell environment. Failing to read from the environment if there is no file will work as it did before with the zero-value checking that happens in the last part of GetEnvironment. If these changes are acceptable to introduce to the parent project would like to do so.

Example usage showing that this:

  • honors the dbconfig.yml if present
  • fails in the same way as before this PR if no file nor no shell environment variables are provided
  • leverages the shell environment variables if present and the dbconfig.yml is not present
 $ go install ./...
~/w/g/s/g/r/sql-migrate * (allow-accepting-env-vars-over-file) (1ef8a56) quinten@workdora 0 14:35:57 
 $ date
Fri Nov 16 14:36:00 EST 2018
~/w/g/s/g/r/sql-migrate * (allow-accepting-env-vars-over-file) (1ef8a56) quinten@workdora 0 14:36:00 
 $ ll ~/workspace/go/bin/sql-migrate 
-rwxrwxr-x. 1 quinten quinten 14090392 Nov 16 14:35 /home/quinten/workspace/go/bin/sql-migrate
~/w/g/s/g/r/sql-migrate * (allow-accepting-env-vars-over-file) (1ef8a56) quinten@workdora 0 14:36:15 
 $ ls dbconfig.yml
dbconfig.yml
~/w/g/s/g/r/sql-migrate * (allow-accepting-env-vars-over-file) (1ef8a56) quinten@workdora 0 14:36:39 
 $ ~/workspace/go/bin/sql-migrate status
+-----------------------------+--------------------------------------+
|          MIGRATION          |               APPLIED                |
+-----------------------------+--------------------------------------+
| 20181115113718-qpinit.sql   | 2018-11-15 16:40:57.640617 +0000 UTC |
| 20181116142709-testtest.sql | 2018-11-16 19:29:19.577488 +0000 UTC |
+-----------------------------+--------------------------------------+
~/w/g/s/g/r/sql-migrate * (allow-accepting-env-vars-over-file) (1ef8a56) quinten@workdora 0 14:37:16 
 $ mv dbconfig.yml dev/
~/w/g/s/g/r/sql-migrate * (allow-accepting-env-vars-over-file) (1ef8a56) quinten@workdora 0 14:37:21 
 $ ~/workspace/go/bin/sql-migrate status
Could not parse config: No dialect specified
~/w/g/s/g/r/sql-migrate * (allow-accepting-env-vars-over-file) (1ef8a56) quinten@workdora 1 14:37:23 
 $ envinjector -file-name ~/dev/env/sqlmigrate.yml ~/workspace/go/bin/sql-migrate status
+-----------------------------+--------------------------------------+
|          MIGRATION          |               APPLIED                |
+-----------------------------+--------------------------------------+
| 20181115113718-qpinit.sql   | 2018-11-15 16:40:57.640617 +0000 UTC |
| 20181116142709-testtest.sql | 2018-11-16 19:29:19.577488 +0000 UTC |
+-----------------------------+--------------------------------------+

Quinten Palmer added 3 commits December 14, 2018 14:32
This function reads the map of config environments to Environment
structs from the dbconfig.yml file. We want to allow these commands to
also read from the shell environment, so for unambiguous names this is
being renamed.
This is in the same vein of the previous commit where we want to be able
to parse an Environment from the shell environment, so to make that
switch easier, this function now returns the Environemnt and can be
substituted for another function which reads from the environment.
We want to build the ability for this command to accept the Environment
through the shell env. If the file is not present, we optimistically
look for the Environment in said shell env. There are existing
zero-value checks to make sure that a user doesn't accidentally end up
with no values for anything if their file was empty. We do a separate
stat to check for the file to keep the complexity of the fallback
simpler.

The envconfig package can be researched more here:

https://github.com/kelseyhightower/envconfig
@quintenpalmer
Copy link
Contributor Author

@rubenv have you had a chance to take a look at this PR? Would like to get this merged to you upstream if possible, let me know if that will be the case please.

@khalilhaji
Copy link

@rubenv I have added env configurability to the ignore-unknown setting as well. If you are interested in env configurability, it would be great if this PR could get merged first so that I can open a PR for the ignore-unknown addition.

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.

2 participants