Advertise here!

Rails, db_structure_dump, and Oracle

I use Ruby on Rails at work with an Oracle 10g database. The problem is that I have a couple new people who are supposed to begin helping with the project and would like to set up development databases. When I was using MySQL this was easy, I used the db_structure_dump task in rake and gave them the output file.

I tried to do the same with Oracle, but apparently the task and adapters aren’t set up for this. So after some looking around the web I came up with my own solution – to implement structure_dump in the oci_adapter. So if anyone needs this, here’s the quick and very dirty code. It could use some loving and cleaning before it gets submitted as a patch.

Add the following method to the OciAdapter class in active_record/connection_adapters/oci_adapter.rb:

def structure_dump
  select_all("select table_name from user_tables") .inject("") do |structure, table|
    string = "CREATE TABLE #{table.to_a.first.last} (\n "
    array = select_all("select column_name, data_type, data_length, data_precision,
       data_scale, data_default, nullable from all_tab_columns where table_name =
       '#{table.to_a.first.last}' order by column_id")
    
    array.collect! do |hash|
      tmp = "#{hash['column_name']} #{hash['data_type']}"	      
      if hash['data_type'] =='NUMBER' and !hash['data_precision'].nil?
        tmp << "(#{hash['data_precision'].to_i}"
        tmp << ",#{hash['data_scale'].to_i})" if !hash['data_scale'].nil?
        tmp << ')'
      elsif hash['data_type'].include?('CHAR')
        tmp << "(#{hash['data_length'].to_i})"  
      end	      
      tmp << " DEFAULT #{hash['data_default']}" if !hash['data_default'].nil?
      tmp << ' NOT NULL' if hash['nullable'] ="=" 'N'
      tmp	      
    end
    array.uniq!
    array.delete_if {|item| item.nil?}
    string << array.join(",\n ")
    string << ");\n\n"
    structure << string
  end
end

…And add “oci” to the case statement in the Rakefile:

desc "Dump the database structure to a SQL file"
task :db_structure_dump => :environment do
  abcs = ActiveRecord::Base.configurations
  case abcs[RAILS_ENV]["adapter"] 
    when "mysql", "oci"
      ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
Posted at 7pm on 06/30/05 | Posted in , , | 3 responses | read on

Rails Day Winners Announced

Speaking of YubNub - the winners of the Rails Day competition have been anounced. Surprisingly, YubNub is not the winner and instead came in second place. The winning application appears to be a billing webapp, and the third place winner  is a sort of distributed volunteering / to-do list site.
Posted at 3am on 06/30/05 | Posted in , , | no responses | read on

The Social Command Line

As a result of the "Rails Day":http://railsday.com/blog/ competition, a very interesting entry has popped up and has been making the rounds. The site is called "YubNub":http://www.yubnub.org/ and it bills itself as 'a (social) command line for the web'. The idea behind it is very simple - turn common web sites into command line "applications" by allowing the users to create a command as a sort of shorthand for invoking the page's internal functions. The problem _was_ that the original impementation was a little too simplistic to do a number of things. Users can create "commands" by sticking in an url and including a variable where the command's arguments would be stuck. The original implementation only accepted a single parameter that could be inserted into the url. However, I just checked back and it's now able to taked named parameters via a "switch" syntax. E.g. bq. xe -amount 100 -from USD -to EUR I'm curious how "Jonathan":http://jonaquino.blogspot.com/ will be able to implement pipes on the service. In many cases the answer is embedded in some generated html file. It would seem that the commands would need to be modified to allow for a custom html parser to extract the answer out (for the next command's input) typically beyond the abilities of the common user and prone to breaking far too often since it's really just screen-scraping. But then, I never thought of the idea of making a generic command line interface for webpages...
Posted at 2am on 06/30/05 | Posted in , , | no responses | read on

Older posts: 1 ... 7 8 9