sciffany
sciffany

Reputation: 420

How to switch Rails database without having to do migrations?

I have a Rails app which uses db my_database_development in my config/database.yml:

development:
  <<: *default
  database: my_database_development

Works correctly when I run rails server.

Now I want to use another db, so I change my config/database.yml:

development:
  <<: *default
  database: my_prev_database

Now when I run rails server, they give me an ActiveRecord::PendingMigrationError. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development. When I run that command, my_prev_database gets cleared. I don't want that to happen. I want to use my_prev_database and all the data it has (which I backed up from somewhere)

How can I switch database in Rails effectively?

Thank you!

Upvotes: 0

Views: 708

Answers (2)

B&#249;i Nhật Duy
B&#249;i Nhật Duy

Reputation: 322

When you switch database, you will have new schema_migrations table. In new database, schema_migrations is empty so Rails will think you have pending_migration. I think you need to re-migrate in your new database. You can use some feature like database dump to migrate date from old database to new database

Upvotes: 1

sciffany
sciffany

Reputation: 420

I am now unable to replicate the problem above. (Not sure why, maybe I was copying over my database incorrectly.)

Also, a possible resolution was to copy tables over individually from my_prev_database to my_database_development

Note for anyone troubleshooting similar problems: The commenters mentioned that, - Running rails db:migrate shouldn't delete database data

Upvotes: 0

Related Questions