tlconnor/activerecord-postgres-array

Default values can't be loaded from schema.rb

emma-borhanian opened this issue · 0 comments

For an empty array, the default ends up looking like :default => '{}' in schema.rb, however, this doesn't get single-quoted when turned into a CREATE TABLE statement, causing rake db:schema:load to fail.

The following patch fixes it for string arrays, by causing it to be encoded in schema.rb as :default => [] and could probably be generalized:

require 'active_record/connection_adapters/postgresql_adapter'
module ConnectionAdapters
  class PostgreSQLColumn < Column
    class << self
      def extract_value_from_default_with_array(default)
        case default
        when NilClass
          nil
        # Arrays
        when /\A'(.*)'::"?character varying.*"?\[\]\z/
          $1.from_postgres_array(:string)
        else
          extract_value_from_default_without_array(default)
        end
      end
      alias_method_chain :extract_value_from_default, :array
    end
  end
end