Skip to content

Commit

Permalink
Don't extract the default column value twice
Browse files Browse the repository at this point in the history
The column default setting, not its value, should be used when creating
a new PostgreSQLColumn because PostgreSQLColumn#initialize calls
extract_value_from_default. When called on an already extracted default
value, the result is a nil value being used instead of the actual default
value defined in the DDL, thus leading to INSERT errors when the column
has a not-NULL constraint and no value is specifed when the record is
created.
  • Loading branch information
davec authored and nofxx committed Oct 17, 2009
1 parent c0a48fb commit 0c81dd0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion MIT-LICENSE
@@ -1,4 +1,4 @@
Spatial Adapter Copyright (c) 2006 Guilhem Vellut <guilhem.vellut+georuby@gmail.com>
Spatial Adapter Copyright (c) 2006 Guilhem Vellut <guilhem.vellut+georuby@gmail.com>
PostGis Adapter Functions (c) 2008 Marcos Piccinini <nofxx>

Permission is hereby granted, free of charge, to any person obtaining
Expand Down
2 changes: 1 addition & 1 deletion lib/postgis_adapter.rb
Expand Up @@ -243,7 +243,7 @@ def columns(table_name, name = nil) #:nodoc:
ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn.new(name, default,raw_geom_info.type, notnull == "f", raw_geom_info.srid, raw_geom_info.with_z, raw_geom_info.with_m)
end
else
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new(name, ActiveRecord::ConnectionAdapters::PostgreSQLColumn.extract_value_from_default( default), type, notnull == "f")
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new(name, default, type, notnull == "f")
end
end
end
Expand Down
55 changes: 51 additions & 4 deletions spec/postgis_adapter_spec.rb
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/spec_helper.rb'

describe "PostgisAdapter" do
Expand Down Expand Up @@ -150,8 +151,8 @@ class Area < ActiveRecord::Base
pts = Area.find_all_by_geom(LineString.from_coordinates([[0,0],[2,2]],4326))
pts.should be_instance_of(Array)
pts.length.should eql(2)
pts[0].data.should match /Point/
pts[1].data.should match /Point/
pts[0].data.should match(/Point/)
pts[1].data.should match(/Point/)
end

it "should find by geom again" do
Expand All @@ -163,8 +164,8 @@ class Area < ActiveRecord::Base
pts = Area.find_all_by_geom([[0,0],[2,2],4326])
pts.should be_instance_of(Array)
pts.length.should eql(2)
pts[0].data.should match /Point/
pts[1].data.should match /Point/
pts[0].data.should match(/Point/)
pts[1].data.should match(/Point/)
end

it "should not mess with rails finder" do
Expand All @@ -174,4 +175,50 @@ class Area < ActiveRecord::Base

end

describe "PostgreSQL-specific types and default values" do

# Verify that a non-NULL column with a default value is handled correctly.
# Additionally, if the database uses UTF8 encoding, set the binary (bytea)
# column value to an illegal UTF8 string; it should be stored as the
# specified binary string and not as a text string. (The binary data test
# cannot fail if the database uses SQL_ASCII or LATIN1 encoding.)

ActiveRecord::Schema.define() do
create_table :binary_defaults, :force => true do |t|
t.string :name, :null => false
t.string :data, :null => false, :default => ''
t.binary :value
end
end

class BinaryDefault < ActiveRecord::Base
end

it "should create some records" do
if BinaryDefault.connection.encoding == "UTF8"
BinaryDefault.create!(:name => "foo", :data => "baz",
:value => "f\xf4o") # fôo as ISO-8859-1 (i.e., not valid UTF-8 data)
BinaryDefault.create!(:name => "bar", # data value not specified, should use default
:value => "b\xe5r") # bår as ISO-8859-1 (i.e., not valid UTF-8 data)
else
BinaryDefault.create!(:name => "foo", :data => "baz")
BinaryDefault.create!(:name => "bar")
end
end

it "should find the records" do
foo = BinaryDefault.find_by_name("foo")
bar = BinaryDefault.find_by_name("bar")

foo.data.should eql("baz")
bar.data.should eql("")

if BinaryDefault.connection.encoding == "UTF8"
foo.value.should eql("f\xf4o")
bar.value.should eql("b\xe5r")
end
end

end

end
6 changes: 3 additions & 3 deletions spec/spec.opts
@@ -1,4 +1,4 @@
--colour
--format profile
--timeout 20
--colour
--format profile
--timeout 20
--diff
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -4,6 +4,7 @@
require 'activerecord'
$:.unshift((File.join(File.dirname(__FILE__), '..', 'lib')))
gem 'activerecord', "=2.3.4"
gem 'nofxx-georuby'
require 'postgis_adapter'

# Monkey patch Schema.define logger
Expand Down

0 comments on commit 0c81dd0

Please sign in to comment.