rubenv / sql-migrate

SQL schema migration tool for Go.
MIT License
3.23k stars 280 forks source link

Allow accepting env vars for config #128

Open quintenpalmer opened 5 years ago

quintenpalmer commented 5 years ago

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:

 $ 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 |
+-----------------------------+--------------------------------------+
quintenpalmer commented 5 years ago

@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 commented 4 years ago

@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.