Skip to content

Commit ffce310

Browse files
committed
Fixes #84
Defines a change_column that works with the array option
1 parent bdccfe5 commit ffce310

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ gemspec
55
unless ENV['CI']
66
if RUBY_PLATFORM =~ /java/
77
gem 'ruby-debug'
8-
elsif RUBY_VERSION == '1.9.3' || RUBY_VERSION == '2.0.0'
9-
gem 'debugger'
8+
elsif RUBY_VERSION == '2.0.0'
9+
gem 'byebug'
1010
end
1111
end
1212
gem 'fivemat'

lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,22 @@ def extensions
480480
select_rows('select extname from pg_extension', 'extensions').map { |row| row[0] }.delete_if {|name| name == 'plpgsql'}
481481
end
482482

483+
def change_column_with_extended_types(table_name, column_name, type, options = {})
484+
if options[:array]
485+
clear_cache!
486+
quoted_table_name = quote_table_name(table_name)
487+
488+
execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}[]"
489+
490+
change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
491+
change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
492+
else
493+
change_column_without_extended_types(table_name, column_name, type, options)
494+
end
495+
end
496+
497+
alias_method_chain :change_column, :extended_types
498+
483499
private
484500

485501
def ipaddr_to_string(value)

spec/migrations/array_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@
8282
end
8383
end
8484

85+
context 'Change Column' do
86+
after { connection.drop_table :data_types }
87+
it 'updates the column definitions' do
88+
lambda do
89+
connection.create_table :data_types do |t|
90+
t.integer :array_1, :array => true, :default => []
91+
end
92+
93+
connection.change_column :data_types, :array_1, :integer, :array => true, :default => [], :null => false
94+
end.should_not raise_exception
95+
96+
columns = connection.columns(:data_types)
97+
98+
array_1 = columns.detect { |c| c.name == 'array_1'}
99+
array_1.sql_type.should eq 'integer[]'
100+
array_1.default.should eq []
101+
array_1.null.should be_false
102+
end
103+
end
104+
85105
context 'Default Values' do
86106
describe 'String defaults' do
87107
after { connection.drop_table :default_strings }

0 commit comments

Comments
 (0)