I am having a lot of issues with rails migration mechanism.
I think I have run a migration file and has been executed partially.
So when I am trying to run
rake db:migrate
again it gives me an error the column name already exist.
I am trying to reset it with
rake db:reset
and gives me an error
Unknown database 'databasename'
Is there a way to reset the whole mechanism ?
Is it a good idea to manually drop all tables and try to run rake db:migrate again ?
You should change the database name in your database.yml.
Related
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
Getting a migration error when I try to migrate my database on Heroku. Found a solution on here offering this advice:
rake db:create
rake db:schema:load
rake db:migrate
but it hasn't made a difference. The error starts like this:
PG::UndefinedColumn: ERROR: column "property_id" of relation "bookings" does not exist
I don't have a property_id column anymore as this got changed in later migrations locally.
My migrations work locally by the way so why not on Heroku?
Could you post your Schema so I can see if you have a column "property_id".
If you do not then you will have to add this column property_id to the bookings class.
Also try
rake db:drop
rake db:migrate
what happens?
Did you migrate data to or from your heroku postgres database and your local one? It seems like the schema_migrations table is out of sync with reality on your heroku instance.
If you can lose all data, I would just start over. The following command will wipe your entire database, destroy all tables:
heroku pg:reset # destructive action, careful
After that, try heroku run rake db:migrate again; it should work.
If that does not work, then you'll probably have to manually inspect the schema_migrations table and make sure that the correct migrations have been applied and perhaps manually put it back in a consistent state.
This situation is quite abnormal and can only have happened by manually touching this data or schema. Because postgres supports transactional DDL, it cannot have happened via heroku run rake db:migrate alone, as in case of errors the whole migration rolls back and the database is left in a consistent state.
When I tried to switching from sqlite3 to postgresql in my existing application I faced this problem with rake db:migrate, I did the following
1 - rake db:create
2- rake db:migrate I got this error:
== AddColumn1: migrating ===================================================== -- add_column(:users, :first_name, :string) rake aborted! An error has occurred, this and all later migrations canceled: PG::Error: ERROR: relation "users" does not exist
3- rake db:reset
4- rake db:migrate , now migration is done with no errors
I have lost my data specially my admin user because of rake db:reset and my questions is :
1- why I forced to use rake db:reset ?
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
3- and for PostgreSQL I couldn’t use a blank password , it said fe_sendauth: no password supplied , after adding password this error is gone , using password in another database engine instead of Sqlite3 is a must ?
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?
So, what you are saying is you lost data in your sqlite3 database? This should not have happened. Did you change your database.yml in config folder to point your app to the new Postgres DB? If you did point it to the new DB, you should not have lost any data in sqlite3 DB since the app is not hitting it any more.
If you have indeed changed your db config to use the new db, you may not have lost any data. I hope this is the case. :)
1- why I forced to use rake db:reset ?
I don't think you had to run rake db:reset on your new DB, unless your migration files are messed up.
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
You can create a rake task or a script that points to two db instances (the old one and the new one you are migrating to). You can then pull data from the old db and then massage data as needed (each RDBM has their own minor syntactical differences) and the load them to the new db.
3- and for PostgreSQL I couldn’t use a blank password , it said fe_sendauth: no password supplied , after adding password this error is gone , using password in another database engine instead of Sqlite3 is a must ?
It depends how you set up your Postgres DB instance you created...
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?
Setting up your DB on Heroku would be very simple. All you have to do is the following:
heroku crate your-app-name
git push heroku master
heroku run db:migrate
But, I think you have some issues in your migration files. Nevertheless, you can actually get a username/password for your DB on Heroku and then use your script to push the data over to it from your local DB.
I hope this helps!
If you are using Devise, you can reload your user account information using:
rake db:seed
Take a look on this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgres so you can recreate the process manually by changing your database.yml and Gemfile.
If you need to migrate your database information run
pgloader ./production.sqlite3 postgres://username:password#localhost/database
Check https://migara.li/2017/07/24/migrating-from-sqlite-to-postgres/
Other alternatives like taps aren't working right now
http://railscasts.com/episodes/342-migrating-to-postgresql
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.