Bypass db:test:prepare but still load test db

We have a development database that has quite a few non-Rails convention
names for tables and fields. Also we have quite a few primary-foreign
key relationships. This database has been inherited from a legacy
system. However, when we run the rspec then Rails runs a
db:test:prepare that loads the system-generated schema.rb file.
However, the schema.rb file doesn’t seem to play well with this legacy
database and doesn’t translate some things well. So the schema.rb
generated transforms some fields from varchar to number, etc.

So in order to build the test database properly, I have made a few edits
to the rspec.rake to run a db:test:purge and then a db:migrate to build
the database structure from development. This is a bit of a hack and I
don’t like it. What’s a better way?

If I need to give you more details, I would be glad to.

Using JRuby 1.5.6
Oracle Enhanced Driver
Rails 2.3.11

I’m calling these two lines directly in the rspec.rake.

Rake::Task[‘db:test:purge’].invoke
Rake::Task[‘db:migrate’].invoke(‘RAILS_ENV=test’)

I’d be the last person to say that this is a great way of doing things.

Thanks for any help,

I guess another option would be to create our own schema.rb for the
structure of our database and somehow amend the database.rake file to
take this schema.rb instead of the one that gets generated via
migrations. However, I’m not sure how I would do that and even if
that’s a good idea.

Hi Charles. We do something like this at Weplay for the same reasons:

https://gist.github.com/987543

Maybe that will help.

Cheers,
Luke

Luke M.
http://www.lukemelia.com/

What I had also done is commented this line out of my rspec.rake.

spec_prereq = File.exist?(File.join(RAILS_ROOT, ‘config’,
‘database.yml’)) ? “db:test:prepare” : :noop

Which I am assuming is running a db:test:prepare as a prerequisite for
running the rspecs.

So will the new code I write for the db:test:prepare be picked up by
these lines above?

No problem. We have it in lib/tasks/databases.rake.

Cheers,
Luke

Luke M.
http://www.lukemelia.com/

Thanks, that is close to what I’m looking for. Where do I put the code
for this, in the rspec.rake or in a separate rake file in my lib/tasks
folder? I’m probably a step above a newbie so sorry if this sounds too
rudimentary.

Yeah, you may want to do this:

remove_task “db:test:prepare”

before you add the new definition of db:test:prepare. Then you can
consider uncommenting the line you mentioned, or running rake
db:test:prepare manually when you need it.

Luke M.
http://www.lukemelia.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs