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!
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
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
Related
I recently set up postgresql in a new workspace. I set it up the same way I usually do. Or at least I thought I did. But then I got this error
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
ActiveRecord::NoDatabaseError:
FATAL: database "app_test" does not exist
It only happens when I change to a branch and run rspec. So far I've just been creating the database and running migrations each time I switch between branches as a solution.
It doesn't impact the development database. And as long as I remain on that branch I don't have to add the test database again. But if I have to leave it for any reason, I'll have to add the database again.
It seems to impact any branch that isn't the master. And this only occurs locally. I am using Cloud 9 and my app is a Rails app. Should I uninstall and reinstall postgresql, then set it up again?
Recently I've had to work on a few branches at the same time, so it's becoming a hassle. I'm tempted to just make a new workspace.
here is my database.yml(with some things hidden of course, app is a stand in for my app's name).
default: &default
adapter: postgresql
encoding: unicode
host: localhost
username: postgres
password: secret
pool: 5
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
Thank you in advance
You should be able to reset your databases by running:
rake db:drop db:create db:migrate db:test:prepare
That'll delete your dev database (along with any data in it), recreate it, rebuild the schema from your migration files, and rebuild the test database. If it doesn't work at that point you may be a permissions issue
I faced with similar issue.
Just try to change this line of database.yml
host: 127.0.0.1
when i provided localhost it used default settings for connection. (i have many DB server version on different ports)
First I deleted my local branches to start clean (it may not be necessary but as most of them altered the schema I thought it'd be cleaner). My changes where all stored remotely so this was wasn't a big deal.
Then I added template: template0 to my database.yml in the development and test sections. I followed this suggestion Fix for PG::Error: ERROR: new encoding (UTF8) is incompatible
because I had this error:
"PG::InvalidParameterValue: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
HINT: Use the same encoding as in the template database, or use template0 as template."
whenever I would try to use rake db:create
After that I followed NM Pennypacker's guidance and did, rake db:drop, rake db:create, rake db:migrate and rake db:test:prepare.
I've made some branches, pulled in the remote changes and run rspec with no problem. Thank you everyone for your advice!
I have created a new rails project and I have added a migration to create a table called blogs. When I ran rake db:migrate it created this table along with other tables I have set in different projects.
Under db/migrate I have only 1 file with the migration I added, there's no reference in the project of the other tables, for some reason it is pulling the migrations from other projects and adding it to my DB project
Rails version:
5.1.4
Ruby version:
2.4.1
Probably you're using the same database. You need to specify different database in the config\database.yml file for each project. You can have same username, same password, etc. but At least the database: (database name) have to be different if you don't want all your tables in one database
You need to configure your database.yml for each project.
And create 3 databases for development, test and production.
And specify all the information needed to access your database.
Eg:
development:
adapter: postgresql
username: postgres
password: blogs_postgres
database: blogs_development
encoding: utf8
host: localhost
pool: 5
timeout: 5000
Refer Here for more info.
I just solved my issue, seems like Spring was mixing things up in my environment.
Reference: https://github.com/rails/rails/issues/31529#issuecomment-353269787
I deleted a migration file I didnt need anymore (deleted, not rollback). Then tried to run migrations and my rake tasks stopped doing anything altogether. What ended up fixing my problem (or so I thought) was adding another migration which ended up working for running rake db:migrate for the latest migration. Then after the latest schema was set my rake tasks stopped working again altogether.
I am using rails 5. I tried using the new rails command for db tasks, but that didnt work.
I know I'm not showing code here, but its kind of difficult for this problem. I included a screenshot of what happens when I run tasks for my app. Youll see various ruby warnings because I should upgrade my version, but those are not related.
Has anyone faced this problem before or have any ideas on how to solve it?
I have had this before, We don't have enough information to give a perfect answer, so I have given you a few suggestions that have worked for me.
before you try anything drastic, run rails db:reset.
Hopefully that works, if not hopefully you are using git or svn. Go back to when it worked, then don't delete the migration file. Create a new migration that drops the table you don't need anymore.
to do this run 'rails g migration drop_TABLE_NAME' that will make a new migration file, you can drop a table like this below.
def change
drop_table :TABLENAME
end
If you aren't using git, I would try editing db/schema.rb.
I had my database in my db.yml set to default. I had to declare actual database names and then run migrations in order to get it to work.
For me my db.yml now looks like this:
default: &default
adapter: postgresql
template: template0
database: job-board-development
pool: 5
timeout: 5000
While previously it looked like this:
default: &default
adapter: postgresql
template: template0
pool: 5
timeout: 5000
I am very new to heroku/ruby on rails and git. I just went through Michael Hartl's Ruby on Rails tutorial and want to push my local database to heroku but am having trouble.
After doing some research I found this article:
pg transfer is the new taps
It seems like it should work, but I do not understand how to set my env var DATABASE_URL:
$ env DATABASE_URL=postgres://localhost/someapp-dev heroku pg:transfer
Specifically I have no idea what I am supposed to directly copy and what to change. I believe I need to enter my own local host and my own database name.
Is this correct? If so how do I find my localhost and how do I find my database name?
My database.yml file looks like this:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
my understanding - you must use a Postgresql database on Heroku. When I pushed an application to Heroku I changed the databases to Postgresq al part of the push - including in Dev. Looking at the link you provided I get the impression it means the database you are copying from is also Postgresql (as opposed to SQLite).
We had a developer developed a website under his local Mac, and the developer just transferred the zip file of rails site to me, but I am using Windows.
Could anyone help me over the steps on how to change from his database to my local postgresql database ? Sorry I am new at this.
So far:
1) I've edited the database.yml file, host is localhost port 5432 and database name is data1, so I went into pGAdminII and create the same database name.
2) I've created a super user and supplied the credential to database.yml.
Is that all I need to do ? Do I need to setup anything else ? thanks!
You'll need to change the adapter in the config/database.yaml file from whatever it was to postgres, something along the lines of:
development:
adapter: postgresql
database: data1
host: localhost
username: someone
password: something
You will also need to ensure you have the relevant gem in your gemfile.
gem 'pg'
That should just about do the trick. Run your rake db:version to test the connection, if all seems well go for a rake db:migrate. Post any errors you get.