Skip to content

Module Poly::Migration

  • Defined in: lib/poly/migration.rb

Migration helpers that keep the schema for polymorphic resource, role, owner and stack columns consistent across tables. Mix into a migration (or call from create_table/change_table blocks). Each column helper accepts either a table builder or a table name.

Public Instance Methods

poly_owner

poly_owner(table_or_builder, type_column: :owner_type, id_column: :owner_id, id_type: :string, null: true)

Table-builder helper (create_table/change_table): poly_owner t

Direct helper (add_column style): poly_owner :coins

  • @param table_or_builder [ActiveRecord::ConnectionAdapters::TableDefinition, Symbol, String]
  • @param type_column [Symbol]
  • @param id_column [Symbol]
  • @param id_type [Symbol]
  • @param null [Boolean]
  • @return [void]

poly_owner_index

poly_owner_index(table, type_column: :owner_type, id_column: :owner_id, unique: false, where: nil, index_name: nil)

Adds an index over the owner type and id columns.

  • @param table [Symbol, String]
  • @param type_column [Symbol]
  • @param id_column [Symbol]
  • @param unique [Boolean]
  • @param where [String, nil] partial-index predicate
  • @param index_name [String, nil]
  • @return [void]

poly_prime_index

poly_prime_index(table, name = :resource)

Partial unique index enforcing exactly one prime per (resource, role).

  • @param table [Symbol, String]
  • @param name [Symbol] association name
  • @return [void]

poly_resource

poly_resource(table_or_builder, name, null: true, id_type: :string)

Table-builder helper (create_table/change_table): poly_resource t, :resource

Direct helper (add_column style): poly_resource :coins, :resource

  • @param table_or_builder [ActiveRecord::ConnectionAdapters::TableDefinition, Symbol, String]
  • @param name [Symbol] association name; adds name_type and name_id
  • @param null [Boolean]
  • @param id_type [Symbol] column type for the id (:string for UUID keys)
  • @return [void]

poly_resource_index

poly_resource_index(table, name, unique: false, where: nil, index_name: nil, columns: nil)

Adds an index over a resource’s type and id columns.

  • @param table [Symbol, String]
  • @param name [Symbol] association name
  • @param unique [Boolean]
  • @param where [String, nil] partial-index predicate
  • @param index_name [String, nil]
  • @param columns [Array, nil] override the indexed columns
  • @return [void]

poly_role

poly_role(table_or_builder, name, null: true)

Table-builder helper (create_table/change_table): poly_role t, :resource

Direct helper (add_column style): poly_role :coins, :resource

  • @param table_or_builder [ActiveRecord::ConnectionAdapters::TableDefinition, Symbol, String]
  • @param name [Symbol] association name; adds a name_role string column
  • @param null [Boolean]
  • @return [void]

poly_stack

poly_stack(table_or_builder, id_type: :string)

Stack columns (golden-child history): the prime marker and the audit edge. Table-builder helper (create_table/change_table): poly_stack t

Direct helper (add_column style): poly_stack :statuses

  • @param table_or_builder [ActiveRecord::ConnectionAdapters::TableDefinition, Symbol, String]
  • @param id_type [Symbol] type of superseded_by_id
  • @return [void]