add context_id and context_type to wiki pages

and populate it; shouldn't do anything yet but
next release we can switch over to direct associations

refs #CNVS-38247

Change-Id: I23e0983d5be61d45db8087738f658b63dc0aeb48
Reviewed-on: https://gerrit.instructure.com/119253
Tested-by: Jenkins
Reviewed-by: Jeremy Stanley <jeremy@instructure.com>
Product-Review: James Williams  <jamesw@instructure.com>
QA-Review: James Williams  <jamesw@instructure.com>
This commit is contained in:
James Williams 2017-07-17 10:09:26 -06:00
parent c00177b54d
commit 711f54fc97
5 changed files with 37 additions and 2 deletions

View File

@ -55,6 +55,8 @@ class WikiPage < ActiveRecord::Base
if: proc { self.context.try(:feature_enabled?, :conditional_release) } if: proc { self.context.try(:feature_enabled?, :conditional_release) }
before_save :set_revised_at before_save :set_revised_at
before_validation :ensure_unique_title before_validation :ensure_unique_title
before_save :ensure_context
after_save :touch_wiki_context after_save :touch_wiki_context
after_save :update_assignment, after_save :update_assignment,
if: proc { self.context.try(:feature_enabled?, :conditional_release) } if: proc { self.context.try(:feature_enabled?, :conditional_release) }
@ -86,6 +88,13 @@ class WikiPage < ActiveRecord::Base
TITLE_LENGTH = 255 TITLE_LENGTH = 255
SIMPLY_VERSIONED_EXCLUDE_FIELDS = [:workflow_state, :editing_roles, :notify_of_update].freeze SIMPLY_VERSIONED_EXCLUDE_FIELDS = [:workflow_state, :editing_roles, :notify_of_update].freeze
def ensure_context
# TODO: get rid of once all existing wiki pages have their context populated and wiki page creation methods can be rewritten
# e.g. course.wiki.wiki_pages.create -> course.wiki_pages.create
self.context_type ||= self.context.class.base_class.name
self.context_id ||= self.context.id
end
def touch_wiki_context def touch_wiki_context
self.wiki.touch_context if self.wiki && self.wiki.context self.wiki.touch_context if self.wiki && self.wiki.context
end end

View File

@ -0,0 +1,10 @@
class AddContextToWikiPages < ActiveRecord::Migration[5.0]
disable_ddl_transaction!
tag :predeploy
def change
add_column :wiki_pages, :context_id, :integer, :limit => 8
add_column :wiki_pages, :context_type, :string
add_index :wiki_pages, [:context_id, :context_type], algorithm: :concurrently
end
end

View File

@ -0,0 +1,8 @@
class PopulateContextOnWikiPages < ActiveRecord::Migration[5.0]
tag :postdeploy
def change
DataFixup::PopulateContextOnWikiPages.send_later_if_production_enqueue_args(:run,
:priority => Delayed::LOW_PRIORITY, :strand => "populate_wiki_page_context_#{Shard.current.database_server.id}")
end
end

View File

@ -0,0 +1,8 @@
module DataFixup::PopulateContextOnWikiPages
def self.run
WikiPage.find_ids_in_ranges do |min_id, max_id|
WikiPage.where(:id => min_id..max_id, :context_id => nil).joins(:wiki => :course).update_all("context_type='Course', context_id=courses.id")
WikiPage.where(:id => min_id..max_id, :context_id => nil).joins(:wiki => :group).update_all("context_type='Group', context_id=groups.id")
end
end
end

View File

@ -186,8 +186,8 @@ describe ContextModule do
it "should not add invalid wiki pages" do it "should not add invalid wiki pages" do
course_module course_module
@course.wiki @course.wiki
@wiki = Wiki.create!(:title => "new wiki") other_course = Account.default.courses.create!
@page = @wiki.wiki_pages.create!(:title => "new page") @page = other_course.wiki.wiki_pages.create!(:title => "new page")
@tag = @module.add_item({:id => @page.id, :type => 'wiki_page'}) @tag = @module.add_item({:id => @page.id, :type => 'wiki_page'})
expect(@tag).to be_nil expect(@tag).to be_nil
end end