Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Dan Manges http://www.dcmanges.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Switch Oracle CLOB to VARCHAR2

This rake task creates a database script to change all Oracle CLOB columns to VARCHAR2 columns.

Changing Oracle CLOBs to VARCHAR2s can result in a huge performance increase, especially if the database and app servers are not close together on the network.

   1  
   2  namespace :db do
   3    task :fix_clobs => [:environment] do |t|
   4      @outfile = File.expand_path(File.join(RAILS_ROOT,"db","fix_clobs.sql"))
   5    
   6      File.open(@outfile, "w") do |file|
   7        ActiveRecord::Base.connection.tables.each do |table_name|
   8          begin
   9            model = eval(table_name.classify)
  10            model.columns.each do |column|
  11              if column.sql_type == "CLOB"
  12                file.write("ALTER TABLE #{table_name} ADD #{column.name}_temp VARCHAR2(4000);\n")
  13                file.write("UPDATE #{table_name} SET #{column.name}_temp = #{column.name};\n")
  14                file.write("COMMIT;\n")
  15                file.write("ALTER TABLE #{table_name} DROP COLUMN #{column.name};\n")
  16                file.write("ALTER TABLE #{table_name} RENAME COLUMN #{column.name}_temp TO #{column.name};\n")
  17                file.write("\n")
  18              end
  19            end
  20           rescue => ex
  21             puts "Failed for #{table_name} with #{ex.class}"
  22           end
  23        end
  24      end
  25    end
  26  end

Adjust Database Sequences

If loading data through fixtures or other means with hard-coded IDs, database sequences may need to be adjusted. This is written for Oracle.

   1  
   2  namespace :db do
   3    task :adjust_sequences => :environment do
   4      ActiveRecord::Base.connection.tables.each do |table|
   5        begin
   6          count = ActiveRecord::Base.count_by_sql("SELECT MAX(id) FROM #{table}")
   7          seq = "#{table}_seq"
   8          ActiveRecord::Base.connection.execute("DROP SEQUENCE #{seq}")
   9          ActiveRecord::Base.connection.execute("CREATE SEQUENCE #{seq} START WITH #{count+1}")
  10        rescue => ex
  11          puts "Failed for #{table} with #{ex.class}"
  12        end
  13      end
  14    end
  15  end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS