DEV: Apply syntax_tree formatting to plugins/*

This commit is contained in:
David Taylor
2023-01-06 20:42:16 +00:00
parent 93e2dad656
commit 055310cea4
110 changed files with 3712 additions and 3158 deletions

View File

@ -1,31 +1,33 @@
# frozen_string_literal: true
class RenameTotalVotesToVoters < ActiveRecord::Migration[4.2]
def up
PostCustomField.where(name: "polls").find_each do |pcf|
polls = ::JSON.parse(pcf.value)
polls.each_value do |poll|
next if poll.has_key?("voters")
poll["voters"] = poll["total_votes"]
poll.delete("total_votes")
PostCustomField
.where(name: "polls")
.find_each do |pcf|
polls = ::JSON.parse(pcf.value)
polls.each_value do |poll|
next if poll.has_key?("voters")
poll["voters"] = poll["total_votes"]
poll.delete("total_votes")
end
pcf.value = polls.to_json
pcf.save
end
pcf.value = polls.to_json
pcf.save
end
end
def down
PostCustomField.where(name: "polls").find_each do |pcf|
polls = ::JSON.parse(pcf.value)
polls.each_value do |poll|
next if poll.has_key?("total_votes")
poll["total_votes"] = poll["voters"]
poll.delete("voters")
PostCustomField
.where(name: "polls")
.find_each do |pcf|
polls = ::JSON.parse(pcf.value)
polls.each_value do |poll|
next if poll.has_key?("total_votes")
poll["total_votes"] = poll["voters"]
poll.delete("voters")
end
pcf.value = polls.to_json
pcf.save
end
pcf.value = polls.to_json
pcf.save
end
end
end

View File

@ -1,22 +1,27 @@
# frozen_string_literal: true
class MergePollsVotes < ActiveRecord::Migration[4.2]
def up
PostCustomField.where(name: "polls").order(:post_id).pluck(:post_id).each do |post_id|
polls_votes = {}
PostCustomField.where(post_id: post_id).where("name LIKE 'polls-votes-%'").find_each do |pcf|
user_id = pcf.name["polls-votes-".size..-1]
polls_votes["#{user_id}"] = ::JSON.parse(pcf.value || "{}")
end
PostCustomField
.where(name: "polls")
.order(:post_id)
.pluck(:post_id)
.each do |post_id|
polls_votes = {}
PostCustomField
.where(post_id: post_id)
.where("name LIKE 'polls-votes-%'")
.find_each do |pcf|
user_id = pcf.name["polls-votes-".size..-1]
polls_votes["#{user_id}"] = ::JSON.parse(pcf.value || "{}")
end
pcf = PostCustomField.find_or_create_by(name: "polls-votes", post_id: post_id)
pcf.value = ::JSON.parse(pcf.value || "{}").merge(polls_votes).to_json
pcf.save
end
pcf = PostCustomField.find_or_create_by(name: "polls-votes", post_id: post_id)
pcf.value = ::JSON.parse(pcf.value || "{}").merge(polls_votes).to_json
pcf.save
end
end
def down
end
end

View File

@ -1,20 +1,19 @@
# frozen_string_literal: true
class ClosePollsInClosedTopics < ActiveRecord::Migration[4.2]
def up
PostCustomField.joins(post: :topic)
PostCustomField
.joins(post: :topic)
.where("post_custom_fields.name = 'polls'")
.where("topics.closed")
.find_each do |pcf|
polls = ::JSON.parse(pcf.value || "{}")
polls.values.each { |poll| poll["status"] = "closed" }
pcf.value = polls.to_json
pcf.save
end
polls = ::JSON.parse(pcf.value || "{}")
polls.values.each { |poll| poll["status"] = "closed" }
pcf.value = polls.to_json
pcf.save
end
end
def down
end
end

View File

@ -17,7 +17,7 @@ class CreatePollsTables < ActiveRecord::Migration[5.2]
t.timestamps
end
add_index :polls, [:post_id, :name], unique: true
add_index :polls, %i[post_id name], unique: true
create_table :poll_options do |t|
t.references :poll, index: true, foreign_key: true
@ -27,7 +27,7 @@ class CreatePollsTables < ActiveRecord::Migration[5.2]
t.timestamps
end
add_index :poll_options, [:poll_id, :digest], unique: true
add_index :poll_options, %i[poll_id digest], unique: true
create_table :poll_votes, id: false do |t|
t.references :poll, foreign_key: true
@ -36,6 +36,6 @@ class CreatePollsTables < ActiveRecord::Migration[5.2]
t.timestamps
end
add_index :poll_votes, [:poll_id, :poll_option_id, :user_id], unique: true
add_index :poll_votes, %i[poll_id poll_option_id user_id], unique: true
end
end

View File

@ -5,11 +5,7 @@ class MigratePollsData < ActiveRecord::Migration[5.2]
PG::Connection.escape_string(text)
end
POLL_TYPES ||= {
"regular" => 0,
"multiple" => 1,
"number" => 2,
}
POLL_TYPES ||= { "regular" => 0, "multiple" => 1, "number" => 2 }
PG_INTEGER_MAX ||= 2_147_483_647
@ -61,43 +57,59 @@ class MigratePollsData < ActiveRecord::Migration[5.2]
ORDER BY polls.post_id
SQL
DB.query(sql).each do |r|
# for some reasons, polls or votes might be an array
r.polls = r.polls[0] if Array === r.polls && r.polls.size > 0
r.votes = r.votes[0] if Array === r.votes && r.votes.size > 0
DB
.query(sql)
.each do |r|
# for some reasons, polls or votes might be an array
r.polls = r.polls[0] if Array === r.polls && r.polls.size > 0
r.votes = r.votes[0] if Array === r.votes && r.votes.size > 0
existing_user_ids = User.where(id: r.votes.keys).pluck(:id).to_set
existing_user_ids = User.where(id: r.votes.keys).pluck(:id).to_set
# Poll votes are stored in a JSON object with the following hierarchy
# user_id -> poll_name -> options
# Since we're iterating over polls, we need to change the hierarchy to
# poll_name -> user_id -> options
# Poll votes are stored in a JSON object with the following hierarchy
# user_id -> poll_name -> options
# Since we're iterating over polls, we need to change the hierarchy to
# poll_name -> user_id -> options
votes = {}
r.votes.each do |user_id, user_votes|
# don't migrate votes from deleted/non-existing users
next unless existing_user_ids.include?(user_id.to_i)
votes = {}
r.votes.each do |user_id, user_votes|
# don't migrate votes from deleted/non-existing users
next unless existing_user_ids.include?(user_id.to_i)
user_votes.each do |poll_name, options|
votes[poll_name] ||= {}
votes[poll_name][user_id] = options
user_votes.each do |poll_name, options|
votes[poll_name] ||= {}
votes[poll_name][user_id] = options
end
end
end
r.polls.values.each do |poll|
name = escape(poll["name"].presence || "poll")
type = POLL_TYPES[(poll["type"].presence || "")[/(regular|multiple|number)/, 1] || "regular"]
status = poll["status"] == "open" ? 0 : 1
visibility = poll["public"] == "true" ? 1 : 0
close_at = (Time.zone.parse(poll["close"]) rescue nil)
min = poll["min"].to_i.clamp(0, PG_INTEGER_MAX)
max = poll["max"].to_i.clamp(0, PG_INTEGER_MAX)
step = poll["step"].to_i.clamp(0, max)
anonymous_voters = poll["anonymous_voters"].to_i.clamp(0, PG_INTEGER_MAX)
r.polls.values.each do |poll|
name = escape(poll["name"].presence || "poll")
type =
POLL_TYPES[(poll["type"].presence || "")[/(regular|multiple|number)/, 1] || "regular"]
status = poll["status"] == "open" ? 0 : 1
visibility = poll["public"] == "true" ? 1 : 0
close_at =
(
begin
Time.zone.parse(poll["close"])
rescue StandardError
nil
end
)
min = poll["min"].to_i.clamp(0, PG_INTEGER_MAX)
max = poll["max"].to_i.clamp(0, PG_INTEGER_MAX)
step = poll["step"].to_i.clamp(0, max)
anonymous_voters = poll["anonymous_voters"].to_i.clamp(0, PG_INTEGER_MAX)
next if DB.query_single("SELECT COUNT(*) FROM polls WHERE post_id = ? AND name = ? LIMIT 1", r.post_id, name).first > 0
if DB.query_single(
"SELECT COUNT(*) FROM polls WHERE post_id = ? AND name = ? LIMIT 1",
r.post_id,
name,
).first > 0
next
end
poll_id = execute(<<~SQL
poll_id = execute(<<~SQL)[0]["id"]
INSERT INTO polls (
post_id,
name,
@ -126,38 +138,41 @@ class MigratePollsData < ActiveRecord::Migration[5.2]
'#{r.updated_at}'
) RETURNING id
SQL
)[0]["id"]
option_ids = Hash[*DB.query_single(<<~SQL
option_ids = Hash[*DB.query_single(<<~SQL)]
INSERT INTO poll_options
(poll_id, digest, html, anonymous_votes, created_at, updated_at)
VALUES
#{poll["options"].map { |option|
"(#{poll_id}, '#{escape(option["id"])}', '#{escape(option["html"].strip)}', #{option["anonymous_votes"].to_i}, '#{r.created_at}', '#{r.updated_at}')" }.join(",")
}
#{
poll["options"]
.map do |option|
"(#{poll_id}, '#{escape(option["id"])}', '#{escape(option["html"].strip)}', #{option["anonymous_votes"].to_i}, '#{r.created_at}', '#{r.updated_at}')"
end
.join(",")
}
RETURNING digest, id
SQL
)]
if votes[name].present?
poll_votes = votes[name].map do |user_id, options|
options
.select { |o| option_ids.has_key?(o) }
.map { |o| "(#{poll_id}, #{option_ids[o]}, #{user_id.to_i}, '#{r.created_at}', '#{r.updated_at}')" }
end
if votes[name].present?
poll_votes =
votes[name].map do |user_id, options|
options
.select { |o| option_ids.has_key?(o) }
.map do |o|
"(#{poll_id}, #{option_ids[o]}, #{user_id.to_i}, '#{r.created_at}', '#{r.updated_at}')"
end
end
poll_votes.flatten!
poll_votes.uniq!
poll_votes.flatten!
poll_votes.uniq!
if poll_votes.present?
execute <<~SQL
execute <<~SQL if poll_votes.present?
INSERT INTO poll_votes (poll_id, poll_option_id, user_id, created_at, updated_at)
VALUES #{poll_votes.join(",")}
SQL
end
end
end
end
execute <<~SQL
INSERT INTO post_custom_fields (name, value, post_id, created_at, updated_at)