[Versions] Rename post/pool archive classes to versions

This just makes sense, and was confusing beforehand.
Case-in-point: Two test classes for pools: pool_versions and pool_archive
This commit is contained in:
Earlopain 2022-08-06 18:58:24 +02:00
parent 9a3b412d3b
commit 4cb88300c7
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
19 changed files with 102 additions and 189 deletions

4
.env
View File

@ -39,13 +39,9 @@
# If you are connecting to PostgreSQL via socket, omit the hostname (e.g.
# postgresql:///danbooru2)
# export DATABASE_URL="postgresql://localhost/danbooru2"
# export ARCHIVE_DATABASE_URL="postgresql://localhost/archive_development"
# export RO_DATABASE_URL="$DATABASE_URL"
# Put these in .env.test to define your test database.
# export DATABASE_URL="postgresql://localhost/danbooru2_test"
# export RO_DATABASE_URL="postgresql://localhost/danbooru2_test"
# export ARCHIVE_DATABASE_URL="postgresql://localhost/danbooru2_archive_test"
#
# Rails

View File

@ -7,15 +7,15 @@ class PoolVersionsController < ApplicationController
@pool = Pool.find(params[:search][:pool_id])
end
@pool_versions = PoolArchive.search(search_params).paginate(params[:page], limit: params[:limit], search_count: params[:search])
@pool_versions = PoolVersion.search(search_params).paginate(params[:page], limit: params[:limit], search_count: params[:search])
respond_with(@pool_versions)
end
def diff
@pool_version = PoolArchive.find(params[:id])
@pool_version = PoolVersion.find(params[:id])
if params[:other_id]
@other_version = PoolArchive.find(params[:other_id])
@other_version = PoolVersion.find(params[:other_id])
else
@other_version = @pool_version.previous
end

View File

@ -4,7 +4,7 @@ class PostVersionsController < ApplicationController
respond_to :js, only: [:undo]
def index
@post_versions = PostArchive.__elasticsearch__.search(PostArchive.build_query(search_params)).paginate(params[:page], :limit => params[:limit], :search_count => params[:search], includes: [:updater, post: [:versions]])
@post_versions = PostVersion.__elasticsearch__.search(PostVersion.build_query(search_params)).paginate(params[:page], :limit => params[:limit], :search_count => params[:search], includes: [:updater, post: [:versions]])
respond_with(@post_versions)
end
@ -12,7 +12,7 @@ class PostVersionsController < ApplicationController
can_edit = CurrentUser.can_post_edit_with_reason
raise User::PrivilegeError.new("Updater #{User.throttle_reason(can_edit)}") unless can_edit == true
@post_version = PostArchive.find(params[:id])
@post_version = PostVersion.find(params[:id])
@post_version.undo!
redirect_back fallback_location: post_versions_path

View File

@ -21,7 +21,7 @@ module Moderator
private
def ip_addr_search_path(type, ip_addr)
# post archive and posts don't support ip searches
# post versions and posts don't support ip searches
case type
when :comment
comments_path(group_by: "comment", search: { ip_addr: ip_addr })

View File

@ -30,7 +30,9 @@ module PostVersionIndex
indexes :locked_tags, type: 'keyword'
end
end
# FIXME: Rename the index to reflect the model name
base.index_name("post_archives")
base.index_name("post_archives_#{Rails.env}") unless Rails.env.production?
base.__elasticsearch__.extend ClassMethods
end

View File

@ -3,7 +3,7 @@ module Moderator
module Queries
class Tag < ::Struct.new(:user, :count)
def self.all(min_date, max_level)
records = PostArchive.where("updated_at > ?", min_date).group(:updater).count.map do |user, count|
records = PostVersion.where("updated_at > ?", min_date).group(:updater).count.map do |user, count|
new(user, count)
end

View File

@ -41,8 +41,8 @@ module Moderator
if with_history
add_by_ip_addr(sums, :artist_version, ip_addrs, ::ArtistVersion, :updater_ip_addr, :updater_id)
add_by_ip_addr(sums, :note_version, ip_addrs, ::NoteVersion, :updater_ip_addr, :updater_id)
add_by_ip_addr(sums, :pool_version, ip_addrs, ::PoolArchive, :updater_ip_addr, :updater_id)
add_by_ip_addr(sums, :post_version, ip_addrs, ::PostArchive, :updater_ip_addr, :updater_id)
add_by_ip_addr(sums, :pool_version, ip_addrs, ::PoolVersion, :updater_ip_addr, :updater_id)
add_by_ip_addr(sums, :post_version, ip_addrs, ::PostVersion, :updater_ip_addr, :updater_id)
add_by_ip_addr(sums, :wiki_page_version, ip_addrs, ::WikiPageVersion, :updater_ip_addr, :updater_id)
end
@ -72,8 +72,8 @@ module Moderator
if with_history
add_by_user_id(sums, :artist_version, user_ids, ::ArtistVersion, :updater_ip_addr, :updater_id)
add_by_user_id(sums, :note_version, user_ids, ::NoteVersion, :updater_ip_addr, :updater_id)
add_by_user_id(sums, :pool_version, user_ids, ::PoolArchive, :updater_ip_addr, :updater_id)
add_by_user_id(sums, :post_version, user_ids, ::PostArchive, :updater_ip_addr, :updater_id)
add_by_user_id(sums, :pool_version, user_ids, ::PoolVersion, :updater_ip_addr, :updater_id)
add_by_user_id(sums, :post_version, user_ids, ::PostVersion, :updater_ip_addr, :updater_id)
add_by_user_id(sums, :wiki_page_version, user_ids, ::WikiPageVersion, :updater_ip_addr, :updater_id)
end

View File

@ -15,13 +15,13 @@ class UserRevert
end
def validate!
if PostArchive.where(updater_id: user_id).count > THRESHOLD
if PostVersion.where(updater_id: user_id).count > THRESHOLD
raise TooManyChangesError.new("This user has too many changes to be reverted")
end
end
def revert_post_changes
PostArchive.where(updater_id: user_id).find_each do |x|
PostVersion.where(updater_id: user_id).find_each do |x|
x.undo!
end
end

View File

@ -168,7 +168,7 @@ class Pool < ApplicationRecord
end
def versions
PoolArchive.where("pool_id = ?", id).order("id asc")
PoolVersion.where("pool_id = ?", id).order("id asc")
end
def is_series?
@ -361,7 +361,7 @@ class Pool < ApplicationRecord
end
def create_version(updater: CurrentUser.user, updater_ip_addr: CurrentUser.ip_addr)
PoolArchive.queue(self, updater, updater_ip_addr)
PoolVersion.queue(self, updater, updater_ip_addr)
end
def last_page

View File

@ -1,11 +1,9 @@
class PoolArchive < ApplicationRecord
class PoolVersion < ApplicationRecord
user_status_counter :pool_edit_count, foreign_key: :updater_id
belongs_to :updater, :class_name => "User"
before_validation :fill_version, on: :create
before_validation :fill_changes, on: :create
self.table_name = "pool_versions"
module SearchMethods
def default_order
order(updated_at: :desc)
@ -59,7 +57,7 @@ class PoolArchive < ApplicationRecord
end
def fill_version
self.version = PoolArchive.calculate_version(self.pool_id)
self.version = PoolVersion.calculate_version(self.pool_id)
end
def fill_changes
@ -96,7 +94,7 @@ class PoolArchive < ApplicationRecord
end
def previous
PoolArchive.where("pool_id = ? and version < ?", pool_id, version).order("version desc").first
PoolVersion.where("pool_id = ? and version < ?", pool_id, version).order("version desc").first
end
def pool

View File

@ -61,7 +61,7 @@ class Post < ApplicationRecord
attr_accessor :old_tag_string, :old_parent_id, :old_source, :old_rating,
:do_not_version_changes, :tag_string_diff, :source_diff, :edit_reason
has_many :versions, -> {order("post_versions.id ASC")}, :class_name => "PostArchive", :dependent => :destroy
has_many :versions, -> {order("post_versions.id ASC")}, :class_name => "PostVersion", :dependent => :destroy
IMAGE_TYPES = %i[original large preview crop]
@ -1323,7 +1323,7 @@ class Post < ApplicationRecord
def create_new_version
# This function name is misleading, this directly creates the version.
# Previously there was a queue involved, now there isn't.
PostArchive.queue(self)
PostVersion.queue(self)
end
def revert_to(target)

View File

@ -1,4 +1,4 @@
class PostArchive < ApplicationRecord
class PostVersion < ApplicationRecord
extend Memoist
belongs_to :post
@ -8,8 +8,6 @@ class PostArchive < ApplicationRecord
before_validation :fill_version, on: :create
before_validation :fill_changes, on: :create
self.table_name = "post_versions"
module SearchMethods
def for_user(user_id)
if user_id
@ -148,7 +146,7 @@ class PostArchive < ApplicationRecord
end
def fill_version
self.version = PostArchive.calculate_version (self.post_id)
self.version = PostVersion.calculate_version (self.post_id)
end
def fill_changes(prev = nil)
@ -200,7 +198,7 @@ class PostArchive < ApplicationRecord
if association(:post).loaded? && post && post.association(:versions).loaded?
post.versions.sort_by(&:version).reverse.find {|v| v.version < version}
else
PostArchive.where("post_id = ? and version < ?", post_id, version).order("version desc").first
PostVersion.where("post_id = ? and version < ?", post_id, version).order("version desc").first
end
end

View File

@ -114,7 +114,7 @@ class User < ApplicationRecord
has_many :post_approvals, :dependent => :destroy
has_many :post_disapprovals, :dependent => :destroy
has_many :post_votes
has_many :post_archives
has_many :post_versions
has_many :note_versions
has_many :bans, -> { order("bans.id desc") }
has_many :staff_notes, -> { order("staff_notes.id desc") }
@ -496,15 +496,15 @@ class User < ApplicationRecord
create_user_throttle(:artist_edit, ->{ Danbooru.config.artist_edit_limit - ArtistVersion.for_user(id).where('updated_at > ?', 1.hour.ago).count },
:general_bypass_throttle?, 7.days)
create_user_throttle(:post_edit, ->{ Danbooru.config.post_edit_limit - PostArchive.for_user(id).where('updated_at > ?', 1.hour.ago).count },
create_user_throttle(:post_edit, ->{ Danbooru.config.post_edit_limit - PostVersion.for_user(id).where('updated_at > ?', 1.hour.ago).count },
:general_bypass_throttle?, 7.days)
create_user_throttle(:wiki_edit, ->{ Danbooru.config.wiki_edit_limit - WikiPageVersion.for_user(id).where('updated_at > ?', 1.hour.ago).count },
:general_bypass_throttle?, 7.days)
create_user_throttle(:pool, ->{ Danbooru.config.pool_limit - Pool.for_user(id).where('created_at > ?', 1.hour.ago).count },
:is_janitor?, 7.days)
create_user_throttle(:pool_edit, ->{ Danbooru.config.pool_edit_limit - PoolArchive.for_user(id).where('updated_at > ?', 1.hour.ago).count },
create_user_throttle(:pool_edit, ->{ Danbooru.config.pool_edit_limit - PoolVersion.for_user(id).where('updated_at > ?', 1.hour.ago).count },
:is_janitor?, 3.days)
create_user_throttle(:pool_post_edit, -> { Danbooru.config.pool_post_edit_limit - PoolArchive.for_user(id).where('updated_at > ?', 1.hour.ago).group(:pool_id).count(:pool_id).length },
create_user_throttle(:pool_post_edit, -> { Danbooru.config.pool_post_edit_limit - PoolVersion.for_user(id).where('updated_at > ?', 1.hour.ago).group(:pool_id).count(:pool_id).length },
:general_bypass_throttle?, 7.days)
create_user_throttle(:note_edit, ->{ Danbooru.config.note_edit_limit - NoteVersion.for_user(id).where('updated_at > ?', 1.hour.ago).count },
:general_bypass_throttle?, 3.days)
@ -773,7 +773,7 @@ class User < ApplicationRecord
UserStatus.where(user_id: id).update_all(
post_count: Post.for_user(id).count,
post_deleted_count: Post.for_user(id).deleted.count,
post_update_count: PostArchive.for_user(id).count,
post_update_count: PostVersion.for_user(id).count,
note_count: NoteVersion.where(updater_id: id).count
)
end

View File

@ -166,7 +166,7 @@ class UserPresenter
end
def recent_tags_with_types
versions = PostArchive.where(updater_id: user.id).where("updated_at > ?", 1.hour.ago).order(id: :desc).limit(150)
versions = PostVersion.where(updater_id: user.id).where("updated_at > ?", 1.hour.ago).order(id: :desc).limit(150)
tags = versions.flat_map(&:added_tags)
tags = tags.reject { |tag| Tag.is_metatag?(tag) }
tags = tags.group_by(&:itself).transform_values(&:size).sort_by { |tag, count| [-count, tag] }.map(&:first)

View File

@ -33,8 +33,8 @@ FileUtils.chdir APP_ROOT do
puts "\n== Preparing search indices =="
system! 'bin/rails r Post.__elasticsearch__.create_index!'
system! 'bin/rails r Post.import'
system! 'bin/rails r PostArchive.__elasticsearch__.create_index!'
system! 'bin/rails r PostArchive.import'
system! 'bin/rails r PostVersion.__elasticsearch__.create_index!'
system! 'bin/rails r PostVersion.import'
puts "\n== Removing old logs and tempfiles =="
system! 'bin/rails log:clear tmp:clear'

View File

@ -4,7 +4,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config',
Post.find_each do |post|
puts "#{post.id}"
prev = PostArchive.new
prev = PostVersion.new
post.versions.each do |v|
v.fill_changes(prev)
v.update_columns(

View File

@ -1,137 +0,0 @@
require 'test_helper'
class PostArchiveTest < ActiveSupport::TestCase
context "A post" do
setup do
Timecop.travel(1.month.ago) do
@user = FactoryBot.create(:user)
end
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "#undo" do
setup do
@post = FactoryBot.create(:post, :tag_string => "1")
@post.update(:tag_string => "1 2")
@post.update(:tag_string => "2 3")
end
subject { @post.versions.sort_by(&:id)[1] }
should "undo the changes" do
subject.undo!
@post.reload
assert_equal("3", @post.tag_string)
end
end
context "that has multiple versions: " do
setup do
@post = FactoryBot.create(:post, :tag_string => "1")
@post.update(:tag_string => "1 2")
@post.update(:tag_string => "2 3")
end
context "a version record" do
setup do
@version = PostArchive.last
end
should "know its previous version" do
assert_not_nil(@version.previous)
assert_equal("1 2", @version.previous.tags)
end
end
end
context "that has been created" do
setup do
@parent = FactoryBot.create(:post)
@post = FactoryBot.create(:post, :tag_string => "aaa bbb ccc", :rating => "e", :parent => @parent, :source => "xyz")
end
should "also create a version" do
assert_equal(1, @post.versions.size)
@version = @post.versions.sort_by(&:id).last
assert_equal("aaa bbb ccc", @version.tags)
assert_equal(@post.rating, @version.rating)
assert_equal(@post.parent_id, @version.parent_id)
assert_equal(@post.source, @version.source)
end
end
context "that is tagged with a pool:<name> metatag" do
setup do
@pool = FactoryBot.create(:pool)
@post = FactoryBot.create(:post, tag_string: "tagme pool:#{@pool.id}")
end
should "create a version" do
assert_equal("tagme", @post.tag_string)
assert_equal("pool:#{@pool.id}", @post.pool_string)
assert_equal(1, @post.versions.size)
assert_equal("tagme", @post.versions.last.tags)
end
end
context "that has been updated" do
setup do
@post = FactoryBot.create(:post, :tag_string => "aaa bbb ccc", :rating => "q", :source => "xyz")
@post.update(:tag_string => "bbb ccc xxx", :source => "")
end
should "also create a version" do
assert_equal(2, @post.versions.size)
@version = @post.versions.sort_by(&:id).last
assert_equal("bbb ccc xxx", @version.tags)
assert_equal("q", @version.rating)
assert_equal("", @version.source)
assert_nil(@version.parent_id)
end
should "not create a version if updating the post fails" do
@post.stubs(:set_tag_counts).raises(NotImplementedError)
assert_equal(2, @post.versions.size)
assert_raise(NotImplementedError) { @post.update(tag_string: "zzz") }
assert_equal(2, @post.versions.size)
end
should "should create a version if the rating changes" do
assert_difference("@post.versions.size", 1) do
@post.update(rating: "s")
assert_equal("s", @post.versions.sort_by(&:id).last.rating)
end
end
should "should create a version if the source changes" do
assert_difference("@post.versions.size", 1) do
@post.update(source: "blah")
assert_equal("blah", @post.versions.sort_by(&:id).last.source)
end
end
should "should create a version if the parent changes" do
assert_difference("@post.versions.size", 1) do
@parent = FactoryBot.create(:post)
@post.update(parent_id: @parent.id)
assert_equal(@parent.id, @post.versions.sort_by(&:id).last.parent_id)
end
end
should "should create a version if the tags change" do
assert_difference("@post.versions.size", 1) do
@post.update(tag_string: "blah")
assert_equal("blah", @post.versions.sort_by(&:id).last.tags)
end
end
end
end
end

View File

@ -1066,14 +1066,14 @@ class PostTest < ActiveSupport::TestCase
context "that has been updated" do
should "create a new version if it's the first version" do
assert_difference("PostArchive.count", 1) do
assert_difference("PostVersion.count", 1) do
post = FactoryBot.create(:post)
end
end
should "create a new version if the post is updated" do
post = FactoryBot.create(:post)
assert_difference("PostArchive.count", 1) do
assert_difference("PostVersion.count", 1) do
post.update(:tag_string => "zzz")
end
end

View File

@ -17,14 +17,14 @@ class PostVersionTest < ActiveSupport::TestCase
context "that has multiple versions: " do
setup do
@post = FactoryBot.create(:post, :tag_string => "1")
@post.update(:tag_string => "1 2")
@post.update(:tag_string => "2 3")
@post = FactoryBot.create(:post, tag_string: "1")
@post.update(tag_string: "1 2")
@post.update(tag_string: "2 3")
end
context "a version record" do
setup do
@version = PostArchive.last
@version = PostVersion.last
end
should "know its previous version" do
@ -32,12 +32,33 @@ class PostVersionTest < ActiveSupport::TestCase
assert_equal("1 2", @version.previous.tags)
end
end
should "undo the changes" do
@post.versions.second.undo!
@post.reload
assert_equal("3", @post.tag_string)
end
end
context "that is tagged with a pool:<name> metatag" do
setup do
@pool = FactoryBot.create(:pool)
@post = FactoryBot.create(:post, tag_string: "tagme pool:#{@pool.id}")
end
should "create a version" do
assert_equal("tagme", @post.tag_string)
assert_equal("pool:#{@pool.id}", @post.pool_string)
assert_equal(1, @post.versions.size)
assert_equal("tagme", @post.versions.last.tags)
end
end
context "that has been created" do
setup do
@parent = FactoryBot.create(:post)
@post = FactoryBot.create(:post, :tag_string => "aaa bbb ccc", :rating => "e", :parent => @parent, :source => "xyz")
@post = FactoryBot.create(:post, tag_string: "aaa bbb ccc", rating: "e", parent: @parent, source: "xyz")
end
should "also create a version" do
@ -52,10 +73,8 @@ class PostVersionTest < ActiveSupport::TestCase
context "that has been updated" do
setup do
Timecop.travel(1.minute.ago) do
@post = FactoryBot.create(:post, :tag_string => "aaa bbb ccc", :rating => "q", :source => "xyz")
end
@post.update(:tag_string => "bbb ccc xxx", :source => "")
@post = FactoryBot.create(:post, tag_string: "aaa bbb ccc", rating: "q", source: "xyz")
@post.update(tag_string: "bbb ccc xxx", source: "")
end
should "also create a version" do
@ -66,6 +85,43 @@ class PostVersionTest < ActiveSupport::TestCase
assert_equal("", @version.source)
assert_nil(@version.parent_id)
end
should "not create a version if updating the post fails" do
@post.stubs(:set_tag_counts).raises(NotImplementedError)
assert_equal(2, @post.versions.size)
assert_raise(NotImplementedError) { @post.update(tag_string: "zzz") }
assert_equal(2, @post.versions.size)
end
should "should create a version if the rating changes" do
assert_difference("@post.versions.size", 1) do
@post.update(rating: "s")
assert_equal("s", @post.versions.last.rating)
end
end
should "should create a version if the source changes" do
assert_difference("@post.versions.size", 1) do
@post.update(source: "blah")
assert_equal("blah", @post.versions.last.source)
end
end
should "should create a version if the parent changes" do
assert_difference("@post.versions.size", 1) do
@parent = FactoryBot.create(:post)
@post.update(parent_id: @parent.id)
assert_equal(@parent.id, @post.versions.last.parent_id)
end
end
should "should create a version if the tags change" do
assert_difference("@post.versions.size", 1) do
@post.update(tag_string: "blah")
assert_equal("blah", @post.versions.last.tags)
end
end
end
end
end