Reputation: 691
I have a few Rails stacks set up on AWS OpsWorks, and I primarily use the OpsWorks console web app to deploy my code to the stack from GitHub.
On the 'Deploy app' page on OpsWorks, there is a 'Migrate database' switch that defaults to off. Database migrations in Rails are idempotent, so it never hurts to run the migration, but it can most definitely hurt if you forget to run the migration when it needed to be run.
Is there any way I can have that switch default to 'Yes' to always run migrations? I don't want to do it with a custom recipe because I'd like the migration to run on one and only one instance during the deploy. Is there some configuration option that I'm missing so that the database migrations automatically run when I deploy code to the stack through the OpsWorks console?
Upvotes: 3
Views: 939
Reputation: 691
While Mircea's answer works, it wasn't ideal for my use case because it runs the migration on all the instances in the stack during the deploy. This will thrash your database if you have a lot of instances defined in your stack.
What I ended up doing in the end was to use a custom cookbook that overrides only the migrate attribute, setting it to true
for one and only one node. this forum post gave me the inspiration.
I already had custom cookbooks enabled for my stack, and for this method to work, you'll need to do the same. I then defined a deploy
cookbook in my custom cookbooks repository that only had one file: deploy/attributes/customize.rb
containing:
migrate_node = 'rails-app1'
current_hostname = node[:opsworks][:instance][:hostname]
application = <your application short name>
if migrate_node == current_hostname
normal[:deploy][application][:migrate] = true
else
normal[:deploy][application][:migrate] = false
end
That code just hard codes 'rails-app1' as the node to run the migrations, and then checks to see if the current node is that one. If so, it queues up the migration for that node. If not, it ensures the migration does not run on that node.
Upvotes: 2
Reputation: 10566
You can try this by configuring it in the custom stack json:
http://docs.aws.amazon.com/opsworks/latest/userguide/workingcookbook-json-override.html
You're interested in deployment attributes: http://docs.aws.amazon.com/opsworks/latest/userguide/workingcookbook-json.html#workingcookbook-json-deploy
Looking at the recipes: https://github.com/aws/opsworks-cookbooks/blob/c1426eee3f79085540312a98441cfc082a18613b/deploy/attributes/deploy.rb#L74
Seems you have to set deploy -> app_name -> migrate to true in the custom json. App_name is probably the name of the layer but you should experiment to see if that's the case.
Upvotes: 1