Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Arsen7 opened this issue Oct 26, 2012 · 13 comments
Closed

Comments

@Arsen7
Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Member

@Arsen7 have you read #7951 (comment)?

@ghost ghost assigned rafaelfranca Oct 26, 2012
@mperham
Copy link
Contributor

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` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
   ...

@mperham
Copy link
Contributor

mperham commented Oct 29, 2012

Works as expected in 3.2.6.

@rafaelfranca
Copy link
Member

@mperham could you check 3-2-stable?

@mperham
Copy link
Contributor

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
Copy link
Contributor

mperham commented Oct 29, 2012

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

@mperham
Copy link
Contributor

mperham commented Oct 29, 2012

Good, looks fixed - now a new error:

     NoMethodError:
       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
Copy link
Contributor

mperham commented Oct 29, 2012

Opened #8067 to talk about that issue.

@rafaelfranca
Copy link
Member

Closing this one since it was fixed by e6b4184

@bornio
Copy link

bornio commented Jun 13, 2013

I have this error now on 4.0.0.rc2 and PostgreSQL

@nozpheratu
Copy link

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
Projects
None yet
Development

No branches or pull requests

5 participants