Rails db:seed unknown attribute - sql

Here is a question.
I have an Rails project. When I want to clear my database and fill it with some test data I run:
rake db:drop db:create db:migrate db:seed
and I have an error:
NoMethodError: undefined method login for #<User:0x007fecf46afe80>
When I run separately:
rake db:drop db:create db:migrate
rake db:seed
all goes fine.
Also all my actions in db/seeds.rb are wrapped in ActiveRecord::Base.transaction block.
I had to add User.reset_column_information in the top of my db/seeds.rb to make
rake db:drop db:create db:migrate db:seed
working.
I didn't have the same errors before without reset_column_information. Does somebody have any ideas why it's happen?
PS: after running rake db:drop db:create db:migrate there is "missing" column in db/schema.rb and I can see this column in DB directly

From the docs:
reset_column_information() public
Resets all the cached information
about columns, which will cause them to be reloaded on the next
request.
The most common usage pattern for this method is probably in a
migration, when just after creating a table you want to populate it
with some default values
You're db:migrate task changes the column information in the users table, but it looks like these changes are not properly written to the db/schema.rb file until the rake cammand finishes. Your db:seed task looks at db/schema.rb to see if the columns it needs exists, but this shows the schema as it was before the db:migrate unless you put it in a separate rake command or you run reset_column_information() before it.

Related

How to check SQL commands executed using rake in Ruby on Rails

I want to count the votes a user has given to an article and save it somewhere.
I want to check all the SQL INSERT or CREATE lines executed when we do something like:
>$ bundle exec rake db:reset
>$ bundle exec rake db:seed
>$ bundle exec rake test:prepare
Is there a way I can check the SQL commands in Ruby on Rails?
You can add a custom Rake task and use it whenever you need to log the SQL output:
task log: :environment do
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
Now you can run:
bundle exec rake log db:reset
bundle exec rake log db:seed
bundle exec rake log test:prepare
See "Is it possible to output the SQL change scripts that 'rake db:migrate' produces?"

Ruby on rails rake db:create already exists

I am trying to create a database using:
rake db:create
That output a error: db already exists.
When I do a
rake db:drop
The output error is: db does not exist.
I have use external to connect to the DB server and indeed the DB does not exist.
Any help is much appreciated.

I get a "database does not exist error" when running unit tests

Every time I run the command:
rake test test/models/post_test.rb test_the_truth
I get the following error:
`initialize': FATAL: database "knome_test" does not exist (PG::Error).
Can someone explain and help me to debug it?
It sounds like you have not created the database.
Try running the following to create, migrate, then prepare your test database.
rake db:create
rake db:migrate
rake db:test:prepare
You have create a new database in postgres and update database.yml "test" section with that db_name, username & password. The same way you have done in "development" section.

Rails 3, rake db:test:prepare doesn't work when using a postgres Schema

I have a Rails 3.2.1 project that I'm trying to run my specs on.
When I run:
rake
rake aborted! PG::Error: ERROR: invalid value for parameter
"search_path": "example" DETAIL: schema "example" does not exist :
SET search_path TO example
From what I can work out, rake db:test:prepare drops the test database and then attempts to recreate it from the schema.rb. The thing is, database.yml has the line
schema_search_path: example
so when it tries to reconnect, it fails with the above error.
I think my question is, how can I get rake db:test:prepare to setup the schema_path too?
One solution is to add a bit of code to the db:create task that creates the schema. Put this in a rake task, for exmaple 'APP_ROOT/lib/tasks/db_create_schema.rake'
namespace :db do
task :create do
config = Rails.configuration.database_configuration[Rails.env].merge!({'schema_search_path' => 'public'})
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.execute("CREATE SCHEMA schema_name")
end
end
https://gist.github.com/4280172
You should set the schema_search_path in your config/database.yml
e.g.
development:
adapter: postgresql
encoding: unicode
database: test
pool: 5
username: user
password: password
#host: localhost
#port: 5432
# Schema search path. The server defaults to $user,public
schema_search_path: example

How to add 'rake test' to project

When I ran rake -T, I discovered rake test was missing. What do I need to do to get this task in there? Specifically, I want to run rake test:benchmark but that doesn't seem to be loaded. For example...
$rake test:benchmark
rake aborted!
Don't know how to build task 'test:benchmark'
My config/application.rb file was missing this line:
require 'rails/all'
You only need:
require "rails/test_unit/railtie"
in your config/application.rb