Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Don't extract the default column value twice

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...
commit 0c81dd0a1f6430ee1cc8ccfac032e5e30ed4ad90 1 parent c0a48fb
@davec davec authored committed
View
2  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
View
2  lib/postgis_adapter.rb
@@ -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
View
55 spec/postgis_adapter_spec.rb
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/spec_helper.rb'
describe "PostgisAdapter" do
@@ -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
@@ -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
@@ -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
View
6 spec/spec.opts
@@ -1,4 +1,4 @@
---colour
---format profile
---timeout 20
+--colour
+--format profile
+--timeout 20
--diff
View
1  spec/spec_helper.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.