-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support environment variables in migration files using go templates #793
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
base: master
Are you sure you want to change the base?
Conversation
@dhui Can you please review this? |
@dhui it would be nice to have this functionality. Is there a chance to get it into main? |
This could be a very nice feature. An use case: it will allow to customize migrations files for testings pipelines that run in parallel and that requires to have different databases for each test thread to avoid conflicts. We currently have to trick migration files with find/replace to obtain a similar feature. The template support will be very interesting. |
Yes, this would be a great feature to have merged into main. I'm having to do the |
+1 |
I made this script file to run
migrate.sh #!/bin/sh
# Import env variables from ./.env
set -a
[ -f .env ] && . .env
echo "DB_NAME = $DB_NAME | DB_USER = $DB_USER | DB_HOST = $DB_HOST | DB_PORT = $DB_PORT"
read -p "⚡️ (Sql-Migrate) Type: <up | d | s> " command
if [[ $command == 'up' ]]
then
sql-migrate up -env="development" sslmode=disable
elif [[ $command == 'd' ]]
then
sql-migrate down -env="development" sslmode=disable
elif [[ $command == 's' ]]
then
sql-migrate status -env="development" sslmode=disable
else
read -p "Run your own command: " custom_command
sql-migrate $custom_command -env="development" sslmode=disable
fi
|
+1 |
templatize migrations: apply patch from pr golang-migrate#793
@dhui Do you guys have a plan to approve it or we should stick with other solutions? |
Can we merge it into main? |
You can wrap the file when using iofs to support templating
hope this helps someone |
+1 |
1 similar comment
+1 |
@ninthclowd thanks for the contribution! I have just tested it in the latest version and it works great. It has been extremely helpful to inject some static environment values in the migrations. |
Hi, is it possible to merge this wonderful PR asap, please? |
will it be merged? may be in feature branch? just to use in my go.mod |
Can this PR be merged? |
This PR adds a
-template
command line option that, when enabled, allows you to substitute any environment variables into your migration file with go templating. i.e. If you set theLOCAL_WAREHOUSE
environment variable toMY_DB
and have a migration file with the following contents:it will be transformed into the following before being executed:
See https://pkg.go.dev/text/template for more information on supported template formats.
As mentioned in #747, this functionality could be done with envsubt, but that requires you to build custom scripts to do so. It ended up being just as much effort to add the functionality with go template and kept our pipelines and local development workflow more simple.