Rails and Tests
An interesting project I’ve had as of late is getting Rails to play nice with Oracle.
One of the major problems I have had is the largely excellent activerecord-oracle_enhanced-adapter doesn’t do everything quite right yet. For example, they don’t provide the code to do structure dumps. Ok, no biggie. They don’t support RAWs yet (I’m lobbying for a pull request to mostly support RAWs) but even if RAWs were supported there is some oddness that when my primary keys are not the default primary key type, they aren’t dumped correctly to the magic Active Record Migration language.
So, what to do? Well, we decided to override the Rake::Task db:test:prepare and after db:test:purge‘ing the database we’ll just re-run the migrations on the test database. Optimal? Not entirely, but you can’t beat the flexibility!
So, how do we do this?
Remove the Old Task / Redefine a New One
So, you can’t just re-define a task. A Task object contains actions. You must clear those actions and then define the task, which appends to the now-empty-task your defined action.
namespace :db do
namespace :test do |s|
s[:prepare].clear
...
Now define the task like you would normally…
task :prepare do ... end
The rest is, in some ways details. Invoke the other Rails tasks you want. Be aware that the rake test call switches environments from development to test during it’s normal runtime. When you override the preparation you need to manage that. In our case we want to call Rake::Task[:db:migrate] in the test environment and then do our unit tests.
Also of note, SEQUENCE objects aren’t automagically deleted by the db:purge task, so if you use that method to purge your test database you’ll also want to delete your SEQUENCE objects. You can look those up in the user_objects table.