Class: LowCardTables::LowCardTable::TableUniqueIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/low_card_tables/low_card_table/table_unique_index.rb

Overview

A TableUniqueIndex represents the concept of a unique index for a given low-card model class. I say “the concept”, because there should only be one instance of this class for any given low-card model class – there isn’t one instance of this class for each actual unique index for the class in question.

This class started as code that was directly part of the RowManager, and was factored out to create this class instead – simply so that the RowManager wouldn’t have any more code in it than necessary.

Instance Method Summary collapse

Constructor Details

#initialize(low_card_model) ⇒ TableUniqueIndex

Creates a new instance for the low-card model class in question.



11
12
13
14
15
16
17
# File 'lib/low_card_tables/low_card_table/table_unique_index.rb', line 11

def initialize(low_card_model)
  unless low_card_model.respond_to?(:is_low_card_table?) && low_card_model.is_low_card_table?
    raise ArgumentError, "You must supply a low-card AR model class, not: #{low_card_model.inspect}"
  end

  @low_card_model = low_card_model
end

Instance Method Details

#ensure_present!(create_if_needed) ⇒ Object

Ensures that the unique index is present. If the index is present, does nothing else.

If the index is not present, then looks at create_if_needed. If this evaluates to true, then it will create the index. If this evaluates to false, then it will raise an exception.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/low_card_tables/low_card_table/table_unique_index.rb', line 23

def ensure_present!(create_if_needed)
  return unless @low_card_model.table_exists?

  current_name = current_unique_all_columns_index_name
  return true if current_name

  if create_if_needed
    create_unique_index!
    true
  else
    message = %{You said that the table '#{low_card_model.table_name}' is a low-card table.
However, it currently does not seem to have a unique index on all its columns. For the
low-card system to work properly, this is *required* -- although the low-card system
tries very hard to lock tables and otherwise ensure that it never will create duplicate
rows, this is important enough that we really want the database to enforce it.

We're looking for an index on the following columns:

  #{value_column_names.sort.join(", ")}

...and we have the following unique indexes:

}
    current_unique_indexes.each do |unique_index|
      message << "  '#{unique_index.name}': #{unique_index.columns.sort.join(", ")}\n"
    end
    message << "\n"

    raise LowCardTables::Errors::LowCardNoUniqueIndexError, message
  end
end

#remove!Object

Removes the unique index, if one is present. If one is not present, does nothing.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/low_card_tables/low_card_table/table_unique_index.rb', line 56

def remove!
  table_name = low_card_model.table_name
  current_name = current_unique_all_columns_index_name

  if current_name
    migrate do
      remove_index table_name, :name => current_name
    end

    now_current_name = current_unique_all_columns_index_name
    if now_current_name
      raise "Whoa -- we tried to remove the unique index on #{table_name}, which was named '#{current_name}', but, after we removed it, we still have a unique all-columns index called '#{now_current_name}'!"
    end
  end
end