lib/arjdbc/postgresql/adapter.rb
def change_column(table_name, column_name, type, options = {})
      quoted_table_name = quote_table_name(table_name)

      sql_type = type_to_sql(type, options[:limit], options[:precision], options[:scale])
      sql_type << "[]" if options[:array]

      begin
        execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{sql_type}"
      rescue ActiveRecord::StatementInvalid => e
        raise e if postgresql_version > 80000
        # This is PostgreSQL 7.x, so we have to use a more arcane way of doing it.
        begin
          begin_db_transaction
          tmp_column_name = "#{column_name}_ar_tmp"
          add_column(table_name, tmp_column_name, type, options)
          execute "UPDATE #{quoted_table_name} SET #{quote_column_name(tmp_column_name)} = CAST(#{quote_column_name(column_name)} AS #{sql_type})"
          remove_column(table_name, column_name)
          rename_column(table_name, tmp_column_name, column_name)
          commit_db_transaction
        rescue
          rollback_db_transaction
        end
      end

      change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
      change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
    end
# Changes the column of a table.
Changes the column of a table.
Changes the column of a table .
def change_column(table_name, column_name, type, options = {})
      quoted_table_name = quote_table_name(table_name)
      sql_type = type_to_sql(type, options[:limit], options[:precision], options[:scale])
      sql_type << "[]" if options[:array]
      begin
        execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{sql_type}"
      rescue ActiveRecord::StatementInvalid => e
        raise e if postgresql_version > 80000
        begin
          begin_db_transaction
          tmp_column_name = "#{column_name}_ar_tmp"
          add_column(table_name, tmp_column_name, type, options)
          execute "UPDATE #{quoted_table_name} SET #{quote_column_name(tmp_column_name)} = CAST(#{quote_column_name(column_name)} AS #{sql_type})"
          remove_column(table_name, column_name)
          rename_column(table_name, tmp_column_name, column_name)
          commit_db_transaction
        rescue
          rollback_db_transaction
        end
      end
      change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
      change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
    end
def change_column ( table_name , column_name , type , options = { } ) quoted_table_name = quote_table_name ( table_name ) sql_type = type_to_sql ( type , options [ :limit ] , options [ :precision ] , options [ :scale ] ) sql_type << "[]" if options [ :array ] begin execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{sql_type}" rescue ActiveRecord :: StatementInvalid => e raise e if postgresql_version > 80000 begin begin_db_transaction tmp_column_name = "#{column_name}_ar_tmp" add_column ( table_name , tmp_column_name , type , options ) execute "UPDATE #{quoted_table_name} SET #{quote_column_name(tmp_column_name)} = CAST(#{quote_column_name(column_name)} AS #{sql_type})" remove_column ( table_name , column_name ) rename_column ( table_name , tmp_column_name , column_name ) commit_db_transaction rescue rollback_db_transaction end end change_column_default ( table_name , column_name , options [ :default ] ) if options_include_default? ( options ) change_column_null ( table_name , column_name , options [ :null ] , options [ :default ] ) if options . key? ( :null ) end
Changes the column of a table.
Changes the column of a table .
# This is PostgreSQL 7.x, so we have to use a more arcane way of doing it.
table_name
column_name
type
options