Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9239 (new defect)

Opened 1 year ago

Last modified 1 year ago

[PATCH] Fixture loading across multiple databases

Reported by: theamazingrando Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: tiny
Cc: tarmo

Description

When trying to load fixtures into multiple databases in a test, loading fails when the default connection is, eg, postgres, and the connection loading into is mysql due to different quoting rules.

This is because, while the Fixures class is smart enough to use the connection of the AR object its trying to load, the Fixture class is not, and uses ActiveRecord::Base for its quoting rules.

If AR::Base.connection is postgres, and the object.connection is mysql, this results in invalid mysql statements to insert the fixture.

Attached is a patch that infers the proper connection for Fixture, the same way the Fixtures class does. I'm not sure how to test this thing, but my change hasn't broken any of the existing tests, and the default behavior has not changed.

Attachments

fixtures_using_correct_connection.patch (1.3 kB) - added by theamazingrando on 08/10/07 19:20:40.

Change History

08/10/07 19:20:40 changed by theamazingrando

  • attachment fixtures_using_correct_connection.patch added.

08/13/07 17:32:55 changed by tarmo

  • keywords set to tiny.
  • summary changed from [TINY][PATCH] Fixture loading across multiple databases to [PATCH] Fixture loading across multiple databases.

Tests pass on mysql, postgresql, sqlite. But there is one issue, class_name is a class name, not a constant, it probably won't respond to "connection", you should use class_name.constantize to get the proper class.

It's indeed a bit hard to test this, but it should be doable with some mocks or stubs. Take a look at fixtures_test.rb and the uses_mocha method defined in abstract_unit.rb which should be wrapped around tests that use mocha (so the tests are skipped when mocha is missing). You could test for example that when initializing Fixture object the correct connection is taken if class_name constantize returns a mock that has "connection" method.

08/13/07 17:33:21 changed by tarmo

  • cc set to tarmo.

08/13/07 18:43:03 changed by norbert

The current patch does not appear to change anything (yes, I tested it) because like tarmo said, class_name will be set to a string. Additionally it seems some of the existing code surrounding the changes in this patch is equally useless. Kudos await for anyone who fixes the issue and cleans up this bit of code though.

08/13/07 18:56:56 changed by theamazingrando

I agree, the fixtures code is a mess.

The patch solves my problem when using the fixtures method in an integration test. I just noticed, though, that it does not when doing a rake db:fixtures:load. In fixtures, class_name is the class; in rake db:fixtures:load, its a string.

I started down fixing that, but without a whole lot of code changes, it would then require all your fixture names be the same as the model, rather than the table. So either way, its a non-tiny patch, or it changes the default behavior. I know its been attempted before, but maybe I'll just write a smarter fixtures plugin. There doesn't seem to be a whole lot of core interest in fixtures, particularly across multiple databases.