Module: ActiveRecord::FullTextSearch::SchemaStatements

Defined in:
lib/active_record/full_text_search/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#add_text_search_configuration_mapping(name, token_types, dictionaries) ⇒ Object



133
134
135
# File 'lib/active_record/full_text_search/schema_statements.rb', line 133

def add_text_search_configuration_mapping(name, token_types, dictionaries)
  execute "ALTER TEXT SEARCH CONFIGURATION public.#{name} ADD MAPPING FOR #{token_types.join(", ")} WITH #{dictionaries.join(", ")}"
end

#change_text_search_configuration_mapping(name, token_types, dictionaries) ⇒ Object



137
138
139
# File 'lib/active_record/full_text_search/schema_statements.rb', line 137

def change_text_search_configuration_mapping(name, token_types, dictionaries)
  execute "ALTER TEXT SEARCH CONFIGURATION public.#{name} ALTER MAPPING FOR #{token_types.join(", ")} WITH #{dictionaries.join(", ")}"
end

#change_text_search_dictionary_option(name, option, value = :default) ⇒ Object



100
101
102
# File 'lib/active_record/full_text_search/schema_statements.rb', line 100

def change_text_search_dictionary_option(name, option, value = :default)
  execute("ALTER TEXT SEARCH DICTIONARY public.#{name} SET #{option} #{value if value != :default}")
end

#create_function(name_with_args, as:, volatility: :volatile, language: nil, returns: :void, replace: true) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_record/full_text_search/schema_statements.rb', line 9

def create_function(name_with_args, as:, volatility: :volatile, language: nil, returns: :void, replace: true)
  name_with_args = "#{name_with_args}()" unless name_with_args.to_s.include?("(")
  language = "plpgsql" if returns == :trigger
  language ||= "sql"
  execute(<<-SQL)
    CREATE #{"OR REPLACE" if replace} FUNCTION #{name_with_args}
    RETURNS #{returns}
    AS $$
    #{as}
    $$
    LANGUAGE #{language}
    #{volatility.to_s.upcase}
  SQL
end

#create_text_search_configuration(name, parser: nil, copy: nil) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/active_record/full_text_search/schema_statements.rb', line 121

def create_text_search_configuration(name, parser: nil, copy: nil)
  if copy
    execute("CREATE TEXT SEARCH CONFIGURATION public.#{name} (COPY = #{copy})")
  else
    execute("CREATE TEXT SEARCH CONFIGURATION public.#{name} (PARSER = '#{parser || "default"}')")
  end
end

#create_text_search_dictionary(name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
# File 'lib/active_record/full_text_search/schema_statements.rb', line 91

def create_text_search_dictionary(name, options = {})
  raise ArgumentError, "Must specify :template" unless (template = options.delete(:template))
  execute("CREATE TEXT SEARCH DICTIONARY public.#{name} (TEMPLATE = #{template}#{options.map { |k, v| ", #{k} = '#{v}'" }.join})")
end

#create_text_search_parser(name, start:, gettoken:, end:, lextypes:, headline: nil) ⇒ Object



108
109
110
111
# File 'lib/active_record/full_text_search/schema_statements.rb', line 108

def create_text_search_parser(name, start:, gettoken:, end:, lextypes:, headline: nil)
  options = {start: start, gettoken: gettoken, end: binding.local_variable_get(:end), lextypes: lextypes, headline: headline}.compact
  execute("CREATE TEXT SEARCH PARSER public.#{name} (#{options.map { |k, v| "#{k.upcase} = #{v}" }.join(", ")})")
end

#create_text_search_template(name, lexize:, init: nil) ⇒ Object



78
79
80
81
# File 'lib/active_record/full_text_search/schema_statements.rb', line 78

def create_text_search_template(name, lexize:, init: nil)
  options = {init: init, lexize: lexize}.compact
  execute("CREATE TEXT SEARCH TEMPLATE public.#{name} (#{options.map { |k, v| "#{k.upcase} = #{v}" }.join(", ")})")
end

#create_trigger(table, function, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/active_record/full_text_search/schema_statements.rb', line 30

def create_trigger(table, function, options = {})
  raise ArgumentError, "function name is invalid" unless /\A\w+\z/.match?(function.to_s)
  raise ArgumentError, "Must specify one and only one of the options :before, :after, or :instead_of" unless %i[before after instead_of].select { |t| options.key?(t) }.count == 1
  raise ArgumentError, "for_each must be :row or :statement" if options[:for_each] && !%i[row statement].include?(options[:for_each])

  timing = %i[before after instead_of].find { |t| options.key?(t) }
  operations = options[timing] || raise(ArgumentError, "Must specify operations for #{timing} trigger")
  operations.detect { |op| %i[insert update delete].exclude?(op) } && raise(ArgumentError, "Invalid operation for trigger: #{operations.inspect}")
  for_each = "FOR EACH #{options[:for_each].to_s.upcase}" if options[:for_each]
  if options[:deferrable] == :initially_deferred
    deferrability = "DEFERRABLE INITIALLY DEFERRED"
  elsif options[:deferrable] == :initially_immediate
    deferrability = "DEFERRABLE INITIALLY IMMEDIATE"
  elsif options[:deferrable] == true
    deferrability = "DEFERRABLE"
  elsif options[:deferrable] == false
    deferrability = "NOT DEFERRABLE"
  elsif options[:deferrable]
    raise ArgumentError, "Invalid value for :deferrable"
  end
  condition = options[:when] ? "WHEN (#{options[:when]})" : ""
  operations = [operations].flatten.map do |event|
    if %i[insert update delete].include?(event)
      event.to_s.upcase
    # elsif event.is_a?(Hash)
    #   raise ArgumentError, "Key must be :update" unless event.keys.size == 1 && event.keys.first == :update
    #   "UPDATE OF #{[event[:update]].flatten.map { |c| quote_column_name(c) }.join(", ")}"
    else
      raise ArgumentError, "Unsupported event: #{event.inspect}"
    end
  end
  name = options[:name] || default_trigger_name(table, function, timing, operations)

  execute "CREATE TRIGGER #{name} #{timing.to_s.upcase} #{operations.join(" OR ")} ON #{table} #{for_each} #{deferrability} #{condition} EXECUTE FUNCTION #{function}()"
end

#drop_function(name_with_args, options = {}) ⇒ Object



24
25
26
27
28
# File 'lib/active_record/full_text_search/schema_statements.rb', line 24

def drop_function(name_with_args, options = {})
  if_exists = options[:if_exists]
  cascade = options[:cascade]
  execute "DROP FUNCTION #{"IF EXISTS" if if_exists} #{name_with_args} #{"CASCADE" if cascade}"
end

#drop_text_search_configuration(name, if_exists: false, cascade: :restrict) ⇒ Object



149
150
151
# File 'lib/active_record/full_text_search/schema_statements.rb', line 149

def drop_text_search_configuration(name, if_exists: false, cascade: :restrict)
  execute "DROP TEXT SEARCH CONFIGURATION #{"IF EXISTS" if if_exists} public.#{name} #{"CASCADE" if cascade == :cascade}"
end

#drop_text_search_configuration_mapping(name, token_types, if_exists: false) ⇒ Object



145
146
147
# File 'lib/active_record/full_text_search/schema_statements.rb', line 145

def drop_text_search_configuration_mapping(name, token_types, if_exists: false)
  execute "ALTER TEXT SEARCH CONFIGURATION public.#{name} DROP MAPPING #{"IF EXISTS" if if_exists} FOR #{token_types.join(", ")}"
end

#drop_text_search_dictionary(name, if_exists: false, cascade: :restrict) ⇒ Object



104
105
106
# File 'lib/active_record/full_text_search/schema_statements.rb', line 104

def drop_text_search_dictionary(name, if_exists: false, cascade: :restrict)
  execute "DROP TEXT SEARCH DICTIONARY #{"IF EXISTS" if if_exists} public.#{name} #{"CASCADE" if cascade == :cascade}"
end

#drop_text_search_parser(name, if_exists: false, cascade: :restrict) ⇒ Object



117
118
119
# File 'lib/active_record/full_text_search/schema_statements.rb', line 117

def drop_text_search_parser(name, if_exists: false, cascade: :restrict)
  execute "DROP TEXT SEARCH PARSER #{"IF EXISTS" if if_exists} public.#{name} #{"CASCADE" if cascade == :cascade}"
end

#drop_text_search_template(name, if_exists: false, cascade: :restrict) ⇒ Object



87
88
89
# File 'lib/active_record/full_text_search/schema_statements.rb', line 87

def drop_text_search_template(name, if_exists: false, cascade: :restrict)
  execute "DROP TEXT SEARCH TEMPLATE #{"IF EXISTS" if if_exists} public.#{name} #{"CASCADE" if cascade == :cascade}"
end

#drop_trigger(table, function, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_record/full_text_search/schema_statements.rb', line 66

def drop_trigger(table, function, options = {})
  if_exists = options[:if_exists]
  cascade = options[:cascade]
  if options.keys.intersect?(%i[before after instead_of])
    raise ArgumentError, "Must specify only one of the options :before, :after, or :instead_of" unless %i[before after instead_of].select { |t| options.key?(t) }.count == 1
    timing = %i[before after instead_of].find { |t| options.key?(t) }
    operations = options[timing] || raise(ArgumentError, "Must specify operations for #{timing} trigger")
  end
  name = options[:name] || default_trigger_name(table, function, timing, operations)
  execute "DROP TRIGGER #{"IF EXISTS" if if_exists} #{name} ON #{table} #{"CASCADE" if cascade}"
end

#max_trigger_name_sizeObject



153
154
155
# File 'lib/active_record/full_text_search/schema_statements.rb', line 153

def max_trigger_name_size
  62
end

#rename_text_search_configuration(name, to) ⇒ Object



129
130
131
# File 'lib/active_record/full_text_search/schema_statements.rb', line 129

def rename_text_search_configuration(name, to)
  execute "ALTER TEXT SEARCH CONFIGURATION public.#{name} RENAME TO #{to}"
end

#rename_text_search_dictionary(name, to) ⇒ Object



96
97
98
# File 'lib/active_record/full_text_search/schema_statements.rb', line 96

def rename_text_search_dictionary(name, to)
  execute("ALTER TEXT SEARCH DICTIONARY public.#{name} RENAME TO #{to}")
end

#rename_text_search_parser(name, to) ⇒ Object



113
114
115
# File 'lib/active_record/full_text_search/schema_statements.rb', line 113

def rename_text_search_parser(name, to)
  execute "ALTER TEXT SEARCH PARSER public.#{name} RENAME TO #{to}"
end

#rename_text_search_template(name, to) ⇒ Object



83
84
85
# File 'lib/active_record/full_text_search/schema_statements.rb', line 83

def rename_text_search_template(name, to)
  execute "ALTER TEXT SEARCH TEMPLATE public.#{name} RENAME TO #{to}"
end

#replace_text_search_configuration_dictionary(name, from:, to:) ⇒ Object



141
142
143
# File 'lib/active_record/full_text_search/schema_statements.rb', line 141

def replace_text_search_configuration_dictionary(name, from:, to:)
  execute "ALTER TEXT SEARCH CONFIGURATION public.#{name} ALTER MAPPING REPLACE #{from} WITH #{to}"
end