Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support :using option to set_column_type on PostgreSQL, to force a sp…

…ecific conversion from the old value to the new value

USING allows you to convert a column from one type to another even
if there is no specific typecast for the conversion.  It's very
flexible, even allowing other columns in the same row to be
referenced.
  • Loading branch information...
commit dd3ede645ca20c734e4a693679395511a1938c09 1 parent 85c421d
@jeremyevans authored
View
2  CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Support :using option to set_column_type on PostgreSQL, to force a specific conversion from the old value to the new value (jeremyevans)
+
* Drop indexes in the reverse order that they were added in the schema dumper (jeremyevans)
* Add :index_names option to schema dumper method, can be set to false or :namespace (stu314, jeremyevans)
View
16 lib/sequel/adapters/shared/postgres.rb
@@ -444,6 +444,22 @@ def views(opts={})
private
+ # Handle :using option for set_column_type op.
+ def alter_table_sql(table, op)
+ case op[:op]
+ when :set_column_type
+ s = super
+ if using = op[:using]
+ using = Sequel::LiteralString.new(using) if using.is_a?(String)
+ s << ' USING '
+ s << literal(using)
+ end
+ s
+ else
+ super
+ end
+ end
+
# If the :synchronous option is given and non-nil, set synchronous_commit
# appropriately. Valid values for the :synchronous option are true,
# :on, false, :off, :local, and :remote_write.
View
4 lib/sequel/database/schema_generator.rb
@@ -406,6 +406,10 @@ def set_column_default(name, default)
# Modify a column's type in the DDL for the table.
#
# set_column_type(:artist_name, 'char(10)') # ALTER COLUMN artist_name TYPE char(10)
+ #
+ # PostgreSQL specific options:
+ #
+ # :using :: Add a USING clause that specifies how to convert existing values to new values.
def set_column_type(name, type, opts={})
@operations << {:op => :set_column_type, :name => name, :type => type}.merge(opts)
end
View
24 spec/adapters/postgres_spec.rb
@@ -158,6 +158,30 @@ def logger.method_missing(m, msg)
@d.lock('EXCLUSIVE'){@d.insert(:name=>'a')}
end
+ specify "should support :using when altering a column's type" do
+ begin
+ @db = POSTGRES_DB
+ @db.create_table!(:atest){Integer :t}
+ @db[:atest].insert(1262304000)
+ @db.alter_table(:atest){set_column_type :t, Time, :using=>'epoch'.cast(Time) + '1 second'.cast(:interval) * :t}
+ @db[:atest].get(:t.extract(:year)).should == 2010
+ ensure
+ @db.drop_table?(:atest)
+ end
+ end
+
+ specify "should support :using with a string when altering a column's type" do
+ begin
+ @db = POSTGRES_DB
+ @db.create_table!(:atest){Integer :t}
+ @db[:atest].insert(1262304000)
+ @db.alter_table(:atest){set_column_type :t, Time, :using=>"'epoch'::timestamp + '1 second'::interval * t"}
+ @db[:atest].get(:t.extract(:year)).should == 2010
+ ensure
+ @db.drop_table?(:atest)
+ end
+ end
+
specify "should have #transaction support various types of synchronous options" do
@db = POSTGRES_DB
@db.transaction(:synchronous=>:on){}
Please sign in to comment.
Something went wrong with that request. Please try again.