Rake db:reset execute in development and test environment - ruby-on-rails

I got a Ruby on Rails v5 application and I'm trying to run the following Rake task in development environment:
rake db:reset
Sadly, Rake runs the task in both development and test environment. Since I don't use the test environment, I don't wanna double my database. I have read somewhere that Rake run task in both environment when there is no RAILS_ENV defined. So I try to add the following line to my .bash_profile without any success:
export RAILS_ENV="development"
I also tried to add RAILS_ENV=development at the end of my task but it also did not worked.
Is there a way to use Rake in development only ?
Update #1
Thanks to Taryn East for the quick comment. I'm trying to update my post as quick as possible to make it easy for you to answer efficiently.
What do you actually observe when you run that command?
The command are simply executed twice. Since I have different database for my development and test environment, it does not show me any error at the moment. On the other hand, it does force me to use two database for no reason at all. I also tried to set the same database for the two environments, but then I was getting error since Rake tried to run the task twice on the database.
eg of output for rake db:drop
Dropped database 'db'
Database 'db' does not exist
It did drop my database, but as you can see Rake tried to run the command another time for the other environment.
Why aren't you using the test environment?
Because I don't see any advantage of it for my current project. I don't have time to create and update tests. I'm also not used to use both development and test environment at the same time.
If you want to run a rake task in just a single environment, you can also add RAILS_ENV to the command line.
As I said in my post, it does not work. Here's what I did:
RAILS_ENV=development rake db:drop
And here's the output:
Dropped database 'db'
Database 'db' does not exist
Update #2
Here's my database.yml configuration:
default: &default
adapter:mysql2
pool: 5
timeout: 5000
development:
<<: *default
database: db
username: something
password: something
host: localhost
port: 3306
test:
<<: *default
database: db_test
username: something
password: something
host: localhost
port: 3306
I don't know if it can help, but my environment is development according to the rake about task.

I have read somewhere that Rake run task in both environment when
there is no RAILS_ENV defined.
This is not correct. If the environment is development, test is also added to current environments. See: https://github.com/rails/rails/blob/f507085fa2a7625324e6f4e3ecef9b27dabefb4f/activerecord/lib/active_record/tasks/database_tasks.rb#L339
So this is expected behaviour.
On the other hand what you're saying will be true for any other env. E.g. RAILS_ENV=test rails db:drop will only execute for test env.

Related

Switching Branches causes database does not exist error when testing

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!

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

Rake unconsistent behaviour between dev and test?

I develop a RoR application using a PostgreSQL database, based on this database.yml definition:
# PostGre databases
default: &default
host : localhost
adapter: postgresql
encoding: unicode
pool: 5
username: keyman
password: keymanApp
schema_search_path: "keyman"
development:
<<: *default
database: keyman_dev
test:
<<: *default
database: keyman_test
I created a small Rake routine, so I can easily drop and create my postgreSQL database, including the schema I work with:
namespace :db do
desc 'Create database schemas before going for the first migration'
task init: ['db:drop','db:create'] do
ActiveRecord::Base.connection.execute("CREATE SCHEMA keyman AUTHORIZATION keyman")
puts 'Database initialised'
end
end
When I run rake db:init, it is executed both on dev and test environments:
$ rake db:init
Dropped database 'keyman_dev'
Dropped database 'keyman_test'
Created database 'keyman_dev'
Created database 'keyman_test'
Database initialised
But the result is not the same: the schema 'keyman' is created for the keyman_dev database, but not for the keyman_test database.
I need to run explicitly rake db:init RAILS_ENV=test to get the schema created on the test database.
It sounds strange to me! Do you have an explanation?
Thanks
when running bin/rake -T db we can see the following descriptions for db:create and db:drop
rake db:create # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to creating the development and test databases
rake db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to dropping the development and test databases
So, when you run those tasks in the development environment, they both do the development and test databases, but that doesn't mean that other tasks (such as your custom task) automatically run in both environments, you're task is still only running in the development environment.
Something like this in your rake task should get that query to run in both tables, though this is untested.
environments = Rails.env.development? ? [:development, :test] : [Rails.env.to_sym]
environments.each do |env|
ActiveRecord::Base.establish_connection(env)
# do something
end

why 'rake test' is trying to connect to my development DB?

I have my config/database.yml like this:
development:
adapter: postgresql
database: psql_dev
username: postgres
min_messages: WARNING
test:
adapter: sqlite3
database: db/test.sqlite3
min_messages: WARNING
When I run rake test:units, it reports an error:
rake aborted!
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Why didn't it connect to my test DB(db/test.sqlite3).
and, If I run the test like this rake test RAILS_ENV=test, it works well.
Isn't RAILS_ENV=test the default setting for rake test?
I'm running rails 2.3.5 with ruby 1.8.7, and my $RAILS_ENV is not defined in my shell.
What's happening is that rake test depends on rake db:test:prepare which will attempt to load the current schema from the development database. That's how the test database gets updated when a migration is run on the development database
do you have a test:units rake task? Run:
rake test
does that work? Also can you paste the output of:
rake -T | grep tests

database not created when run db:create with RAILS_ENV

I am using Rails v2.3. and MySQL v5.1
I created a new Rails environment named "special" by copy the config/environments/development.rb to config/environments/special.rb
Then, I defined the following thing in config/database.yml :
special:
adapter: mysql2
host: localhost
username: My_user_name
password: My_pwd
database: special_db
encoding: latin1
Then, I go to the command line to run the command:
$ RAILS_ENV=special rake db:create
also tried $rake db:create RAILS_ENV=special
I expect a new database named special_db should be created, but it isn't .
Why? Why I have created a new environment and run db:create in that environment, but database is not created? Am I missing something?
Have you created the database in MySQL?
mysqladmin create special_db
Are you sure that the username you are using have privileges to the database?
GRANT ALL PRIVILEGES ON special_db.* TO 'user'#'host'; FLUSH PRIVILEGES;
What is the output from the rake task? Have you tried running with the -t option? If so can you post the output?

Resources