Created
May 28, 2014 21:10
-
-
Save lardcanoe/f368de446df0ac498ed4 to your computer and use it in GitHub Desktop.
ActiveRecord module to get BIGINT Primary Keys in schema.rb when using schema_plus
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
# This gives us the ability to get BIGINT PK's in schema.rb | |
# It also plays nice with the schema_plus gem | |
module MyCompany | |
module ActiveRecord | |
module SchemaDumper | |
def self.included(base) #:nodoc: | |
base.class_eval do | |
private | |
alias_method_chain :table, :bigint_support | |
end | |
end | |
private | |
def table_with_bigint_support(table, ignore) #:nodoc: | |
stream = StringIO.new | |
tbl_name = remove_prefix_and_suffix(table) | |
table_without_schema_plus(table, stream) | |
stream_string = stream.string | |
create_bigint_pk = false | |
pk = nil | |
if @connection.respond_to?(:primary_key) | |
pk = @connection.primary_key(table) | |
end | |
@connection.columns(table).each do |column| | |
if !column.default_expr.nil? | |
stream_string.gsub!("\"#{column.name}\"", "\"#{column.name}\", :default => { :expr => #{column.default_expr.inspect} }") | |
end | |
if column.name == pk | |
create_bigint_pk = column.type == :integer && column.limit == 8 | |
end | |
end | |
if create_bigint_pk | |
stream_string += " change_column :#{remove_prefix_and_suffix(table)}, :#{pk}, \"BIGINT NOT NULL AUTO_INCREMENT\"\n\n" | |
end | |
@table_dumps[table] = stream_string | |
end | |
end | |
end | |
end | |
::ActiveRecord::SchemaDumper.send(:include, ::MyCompany::ActiveRecord::SchemaDumper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment