daily pastebin goal
38%
SHARE
TWEET

Untitled

a guest Dec 9th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Provide varbinary columns in a Migration.
  2. # Probably a better way to do this?
  3. #
  4. # Usage:
  5. #     => t.varbinary :column, :limit => 20, ....[options]
  6. #
  7.  
  8. ActiveRecord::ConnectionAdapters::SchemaStatements.module_eval do
  9.   def type_to_sql_with_varbinary(type, limit = nil, precision = nil, scale = nil)
  10.     return type_to_sql_without_varbinary(type, limit, precision, scale) unless :varbinary == type.to_sym
  11.     "varbinary(#{limit})"
  12.   end
  13.   alias_method_chain :type_to_sql, :varbinary
  14. end
  15.  
  16. ActiveRecord::ConnectionAdapters::TableDefinition.class_eval do
  17.   def varbinary(*args)
  18.     options = args.extract_options!
  19.     column_names = args
  20.     type = :varbinary
  21.     column_names.each { |name| column(name, type, options) }
  22.   end
  23. end
  24.  
  25. ActiveRecord::ConnectionAdapters::Table.class_eval do
  26.   def varbinary(*args)
  27.     options = args.extract_options!
  28.     column_names = args
  29.     type = :varbinary
  30.     column_names.each do |name|
  31.       column = ColumnDefinition.new(@base, name.to_s, type)
  32.       if options[:limit]
  33.         column.limit = options[:limit]
  34.       elsif native[type].is_a?(Hash)
  35.         column.limit = native[type][:limit]
  36.       end
  37.       column.precision = options[:precision]
  38.       column.scale = options[:scale]
  39.       column.default = options[:default]
  40.       column.null = options[:null]
  41.       @base.add_column(@table_name, name, column.sql_type, options)
  42.     end
  43.   end
  44. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top