{"id":13879570,"url":"https://github.com/alassek/activerecord-pg_enum","last_synced_at":"2025-04-04T16:13:24.394Z","repository":{"id":45396094,"uuid":"138442095","full_name":"alassek/activerecord-pg_enum","owner":"alassek","description":"Integrate PostgreSQL's enumerated types with the Rails enum feature","archived":false,"fork":false,"pushed_at":"2022-09-19T19:56:58.000Z","size":121,"stargazers_count":168,"open_issues_count":1,"forks_count":10,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-30T05:57:19.235Z","etag":null,"topics":["enum","enumerated-types","migrations","postgresql","ruby-on-rails"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alassek.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-24T00:20:19.000Z","updated_at":"2024-05-09T11:49:13.000Z","dependencies_parsed_at":"2022-09-16T09:21:40.856Z","dependency_job_id":null,"html_url":"https://github.com/alassek/activerecord-pg_enum","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alassek%2Factiverecord-pg_enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alassek%2Factiverecord-pg_enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alassek%2Factiverecord-pg_enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alassek%2Factiverecord-pg_enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alassek","download_url":"https://codeload.github.com/alassek/activerecord-pg_enum/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247208139,"owners_count":20901570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enum","enumerated-types","migrations","postgresql","ruby-on-rails"],"created_at":"2024-08-06T08:02:25.525Z","updated_at":"2025-04-04T16:13:24.376Z","avatar_url":"https://github.com/alassek.png","language":"Ruby","funding_links":[],"categories":["Ruby","Gems"],"sub_categories":["Articles"],"readme":"# ActiveRecord::PGEnum [![Build Status](https://circleci.com/gh/alassek/activerecord-pg_enum.svg?style=shield)](https://app.circleci.com/pipelines/github/alassek/activerecord-pg_enum)\n\n**Note**: This was originally written before Rails added support for native enums. See the comments in [version\nsupport](#version-support).\n\nThe `enum` feature in Rails has bad developer ergonomics. It uses integer types at the DB layer, which means trying to understand SQL output is a pain.\n\nUsing the easy form of the helper syntax is a minor footgun:\n\n```ruby\nenum status: %w[new active archived]\n```\n\nIt's not obvious that the above code is order-dependent, but if you decide to add a new enum anywhere but the end, you're in trouble.\n\nIf you choose the use `varchar` fields instead, now you have to write annoying check constraints and lose the efficient storage.\n\n```ruby\nenum status: { new: \"new\", active: \"active\", archived: \"archived\" }\n```\n\nNobody has time to write that nonsense.\n\n## Enumerated Types: The Best of Both Worlds\n\nDid you know you can define your own types in PostgreSQL? You can, and this type system also supports enumeration.\n\n```SQL\nCREATE TYPE status_type AS ENUM ('new', 'active', 'archived');\n```\n\nNot only does this give you full type safety at the DB layer, the implementation is highly efficient. An enum value only takes up [four bytes](https://www.postgresql.org/docs/11/datatype-enum.html).\n\nThe best part is that PostgreSQL supports inserting new values at any point of the list without having to migrate your data.\n\n```SQL\nALTER TYPE status_type ADD VALUE 'pending' BEFORE 'active';\n```\n\n## schema.rb Support\n\nThe principle motivation of this gem is to seamlessly integrate PG enums into your `schema.rb` file. This means you can use them in your database columns without switching to `structure.sql`.\n\n```ruby\nActiveRecord::Schema.define(version: 2019_06_19_214914) do\n\n  # These are extensions that must be enabled in order to support this database\n  enable_extension \"plpgsql\"\n\n  create_enum \"status_type\", %w[new pending active archived]\n  \n  create_table \"orders\", id: :serial, force: :cascade do |t|\n    t.enum \"status\", as: \"status_type\", default: \"new\"\n  end\n\nend\n```\n\n## Version support\n\nEvery version of Rails with an `enum` macro is supported. This means 4.1 through master. Yes, this was annoying and difficult.\n\nThe monkeypatches in this library are extremely narrow and contained; the dirty hacks I had to do to make 4.1 work, for instance, have no impact on 6.0.\n\nMonkeypatching Rails internals is **scary**. So this library has a comprehensive test suite that runs against every known minor version.\n\nRails 7 added support for native enums, but they have so far neglected to support altering or dropping enums in the API,\nso this gem remains to fill in the gaps. I expect to slowly deprecate this over time.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'activerecord-pg_enum'\n```\n\nAnd then execute:\n\n    $ bundle\n\n## Usage\n\n### Migrations\n\nDefining a new ENUM\n\n```ruby\nclass AddContactMethodType \u003c ActiveRecord::Migration[5.2]\n  def up\n    create_enum \"contact_method_type\", %w[Email Phone]\n  end\n\n  def down\n    drop_enum \"contact_method_type\"\n  end\nend\n```\n\nAdding a value to an existing ENUM (you must disable the wrapping transaction on PostgreSQL versions older than 12)\n\n```ruby\nclass AddSMSToContactMethodType \u003c ActiveRecord::Migration[5.2]\n  disable_ddl_transaction!\n\n  def up\n    add_enum_value \"contact_method_type\", \"SMS\", before: \"Phone\"\n  end\n\n  def down\n    raise ActiveRecord::IrreversibleMigration\n  end\nend\n```\n\nAdding an enum column to a table\n\n```ruby\nclass AddStatusToOrder \u003c ActiveRecord::Migration[5.2]\n  def change\n    change_table :orders do |t|\n      t.enum :status, as: \"status_type\", default: \"new\"\n    end\n  end\nend\n```\n\nRenaming an enum type\n\n```ruby\nclass RenameStatusType \u003c ActiveRecord::Migration[6.0]\n  def change\n    rename_enum \"status_type\", to: \"order_status_type\"\n  end\nend\n```\n\n```SQL\nALTER TYPE status_type RENAME TO order_status_type;\n```\n\n**PostgreSQL 10+ required**:\n\nChanging an enum label\n\n```ruby\nclass ChangeStatusHoldLabel \u003c ActiveRecord::Migration[6.0]\n  def change\n    rename_enum_value \"status_type\", from: \"on hold\", to: \"OnHold\"\n  end\nend\n```\n\n```SQL\nALTER TYPE status_type RENAME VALUE 'on hold' TO 'OnHold';\n```\n\n### Module Builder\n\n```ruby\nclass ContactInfo \u003c ActiveRecord::Base\n  include PGEnum(contact_method: %w[Email SMS Phone])\nend\n```\n\nThe generated module calls the official `enum` method converting array syntax into strings. The above example is equivalent to:\n\n```ruby\nclass ContactInfo \u003c ActiveRecord::Base\n  enum contact_method: { Email: \"Email\", SMS: \"SMS\", Phone: \"Phone\" }\nend\n```\n\nAdditionally, `enum` options are fully supported, for example\n```ruby\nclass User \u003c ActiveRecord::Base\n  include PGEnum(status: %w[active inactive deleted], _prefix: 'user', _suffix: true)\nend\n```\n\nis equivalent to\n```ruby\nclass User \u003c ActiveRecord::Base\n  enum status: { active: 'active', inactive: 'inactive', deleted: 'deleted' }, _prefix: 'user', _suffix: true\nend\n```\n\nThere's no technical reason why you couldn't detect enum columns at startup time and automatically do this wireup, but I feel that the benefit of self-documenting outweighs the convenience.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `appraisal rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTest a specific version with `appraisal 6.0 rake spec`. This is usually necessary because different versions have different Ruby version support.\n\nTo install this gem onto your local machine, run `bundle exec rake install`.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/alassek/activerecord-pg_enum.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falassek%2Factiverecord-pg_enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falassek%2Factiverecord-pg_enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falassek%2Factiverecord-pg_enum/lists"}