alassek / activerecord-pg_enum

Integrate PostgreSQL's enumerated types with the Rails enum feature
MIT License
168 stars 10 forks source link

Schema dumper assumes that enum values will not contain spaces #3

Closed AndrewSpeed closed 5 years ago

AndrewSpeed commented 5 years ago

Hi 👋

I've been using activerecord-pg_enum for a few weeks now, it's a fantastic gem, thanks for writing it.

Today I was adding a migration to create an enum which has values containing spaces, but the dumper doesn't appear to handle this correctly.

I've been taking a look at the codebase and would be happy to try and make this change, assuming it's deemed valid.

Steps to reproduce

Rails version: 5.2.2.1 Ruby version: 2.6.3 activerecord-pg_enum version: 1.0.2

Migration

# frozen_string_literal: true
​
class AddStatusEnum < ActiveRecord::Migration[5.2]
  def up
    create_enum 'status', ['sick', 'on vacation', 'working from home']
  end
​
  def down
    drop_enum 'status'
  end
end

Schema

Expected Schema

ActiveRecord::Schema.define(version: 2019_09_27_145140) do
  # ...

  # These are custom enum types that must be created before they can be used in the schema definition
  create_enum "status", ['sick', 'on vacation', 'working from home']
end

Actual Schema

ActiveRecord::Schema.define(version: 2019_09_27_145140) do
  # ...

  # These are custom enum types that must be created before they can be used in the schema definition
  create_enum "status", %w[sick on vacation working from home]
end

This ☝️will lead to the enum being created with values ["sick", "on", "vacation", "working", "from", "home"]

alassek commented 5 years ago

@AndrewSpeed thanks for the bug report, I hadn't even considered making enums with spaces, but you're absolutely right that they are allowed.

alassek commented 5 years ago

If you'd like to take a crack at this, it shouldn't be very difficult; the code is here: https://github.com/alassek/activerecord-pg_enum/blob/master/lib/active_record/pg_enum/4.1/schema_dumper.rb#L22 Otherwise I can handle it

AndrewSpeed commented 5 years ago

👍 thanks for the response @alassek, I'll have a go at this and let you know if I run into any problems

alassek commented 5 years ago

Fixed in v1.0.3