Last active
March 2, 2016 04:00
-
-
Save sax/cc6ebf1805e732112134 to your computer and use it in GitHub Desktop.
Monkey patch ActiveRecord to use bigints for ids and references
This file contains hidden or 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
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