do incremented_value_for in a transaction to make sure we get the right key back
do sqlite checks based on Keystore.connection.adapter_name rather than assuming one environment uses sqlite and the others don't
This commit is contained in:
parent
72b67b0103
commit
47f1c44fc9
|
@ -8,7 +8,7 @@ class Keystore < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.put(key, value)
|
def self.put(key, value)
|
||||||
if Rails.env == "test"
|
if Keystore.connection.adapter_name == "SQLite"
|
||||||
Keystore.connection.execute("INSERT OR REPLACE INTO " <<
|
Keystore.connection.execute("INSERT OR REPLACE INTO " <<
|
||||||
"#{Keystore.table_name} (`key`, `value`) VALUES " <<
|
"#{Keystore.table_name} (`key`, `value`) VALUES " <<
|
||||||
"(#{q(key)}, #{q(value)})")
|
"(#{q(key)}, #{q(value)})")
|
||||||
|
@ -28,19 +28,23 @@ class Keystore < ActiveRecord::Base
|
||||||
def self.incremented_value_for(key, amount = 1)
|
def self.incremented_value_for(key, amount = 1)
|
||||||
new_value = nil
|
new_value = nil
|
||||||
|
|
||||||
if Rails.env == "test"
|
Keystore.transaction do
|
||||||
Keystore.connection.execute("INSERT OR IGNORE INTO " <<
|
if Keystore.connection.adapter_name == "SQLite"
|
||||||
"#{Keystore.table_name} (`key`, `value`) VALUES " <<
|
Keystore.connection.execute("INSERT OR IGNORE INTO " <<
|
||||||
"(#{q(key)}, 0)")
|
"#{Keystore.table_name} (`key`, `value`) VALUES " <<
|
||||||
Keystore.connection.execute("UPDATE #{Keystore.table_name} " <<
|
"(#{q(key)}, 0)")
|
||||||
"SET `value` = `value` + #{q(amount)} WHERE `key` = #{q(key)}")
|
Keystore.connection.execute("UPDATE #{Keystore.table_name} " <<
|
||||||
else
|
"SET `value` = `value` + #{q(amount)} WHERE `key` = #{q(key)}")
|
||||||
Keystore.connection.execute("INSERT INTO #{Keystore.table_name} (" +
|
else
|
||||||
"`key`, `value`) VALUES (#{q(key)}, #{q(amount)}) ON DUPLICATE KEY " +
|
Keystore.connection.execute("INSERT INTO #{Keystore.table_name} (" +
|
||||||
"UPDATE `value` = `value` + #{q(amount)}")
|
"`key`, `value`) VALUES (#{q(key)}, #{q(amount)}) ON DUPLICATE KEY " +
|
||||||
|
"UPDATE `value` = `value` + #{q(amount)}")
|
||||||
|
end
|
||||||
|
|
||||||
|
new_value = self.value_for(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
return self.value_for(key)
|
return new_value
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.decrement_value_for(key, amount = -1)
|
def self.decrement_value_for(key, amount = -1)
|
||||||
|
|
Loading…
Reference in a new issue