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



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

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



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

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



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

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

#hashObject



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

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

#initialize_dup(*args) ⇒ Object



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

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

#quoted_idObject



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

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



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

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

#save!Object



113
114
115
116
# File 'lib/switchman/active_record/base.rb', line 113

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

#shardObject



94
95
96
# File 'lib/switchman/active_record/base.rb', line 94

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

#shard=(new_shard) ⇒ Object

Raises:

  • (::ActiveRecord::ReadOnlyRecord)


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

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



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

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

#transaction(options = {}, &block) ⇒ Object



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

def transaction(options={}, &block)
  shard.activate(self.class.shard_category) do
    self.class.transaction(options, &block)
  end
end

#update_columnsObject



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

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