rails db:migrate vs rake db:migrate - ruby-on-rails

I'm new to rails. I noticed when generating data migration in rails 5, some people use rails db:migrate over rake db:migrate. Can someone explain the difference between the rails vs rake command in database migration? Does it mean rake command is obsolete in rails 5?
many thanks

Rails core team decided to have consistency by enabling rails command to support everything that rake does.
For example in Rails 5 commands like db:migrate, db:setup, db:test etc which are part of rake command in Rails 4 are now being supported by rails command. However you can still choose to use rake to run those commands similar to how they were run in Rails 4. This is because Rails community has introduced Rake Proxy instead of completely moving the command options from rake to rails.
What happens internally is that when rails db:migrate command is executed, Rails checks if db:migrate is something that rails natively supports or not. In this case db:migrate is not natively supported by rails, so Rails delegates the execution to Rake via Rake Proxy.
If you want to see all the commands that is supported by rails in Rails 5 then you can get a long list of options by executing rails --help.

Related

problem when run rails db:setup : schema.rb doesn't exist yet?

I am newbie with Rails and having this problem.
I created a rails folder named freelancer. Everthing seems ok.
Then, I went to this folder and ran this cmd :
rails db:setup
It said to me this error : /freelancer/db/schema.rb doesn't exist yet. Run rails db*migrate to create it, then try again. If you do not intend to use a database, you should instead alter /home/anh/Desktop/freelancer/config/application.rb to limit the frameworks that will be loaded
Then I ran this command as they said
rails db:migrate
Then I ran the command rails db:setup, they said to me that
Database 'bai9freelancer_development' already exists
Database 'bai9freelancer_test' already exists
I am very confusing because those problem. First, when I create my rails folder freelancer, no problem. Why schema.rb doesn't exist ? Second, They said to me that I must run rails db:migrate then try rails db:setup again, then they said to me that already exists. That why ? Could you please explain those problem for me ? Thank you very much.
When you ran
rails db:migrate
you already created db/schema.rb
Running rails db:setup is the equivalent of
rails db:create
rails db:migrate
rails db:seed
So after you ran rails db:migrate you don't need to recall rails db:setup anymore. You can find out more in the documentation here

rails routes vs rake routes [duplicate]

I'm new to rails. I noticed when generating data migration in rails 5, some people use rails db:migrate over rake db:migrate. Can someone explain the difference between the rails vs rake command in database migration? Does it mean rake command is obsolete in rails 5?
many thanks
Rails core team decided to have consistency by enabling rails command to support everything that rake does.
For example in Rails 5 commands like db:migrate, db:setup, db:test etc which are part of rake command in Rails 4 are now being supported by rails command. However you can still choose to use rake to run those commands similar to how they were run in Rails 4. This is because Rails community has introduced Rake Proxy instead of completely moving the command options from rake to rails.
What happens internally is that when rails db:migrate command is executed, Rails checks if db:migrate is something that rails natively supports or not. In this case db:migrate is not natively supported by rails, so Rails delegates the execution to Rake via Rake Proxy.
If you want to see all the commands that is supported by rails in Rails 5 then you can get a long list of options by executing rails --help.

rake db:migrate does nothing, even on reset

Here is a very strange issue I have on my new computer setup (otherwise, it's working on my other setups).
I'm running : rake db:migrate
No errors, but it does nothing...
rake db:migrate:status show me the list of pendings migrations (marked as "down"), the ones that I effectively have in my bd/migrate folder.
Even if I run those commands or removing files in db/* manually, db:migrate is still useless.
rake db:drop:all
rake db:create
rake db:migrate
I have tried also db:reset, db:rollback STEP=1000.
If I specify a VERSION number (one from the list given by db:migrate:status) as:
rake db:migrate VERSION=20150106184930
I've got the following error:
No migration with version number 20150106184930
I have also generated a new migration with:
rails generate migration TestMigration
And again, db:migrate completely ignor it.
My current setup is: windows7, rails 4.2.0, rake 10.3.2.
Thanks for any help, clues...
After hours of deep debugging in rake, reinstalled all my complete setup, I finally figured out that the problem was comming from the "non so special" characters [ or ] somewere in my project path!!
DAMN RAILS!
Due to readability, all my project's folders start with "[NAME-OF-PROJECT]xxxx/"... then in this particular rails project comes a subfolder for the rails app.
No error, nothing that point you out that the path name could be the issue. I'm quite sur that "[" and "]" are not forbidden character (even on linux) : http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
And why "[" or "]" makes rake db:migrate failing and not rake db:migrate:status???
For me it clearly shows some weakness in rails architecture. I should probably do a bug report for that... can someone point me some report mailing list or whatever?
I hope that my misadventure will save hours for others.
If this is a new rails setup, you may not have gems on your system, but only in your app. Try prepending bundle exec to your rake commands.
bundle exec rake db:migrate
Try run gem update and try rake db:migrate again.

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

Do I have to run rake db:test:load each time manually before runnings tests?

I'm new to Ruby on Rails.
I'm trying to set up a simple WebApp via Scaffolding. And using RSpec for my tests. Now after the scaffold command:
rails generate scaffold VideoSegment file_path:string short_name:string description:string
I ran rake db:migrate, but thats clear, bringing the data to my development database.
But the tests where not green before I did:
rake db:test:load
To bring the schema of my development database to the test database. Isn't there a way to automate this step? Or do I have to load test database again after each scaffold?
PS: Of course I know Scaffold is not doing the finest things, but for my proof of concept need it's sufficient.
Thanks for any suggestions.
Whenever you run rspec it will prepare the test schema for you using the task: db:test:prepare
So after generating migrations you have to do rake db:migrate to update the development schema and then run you spec which will automatically prepare the test database for you.

Resources