Skip to content

Instantly share code, notes, and snippets.

@sax
Last active March 2, 2016 04:00
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 sax/cc6ebf1805e732112134 to your computer and use it in GitHub Desktop.
Save sax/cc6ebf1805e732112134 to your computer and use it in GitHub Desktop.
Monkey patch ActiveRecord to use bigints for ids and references
require 'active_record/connection_adapters/postgresql_adapter'
module ActiveRecord
module ConnectionAdapters
class TableDefinition
def references(*args)
options = args.extract_options!
polymorphic = options.delete(:polymorphic)
index_options = options.delete(:index)
args.each do |col|
column("#{col}_id", :bigint, options)
column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
index(polymorphic ? %w(id type).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
end
end
end
class PostgreSQLAdapter < AbstractAdapter
NATIVE_DATABASE_TYPES[:primary_key] = 'bigserial primary key'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment