Last Modified
2014-12-15 05:57:58 -0500
Requires

Description

The pg_enum extension adds support for Sequel to handle PostgreSQL's enum types. To use this extension, first load it into your Database instance:

DB.extension :pg_enum

It allows creation of enum types using create_enum:

DB.create_enum(:type_name, %w'value1 value2 value3')

You can also add values to existing enums via add_enum_value:

DB.add_enum_value(:enum_type_name, 'value4')

If you want to drop an enum type, you can use drop_enum:

DB.drop_enum(:type_name)

Just like any user-created type, after creating the type, you can create tables that have a column of that type:

DB.create_table(:table_name)
  enum_type_name :column_name
end

When parsing the schema, enum types are recognized, and available values returned in the schema hash:

DB.schema(:table_name)
[[:column_name, {:type=>:enum, :enum_values=>['value1', 'value2']}]]

If the pg_array extension is used, arrays of enums are returned as a PGArray:

DB.create_table(:table_name)
  column :column_name, 'enum_type_name[]'
end
DB[:table_name].get(:column_name)
# ['value1', 'value2']

Finally, typecasting for enums is setup to cast to strings, which allows you to use symbols in your model code. Similar, you can provide the enum values as symbols when creating enums using create_enum or add_enum_value.