I have created a migration file for creating a table UserDetails in my db.. and it was working fine b4...
Unknowingly I deleted that table to do some modification .. But now when i gave
rake db:migrate .. its not creating the table ..
I have the migration file .. BUt when i give rake db:migrate its not creating.. How to do so ??
Rails maintains a 'schema_migrations' table to know what migrations have already run and will not run the migrations that have already been completed. Since, you had already run the migration once, it would have updated the 'schema_migrations' table with the version no. of the migration. You can do rake db:rollback to revert the last migration run or rake db:migrate:down VERSION=<version_number> to revert any migration that was run before.
Now, I suggest if you know the version no (Time stamp of the file), you can manually delete from 'schema_migrations' table and re-run db:migrate.
Rake stores the time of the most recent migration inside the database - if the migration you're trying to run is older than that, it won't run with rake db:migrate.
I don't know how to run a specific migration manually, sorry :/
Could you just run that specific migration as stated in the Guides. http://guides.rubyonrails.org/migrations.html#being-specific
I have run specific migrations by their timestamp before. It seems like it would work in this situation also.
Related
I initially ran a migration when I created the database and everything worked fine. Then I went and deleted the table in postgresql manually. Now when I run rake db:migrate, it runs but doesn't create the table.
It depends on what your migrations do. rake db:migrate can create or change tables, depending on your definitions. To setup the database and the tables in the beginning you can use rake db:schema:load or rake db:setup. rake -T gives an overview over all available rake tasks.
Database migrations are stored in the table schema_migrations which has one column version. As long as the version of the migration in question can be found here, the migration is not executed again.
I'm currently following the ruby on rails tutorial: http://guides.rubyonrails.org/getting_started.html.
I am trying to save data into the database. However, when I run: rails server I get the following error:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
I've looked at the other articles and when I run:
bin/rake db:migrate
I get a rake aborted!
After running:
rake db:abort_if_pending_migrations....
I see that:
You have 1 pending migration:
20150805200129 CreateDatabases
SQLite3::SQLException: table "databases" already exists:
and it just tells me to run rake db:migrate to start again.
It seems that it already exists. Is there a way to cancel the pending migration?
Sometimes, even dropping a local development database is not a good idea.
There are better ways to delete/destroy a specific migration in your Rails application.
You could use rails d migration command to destroy a particular migration:
rails d migration MigrationName
To undo the changes corresponding to a particular migration, you can use db:migrate:down method like this:
rake db:migrate:down VERSION=XXX
Sometimes, things could get more messy and in those situation another handy thing is to take a look at the schema_migrations table in your database which has all the migrations with their version saved in it.
You can delete a particular migration from this table like this:
delete from schema_migrations WHERE version = VERSION;
if you don't want that migration to be present anymore.
Your migration may have failed midway (so it created the table, but didn't finish).
You are just using development environment, so it's okay to just drop the database and rebuild it from scratch:
rake db:drop # THIS WILL DELETE YOUR DATABASE
rake db:create
rake db:migrate
If you are like me and maintain your database structure outside of Rails, you can just delete the migration file from db/migration. I got the error in the OP's question when I used the rails generate command to create a model class, forgetting that it also creates a migration file.
Do not use this method if you rely on Rails to maintain your database structure!
I keep my Rails structure file up to date by building it from the database using:
bundle exec rake db:structure:dump
I do not encourage to drop the database and start from the beginning especially when you already have the data inside the database.
My approach to this will be migrate first, then rollback. After that you can safely delete the migration file. So the procedure is as following.
rails db:migrate
rails db rollback
rm db/migrate/your_last_migration_file.rb
You can recreate database and run all migrations in your development environment with such command
rails db:migrate:reset
If you want to revert the wrong migrations, You can drop the whole db using this:
rake db:drop
Then remove the migrations file manually(This wont corrupt the db when you recreate as the Schema migrations would be dropped as well).
Then run
rake db:migrate
And if there is data to be seeded, then run this as well
rake db:setup
when I new a migration and run it,error occurred:
$ rake db:migrate
== CreateEReadings: migrating ================================================
-- create_table(:e_readings) rake aborted! An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "e_readings" already exists
while this e_readings is the last table I created using migration.
The migration file name is :20120508145115_create_e_readings.rb
and the version in db/schema.rb is :
:version => 20120508145115
Seems that rails forget that I already have run this migration and try to re-play it, so error occurred,but why is this happening and how can I solve this?
It seems like you may have run it before and it failed after creating the table for some reason. If you're sure it has already run, you can manually add a record to the "schema_migrations" table with the 20120508145115 as the version.
If this is just a dev environment and you don't mind blowing it away, you could also run rake db:reset and that would drop, create, load the schema and reseed it.
I think beerlington is right. Your migration probably failed the first time you ran it. In addition to his suggestions you could also try manually dropping the table from the database and re-running the migration to see what went wrong the first time
I agree with both Beerlington and Andy. If it's a development environment, try the following in the terminal:
rake db:drop
rake db:create
rake db:migrate
That will destroy your database, recreate it and run all your migrations.
The other thing you could try is to rollback using rake db:rollback until you see that this migration (or the one before it) has been rolled back, and then run rake db:migrate to run from that point till your latest point again. rake db:rollback rolls back one migration file at a time.
I'd go with the drop and recreate, though, just to make sure nothing funny has remained.
Hope this helps.
Recently, after I deleted some manually created migrations that were named 99999999xxx_createwhatever, each migration I generate now start with 99999999999999xxx_etc
Any idea how to fix this so that they are generated like 2011xxxxxx again?
If you want to keep your data in database, use mysqldump to backup first.
Then reset your migration to version 0 rake db:migrate VERSION=0
Make sure there isn't any 99999999x migration file, then run rake db:migrate
Finally, restore your database.
I would like to remove/delete a migration file. How would I go about doing that? I know there are similar questions on here but as an update, is there a better way than doing script/destroy?
Also, should I do a db:reset or db:drop if I remove/delete a migration?
I usually:
Perform a rake db:migrate VERSION=XXX on all environments, to the version before the one I want to delete.
Delete the migration file manually.
If there are pending migrations (i.e., the migration I removed was not the last one), I just perform a new rake db:migrate again.
If your application is already on production or staging, it's safer to just write another migration that destroys your table or columns.
Another great reference for migrations is: http://guides.rubyonrails.org/migrations.html
Another way to delete the migration:
$ rails d migration SameMigrationNameAsUsedToGenerate
Use it before rake db:migrate is executed because changes in database will stay forever :) - or remove changes in Database manually
Run below commands from app's home directory:
rake db:migrate:down VERSION="20140311142212" (here version is the timestamp prepended by rails when migration was created. This action will revert DB changes due to this migration)
Run "rails destroy migration migration_name" (migration_name is the one use chose while creating migration. Remove "timestamp_" from your migration file name to get it)
None of these answers quite fit the problem i had as the migration i wanted to delete was missing:
I had created and run a migration in some other branch, which was then discarded. The problem is when a migration is run, rails adds the version into a schema_migrations table in the database. So even if it isn't listed in your db structure or schema, rails looks for it.
You can reveal these orphaned migrations by running:
rails db:migrate:status
Note the versions of the missing migrations and head into the db console:
rails dbconsole
Now remove the versions from the migration table manually:
delete from schema_migrations where version='<version>';
You should now be good.
You can also run a down migration like so:
rake db:migrate:down VERSION=versionnumber
Refer to the Ruby on Rails guide on migrations for more info.
We can use,
$ rails d migration table_name
Which will delete the migration.
Sometimes I found myself deleting the migration file and then deleting the corresponding entry on the table schema_migrations from the database. Not pretty but it works.
This also works in Rails 5.
If the migration was the most recent one you can remove the database column(s) that the migration added by doing:
rake db:rollback
then remove the migration file itself by running:
rails d migration WhateverYourMigrationWasNamed.rb
Look at 4.1 Rolling Back
http://guides.rubyonrails.org/migrations.html
$ rake db:rollback
I just had this same problem:
rails d migration fuu
-this deleted the migration with the last timestamp
rails d migration fuu
-this deleted the other migration
use git status to check that is not on the untracked files anymore
rails g migration fuu
That fixed it for me
Side Note:
Starting at rails 5.0.0
rake has been changed to rails
So perform the following
rails db:migrate VERSION=0