Skip to content

Instantly share code, notes, and snippets.

@dre1080
Created August 24, 2012 14:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dre1080/3451122 to your computer and use it in GitHub Desktop.
Save dre1080/3451122 to your computer and use it in GitHub Desktop.
Setup varbinary columns or any other unsupported data type in rails migrations
# Provide varbinary columns in a Migration.
# Probably a better way to do this?
#
# Usage:
# => t.varbinary :column, :limit => 20, ....[options]
#
ActiveRecord::ConnectionAdapters::SchemaStatements.module_eval do
def type_to_sql_with_varbinary(type, limit = nil, precision = nil, scale = nil)
return type_to_sql_without_varbinary(type, limit, precision, scale) unless :varbinary == type.to_sym
"varbinary(#{limit})"
end
alias_method_chain :type_to_sql, :varbinary
end
ActiveRecord::ConnectionAdapters::TableDefinition.class_eval do
def varbinary(*args)
options = args.extract_options!
column_names = args
type = :varbinary
column_names.each { |name| column(name, type, options) }
end
end
ActiveRecord::ConnectionAdapters::Table.class_eval do
def varbinary(*args)
options = args.extract_options!
column_names = args
type = :varbinary
column_names.each do |name|
column = ColumnDefinition.new(@base, name.to_s, type)
if options[:limit]
column.limit = options[:limit]
elsif native[type].is_a?(Hash)
column.limit = native[type][:limit]
end
column.precision = options[:precision]
column.scale = options[:scale]
column.default = options[:default]
column.null = options[:null]
@base.add_column(@table_name, name, column.sql_type, options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment