Switching Branches causes database does not exist error when testing - ruby-on-rails

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!

Related

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!
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

Postgres db not working on Heroku

I've spend several hours figuring out how to get my database up and running. I created a new rails app and wanted to deploy it to heroku. I followed the instructions from heroku (to switch from sqlite3 -> postgresql) but it just doesn't work.
This is in my database.yml file:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
url: <%= ENV['DATABASE_URL'] %>
I can't create or seed any data in the database. Sometimes it executes the db:migrate, but even then it doesn't create anything. This is what I get when running:
heroku run rake db:create
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Does anyone has an idea on how to solve this? I don't have a clue anymore ...
Thanks!
By default you don't need to create a db on heroku.
Just run heroku run rails db:migrate and rest of the stuff will be handled by heroku itself.
Also your database.yml should be changed to following for Production env.
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Also your rails app is by default assigned with a Postgres addon.
You can check this by running command heroku addons in the console.
Source -
Heroku Getting Started Guide
Heroku Postgres Addon Guide
You cannot create a database on Heroku using db:create (you cannot drop it neither). Your database is created when you add an add-on (such as Heroku Postgres). You can only migrate and seed. And if you want to start over, you can use pg:reset (instead of drop and create)
So the correct sequence should be:
add the Heroku add-on (such as Heroku Postgres). Add-ons are located here: https://elements.heroku.com/addons.
rake db:migrate
rake db:seed
if you want to start over
rake pg:reset
rake db:migrate
rake db:seed
From Heroku documentation: https://devcenter.heroku.com/articles/heroku-postgresql
The PostgreSQL user your database is assigned doesn’t have permission to create or drop databases. To drop and recreate your database use pg:reset.
As per the given stacktrace, it seems like you are trying to create a database on heroku which in turn is giving you Permission Denied Error.
Firstly, you do not need to run
heroku run rake db:create
Instead run
heroku run rake db:migrate
and it should migrate the migrations which are down.
For checking the current status of migrations run the following command:
heroku run rake db:migrate:status
Other Point you mentioned:
-> I can't create or seed any data in the database
As already mentioned above you can't create a database as heroku does it for you .
For seeding data in the database run the following command:
heroku run rake db:seed

fe_sendauth: no password supplied error after setting up PostgreSQL database on Cloud9 IDE

So I've set up a PostgreSQL database on my cloud9 IDE account according to the instructions of the top voted comment: Cloud9 postgres.
This is what my database.yml file looks like:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>
host: <%= ENV['IP'] %>
development:
<<: *default
database: sample_app_development
test:
<<: *default
database: sample_app_test
production:
<<: *default
database: sample_app_production
I then created a user model, migrated it, and reloaded my app. However when I loaded the app on the local server using this command: rails server -b $IP -p $PORT on the cloud9 command line, I get the following error message: fe_sendauth: no password supplied error. I do not understand what is going wrong since I have created a superuser with a password and have included those in my database.yml file. The only thing I found was that this error can be fixed by changing the pg_hba.conf file to trust the local server. However I thought the whole point of creating a superuser and password in the Cloud9 IDE was so that you don't have to do this. Also Cloud9 doesn't give you access to the pg_hba.conf file.
You don't need to change the pg_hba.conf file.
Just type in these commands in the terminal:
$ source ~/.profile
$ rake db:create
$ rake db:migrate
Then restart the server normally.
I know you already ran the source ~/.profile command at the beginning of the instructions you mentioned (I have used the answer before and struggled with the same issue) but for some mysterious reason Cloud9 environment (I never had to to do any of this on my Mac) requires to manually source the '.profile' file again after doing the setup specified in those instructions (Cloud 9 just decides to be weird sometimes - like all machines... duh!). The second command creates the new pg database in your development environment with all the configuration from your pg gem and your database.yml file.
The final 'migrate' command might not be necessary but again for mysterious reasons I have also had database issues fixed after just running a 'rake db:migrate' (If I am correct it just updates your database to be able to use your Models or any new dummy data you might have added to your development environment).
Hope this helps.

Adding a production database to a Rails 4 site on Heroku

Well I've deployed my site on Heroku. When I navigate to the app page it shows a database listed on the Add ons section
I navigate ahead to the database details and this is what I get to see
This is my production database code
production:
adapter: mysql2
encoding: utf8
database: dfu332icuvq6bs
pool: 5
username: cekapynykihldm
password: b1XuQq9m1m8Ok3zeyABEgNiiz3
socket: /tmp/mysql.sock
My question is when I fill in the details from Heroku into the production database and push it bask to the server, the app doesnt talk to the database as none of my routes except the home#index works. How do I make my app talk to the database on Heroku.
From your comment, it appears that you need to run the database migrations and then restart your instance:
heroku run rake db:migrate
heroku ps:restart
Guess I missed the most important step. I was able to fix the problem by running
heroku run rake db:migrate

database.yml file configuration and postgres - rake db:drop db:create db:migrate

I recently switched to postgres for a rails app in development since I deployed on Heroku and want to match production. Postgres is installed and working correctly for my application when I set the database name equal to 'postgres', which is the default name from what I understand.
However, I would like to change the database name for development, test and production. If I simply change the names in database.yml, and try to run a rake task, I get the following error in my terminal:
NOTICE: database "something_development" does not exist, skipping
Couldn't drop something_test : #<PG::Error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?>
rake aborted!
FATAL: database "something_development" does not exist
Here is what my database.yml file currently looks:
development:
adapter: postgresql
host: localhost
database: something_development
username: name
test:
adapter: postgresql
database: something_test
pool: 5
timeout: 5000
production:
adapter: postgresql
host: localhost
username: name
database: something_production
Are there some other steps I am missing? Apologies if I am missing something obvious here! Thanks!
Changing the name in the database.yml file only changes the place that it looks for the database, it does not actually rename the database itself. If you change the specified location, you will need to rename the database to match, using a GUI tool (like navicat or pgadmin) or the command line.
Also, it is probably an obfuscation omission, but your posted output refers to both shredset_development and something_development. If this continues to fail, perhaps verify that all of your names are the same everywhere...

Resources