Test Oracle Database Views with Rails

Testing Rails with a legacy oracle schema as a backend is always a challenge (fun?). Rails relies no schema.rb to create its test database. The problem is that using the latest activerecord-oracle_enhanced-adapter (1.4.0), the application is unaware of the database views. So, I’ve decided to create those DB views (development) as DB tables (test). {% highlight ruby %} #names of views I’d like created views = %W(VIEW1 VIEW2 VIEW2) dev_schema = Rails.configuration.database_configuration[“development”][“username”].upcase test_schema = Rails.configuration.database_configuration[“test”][“username”].upcase views.each do |view| #for each of the views I need created # establish a connection first to development database ActiveRecord::Base.establish_connection(:development) # grant select on the view to the test user ActiveRecord::Base.connection.execute “GRANT SELECT ON #{dev_schema}.#{view} TO #{test_schema}” # establish a connection to the test database ActiveRecord::Base.establish_connection(:test) # create the view as a table in the test database – ‘where 1=0’ discards importing data ActiveRecord::Base.connection.execute “CREATE TABLE #{test_schema}.#{view} AS SELECT * FROM #{dev_schema}.#{view} where 1=0” end {% endhighlight %} The script can run as a db:test:prepare rake task or as being explicitly required somewhere at the beginning of the test_helper. And of course, this is just the initial working concept – it can be greatly improved. JRuby 1.6.3 Rails 3.1 activerecord-oracle_enhanced-adapter 1.4.0