I created reset_db.bat on Windows with the following content:
rake db:drop RAILS_ENV=development
rake db:create
rake db:migrate
rake db:seed
When I type reset_db only the first command (drop) is executed. Why ?
I assume that the rake is also a .bat file.
Then you only have to prepend it with a call
call rake db:drop RAILS_ENV=development
call rake db:create
call rake db:migrate
call rake db:seed
Related
this is what I get when I run the project
Hi, I'm working on a project.
When I run my project, I got this problem:
Migrations are pending. To resolve this issue, run:
bin/rake db:migrate RAILS_ENV=development
raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?
(connection)
I've already tried several solutions below:
1)
rake db:drop
rake db:create
rake db:migrate
2) bundle exec rake db:migrate
3) bin/rake db:migrate RAILS_ENV=development
but they didn't work, and I got the same error over and over again.
What can I do?
Your first try was close, you need to do
rake db:drop
rake db:create
rake db:schema:load
Check this article it may be helpful.
http://icebergist.com/posts/rake-db-migrate-vs-rake-db-schema-load/
$ rm db/schema.rb
$ bundle exec rake db:drop
$ bundle exec rake db:create
$ bundle exec rake db:migrate
Or just rake db:reset. That always works for me, when I get stuck.
I'm typing in the terminal
rake db:migrate
Here's the error Im getting
ActiveRecord::NoDatabaseError: FATAL: database "db/development.sqlite3" does not exist
rake db:migrate doesn't create database, rather requires it exists.
rake db:create creates a database without loading database schema.
Remember you should run rake db:create before you can run rake db:migrate if you dropped database with rake db:drop.
Every time I have changes to my schema or new migration files, I run this command:
rake db:drop db:create db:migrate db:seed
Is there a prebuilt equivalent way to do this?
I thought from what I've read that rake db:reset doesn't quite do the same thing, but i could be wrong.
you could create a custom rake task for this - lib/tasks/db_rebuild_all.rake
namespace :db_tasks do
desc "Rebuild database"
task :rebuild, [] => :environment do
raise "Not allowed to run on production" if Rails.env.production?
Rake::Task['db:drop'].execute
Rake::Task['db:create'].execute
Rake::Task['db:migrate'].execute
Rake::Task['db:seed'].execute
end
end
then just run bundle exec rake db_tasks:rebuild
You could run rake db:drop and then rake db:setup.
db:setup will run rake db:create db:schema:load and db:seed
But why are you dropping and recreating your database everytime you have new migrations? That's what the migrations are there for, to make incremental changes to your existing database.
Run rake db:reset && rake db:seed (Note: You must have db/schema.rb file updated)
OR
Run rake db:migrate:reset && rake db:seed
As of rails 6 you can run rake db:prepare which creates the db, runs migrations and seeds
If you want to completely reset the db, run rake db:drop && rake db:prepare
https://guides.rubyonrails.org/v6.0/6_0_release_notes.html#active-record-notable-changes
First create the task file (lib/tasks/db.rake) with:
rails g task db reseed
Then write in it:
namespace :db do
desc "Reseed database task"
task reseed: [ 'db:drop', 'db:create', 'db:migrate', 'db:seed' ] do
puts 'Reseeding completed.'
end
end
Is there a way to do multiple Rails 3 rake commands on one line, requiring the environment to be initiated only once?
I know this is possible:
rake db:rollback db:migrate
But if options are passed,
rake db:migrate VERSION=0 db:migrate
the second 'db:migrate' won't run.
I don't think that is possible.
The quickest solution I can think of is:
RAILS_ENV=test rake db:migrate VERSION=0 && rake db:migrate
The reason why I believe this is not possible is because VERSION is simply a constant, not an attribute that is passed as a db:migrate option. For example, all of these commands work:
rake db:migrate VERSION=0
rake VERSION=0 db:migrate
VERSION=0 rake db:migrate
And since you can't rewrite the constant in the same action again, you basically call db:migrate VERSION=0 twice.
Why not try Zeus?
It preloads environments for rails, rake, test environments, etc.
Utterly confused at this mess:
rake db:drop
>
rake db:create
> my_database already exists
rake db:migrate
> unknown database my_database
Appreciate any insight.
That's because first time when u do rake db:create it creates two databases one is development and other is test. then when u do rake db:drop it drops the database, but it only drops the development database not the test database. so try removing the test database explicitly and everything should be fine.
Try :
rake db:drop
rake db:create RAILS_ENV=development
rake db:migrate RAILS_ENV=development
if getting same error then open mysql terminal and create database manually :
CREATE DATABASE database_name;
then run
rake db:migrate