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
- #_run_initialize_callbacks ⇒ Object
- #clone ⇒ Object
- #destroy ⇒ Object
- #hash ⇒ Object
- #id_for_database ⇒ Object
- #initialize_dup(*args) ⇒ Object
- #save ⇒ Object
- #save! ⇒ Object
- #save_shadow_record(new_attrs: attributes, target_shard: Shard.current) ⇒ Object
- #shadow_record? ⇒ Boolean
- #shard ⇒ Object
- #shard=(new_shard) ⇒ Object
- #to_param ⇒ Object
- #transaction(**kwargs, &block) ⇒ Object
- #update_columns ⇒ Object
- #with_transaction_returning_status ⇒ Object
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_callbacks ⇒ Object
135 136 137 138 139 140 141 142 143 |
# 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 readonly! if shadow_record? super end |
#clone ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/switchman/active_record/base.rb', line 197 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
193 194 195 |
# File 'lib/switchman/active_record/base.rb', line 193 def destroy shard.activate(self.class.connection_class_for_self) { super } end |
#hash ⇒ Object
219 220 221 |
# File 'lib/switchman/active_record/base.rb', line 219 def hash self.class.sharded_primary_key? ? self.class.hash ^ global_id.hash : super end |
#id_for_database ⇒ Object
239 240 241 242 243 244 245 246 247 |
# File 'lib/switchman/active_record/base.rb', line 239 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
228 229 230 231 232 |
# File 'lib/switchman/active_record/base.rb', line 228 def initialize_dup(*args) copy = super @shard_set_in_stone = false copy end |
#save ⇒ Object
183 184 185 186 |
# File 'lib/switchman/active_record/base.rb', line 183 def save(*, **) @shard_set_in_stone = true super end |
#save! ⇒ Object
188 189 190 191 |
# File 'lib/switchman/active_record/base.rb', line 188 def save!(*, **) @shard_set_in_stone = true super end |
#save_shadow_record(new_attrs: attributes, target_shard: Shard.current) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/switchman/active_record/base.rb', line 152 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
145 146 147 148 149 150 |
# File 'lib/switchman/active_record/base.rb', line 145 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 |
#shard ⇒ Object
168 169 170 |
# File 'lib/switchman/active_record/base.rb', line 168 def shard @shard || Shard.current(self.class.connection_class_for_self) || Shard.default end |
#shard=(new_shard) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/switchman/active_record/base.rb', line 172 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
223 224 225 226 |
# File 'lib/switchman/active_record/base.rb', line 223 def to_param short_id = Shard.short_id_for(id) short_id&.to_s end |
#transaction(**kwargs, &block) ⇒ Object
206 207 208 209 210 |
# File 'lib/switchman/active_record/base.rb', line 206 def transaction(**kwargs, &block) shard.activate(self.class.connection_class_for_self) do self.class.transaction(**kwargs, &block) end end |
#update_columns ⇒ Object
234 235 236 237 |
# File 'lib/switchman/active_record/base.rb', line 234 def update_columns(*) db = shard.database_server db.unguard { super } end |
#with_transaction_returning_status ⇒ Object
212 213 214 215 216 217 |
# File 'lib/switchman/active_record/base.rb', line 212 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 |