Skip to content

table_name_prefix not honored under rake test #837

Closed
lighthouse-import opened this Issue May 16, 2011 · 3 comments

1 participant

@lighthouse-import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6403
Created by Fredrik Thuresson - 2011-02-09 19:14:50 UTC

Creating a brand new app under Rails 3.0.4 with one model in it,

config/database.yml
development:
  database: bogus
test:
  database: bogus_test

The table exists in products database

class Item < ActiveRecord::Base
  def self.table_name_prefix
    'products.'
  end
end

the table_name_prefix is not honered when running rake test. It works fine in console and in browser to pick it up from the products database.

rake test produces

Mysql2::Error: Table 'bogus_test.items' doesn't exist: DELETE FROM `items`

Is this expected?

@lighthouse-import

Imported from Lighthouse.
Comment by kenmazaika - 2011-02-14 01:35:57 UTC

I do not believe MySQL supports having periods in table names, because if it did queries could be ambiguous with federated tables:

mysql> create table something.test (id INT);
ERROR 1049 (42000): Unknown database 'something'
mysql> create table something_test (id INT);
Query OK, 0 rows affected (0.00 sec)

I suspect this will work with other database engines though.

-ken

@lighthouse-import

Imported from Lighthouse.
Comment by Fredrik Thuresson - 2011-03-16 14:22:17 UTC

kenmazaika: The period is separating the database and the table name.
Database: bogus_test, Table Name: items

The following worked for me in test/test_helper.rb

Replaced
@connection = class_name.connection if class_name.respond_to?(:connection)
with
@connection = table_name.classify.constantize.connection

class Fixtures
def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
@name = table_name # preserve fixture base name
@class_name = class_name ||
(ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize)
@table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}"
@table_name = class_name.table_name if class_name.respond_to?(:table_name)
@connection = table_name.classify.constantize.connection
read_fixture_files
end
end

-Fredrik

@lighthouse-import

Imported from Lighthouse.
Comment by Fredrik Thuresson - 2011-03-16 14:25:23 UTC

Obviously I don't know how to format. Here is another attempt. Ignore the previous comment.

kenmazaika: The period is separating the database and the table name.
Database: bogus_test, Table Name: items
The following worked for me in test/test_helper.rb

Replaced
@connection = class_name.connection if class_name.respond_to?(:connection) 
with
@connection = table_name.classify.constantize.connection
class Fixtures
  def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
    @connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
    @name = table_name # preserve fixture base name
    @class_name = class_name ||
              (ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize)
    @table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}"
    @table_name = class_name.table_name if class_name.respond_to?(:table_name)
    @connection = table_name.classify.constantize.connection
    read_fixture_files
  end 
end

-Fredrik

@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011
@tekin tekin Updated rdoc to reflect changes to form option helpers
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#837 state:committed]
d676a7f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.