Postgres 9.3, Ruby 2.1.0
rake db:create isn't making a test database. I already have a production database. I tried using RAILS_ENV=test rake db:create to force it but it returns "test database is not configured.
My database.yml ->
development:
adapter: postgresql
database: app_prod
host: localhost
test: &test
adapter: postgresql
database: app_test
host: localhost
cucumber:
<<: *test
production:
adapter: postgresql
database: app_prod
host: localhost
So it is configured. I also tried just using a console createdb app_test to create my test database but I receive the same error when I try to run rake db:test:prepare.
Anyone have any ideas?
this is --trace on db:create:all
** Invoke db:create:all (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:create:all
rake aborted!
undefined method `[]' for nil:NilClass
/Users/username/.rvm/rubies/ruby-2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.3/lib/active_record/tasks/database_tasks.rb:189:in `block in each_local_configuration'
this is trace on db:test:prepare
** Invoke db:test:prepare (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config
** Execute db:test:purge
rake aborted!
undefined method `[]' for nil:NilClass
/Users/username/.rvm/rubies/ruby-2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.3/lib/active_record/tasks/database_tasks.rb:137:in `purge'
Try running this in console
ActiveRecord::Base.configurations
You should get your database configurations.
Line 3 in this method(Line 189 in github) is failing in your case because configuration is nil
def each_local_configuration
ActiveRecord::Base.configurations.each_value do |configuration|
next unless configuration['database']
if local_database?(configuration)
yield configuration
else
$stderr.puts "This task only modifies local databases. #{configuration['database']} is on a remote host."
end
end
end
Related
When I rollback migrations in rails I get the error:
NoMethodError: undefined method `trim' for LL():Rake::Scope::EmptyScope
The migration goes through but it prevents me from running multiple migrations as it crashes after rollbacking.
Here is stack trace:
>> rails db:rollback ^2 --trace
** Invoke db:rollback (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:load_config
** Execute multiverse:load_config
** Execute db:rollback
== 20201007084505 CreateValuation: reverting ==================================
-- drop_table(:valuations)
-> 0.0030s
== 20201007084505 CreateValuation: reverted (0.0080s) =========================
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke db:load_config
** Execute db:schema:dump
rails aborted!
NoMethodError: undefined method `trim' for LL():Rake::Scope::EmptyScope
Any ideas? I don't even know where to start.
EDIT:
Migrations code per request.
class CreateValuation < ActiveRecord::Migration[5.2]
def change
create_table :valuations do |t|
t.integer :car_id, null: false
t.integer :value
t.datetime :date, null: false
t.timestamps
end
end
end
When I making migration db Cassandra with command:
es#es:~/server-new$ rake ks2:migrate_old
After this I get error:
rake aborted!
SystemStackError: stack level too deep
Tasks: TOP => ks2:migrate_old => ks2:set_keyspace2 => ks2:configure2 => environment
(See full trace by running task with --trace)
When I start command: rake ks2:migrate_old --trace
I get error:
** Invoke ks2:migrate_old (first_time)
** Invoke ks2:set_keyspace2 (first_time)
** Invoke ks2:configure2 (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
SystemStackError: stack level too deep
/home/es/.rvm/gems/ruby-2.0.0-p353/gems/rake-10.4.2/lib/rake/task.rb:183
Tasks: TOP => ks2:migrate_old => ks2:set_keyspace2 => ks2:configure2 => environment
How to make migration work?
The way I set up environment variables is like it was outlined here.
In other words I created a config/app_env_vars.rb file and put:
unless Rails.env.production?
ENV['DB_PASSWORD'] = 'password'
ENV['DB_USERNAME'] = 'username'
end
puts 'ECHO app_env_vars.rb'
In config/environment.rb I put:
app_env_vars = File.join(Rails.root, 'config', 'app_env_vars.rb')
load(app_env_vars) if File.exists?(app_env_vars)
puts "ECHO environment.rb"
right before Rails.application.initialize!
In database.yml:
development:
adapter: postgresql
encoding: unicode
database: my_app_development
host: localhost
pool: 5
password: <%= ENV['DB_PASSWORD'] %>
test:
adapter: postgresql
encoding: unicode
database: my_app_test
host: localhost
pool: 5
password: <%= ENV['DB_PASSWORD'] %>
When I run tasks such as bundle exec rake db:migrate and bundle exec rake db:reset it works fine but when I run bundle exec rake db:migrate:reset it will fail displaying:
fe_sendauth: no password supplied
.
.
.
Couldn't drop my_app_development
fe_sendauth: no password supplied
.
.
.
Couldn't drop my_app_test
fe_sendauth: no password supplied
ECHO app_env_vars.rb
ECHO environment.rb
Usually when I run bundle exec rake db:migrate (and db:drop) it will first display the ECHO parts and then do the task and out put the log. But in the case of the db:migrate:reset it won't initialize the environment variables until after the task.
When in database.yml I put the actual passwords rather than the env variables then the
db:migrate:reset works with no problem.
Here is rake db:migrate:reset with --trace option
** Invoke db:migrate:reset (first_time)
** Invoke db:drop (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:drop
fe_sendauth: no password supplied
.
. # gem trace stuff...
.
Couldn't drop my_app_development
fe_sendauth: no password supplied
.
. # gem trace stuff...
.
Couldn't drop my_app_test
** Invoke db:create (first_time)
** Invoke db:load_config
** Execute db:create
fe_sendauth: no password supplied
.
. # gem trace stuff...
.
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"my_app_development", "host"=>"localhost", "pool"=>5, "password"=>nil}
fe_sendauth: no password supplied
.
. # gem trace stuff...
.
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"my_app_test", "host"=>"localhost", "pool"=>5, "password"=>nil}
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
ECHO app_env_vars.rb
ECHO environment.rb
** Invoke db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump
** Execute db:migrate:reset
Here is the --trace for rake db:reset
** Invoke db:reset (first_time)
** Invoke environment (first_time)
** Execute environment
ECHO app_env_vars.rb
ECHO environment.rb
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:reset
** Invoke db:drop (first_time)
** Invoke db:load_config
** Execute db:drop
** Invoke db:setup (first_time)
** Invoke db:schema:load_if_ruby (first_time)
** Invoke db:create (first_time)
** Invoke db:load_config
** Execute db:create
** Invoke environment
** Execute db:schema:load_if_ruby
** Invoke db:schema:load (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:load
-- enable_extension("plpgsql")
-> 0.0772s
-- create_table("users", {:force=>true})
-> 0.0795s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true, :using=>:btree})
-> 0.0406s
-- initialize_schema_migrations_table()
-> 0.0829s
** Invoke db:structure:load_if_sql (first_time)
** Invoke db:create
** Invoke environment
** Execute db:structure:load_if_sql
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Execute db:abort_if_pending_migrations
** Execute db:setup
In the case of db:reset the ** Execute environment part is done before any executions, while with db:migrate:reset the ** Execute environment is done after a few executions
The reason why this setup (setting env variables in a file then load them trough environment.rb) doesn't work is because active record can be used outside rails therefore now every task runs with initialized rails environment.
A better solution is to use gems like figaro or dot-env which will load with every rake task. How exactly is that done with those gems I am not sure but if I get into it and find out I'll post here.
My Capistrano deployment does not set the RAILS_ENV variable when running the bundle command. I don't understand why.
My Gemfile is :
source 'https://rubygems.org'
ruby '2.1.3'
gem 'capistrano', '~> 3.2.1'
gem 'capistrano-rails', '~> 1.1.1'
My deploy.rb :
set :stage, :production
set :rails_env, 'production'
set :bundle_flags, '--deployment'
set :bundle_env_variables, { rails_env: "production" }
namespace :sphinx do
desc "Index Sphinx"
task :index do
on roles(:app) do
within release_path do
execute :rake, "rake ts:index"
end
end
end
When running :
➜ ansible-sharetribe git:(master) ✗ bin/cap production sphinx:index --trace
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke bundler:map_bins (first_time)
** Execute bundler:map_bins
** Invoke deploy:set_rails_env (first_time)
** Execute deploy:set_rails_env
** Invoke deploy:set_rails_env
** Invoke sphinx:index (first_time)
** Execute sphinx:index
INFO[3ca70ab5] Running bundle exec rake rake ts:index on www.myapplication.com
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host www.myapplication.com: rake exit status: 1
rake stdout: Nothing written
rake stderr: Digest::Digest is deprecated; use Digest
rake aborted!
NameError: uninitialized constant Annotate
If I write :
execute :rake, "rake ts:index RAILS_ENV=production"
it works. Why is RAILS_ENV=production not set automatically?
You can use this syntax in your task
execute :rake, "rake ts:index", "RAILS_ENV=#{fetch :rails_env}"
I have the same problem.
I ran rake secret and got "NameError: uninitialized constant Annotate" error.
After i set RAILS_ENV=production, it worked like charm.
I dont know why RAILS_ENV wasnt set but BTW thanks so much.
Saved me a lot of time
I was hoping to run some unit tests but this is what I am getting. The internets are silent on the issue. Rails 2.1
Let me know if there is any other info that would help.
mike#sleepycat:~/projects/myapp$ rake test:units --trace
(in /home/mike/projects/myapp)
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:clone (first_time)
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
rake aborted!
Mysql::Error: Incorrect database name '': DROP DATABASE IF EXISTS ``
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:147:in `log'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:299:in `execute'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:384:in `drop_database'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:364:in `recreate_database'
/usr/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/tasks/databases.rake:315
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
My database.yml:
# MySQL (default setup). Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql
# On MacOS X:
# gem install mysql -- --include=/usr/local/lib
# On Windows:
# gem install mysql
# Choose the win32 build.
# Install MySQL and put its /bin directory on your path.
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql
database: myapp_development
username: root
password: mikespass
host: 127.0.0.1
#socket: /tmp/mysql.sock
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
adapter: mysql
database: myapp_test
username: root
password: mikespass
host: 127.0.0.1
production:
adapter: mysql
database: myapp_production
username: root
password:
mikes:
adapter: mysql
database: myapp_development
username: root
password: mikespass
host: 127.0.0.1
staging:
adapter: mysql
database: myapp_development
username: root
password: mikespass
host: 127.0.0.1
It looks like you haven't specified a database name for the test environment in your config/database.yml file.
cant end lines in comments so i'll just put it here
can you run script/console then do :
#a = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml"))
#a
then paste the contents of #a