문제

I have a rails application on openshift. I want to run rake db:migrate in a deploy hook on openshift, except on first deploy, when I want to run rake db:setup.

This is not necessarily openshift-specific, since deploy hooks are just bash scripts which run when the application is deployed.

Is there any way of knowing if the application has been deployed before or whether the database has already been created from a deploy hook?

도움이 되었습니까?

해결책 2

I found a solution in the openshift docs, which is actually not even valid sh. Here is my version.

 if echo "use $OPENSHIFT_APP_NAME; show tables" | mysql | grep schema_migrations 2>&1 > /dev/null
 then
     bundle exec rake db:migrate RAILS_ENV="production"
 else
     bundle exec rake db:setup RAILS_ENV="production"
 fi

If you are using postgres, here is a similar command

if echo "\c $PGDATABASE; \dt" | psql | grep schema_migrations 2>&1 >/dev/null
then
   bundle exec rake db:migrate RAILS_ENV="production"
else
   bundle exec rake db:setup RAILS_ENV="production"
fi

I'm sure there is a similar environment variable as $PGDATABASE for mysql that you can use instead of $OPENSHIFT_APP_NAME. You can find it by running rhc ssh -a app-name, then running env to get a list of the environment variables.

다른 팁

I don't think you have to make this distinction. rake db_setup can be called even if the database already exist. See also - How to check if the database exists or not in rails before doing a rake db:setup

Alternatively, you could create a custom rails task. This task could for example try to access the database to check whether it exists. If not you can call db:setup. To learn more about custom rake tasks have a look at this nice video - http://railscasts.com/episodes/66-custom-rake-tasks. Using rake has the benefit that your solution is independent of OpenShift and by using Rake you have access to the Rails environment.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top