Class: DB::Migrate::CreateIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/db/migrate/create_index.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, table, columns = [], unique: false, drop_if_exists: false, if_not_exists: false, method: nil) ⇒ CreateIndex

Returns a new instance of CreateIndex.



11
12
13
14
15
16
17
18
19
20
# File 'lib/db/migrate/create_index.rb', line 11

def initialize(name, table, columns = [], unique: false, drop_if_exists: false, if_not_exists: false, method: nil)
	@name = name
	@table = table
	@columns = Array(columns)
	
	@unique = unique
	@drop_if_exists = drop_if_exists
	@if_not_exists = if_not_exists
	@method = method
end

Instance Method Details

#call(session) ⇒ Object



22
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
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/db/migrate/create_index.rb', line 22

def call(session)
	if @drop_if_exists
		DropIndex.new(@name, if_exists: true).call(session)
	end
	
	if @unique
		statement = session.clause("CREATE UNIQUE INDEX")
	else
		statement = session.clause("CREATE INDEX")
	end
	
	if @if_not_exists
		statement.clause("IF NOT EXISTS")
	end
	
	statement.identifier(@name)
	
	statement.clause("ON")
	statement.identifier(@table)
	
	if @method
		statement.clause("USING")
		statement.identifier(@method)
	end
	
	statement.clause("(")
	first = true
	indexes = []
	
	@columns.each do |name|
		if first
			first = false
		else
			statement.clause(",")
		end
		
		statement.identifier(name)
	end
	
	statement.clause(")")
	
	Console.logger.info(self, statement)
	statement.call
end