I accidentally ran rake db:migrate, and then ran rake db:rollback. Now the database table I had created before is completely gone. Is there any way to undo a rollback, or should I just revert to the most recent branch?
You can do a rake db:migrate to perform the same migration once again.
Just an update to existing answer by #sabaeus
If rake db:migrate doesn't work for anyone, you can use rake db:migrate:redo.
rake db:migrate is to run all current migrations.
Related
I was working on Nitrous.io on a Rails Project.
I have created a new branch and then after some coding , I reset the database with
rake db:reset.
After this action my Projects folders were automatically deleted.
Someone could help me understand what is wrong on my command?
db:reset will do exactly as described - it will "reset" the database. So db:reset is the same as executing the following commands: db:drop (which will drop/delete all entries in the database) + db:setup
I somehow doubt it that rake db:reset deletes your .rb files. Must be something else. Maybe you messed up with git?
rake db:reset
rake db:create
rake db:migrate
works just fine, normally.
I appear to have a circular issue in regards to Ruby on Rails migration procedure. I am following the introduction article and I have reached the point when I need to create my first table.
I have ran the following,
[tims#web2 working_ror]# rails generate model Homepage first_name:string last_name:string email:string message:text
invoke active_record
create db/migrate/20131119203948_create_homepages.rb
create app/models/homepage.rb
invoke test_unit
createtest /models/homepage_test.rb
createtest /fixtures/homepages.yml
I then proceeded with the migration,
[tims#web2 working_ror]# rake db:migrate
== CreateHomepages: migrating ================================================
-- create_table(:homepages)
-> 0.0493s
== CreateHomepages: migrated (0.0494s) =======================================
, however, when I run my application I see the following message,
Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.
but, IF I run the above,
[tims#web2 working_ror]# rake db:migrate RAILS_ENV=development
[tims#web2 working_ror]#
and the message continues ...
I have spent considerable amount of time researching forums in-which the closest I could find was to drop and re-build everything, which have done the following.
rake db:drop
rake db:create
rake db:migrate
and the results are the same.
You need to do
bundle exec rake test:prepare
or
bundle exec rake db:test:prepare
and then
bundle exec rake db:migrate
before running the specs
Cheers
cited from : Why am I asked to run 'rake db:migrate RAILS_ENV=test'?
you can do
bundle exec rake test:prepare
In Rails 4.1+, they deprecated db:test:prepare
You can now just use:
ActiveRecord::Migration.maintain_test_schema!
If you need to do it manually
rake db:schema:load RAILS_ENV=test
and then
bundle exec rake db:migrate
try
In RAILS_ROOT/config/environments/development.rb Set the following setting to false:
config.active_record.migration_error = false#:page_load
One weird trick that you can use when your migrations are screwed (file deleted, manually renamed, etc.)
Fire up your favourite DB admin tool (eg. PGAdmin3) and browse to the database in question.
Look for a table called schema_migrations and browse its content. It should have a single column called version. This field is used by Rails to check whether migrations are up to date.
Make sure that your migration timestamps corresponds with the data in this column. If you have deleted an older migration, delete the corresponding timestamp.
Check to make sure that table doesn't already exist:
type - rails dbconsole
type - .tables (check to see if there was an error during the rake db:migrate that has the table name like -- create_table(:test) rake aborted!)
If you see the table name after running the .tables in the console type - drop table TABLENAME;
Then .quit to go back to the branch and run the rake db:migrate command again.
this was what i did:
rails db:environment:set RAILS_ENV=test
If you need to do it manually
rake db:schema:load RAILS_ENV=test
and then
bundle exec rake db:migrate
Thanks to Ahmed Ali....... your comment was helpful.
Is there a quick rake db:rollback command for all of the migrations?
Rolling back all migrations
To rollback all migrations the best solution is the one #Claudio Floreani proposed:
rake db:migrate VERSION=0
This will rollback every migration. You can read why this is the best approach in his answer. Then, run all migrations again with
rake db:migrate
Resetting the database
Reset
rake db:migrate:reset #runs db:drop db:create db:migrate
This method drops the database and runs the migrations again.
Loading the last schema
rake db:reset
This method will drop the database and load the data from the last schema.
You can see more information in this post: Difference between rake db:migrate db:reset and db:schema:load
Thanks to #Claudio Floreani and all the users who commented to improve the answer.
If you really want to rollback all of the migrations, and not just take the database to a pristine state or to the last schema, you have to run:
rake db:migrate VERSION=0
This will actually rollback all the way down every migration and ensure that every migration is reversible.
If you now issue
rake db:migrate:status
you will see that all the migrations are still there but they are in a 'down' (not applied) state.
Other commands that imply a rake db:reset or rake db:drop (such as in the answers by #Orlando or #Alex Falke) won't do any rollback at all: that is, they won't ensure that every migration is reversible.
Moreover, rake db:drop cannot be run while the database is being accessed by other users, while rollbacks can be performed live (even if this is generally not recommended). And last but not least, simply dropping and recreating the database will also delete the schema migrations table: if someone runs rake db:migrate:status after the database has been dropped, he will be replied with "Schema migrations table does not exist yet" and will have no clues about which migrations can be applied (unless he knows it yet or can list them).
just use rake db:reset, that will drop your database (same as undoing all migrations) and reset to the last schema.
UPDATE: a more correct approach will be using rake db:migrate:reset. That will drop the database, create it again and run all the migrations, instead of resetting to the latest schema.
If a permission problem raises (like it happened to me), maybe you could try dropping all database tables like I did with rubymine (just open database tool window, select all tables and right-click -> drop), it should be similar with other IDEs. Some tables like sqlite_master and sqlite_sequence were conveniently ignored in the drop.
This allowed me to do
rails db:migrate
and everything worked fine. Of course you loose all data!
After adding migration files in the db/migrate folder and running rake db:migrate, I want get back to the previous step, I think using VERSION=n is the right way to do that, but I don't know the correct value of n to use. Is there any command to check the current n value?
It would be great if anyone could provide full instructions on how to use rake db:migrate.
For starters
rake db:rollback will get you back one step
then
rake db:rollback STEP=n
Will roll you back n migrations where n is the number of recent migrations you want to rollback.
More references here.
Roll back the most recent migration:
rake db:rollback
Roll back the n most recent migrations:
rake db:rollback STEP=n
You can find full instructions on the use of Rails migration tasks for rake on the Rails Guide for running migrations.
Here's some more:
rake db:migrate - Run all migrations that haven't been run already
rake db:migrate VERSION=20080906120000 - Run all necessary migrations (up or down) to get to the given version
rake db:migrate RAILS_ENV=test - Run migrations in the given environment
rake db:migrate:redo - Roll back one migration and run it again
rake db:migrate:redo STEP=n - Roll back the last n migrations and run them again
rake db:migrate:up VERSION=20080906120000 - Run the up method for the given migration
rake db:migrate:down VERSION=20080906120000 - Run the down method for the given migration
And to answer your question about where you get a migration's version number from:
The version is the numerical prefix on the migration's filename. For
example, to migrate to version 20080906120000 run
$ rake db:migrate VERSION=20080906120000
(From Running Migrations in the Rails Guides)
Best way is running Particular migration again by using down or up(in rails 4. It's change)
rails db:migrate:up VERSION=timestamp
Now how you get the timestamp.
Go to this path
/db/migrate
Identify migration file you want to revert.pick the timestamp from that file name.
If the version is 20150616132425, then use:
rails db:migrate:down VERSION=20150616132425
Other people have already answered you how to rollback, but you also asked how you could identify the version number of a migration.
rake db:migrate:status gives a list of your migrations version, name and status (up or down)
Your can also find the migration file, which contain a timestamp in the filename, that is the version number. Migrations are located in folder: /db/migrate
try {
$result=DB::table('users')->whereExists(function ($Query){
$Query->where('id','<','14162756');
$Query->whereBetween('password',[14162756,48384486]);
$Query->whereIn('id',[3,8,12]);
});
}catch (\Exception $error){
Log::error($error);
DB::rollBack(1);
return redirect()->route('bye');
}
If I have two migrations, mig1 and mig2, I run rake db:migrate, then I go back to mig1 and change the default value of a column, will this change be reflected when I run rake db:migrate again? Or do I have to make a new migration just for that column to make the change?
You can redo a given VERSION by running the following:
rake db:migrate:down VERSION=___________
rake db:migrate:up VERSION=____________
You should either make a new migration or use the rake db:rollback task to move back to the version of your database before the migration in question was ran. Changes to migration scripts won't be automatically picked up.
The current version of your schema is tracked and applied to migrations, so running rake db:migrate will not rerun old migrations. It is for this reason that you are able to use the rollback feature, as long as you provided correct self.down methods on your migration. Rolling back executes these down methods, undoing the migrations as it goes.
You can then edit the migration and re-migrate.
rake db:migrate:redo VERSION=____