Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

rake db:test:prepare tries to load the structure.sql into development database #8032

Arsen7 opened this Issue Oct 26, 2012 · 13 comments


None yet
5 participants

Arsen7 commented Oct 26, 2012

The last revision of 3-2-stable branch - commit 7f09bcd - drops, creates the test database, but the structure is loaded into development one.

A snippet from PostgreSQL log:

[case mz_test] FATAL:  database "mz_test" does not exist
[case postgres] LOG:  duration: 7211.694 ms  statement: CREATE DATABASE "mz_test" ENCODING = 'unicode'
[case mz_devel] ERROR:  must be owner of extension plpgsql
[case mz_devel] STATEMENT:  COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
[case mz_devel] ERROR:  relation "cards" already exists
[case mz_devel] STATEMENT:  CREATE TABLE cards (

and so on..

I am looking for the exact commit somewhere between commit 45d78a3 and 7f09bcd where the problem has been introduced.


Arsen7 commented Oct 26, 2012

The error is in line 415 of activerecord/lib/active_record/railties/databases.rake:

--- a/activerecord/lib/active_record/railties/databases.rake    2012-10-26 10:20:13.000000000 +0200
+++ b/activerecord/lib/active_record/railties/databases.rake    2012-10-26 11:57:12.000000000 +0200
@@ -412,7 +412,7 @@

     # desc "Recreate the databases from the structure.sql file"
     task :load => [:environment, :load_config] do
-      env = Rails.env
+      env = ENV['RAILS_ENV'] || 'test'

       abcs = ActiveRecord::Base.configurations
       filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")

I'm not sure how this fix stays in line with recent changes in environments in rakefiles, but now my test database is properly created and rake test works.


Arsen7 commented Oct 26, 2012

This was exactly the commit f4ddc49
Author: Martin Grandrath mg@insfx.com
Date: Thu Oct 18 10:56:44 2012 +0200

Use `Rails.env` instead of `ENV['RAILS_ENV']`; #7951

`ENV['RAILS_ENV']` is not defined unless explicitly specified on the
command line when running `rake db:structure:load`. This patch lets
the rake task retrieve the environment from `Rails.env` which defaults
to "development".

In this case the env should be "test" instead of "development".


rafaelfranca commented Oct 26, 2012

@Arsen7 have you read #7951 (comment)?

@ghost ghost assigned rafaelfranca Oct 26, 2012


mperham commented Oct 29, 2012

We're seeing this in 3.2.9.rc1:

> rake --trace
** Invoke default (first_time)
** Invoke spec (first_time)
** Invoke db:test:clone_structure (first_time)
** Invoke db:structure:dump (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:structure:dump
** Invoke db:test:load_structure (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment 
** Invoke db:load_config 
** Execute db:test:purge
** Execute db:test:load_structure
** Invoke db:structure:load (first_time)
** Invoke environment 
** Invoke db:load_config 
** Execute db:structure:load
rake aborted!
Mysql2::Error: Table 'accounts' already exists: CREATE TABLE `accounts` (

mperham commented Oct 29, 2012

Works as expected in 3.2.6.


rafaelfranca commented Oct 29, 2012

@mperham could you check 3-2-stable?


mperham commented Oct 29, 2012

Not sure.

gem 'rails', :path => '../rails', :branch => '3-2-stable'
> bundle update rails
Fetching gem metadata from http://rubygems.org/......
Fetching gem metadata from http://rubygems.org/...........
Could not find gem 'activerecord-deprecated_finders (= 0.0.1) ruby', which is required by gem 'rails (>= 0) ruby', in any of the sources.

mperham commented Oct 29, 2012

Figured it out, needed to checkout 3-2-stable and remove the branch option.


mperham commented Oct 29, 2012

Good, looks fixed - now a new error:

       undefined method `to_i' for true:TrueClass
     # /Users/mperham/src/rails/activerecord/lib/active_record/connection_adapters/column.rb:78:in `type_cast'
     # /Users/mperham/src/rails/activerecord/lib/active_record/attribute_methods/dirty.rb:86:in `_field_changed?'
     # /Users/mperham/src/rails/activerecord/lib/active_record/attribute_methods/dirty.rb:63:in `write_attribute'
     # /Users/mperham/src/rails/activerecord/lib/active_record/attribute_methods/write.rb:14:in `admin='

mperham commented Oct 29, 2012

Opened #8067 to talk about that issue.


rafaelfranca commented Oct 30, 2012

Closing this one since it was fixed by e6b4184

rafaelfranca added a commit that referenced this issue Oct 30, 2012

Add CHANGELOG entry to #8032 fix
e6b4184 fixes that issue too.

[ci skip]

bornio commented Jun 13, 2013

I have this error now on 4.0.0.rc2 and PostgreSQL

Saw this warning with rails 4.1.7 after changing my schema_format to :sql and manually running rake db:test:prepare (as Rails test helper didn't seem to pull in the changes automatically).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment