views:

385

answers:

2

Can I set rails to use mysql with MEMORY as the DB engine? I never restart mysql, and rebuild the db so often I'd rather have it be fast. Having the memory db for running tests would be nice too.

EDIT: I should have specified this is for dev/testing only, not production.

A: 

I use sqlite3 in memory database for testing. It's usually a little bit faster than file based, but not THAT much unless you have a ton of test data.

To set that up your database.yml will look like this:

test:
adapter: sqlite3
database: ":memory:"

You'll also have to load your schema into your in memory database in your test helper like so:

config = YAML::load(IO.read(File.dirname(__FILE__) + "/../config/database.yml"))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/../log/debug.log")
ActiveRecord::Base.establish_connection(config["test"])
load(File.dirname(__FILE__) + "/../db/schema.rb")
Jason stewart
Why would you need to establish the connection in the test? I thought this is something Rails would do for you.
Ryan Bigg
I've done this too and it is sweet, but I stopped using SQLite locally due to the differences between it and MySQL/PostgreSQL. ActiveRecord hides most of the details but there's enough difference to make it annoying.
Luke Francl
The connection has to be established before the tests are run so that the schema can be loaded into the memory database before rails is loaded.The only place that I know of to load it is in the test helper, and at this point there is no connection made to the database by rails. Please correct me if I'm wrong, or if there's an easier way to do it.
Jason stewart
+2  A: 

I don't see why you couldn't; your choice of storage engine is a MySQL implementation detail. All you should need to do is set :options => "ENGINE=MEMORY" in your create_table declaration in your migrations.

Of course, I also don't see why you would -- especially in production. The MySQL documentation for the MEMORY engine is full of caveats, like fixed length field allocation, and the speed gain you'd realize has got to be trivial compared to the risk of losing everything. If your application is such that nothing needs to be persisted, ever, why not just skip ActiveRecord completely and layer your models on top of Memcached?

SFEley
I should have said originally, but this is NOT for production, just me developing. Putting the engine in the migration is not an option then (unless I conditionally do it based on environment)
Daniel Huckstep
So do it conditionally based on environment. Why is that not an option? `:options => (RAILS_ENV != 'production' ? "ENGINE=MEMORY" : nil)` (Just make sure your testing isn't thrown off by some weirdness that only exists in the memory engine. Personally, I still wouldn't bother with this; but if you really think database speed is your testing bottleneck, knock yourself out.)
SFEley
Because I don't want to commit the migrations to the scm with all that crap in there. I wanted to just do it locally, in my own little world, and if it made things a bit faster for me when I had to do db rebuilds or something, then win, but if not, who cares, I'd just use the regular setup.From the answers and some other reading, it's more work than it's (potentially not even) worth.
Daniel Huckstep
Upvote and accept since this does seem to be the way to do it though.
Daniel Huckstep