Skip to content

Fix defaults when dumping ruby schema #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/activerecord-postgres-array/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ def type_cast_code_with_array(var_name)
end
alias_method_chain :type_cast_code, :array

def type_cast_with_array(value)
if value.present? && type.to_s =~ /_array$/
base_type = type.to_s.gsub(/_array/, '')
value.from_postgres_array(base_type.parameterize('_').to_sym)
else
type_cast_without_array(value)
end
end
alias_method_chain :type_cast, :array

# Adds the array type for the column.
def simplified_type_with_array(field_type)
if field_type =~ /^numeric.+\[\]$/
Expand Down
9 changes: 8 additions & 1 deletion spec/array_ext_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'
require 'activerecord-postgres-array/array'

describe "Array" do
describe Array do
describe "#to_postgres_array" do
it "returns '{}' if used on an empty array" do
[].to_postgres_array.should == "'{}'"
Expand All @@ -27,4 +27,11 @@
["\\","\""].to_postgres_array.should == '\'{"\\\\","\\""}\''
end
end

describe "#from_postgres_array" do
subject(:array) { [:foo, :bar, :baz] }
it "returns the array itself" do
array.from_postgres_array.should eq array
end
end
end
11 changes: 11 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,14 @@
end
end
end

describe DefaultArticle do
it "creates a model with defaults" do
article = DefaultArticle.create
article.reload
article.name.should == 'AbC'
article.author_ids.should == [1,2,3]
article.prices.should == [12.519267, 16.0]
article.defaults.should == ['foo', 'bar', 'baz qux']
end
end
3 changes: 3 additions & 0 deletions spec/internal/app/models/default_article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class DefaultArticle < ActiveRecord::Base
serialize :serialized_column
end
10 changes: 10 additions & 0 deletions spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
t.string_array :languages
t.integer_array :author_ids
t.float_array :prices
t.string_array :defaults, :default => ['foo', 'bar']

# To make sure we don't interfere with YAML serialization
t.string :serialized_column
Expand All @@ -14,4 +15,13 @@
t.hstore :hstore_column
end
add_hstore_index :articles, :hstore_column

# Creating a new model table because we don't seem to be able to dump schema for hstore tables well
create_table(:default_articles, :force => true) do |t|
t.string :name, :default => 'AbC'
t.string_array :languages
t.integer_array :author_ids, :default => [1,2,3]
t.float_array :prices, :default => [12.519267, 16.0]
t.string_array :defaults, :default => ['foo', 'bar', 'baz qux']
end
end
33 changes: 33 additions & 0 deletions spec/schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

describe "Schema" do
subject(:dump_stream) { StringIO.new }

before { ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, dump_stream) }

it "can dump the schema" do
dump_stream.string.should_not be_blank
end

it "should dump the schema including the array types" do
dump_stream.string.should include("t.string_array")
dump_stream.string.should include("t.float_array")
dump_stream.string.should include("t.integer_array")
end

it "should still dump columns with normal defaults correctly" do
dump_stream.string.should include(':default => "AbC"')
end

it "should dump schemas including defaults for string arrays" do
dump_stream.string.should include(':default => ["foo", "bar", "baz qux"]')
end

it "should dump schemas including defaults for integer arrays" do
dump_stream.string.should include(":default => [1, 2, 3]")
end

it "should dump schemas including defaults for float arrays" do
dump_stream.string.should include(":default => [12.519267, 16.0]")
end
end
18 changes: 18 additions & 0 deletions spec/string_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,23 @@
it 'correctly handles multi line content' do
"{A\nB\nC,X\r\nY\r\nZ}".from_postgres_array.should == ["A\nB\nC", "X\r\nY\r\nZ"]
end

it 'handles decimal content' do
"{15.49151, 16.0}".from_postgres_array(:decimal).should == [15.49151, 16.0]
end

it 'handles float content' do
"{15.49151, 16.0}".from_postgres_array(:float).should == [15.49151, 16.0]
end

it 'handles integer content' do
"{1,2,3}".from_postgres_array(:integer).should == [1,2,3]
end

it 'handles timestamp content' do
t1 = Time.at(628232400)
t2 = Time.at(1362018690)
"{#{t1},#{t2}}".from_postgres_array(:timestamp).should == [t1, t2]
end
end
end