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

.included(klass) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/switchman/active_record/base.rb', line 83

def self.included(klass)
  klass.extend(ClassMethods)
  klass.set_callback(:initialize, :before) do
    unless @shard
      if self.class.sharded_primary_key?
        @shard = Shard.shard_for(self[self.class.primary_key], Shard.current(self.class.shard_category))
      else
        @shard = Shard.current(self.class.shard_category)
      end
    end
  end
end

Instance Method Details

#cloneObject



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

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, self.shard)
  result
end

#destroyObject



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

def destroy
  self.class.shard(shard, :implicit).scoping { super }
end

#hashObject



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

def hash
  self.class.sharded_primary_key? ? self.class.hash ^ Shard.global_id_for(id).hash : super
end

#initialize_dup(*args) ⇒ Object



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

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

#quoted_idObject



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

def quoted_id
  return super unless self.class.sharded_primary_key?
  # do this the Rails 4.2 way, so that if Shard.current != self.shard, the id gets transposed
  self.class.connection.quote(id)
end

#saveObject



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

def save(*, **)
  @shard_set_in_stone = true
  (self.class.current_scope || self.class.default_scoped).shard(shard, :implicit).scoping { super }
end

#save!Object



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

def save!(*, **)
  @shard_set_in_stone = true
  (self.class.current_scope || self.class.default_scoped).shard(shard, :implicit).scoping { super }
end

#shardObject



96
97
98
# File 'lib/switchman/active_record/base.rb', line 96

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

#shard=(new_shard) ⇒ Object

Raises:

  • (::ActiveRecord::ReadOnlyRecord)


100
101
102
103
104
105
106
107
108
# File 'lib/switchman/active_record/base.rb', line 100

def shard=(new_shard)
  raise ::ActiveRecord::ReadOnlyRecord if !self.new_record? || @shard_set_in_stone
  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
end

#to_paramObject



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

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

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



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

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

#update_columnsObject



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

def update_columns(*)
  db = Shard.current(self.class.shard_category).database_server
  if ::GuardRail.environment != db.guard_rail_environment
    return db.unguard { super }
  else
    super
  end
end