Automatically running rake tasks when you switch branches in git - ruby-on-rails

Whenever I switch branches I am having to run the commands:
rake db:drop
rake db:migrate
rake db:setup
Is there a way to automatically run these commands in terminal for my application? I am developing a ruby on rails application and have many branches on GitHub. Is there a file I can add to that will automatically run this command?
For example, I will do this "git checkout branch101"
and then I want to run the 3 commands above.

Save this shell script to the file /path/to/repo/.git/hooks/post-checkout, and make it executable.
#! /bin/sh
# Start from the repository root.
cd ./$(git rev-parse --show-cdup)
# drop migrate and set up.
rake db:drop
rake db:migrate
rake db:setup

What you're looking for is something like Guard that runs your test suite whenever code changes in your repo. You should be able to tweak Guard to run your rake tasks instead of tests.
Railscasts has a great screencast explaining the Guard.
After a quick google search, looks like there's a gem available that can run rake tasks for you: guard-rake . Unfortunately, it doesn't seem to be widely used though.

Related

couldn't run test in a modular rails app

Before I ask my question, I like you to see the structure of my modular rails app, in which apps are engines and plugged into Domain/Parent empty app having only configurations.
See image below:
However, when I run test with rails test or rake test in the domain/parent directory that has nothing aside configuration, I get:
However, I can run rake tasks like bundle install,rake db:migrate etc from the root of the app or domain/parent directory successfully because the engine app is plugged into the root of the app. So it baffles my why rails test or rake test does not work.
I proceeded and cd into engines, into the root app inside the engine and I ran rails test or rake test or rails test test/models/csv_importer/user_test.rb but I get a different thing. See it below:
I did the response but test didn't still run
How do I test a modular rails app? Any help is appreciated.
When I run: rake db:test:prepare from the root of the engine, I get:
Okay, I fix the problem. What I had to do is:
From the root of the engine application (which is csv_importer as shown in the image in the question), I ran the following rails tasks:
bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:migrate RAILS_ENV=test
Then when I run rails test, my test starts running.

Jenkins Rspec tests complaining about pending migrations?

I'm attempting to setup a Jenkins automated job to run my Rspec tests before doing a deploy. The goal is to run the test suite on remote server without the runtime databases.
I'm using the rake plugin and have the following commands, (keep in mind these are both prepended by rake automatically)
db:test:prepare RAILS_ENV=test
ci:setup:rspec spec RAILS_ENV=test
This works fine in one of my projects, but the other complains about pending migrations. My job output is as follows,
[Dev portal-admin tests] $ /var/lib/jenkins/.rvm/gems/ruby-2.0.0-p353/bin/rake db:test:prepare RAILS_ENV=test
[Dev portal-admin tests] $ /var/lib/jenkins/.rvm/gems/ruby-2.0.0-p353/bin/rake ci:setup:rspec spec RAILS_ENV=test
rm -rf spec/reports
Run `rake db:migrate` to update your database then try again.
You have 16 pending migrations:
20131219204223 DeviseCreateUsers
20131219205648 DeviseInvitableAddToUsers
etc
As far as I understand, db:test:prepare should load the database scheme from the scheme.rb, so I should be good with migrations. I also don't understand why it complains AFTER I get to the Rspec step, instead of the first test:prepare step.
What am I doing wrong here?
I believe your first rake task, db:test:prepare, loaded the database from your existing schema.rb file without objection, as it has no requirement on the migrations being current.
I suspect your subsequent ci:setup:rspec rake task has a dependency on test:prepare which has a dependency on db:abort_if_pending_migrations, which caused the abort you see.
For Rails 4, at least, see activerecord-4.0.0/lib/active_record/railties/databases.rake in your gems directory for details of these rake task definitions.

Execute rake command inside bash

I am seeing many times the question about execution of bash files inside rake (task) files.
My question is, how to execute a rake command inside the bash file?
I have a migrate.sh file inside each rails app on my server and I'm using a general publish.sh. All of this runs ok.
Then, I have a command like rake tmp:clear assets:clean log:clear RAILS_ENV=production inside each migrate.sh that gives me a rake: command not found error.
Help?
Basically rake is not resolved as the PATH variable is not correct. You can try doing echo $PATH. Also you can create a bash script and provide some environment variables required by rake like this:
#!/bin/bash
GEM_HOME=/home/tuxdna/.gems
SHELL=/bin/bash
USER=tuxdna
PATH=/home/tuxdna/.gems/bin:/usr/lib/ruby/gems/1.8/bin/:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
GEM_PATH=/home/tuxdna/.gems:/usr/lib/ruby/gems/1.8
cd ~/somesite
export RAILS_ENV=production
bundle exec rake mytask:task1
klashxx's supposition was correct. It was a permissions/profile issue. I had change my user to root to be able to do other previous tasks and found out that my root was not able to run rake tasks.
This will not be an issue on production server though.
Thanks klashxx

Rails + Rake: How to simulate running of rake task to see if it works?

Is there a way to do a test run of a rake task? If so what is the command to do it? I don't want to actually touch the development database since my task manipulates the data.
Yes you can actually run bundle exec rake COMMAND -n
Having a look at rakes help we can see it here.
# rake --help
--dry-run Do a dry run without executing actions.

Rails: Running more than one rake task at once?

Been loving rails, but still struggling with how long it takes to run tasks from the command line.
I'm wondering if it's possible to combine multiple rake tasks without reloading the environment each time. For example, if I run
rails generate rspec:install && rails generate model test_model title:string && rake db:migrate
it will spend 10 seconds loading up rails to run the rspec install, then another ten seconds to load up the environment for generate model, then another for the migration. Is there a way to keep the same environment open for all 3 commands?
Take a look at the rails-sh gem - it boots the Rails environment, and then presents a command line interface, so you can run your command within it. There are limitations when using it (I don't think you can change your RAILS_ENV), but for your use-case, it should be perfect.
If your commands are just rake tasks, you can use spaces to separate them, e.g.:
rake db:migrate && rake db:test:clone_structure
would become
rake db:migrate db:test:clone_structure

Resources