Created
August 24, 2012 14:18
-
-
Save dre1080/3451122 to your computer and use it in GitHub Desktop.
Setup varbinary columns or any other unsupported data type in rails migrations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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