Class: GroongaClientModel::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga_client_model/migration.rb

Defined Under Namespace

Classes: CreateTableMigration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Migration

Returns a new instance of Migration.



51
52
53
54
55
56
# File 'lib/groonga_client_model/migration.rb', line 51

def initialize(client)
  @client = client
  @output = nil
  @reverting = false
  @pending_actions = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



49
50
51
# File 'lib/groonga_client_model/migration.rb', line 49

def client
  @client
end

#outputObject

Returns the value of attribute output.



48
49
50
# File 'lib/groonga_client_model/migration.rb', line 48

def output
  @output
end

Instance Method Details

#add_column(table_name, column_name, value_type, flags: nil, type: nil, sources: nil, source: nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/groonga_client_model/migration.rb', line 140

def add_column(table_name, column_name, value_type,
               flags: nil,
               type: nil,
               sources: nil,
               source: nil)
  if @reverting
    @pending_actions << [:remove_column, table_name, column_name]
    return
  end

  value_type = normalize_type(value_type)
  type = normalize_column_type(type || :scalar)
  sources ||= source || []
  flags = Array(flags) | [type]
  if type == "COLUMN_INDEX"
    schema = GroongaClientModel::Schema.new
    case schema.tables[table_name].tokenizer
    when nil, "TokenDelimit"
      # do nothing
    else
      flags |= ["WITH_POSITION"]
    end
    if sources.size > 1
      flags |= ["WITH_SECTION"]
    end
  end
  options = {
    flags: flags,
    value_type: value_type,
  }
  options[:sources] = sources unless sources.blank?
  report(__method__, [table_name, column_name, options]) do
    @client.request(:column_create).
      parameter(:table, table_name).
      parameter(:name, column_name).
      flags_parameter(:flags, flags).
      parameter(:type, value_type).
      values_parameter(:source, sources).
      response
  end
end

#add_index(table_name, source_table_name, source_column_names, **options) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/groonga_client_model/migration.rb', line 242

def add_index(table_name, source_table_name, source_column_names, **options)
  source_column_names = Array(source_column_names)
  column_name = options.delete(:name)
  column_name ||= [source_table_name, *source_column_names].join("_")
  add_column(table_name,
             column_name,
             source_table_name,
             **options.merge(:type => :index,
                             :sources => source_column_names))
end

#add_timestamp_columns(table_name) ⇒ Object



212
213
214
215
# File 'lib/groonga_client_model/migration.rb', line 212

def add_timestamp_columns(table_name)
  add_column(table_name, :created_at, :time)
  add_column(table_name, :updated_at, :time)
end

#copy_column(from_full_column_name, to_full_column_name) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/groonga_client_model/migration.rb', line 222

def copy_column(from_full_column_name,
                to_full_column_name)
  if @reverting
    message = "can't revert copy_column"
    message << "(#{from_full_column_name}, #{to_full_column_name})"
    raise IrreversibleMigrationError, message
  end

  from_table_name, from_column_name = from_full_column_name.split(".", 2)
  to_table_name, to_column_name = to_full_column_name.split(".", 2)
  report(__method__, [from_full_column_name, to_full_column_name]) do
    @client.request(:column_copy).
      parameter(:from_table, from_table_name).
      parameter(:from_name, from_column_name).
      parameter(:to_table, to_table_name).
      parameter(:to_name, to_column_name).
      response
  end
end

#copy_table(from_table_name, to_table_name) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/groonga_client_model/migration.rb', line 126

def copy_table(from_table_name, to_table_name)
  if @reverting
    message = "can't revert copy_table(#{from_table_name}, #{to_table_name})"
    raise IrreversibleMigrationError, message
  end

  report(__method__, [from_table_name, to_table_name]) do
    @client.request(:table_copy).
      parameter(:from_name, from_table_name).
      parameter(:to_name, to_table_name).
      response
  end
end

#create_table(name, type: nil, key_type: nil, tokenizer: nil, default_tokenizer: nil, normalizer: nil, propose: nil) {|CreateTableMigration.new(self, name)| ... } ⇒ Object

Yields:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/groonga_client_model/migration.rb', line 68

def create_table(name,
                 type: nil,
                 key_type: nil,
                 tokenizer: nil,
                 default_tokenizer: nil,
                 normalizer: nil,
                 propose: nil)
  if @reverting
    @pending_actions << [:remove_table, name]
    return
  end

  case propose
  when :full_text_search
    type ||= :patricia_trie
    tokenizer ||= :bigram
    normalizer ||= :auto
  end

  type = normalize_table_type(type || :array)
  if type != "TABLE_NO_KEY" and key_type.nil?
    key_type ||= "ShortText"
  end
  key_type = normalize_type(key_type)
  if type != "TABLE_NO_KEY" and key_type == "ShortText"
    tokenizer ||= default_tokenizer
    tokenizer = normalize_tokenizer(tokenizer)
    normalizer = normalize_normalizer(normalizer)
  end
  options = {type: type}
  options[:key_type] = key_type if key_type
  options[:tokenizer] = tokenizer if tokenizer
  options[:normalizer] = normalizer if normalizer
  report(__method__, [name, options]) do
    @client.request(:table_create).
      parameter(:name, name).
      flags_parameter(:flags, [type]).
      parameter(:key_type, key_type).
      parameter(:default_tokenizer, tokenizer).
      parameter(:normalizer, normalizer).
      response
  end

  yield(CreateTableMigration.new(self, name)) if block_given?
end

#delete_config(key) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/groonga_client_model/migration.rb', line 267

def delete_config(key)
  if @reverting
    message = "can't revert delete_config(#{key.inspect})"
    raise IrreversibleMigrationError, message
  end

  report(__method__, [key]) do
    @client.request(:config_delete).
      parameter(:key, key).
      response
  end
end

#downObject



62
63
64
65
66
# File 'lib/groonga_client_model/migration.rb', line 62

def down
  revert do
    change
  end
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


303
304
305
306
307
308
# File 'lib/groonga_client_model/migration.rb', line 303

def exist?(name)
  @client.request(:object_exist).
    parameter(:name, name).
    response.
    body
end

#load(table_name, values, options = {}) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/groonga_client_model/migration.rb', line 280

def load(table_name, values, options={})
  if @reverting
    message = "can't revert load(#{table_name.inspect})"
    raise IrreversibleMigrationError, message
  end

  case values
  when Hash
    json_values = [values].to_json
  when Array
    json_values = values.to_json
  else
    json_values = values
  end
  report(__method__, [table_name]) do
    @client.request(:load).
      parameter(:table, table_name).
      values_parameter(:columns, options[:columns]).
      parameter(:values, json_values).
      response
  end
end

#remove_column(table_name, column_name) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/groonga_client_model/migration.rb', line 198

def remove_column(table_name, column_name)
  if @reverting
    message = "can't revert remove_column(#{table_name}, #{column_name})"
    raise IrreversibleMigrationError, message
  end

  report(__method__, [table_name, column_name]) do
    @client.request(:column_remove).
      parameter(:table, table_name).
      parameter(:name, column_name).
      response
  end
end

#remove_table(name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/groonga_client_model/migration.rb', line 114

def remove_table(name)
  if @reverting
    raise IrreversibleMigrationError, "can't revert remove_table(#{name})"
  end

  report(__method__, [name]) do
    @client.request(:table_remove).
      parameter(:name, name).
      response
  end
end

#remove_timestamp_columns(table_name) ⇒ Object



217
218
219
220
# File 'lib/groonga_client_model/migration.rb', line 217

def remove_timestamp_columns(table_name)
  remove_column(table_name, :updated_at)
  remove_column(table_name, :created_at)
end

#rename_column(table_name, old_column_name, new_column_name) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/groonga_client_model/migration.rb', line 182

def rename_column(table_name,
                  old_column_name, new_column_name)
  if @reverting
    @pending_actions << [:rename_column, table_name, new_column_name, old_column_name]
    return
  end

  report(__method__, [table_name, old_column_name, new_column_name]) do
    @client.request(:column_rename).
      parameter(:table, table_name).
      parameter(:name, old_column_name).
      parameter(:new_name, new_column_name).
      response
  end
end

#reverting?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/groonga_client_model/migration.rb', line 310

def reverting?
  @reverting
end

#set_config(key, value) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/groonga_client_model/migration.rb', line 253

def set_config(key, value)
  if @reverting
    message = "can't revert set_config(#{key.inspect}, #{value.inspect})"
    raise IrreversibleMigrationError, message
  end

  report(__method__, [key, value]) do
    @client.request(:config_set).
      parameter(:key, key).
      parameter(:value, value).
      response
  end
end

#upObject



58
59
60
# File 'lib/groonga_client_model/migration.rb', line 58

def up
  change
end