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



90
91
92
# File 'lib/switchman/active_record/base.rb', line 90

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

Instance Method Details

#_run_initialize_callbacksObject



94
95
96
97
98
99
100
101
# File 'lib/switchman/active_record/base.rb', line 94

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



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

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



128
129
130
# File 'lib/switchman/active_record/base.rb', line 128

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

#hashObject



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

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

#id_for_databaseObject



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

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



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

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

#saveObject



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

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

#save!Object



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

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

#shardObject



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

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

#shard=(new_shard) ⇒ Object

Raises:

  • (::ActiveRecord::ReadOnlyRecord)


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

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



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

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

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



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

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

#update_columnsObject



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

def update_columns(*)
  db = shard.database_server
  return db.unguard { super } if ::GuardRail.environment != db.guard_rail_environment

  super
end

#with_transaction_returning_statusObject



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

def with_transaction_returning_status
  shard.activate(self.class.connection_classes) do
    super
  end
end