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



104
105
106
# File 'lib/switchman/active_record/base.rb', line 104

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

Instance Method Details

#_run_initialize_callbacksObject



108
109
110
111
112
113
114
115
# File 'lib/switchman/active_record/base.rb', line 108

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

#cloneObject



146
147
148
149
150
151
152
153
# File 'lib/switchman/active_record/base.rb', line 146

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



142
143
144
# File 'lib/switchman/active_record/base.rb', line 142

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

#hashObject



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

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

#id_for_databaseObject



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

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



177
178
179
180
181
# File 'lib/switchman/active_record/base.rb', line 177

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

#saveObject



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

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

#save!Object



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

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

#shardObject



117
118
119
# File 'lib/switchman/active_record/base.rb', line 117

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

#shard=(new_shard) ⇒ Object

Raises:

  • (::ActiveRecord::ReadOnlyRecord)


121
122
123
124
125
126
127
128
129
130
# File 'lib/switchman/active_record/base.rb', line 121

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



172
173
174
175
# File 'lib/switchman/active_record/base.rb', line 172

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

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



155
156
157
158
159
# File 'lib/switchman/active_record/base.rb', line 155

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

#update_columnsObject



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

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

#with_transaction_returning_statusObject



161
162
163
164
165
166
# File 'lib/switchman/active_record/base.rb', line 161

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