-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean orphaned page article link records #4175
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,3 @@ | ||
# create_table :articles do |t| | ||
# # t.column :name, :string | ||
# t.column :title, :string | ||
# t.column :source_text, :text | ||
# # automated stuff | ||
# t.column :created_on, :datetime | ||
# t.column :lock_version, :integer, :default => 0 | ||
# end | ||
# == Schema Information | ||
# | ||
# Table name: articles | ||
|
@@ -41,21 +33,20 @@ class Article < ApplicationRecord | |
validates :latitude, allow_blank: true, numericality: { less_than_or_equal_to: 90, greater_than_or_equal_to: -90} | ||
validates :longitude, allow_blank: true, numericality: { less_than_or_equal_to: 180, greater_than_or_equal_to: -180} | ||
|
||
|
||
has_and_belongs_to_many :categories, -> { distinct } | ||
belongs_to :collection, optional: true | ||
has_many(:target_article_links, :foreign_key => "target_article_id", :class_name => 'ArticleArticleLink') | ||
scope :target_article_links, -> { include 'source_article' } | ||
scope :target_article_links, -> { order "articles.title ASC" } | ||
|
||
has_many(:source_article_links, :foreign_key => "source_article_id", :class_name => 'ArticleArticleLink') | ||
has_many(:page_article_links) | ||
has_many :page_article_links, dependent: :destroy | ||
scope :page_article_links, -> { includes(:page) } | ||
scope :page_article_links, -> { order("pages.work_id, pages.position ASC") } | ||
|
||
scope :pages_for_this_article, -> { order("pages.work_id, pages.position ASC").includes(:pages)} | ||
scope :pages_for_this_article, -> { order("pages.work_id, pages.position ASC").includes(:pages) } | ||
|
||
has_many :pages, through: :page_article_links, counter_cache: true | ||
has_many :pages, through: :page_article_links | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting we're missing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bug in current rails version. This ticket provides more discussions on that |
||
has_many :article_versions, -> { order 'version DESC' }, dependent: :destroy | ||
|
||
|
@@ -78,10 +69,10 @@ def source_text | |
self[:source_text] || '' | ||
end | ||
|
||
|
||
def self.delete_orphan_articles | ||
# don't delete orphan articles with contents | ||
Article.delete_all("source_text IS NULL AND id NOT IN (select article_id from page_article_links)") | ||
Article.where(provenance: nil). | ||
where('source_text IS NULL AND id NOT IN (SELECT article_id FROM page_article_links)').destroy_all | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this was not working the way we expected it to, it could account for some of what we're seeing in #4269 |
||
|
||
####################### | ||
|
@@ -146,7 +137,7 @@ def possible_duplicates | |
def clear_links(type='does_not_apply') | ||
# clear out the existing links to this page | ||
if self.id | ||
ArticleArticleLink.where("source_article_id = #{self.id}").delete_all | ||
ArticleArticleLink.where("source_article_id = #{self.id}").destroy_all | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,9 +56,8 @@ class Page < ApplicationRecord | |
acts_as_list :scope => :work | ||
belongs_to :last_editor, :class_name => 'User', :foreign_key => 'last_editor_user_id', optional: true | ||
|
||
|
||
has_many :page_article_links, :dependent => :destroy | ||
has_many :articles, :through => :page_article_links | ||
has_many :page_article_links, dependent: :destroy | ||
has_many :articles, through: :page_article_links | ||
has_many :page_versions, -> { order 'page_version DESC' }, :dependent => :destroy | ||
|
||
belongs_to :current_version, :class_name => 'PageVersion', :foreign_key => 'page_version_id', optional: true | ||
|
@@ -529,7 +528,7 @@ def clear_links(text_type) | |
if self.page_article_links.present? | ||
self.clear_article_graphs | ||
# clear out the existing links to this page | ||
PageArticleLink.where("page_id = #{self.id} and text_type = '#{text_type}'").delete_all | ||
PageArticleLink.where("page_id = #{self.id} and text_type = '#{text_type}'").destroy_all | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing delete_all does not trigger active_record callbacks , therefore no validations. Moving forward, unless we specifically want to avoid callbacks, we should use destroy instead of delete in order to avoid unvalidated changes. |
||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CleanOrphanPageLinkRecords < ActiveRecord::Migration[6.1] | ||
|
||
def up | ||
page_article_links = PageArticleLink.where(article_id: nil).or(PageArticleLink.where(page_id: nil)) | ||
|
||
page_article_links.in_batches(of: 1000).destroy_all | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. migration to clean existing bad records There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same caveat here. |
||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This callback is in page.rb, but seems missing on article side.