2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
class CreateDigestUnsubscribeKeys < ActiveRecord::Migration[4.2]
|
2015-02-13 14:15:49 -05:00
|
|
|
def up
|
|
|
|
create_table :digest_unsubscribe_keys, id: false do |t|
|
|
|
|
t.string :key, limit: 64, null: false
|
|
|
|
t.integer :user_id, null: false
|
2017-08-07 11:48:36 -04:00
|
|
|
t.timestamps null: false
|
2015-02-13 14:15:49 -05:00
|
|
|
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?
|
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
temp_keys = Discourse.redis.keys("temporary_key:*")
|
2015-02-13 14:15:49 -05:00
|
|
|
if temp_keys.present?
|
|
|
|
temp_keys.map! do |key|
|
2019-12-03 04:05:53 -05:00
|
|
|
user_id = Discourse.redis.get(key).to_i
|
|
|
|
ttl = Discourse.redis.ttl(key).to_i
|
2015-02-13 14:15:49 -05:00
|
|
|
|
|
|
|
if ttl > 0
|
|
|
|
ttl = "'#{ttl.seconds.ago.strftime("%Y-%m-%d %H:%M:%S")}'"
|
|
|
|
else
|
|
|
|
ttl = "CURRENT_TIMESTAMP"
|
|
|
|
end
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.del(key)
|
2015-02-13 14:15:49 -05:00
|
|
|
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 StandardError
|
|
|
|
# If anything goes wrong, continue with other migrations
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
drop_table :digest_unsubscribe_keys
|
|
|
|
end
|
|
|
|
end
|