mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Migrate unsubscribe keys to the database.
This should reduce a lot of the keys in redis.
This commit is contained in:
46
db/migrate/20150213174159_create_digest_unsubscribe_keys.rb
Normal file
46
db/migrate/20150213174159_create_digest_unsubscribe_keys.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
class CreateDigestUnsubscribeKeys < ActiveRecord::Migration
|
||||
def up
|
||||
create_table :digest_unsubscribe_keys, id: false do |t|
|
||||
t.string :key, limit: 64, null: false
|
||||
t.integer :user_id, null: false
|
||||
t.timestamps
|
||||
end
|
||||
execute "ALTER TABLE digest_unsubscribe_keys ADD PRIMARY KEY (key)"
|
||||
add_index :digest_unsubscribe_keys, :created_at
|
||||
|
||||
migrate_redis_keys
|
||||
end
|
||||
|
||||
# It is slightly odd to migrate from redis to postgres; I imagine a lot
|
||||
# could fail, so if anything does we just rescue
|
||||
def migrate_redis_keys
|
||||
return if Rails.env.test?
|
||||
|
||||
temp_keys = $redis.keys('temporary_key:*')
|
||||
if temp_keys.present?
|
||||
temp_keys.map! do |key|
|
||||
user_id = $redis.get(key).to_i
|
||||
ttl = $redis.ttl(key).to_i
|
||||
|
||||
if ttl > 0
|
||||
ttl = "'#{ttl.seconds.ago.strftime('%Y-%m-%d %H:%M:%S')}'"
|
||||
else
|
||||
ttl = "CURRENT_TIMESTAMP"
|
||||
end
|
||||
$redis.del(key)
|
||||
key.gsub!('temporary_key:', '')
|
||||
user_id ? "('#{key}', #{user_id}, #{ttl}, #{ttl})" : nil
|
||||
end
|
||||
temp_keys.compact!
|
||||
if temp_keys.present?
|
||||
execute "INSERT INTO digest_unsubscribe_keys (key, user_id, created_at, updated_at) VALUES #{temp_keys.join(', ')}"
|
||||
end
|
||||
end
|
||||
rescue
|
||||
# If anything goes wrong, continue with other migrations
|
||||
end
|
||||
|
||||
def down
|
||||
drop_table :digest_unsubscribe_keys
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user