Module: Switchman::ActiveRecord::Base

Defined in:
lib/switchman/active_record/base.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(klass) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/switchman/active_record/base.rb', line 200

def self.prepended(klass)
  klass.singleton_class.prepend(ClassMethods)
  klass.scope :non_shadow, lambda { |key = primary_key|
                             where(key => (QueryMethods::NonTransposingValue.new(0)..
                                           QueryMethods::NonTransposingValue.new(Shard::IDS_PER_SHARD)))
                           }
  klass.scope :shadow, lambda { |key = primary_key|
                         where(key => QueryMethods::NonTransposingValue.new(Shard::IDS_PER_SHARD)..)
                       }
end

Instance Method Details

#_run_initialize_callbacksObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/switchman/active_record/base.rb', line 211

def _run_initialize_callbacks
  @shard ||= if self.class.sharded_primary_key?
               Shard.shard_for(self[self.class.primary_key], Shard.current(self.class.connection_class_for_self))
             else
               Shard.current(self.class.connection_class_for_self)
             end

  @loaded_from_shard ||= Shard.current(self.class.connection_class_for_self)
  if shadow_record? && !Switchman.config[:writable_shadow_records]
    @readonly = true
    @readonly_from_shadow ||= true
  end
  super
end

#canonical?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/switchman/active_record/base.rb', line 238

def canonical?
  !shadow_record?
end

#cloneObject



317
318
319
320
321
322
323
324
# File 'lib/switchman/active_record/base.rb', line 317

def clone
  result = super
  # TODO: adjust foreign keys
  # don't use the setter, cause the foreign keys are already
  # relative to this shard
  result.instance_variable_set(:@shard, shard)
  result
end

#destroyObject



313
314
315
# File 'lib/switchman/active_record/base.rb', line 313

def destroy
  shard.activate(self.class.connection_class_for_self) { super }
end

#destroy_shadow_records(target_shards: [Shard.current]) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/switchman/active_record/base.rb', line 258

def destroy_shadow_records(target_shards: [Shard.current])
  raise Errors::ShadowRecordError, "Cannot be called on a shadow record." if shadow_record?

  unless self.class.sharded_column?(self.class.primary_key)
    raise Errors::MethodUnsupportedForUnshardedTableError,
          "Cannot be called on a record belonging to an unsharded table."
  end

  Array(target_shards).each do |target_shard|
    next if target_shard == shard

    target_shard.activate { self.class.where("id = ?", global_id).delete_all }
  end
end

#hashObject



339
340
341
# File 'lib/switchman/active_record/base.rb', line 339

def hash
  self.class.sharded_primary_key? ? [self.class, global_id].hash : super
end

#id_for_databaseObject



359
360
361
362
363
364
365
366
367
368
369
# File 'lib/switchman/active_record/base.rb', line 359

def id_for_database
  if self.class.sharded_primary_key?
    # It's an int, so it's safe to just return it without passing it
    # through anything else. In theory we should do
    # `@attributes[@primary_key].type.serialize(id)`, but that seems to
    # have surprising side-effects
    id
  else
    super
  end
end

#initialize_dup(*args) ⇒ Object



348
349
350
351
352
# File 'lib/switchman/active_record/base.rb', line 348

def initialize_dup(*args)
  copy = super
  @shard_set_in_stone = false
  copy
end

#loaded_from_shardObject

Returns “the shard that this record was actually loaded from” , as opposed to “the shard this record belongs on”, which might be different if this is a shadow record.



276
277
278
# File 'lib/switchman/active_record/base.rb', line 276

def loaded_from_shard
  @loaded_from_shard || shard
end

#readonly!Object



226
227
228
229
# File 'lib/switchman/active_record/base.rb', line 226

def readonly!
  @readonly_from_shadow = false
  super
end

#saveObject



299
300
301
302
303
304
# File 'lib/switchman/active_record/base.rb', line 299

def save(*, **)
  raise Errors::ManuallyCreatedShadowRecordError if creating_shadow_record?

  @shard_set_in_stone = true
  super
end

#save!Object



306
307
308
309
310
311
# File 'lib/switchman/active_record/base.rb', line 306

def save!(*, **)
  raise Errors::ManuallyCreatedShadowRecordError if creating_shadow_record?

  @shard_set_in_stone = true
  super
end

#save_shadow_record(new_attrs: attributes, target_shard: Shard.current) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/switchman/active_record/base.rb', line 242

def save_shadow_record(new_attrs: attributes, target_shard: Shard.current)
  return if target_shard == shard

  shadow_attrs = {}
  new_attrs.each do |attr, value|
    shadow_attrs[attr] = if self.class.sharded_column?(attr)
                           Shard.relative_id_for(value, shard, target_shard)
                         else
                           value
                         end
  end
  target_shard.activate do
    self.class.upsert(shadow_attrs, unique_by: self.class.primary_key)
  end
end

#shadow_record?Boolean

Returns:

  • (Boolean)


231
232
233
234
235
236
# File 'lib/switchman/active_record/base.rb', line 231

def shadow_record?
  pkey = self[self.class.primary_key]
  return false unless self.class.sharded_column?(self.class.primary_key) && pkey

  pkey > Shard::IDS_PER_SHARD
end

#shardObject



280
281
282
# File 'lib/switchman/active_record/base.rb', line 280

def shard
  @shard || fallback_shard
end

#shard=(new_shard) ⇒ Object

Raises:

  • (::ActiveRecord::ReadOnlyRecord)


284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/switchman/active_record/base.rb', line 284

def shard=(new_shard)
  raise ::ActiveRecord::ReadOnlyRecord if !new_record? || @shard_set_in_stone

  if shard == new_shard
    @loaded_from_shard = new_shard
    return
  end

  attributes.each do |attr, value|
    self[attr] = Shard.relative_id_for(value, shard, new_shard) if self.class.sharded_column?(attr)
  end
  @loaded_from_shard = new_shard
  @shard = new_shard
end

#to_paramObject



343
344
345
346
# File 'lib/switchman/active_record/base.rb', line 343

def to_param
  short_id = Shard.short_id_for(id)
  short_id&.to_s
end

#transactionObject



326
327
328
329
330
# File 'lib/switchman/active_record/base.rb', line 326

def transaction(...)
  shard.activate(self.class.connection_class_for_self) do
    self.class.transaction(...)
  end
end

#update_columnsObject



354
355
356
357
# File 'lib/switchman/active_record/base.rb', line 354

def update_columns(*)
  db = shard.database_server
  db.unguard { super }
end

#with_transaction_returning_statusObject



332
333
334
335
336
337
# File 'lib/switchman/active_record/base.rb', line 332

def with_transaction_returning_status
  shard.activate(self.class.connection_class_for_self) do
    db = Shard.current(self.class.connection_class_for_self).database_server
    db.unguard { super }
  end
end