Skip to content

Instantly share code, notes, and snippets.

@tilo
Last active February 7, 2018 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tilo/36febe6a8843e8d588ed87b385f71cdc to your computer and use it in GitHub Desktop.
Save tilo/36febe6a8843e8d588ed87b385f71cdc to your computer and use it in GitHub Desktop.
ActiveRecord Monkey Patch to make Rails 5 dump DB agnostic format (2 of 2)
# config/initializers/active_record/connection_adapters/abstract/schema_dumper.rb
# DANGER, Will Robinson!!!
#
# This is a monkey patch for Rails 5.0 ONLY
#
if Rails.version !~ /^5.0/ # code taken from 5.0.6
puts "\n
--------------------------------------------------------------------------------
WARNING: you are using Rails version #{Rails.version}
Please double check that the monkey-patch for Schema Dumping still works!
see: \"#{__FILE__}\"
--------------------------------------------------------------------------------
"
else
module ActiveRecord
module ConnectionAdapters # :nodoc:
module ColumnDumper
def prepare_column_options(column) # code taken from 5.0.6
spec = {}
# if limit = schema_limit(column)
# spec[:limit] = limit
# end
#
# --- vvvvvvv ADDED LINES vvvvvvv ---------------------------------
# we are only ever dumping from MySQL:
types = ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::NATIVE_DATABASE_TYPES.deep_dup
types[:boolean].delete(:limit) # don't dump limit for boolean; SEE: https://stackoverflow.com/questions/32009672/limit-1-difference-in-schema-rb-boolean-fields
# ALWAYS dump the limit, even if it is the default value
limit = column.limit || types[column.type][:limit]
spec[:limit] = limit.inspect if limit
# --- ^^^^^^^ ADDED LINES ^^^^^^^ ---------------------------------
if precision = schema_precision(column)
spec[:precision] = precision
end
if scale = schema_scale(column)
spec[:scale] = scale
end
default = schema_default(column) if column.has_default?
spec[:default] = default unless default.nil?
spec[:null] = 'false' unless column.null
if collation = schema_collation(column)
spec[:collation] = collation
end
spec[:comment] = column.comment.inspect if column.comment.present?
spec
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment