Method: TimeRangeUniqueness::MigrationAdditions#add_time_range_uniqueness

Defined in:
lib/time_range_uniqueness/migration_additions.rb

#add_time_range_uniqueness(table, options = {}) ⇒ Object

Adds a time range column and an exclusion constraint to the specified table.

This method creates or modifies a column to store time ranges and ensures that no two time ranges overlap for records with the same scoped columns.

Parameters:

  • table (Symbol, String)

    The name of the table to which the time range uniqueness constraint will be added.

  • options (Hash) (defaults to: {})

    The options for the constraint.

Options Hash (options):

  • :with (Symbol)

    The name of the time range column.

  • :scope (Array<Symbol>) — default: Optional

    Columns to scope the uniqueness check.

  • :column_type (Symbol) — default: Optional

    The type of the time range column (default: :tstzrange).

  • :name (String) — default: Optional

    The name of the constraint.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/time_range_uniqueness/migration_additions.rb', line 45

def add_time_range_uniqueness(table, options = {})
  time_range_column = options[:with] || :time_range
  scope_columns = Array(options[:scope])
  column_type = :tstzrange
  constraint_name = options[:name] || generate_constraint_name(table, scope_columns, time_range_column)

  reversible do |dir|
    dir.up { apply_up_migration(table, time_range_column, column_type, options, constraint_name, scope_columns) }
    dir.down { apply_down_migration(table, time_range_column, constraint_name) }
  end
end