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



125
126
127
# File 'lib/switchman/active_record/base.rb', line 125

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

Instance Method Details

#_run_initialize_callbacks ⇒ Object



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

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

#clone ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/switchman/active_record/base.rb', line 167

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

#destroy ⇒ Object



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

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

#hash ⇒ Object



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

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

#id_for_database ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/switchman/active_record/base.rb', line 209

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



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

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

#save ⇒ Object



153
154
155
156
# File 'lib/switchman/active_record/base.rb', line 153

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

#save! ⇒ Object



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

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

#shard ⇒ Object



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

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

#shard=(new_shard) ⇒ Object

Raises:

  • (::ActiveRecord::ReadOnlyRecord)


142
143
144
145
146
147
148
149
150
151
# File 'lib/switchman/active_record/base.rb', line 142

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_param ⇒ Object



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

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

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



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

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

#update_columns ⇒ Object



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

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

#with_transaction_returning_status ⇒ Object



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

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