FSevaistre / pg_enums

Simple usage of postgreSQL enums for Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PostgreSQL enums made simple Build Status

Simple usage of postgreSQL enums for Rails.

This gems provides some helpers to create, edit and delete enums in a postgreSQL base using ActiveRecord migrations.

Installation

Add this line to your application's Gemfile:

gem 'pg_enums'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pg_enums

Usage

The transactions has to be disabled to run the migrations. It is a pain, but pg doesn't allow some operation within a transaction :'(

Example

Add the possibility to store bananas in the shelfs table

include PGEnums

class AddSingle < ActiveRecord::Migration[5.1]

  self.disable_ddl_transaction!

  def up
    add_to_enum(enum_type: "fruits", new_value: "banana")
  end

  def down
    delete_from_enum(
      table: "shelfs",
      column: "fruits",
      enum_type: "fruits",
      value_to_drop: "banana",
      map_to: "not_a_fruit"
    )
  end

end

List of the available methods:

create_enum(enum_type:, table:, column:, values:)
delete_enum(enum:, table:, column:)
add_to_enum(enum_type:, new_value:)
rename_enum(enum:, new_name:)
update_enum(table:, column: enum_type:, old_value:, new_value:)
delete_from_enum(table:, column: enum_type:, value_to_drop:, map_to:)

Details

create_enum

Add a new column to the table, with a new type "enum_type" values is an array of the different values the new enum will be able to take

create_enum(enum_type:, table:, column:, values:)

delete_enum

Delete the column of the table and the enum associated

delete_enum(enum:, table:, column:)

add_to_enum

Add a new value to an existing enum

add_to_enum(enum_type:, new_value:)

rename_enum

Change the name of the enum

rename_enum(enum:, new_name:)

update_enum

Change the name of one of the values and map the existing records to the new value Can be really usefull if a typo has be made on a previous migration

update_enum(table:, column: enum_type:, old_value:, new_value:)

delete_from_enum

Delete one value and map the existing records to the "map_to" value. The map_to value has to be an existing value of the enum

delete_from_enum(table:, column: enum_type:, value_to_drop:, map_to:)

Author

François Sevaistre

License

MIT License

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/FSevaistre/pg_enums

About

Simple usage of postgreSQL enums for Rails

License:MIT License


Languages

Language:Ruby 100.0%