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

Ticket #5497 (closed defect: fixed)

Opened 4 years ago

Last modified 3 years ago

[PATCH] Ignoring ActiveRecord breaks tests.

Reported by: bob@sporkmonger.com Assigned to: rails@bitsweat.net
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: 1.2.0
Severity: normal Keywords:
Cc: bitsweat

Description

If you have a Rails application that doesn't make use of a database, it doesn't make sense to have supply a database.yml file, much less to supply valid connection information. But if you delete database.yml or leave the database.yml file with the default values, when you run your tests they error out because a connection with the database couldn't be established. Removing ActiveRecord from config.frameworks in the Rails::Initializer.run block in environment.rb changes the error a bit, but there's still an error.

It seems to be very difficult to get Rails not to load the ActiveRecord files at all, so rather than working out how to optionally disable all of the requires where code assumes that you will always use ActiveRecord, I changed the three places that actually directly try to use the connection object despite ActiveRecord not actually being used.

In my particular case, I'm writing an app that primarily uses the file system as a data store.

Long-term, I don't think Rails should assume that ActiveRecord will always be used in an app, but in the meantime, this patch should allow ActiveRecord to be ignored if necessary. If the problem is fixed eventually, this patch should continue to function correctly.

Attachments

ignoring_active_record_breaks_tests.diff (1.8 kB) - added by bob@sporkmonger.com on 06/24/06 22:42:10.
Patch for allowing ActiveRecord to be ignored.
fixtures.rb.diff (305 bytes) - added by zdennis on 01/19/07 19:03:36.
patch to fix issue where a pre-established connection is not taken into consideration in rails 1.2

Change History

06/24/06 22:42:10 changed by bob@sporkmonger.com

  • attachment ignoring_active_record_breaks_tests.diff added.

Patch for allowing ActiveRecord to be ignored.

06/28/06 23:51:53 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [4505]) Don't assume Active Record is available. Closes #5497.

06/29/06 01:13:56 changed by bitsweat

(In [4508]) r4720@asus: jeremy | 2006-06-28 18:12:57 -0700

Don't be such a hardass. References #5497.

06/29/06 14:20:13 changed by bob@sporkmonger.com

  • status changed from closed to reopened.
  • resolution deleted.

Jeremy, please carefully read the ticket, ActiveRecord is loaded by about a billion different things that erroneously assume that it should always be present, so the check for whether ActiveRecord has been defined will always pass until all of those references are cleaned up. I talked to David at RailsConf about the problem and we agreed that this solution was probably the simplest work-around. In my case, I removed the database.yml file as way of making sure that Rails didn't try to make a connection when it shouldn't. If you check for the missing connection exception instead, you introduce a slowdown for exception handling and you probably suppress errors that you actually want raised. At the moment, the only good way to make sure that the programmer really didn't want ActiveRecord is to check if they omitted database.yml entirely. The check for whether ActiveRecord was defined was a bit of future-proofing in case the assumptions about ActiveRecord are ever fixed. Please restore the state to what was in my patch, because your fix for my fix has the exact same problem that the original code did. Thanks!

06/29/06 15:56:25 changed by bitsweat

  • cc set to bitsweat.

I backed it out because AR itself doesn't use any configurations for its testing, so all tests with fixtures fail. I'd be glad to apply a workaround that satisfies your app while keeping the test suite happy (e.g. have the test suite set up a dummy config.)

06/29/06 16:54:38 changed by bob@sporkmonger.com

Hmm, that constraint could be really tricky to deal with. Let me see what I can come up with. I'm kinda surprised it got checked in at all then under the circumstances. Thanks.

06/29/06 18:29:36 changed by bob@sporkmonger.com

Is it possible to access the value of config.frameworks from when it was set in environment.rb? That would probably be the ideal way of dealing with the issue.

07/05/06 01:45:18 changed by david

  • keywords set to unverified.
  • owner changed from David to rails@bitsweat.net.
  • status changed from reopened to new.

07/10/06 19:37:43 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(follow-up: ↓ 11 ) 01/19/07 19:02:41 changed by zdennis

  • status changed from closed to reopened.
  • version changed from 0.14.2 to 1.2.0.
  • resolution deleted.

This is in the 1-2-pre-release branch, but it's not in the stable branch or in trunk.

Is this going to be merged to trunk, or is trunk going to be merged to 1.2?

Attached is a patch file which fixes the problem even further because it currently doesn't take into consideration if there are no configurations there is a pre-established connection. This would occur if you passed in a hash to ActiveRecord::Base.establish_connection without setting configurations=

01/19/07 19:03:36 changed by zdennis

  • attachment fixtures.rb.diff added.

patch to fix issue where a pre-established connection is not taken into consideration in rails 1.2

05/22/07 17:25:13 changed by kevinclark

Is this still an issue? Could you wrap the two patches into one so others can try it out more easily?

(in reply to: ↑ 9 ) 06/07/07 23:13:41 changed by josh

  • keywords deleted.
  • status changed from reopened to closed.
  • resolution set to fixed.

Replying to zdennis:

If this is still a problem, please start a new ticket for your concern. Also your patch needs to a in diff format.

I'm closing this ticket.