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



131
132
133
# File 'lib/switchman/active_record/base.rb', line 131

def self.prepended(klass)
  klass.singleton_class.prepend(ClassMethods)
end

Instance Method Details

#_run_initialize_callbacksObject



135
136
137
138
139
140
141
142
# File 'lib/switchman/active_record/base.rb', line 135

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
  super
end

#cloneObject



173
174
175
176
177
178
179
180
# File 'lib/switchman/active_record/base.rb', line 173

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



169
170
171
# File 'lib/switchman/active_record/base.rb', line 169

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

#hashObject



195
196
197
# File 'lib/switchman/active_record/base.rb', line 195

def hash
  self.class.sharded_primary_key? ? self.class.hash ^ global_id.hash : super
end

#id_for_databaseObject



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

def id_for_database
  if self.class.sharded_primary_key?
    # It's an int, so 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



204
205
206
207
208
# File 'lib/switchman/active_record/base.rb', line 204

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

#saveObject



159
160
161
162
# File 'lib/switchman/active_record/base.rb', line 159

def save(*, **)
  @shard_set_in_stone = true
  super
end

#save!Object



164
165
166
167
# File 'lib/switchman/active_record/base.rb', line 164

def save!(*, **)
  @shard_set_in_stone = true
  super
end

#shardObject



144
145
146
# File 'lib/switchman/active_record/base.rb', line 144

def shard
  @shard || Shard.current(self.class.connection_class_for_self) || Shard.default
end

#shard=(new_shard) ⇒ Object

Raises:

  • (::ActiveRecord::ReadOnlyRecord)


148
149
150
151
152
153
154
155
156
157
# File 'lib/switchman/active_record/base.rb', line 148

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

  return if shard == new_shard

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

#to_paramObject



199
200
201
202
# File 'lib/switchman/active_record/base.rb', line 199

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

#transaction(**kwargs, &block) ⇒ Object



182
183
184
185
186
# File 'lib/switchman/active_record/base.rb', line 182

def transaction(**kwargs, &block)
  shard.activate(self.class.connection_class_for_self) do
    self.class.transaction(**kwargs, &block)
  end
end

#update_columnsObject



210
211
212
213
# File 'lib/switchman/active_record/base.rb', line 210

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

#with_transaction_returning_statusObject



188
189
190
191
192
193
# File 'lib/switchman/active_record/base.rb', line 188

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