lib/og/backends/psql.rb in nitro-0.5.0 vs lib/og/backends/psql.rb in nitro-0.6.0

- old
+ new

@@ -1,10 +1,10 @@ # code: # * George Moschovitis <gm@navel.gr> # # (c) 2004 Navel, all rights reserved. -# $Id: psql.rb 159 2004-11-18 10:18:30Z gmosx $ +# $Id: psql.rb 185 2004-12-10 13:29:09Z gmosx $ require "postgres" require "og/backend" @@ -266,12 +266,12 @@ sql << ") WITHOUT OIDS;" # Create indices - if klass.__meta - for data in klass.__meta[:sql_index] + if klass.__meta and indices = klass.__meta[:sql_index] + for data in indices idx, options = *data idx = idx.to_s pre_sql, post_sql = options[:pre], options[:post] idxname = idx.gsub(/ /, "").gsub(/,/, "_").gsub(/\(.*\)/, "") sql << " CREATE #{pre_sql} INDEX #{klass::DBTABLE}_#{idxname}_idx #{post_sql} ON #{klass::DBTABLE} (#{idx});" @@ -302,9 +302,50 @@ $log.debug "Sequence already exists" if $DBG else raise end end + + # Create join tables if needed. Join tables are used in + # 'many_to_many' relations. + + if klass.__meta and joins = klass.__meta[:sql_join] + for data in joins + # the class to join to and some options. + join_class, options = *data + + # gmosx: dont use DBTABLE here, perhaps the join class + # is not managed yet. + join_table = "#{Og::Utils.join_table(klass, join_class)}" + join_src = "#{Og::Utils.encode(klass)}_oid" + join_dst = "#{Og::Utils.encode(join_class)}_oid" + begin + exec "CREATE TABLE #{join_table} ( key1 integer, key2 integer )" + exec "CREATE INDEX #{join_table}_key1_idx ON #{join_table} (key1)" + exec "CREATE INDEX #{join_table}_key2_idx ON #{join_table} (key2)" + rescue => ex + # gmosx: any idea how to better test this? + if ex.to_s =~ /relation .* already exists/i + $log.debug "Join table already exists" if $DBG + else + raise + end + end + end + end + + begin + exec(sql) + $log.info "Created join table '#{join_table}'." + rescue => ex + # gmosx: any idea how to better test this? + if ex.to_s =~ /relation .* already exists/i + $log.debug "Join table already exists" if $DBG + else + raise + end + end + end # Drop the managed object table # def drop_table(klass)