/
index.cr
43 lines (35 loc) · 1.35 KB
/
index.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require "../concerns/can_format_strings_or_symbols"
module Marten
module DB
module Management
# Represents an index used when creating or altering tables.
class Index
include CanFormatStringsOrSymbols
@name : String
@column_names : Array(String)
# Returns the index name.
getter name
# Returns the column names that are part of the index.
getter column_names
def initialize(name : String | Symbol, column_names : Array(String | Symbol))
@name = name.to_s
@column_names = column_names.map(&.to_s)
end
# Returns true if the other index corresponds to the same index configuration.
def ==(other : self)
super || (name == other.name && column_names.to_set == other.column_names.to_set)
end
# Returns a copy of the index.
def clone
self.class.new(@name.dup, @column_names.clone)
end
# Returns a serialized version of the index arguments to be used when generating migrations.
#
# The returned string will be used in the context of `add_index` / `index` statements in generated migrations.
def serialize_args : ::String
"#{format_string_or_symbol(name)}, [#{column_names.map { |n| format_string_or_symbol(n) }.join(", ")}]"
end
end
end
end
end