Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

table_name_prefix not honored under rake test

#6403

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?

Reported by Fredrik Thuresson · February 9th, 2011 @ 07:14 PM

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. kenmazaika
    kenmazaika

    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

    February 14th, 2011 @ 01:35 AM

  2. Fredrik Thuresson
    Fredrik Thuresson

    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

    March 16th, 2011 @ 02:22 PM

  3. Fredrik Thuresson
    Fredrik Thuresson

    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

    March 16th, 2011 @ 02:25 PM