Fix Wiki migration task and add more test coverage.

There was an error in the #extract_attributes_from_page method that
caused an exception when checking to see if a page named "Home"
already exists.

The check occurs to handle the renaming of the main index page to
"Home" to match the Gollum standard. If there is already a page
called "Home" then the migrator will leave that page and create
the Index page as usual. Users will need to manually rename their
old "Home" page to something else then rename their "Index" page
to "Home". Fortunately, I would be highly surprised if this case
ever comes up.

I also added more verbosity to the logging so if an error does
occur, it will be easier to track down which Wiki page is causing
the problem.
This commit is contained in:
Dan Knox 2013-03-16 18:01:48 -07:00
parent c367cc5ca6
commit 330fe4162e
3 changed files with 154 additions and 6 deletions

View file

@ -16,5 +16,27 @@ namespace :gitlab do
wiki_migrator = WikiToGollumMigrator.new
wiki_migrator.migrate!
end
# This task will destroy all of the Wiki repos
# that the Wiki migration task created. Run this
# to clean up your environment if you experienced
# problems during the original migration. After
# executing this task, you can attempt the original
# migration again.
#
# Notes:
# * This will not affect Wikis that have been created
# as Gollum Wikis only. It will only remove the wikis
# for the repositories that have old Wiki data in the
# dataabase.
# * If you have any repositories already named
# namespace/project.wiki that you do not wish
# to be removed you may want to perform a manual
# cleanup instead.
desc "GITLAB | Remove the Wiki repositories created by the `gitlab:wiki:migrate` task."
task :rollback => :environment do
wiki_migrator = WikiToGollumMigrator.new
wiki_migrator.rollback!
end
end
end

View file

@ -19,12 +19,30 @@ class WikiToGollumMigrator
end
end
def rollback!
log "\nBeginning Wiki Migration Rollback..."
projects.each do |project|
destroy_gollum_repo project
end
log "\nWiki Rollback Complete."
end
private
def create_gollum_repo(project)
GollumWiki.new(project, nil).wiki
end
def destroy_gollum_repo(project)
log " Removing Wiki repo for project: #{project.path_with_namespace}"
path = GollumWiki.new(project, nil).path_with_namespace
if Gitlab::Shell.new.remove_repository(path)
log " Wiki destroyed successfully. " + "[OK}".green
else
log " Problem destroying wiki. Please remove it manually. " + "[FAILED]".red
end
end
def create_pages(project, wiki)
pages = project.wikis.group(:slug).all
@ -45,8 +63,9 @@ class WikiToGollumMigrator
wiki = GollumWiki.new(project, page.user)
wiki_page = WikiPage.new(wiki)
attributes = extract_attributes_from_page(first_rev)
attributes = extract_attributes_from_page(first_rev, project)
log " Creating page '#{first_rev.title}'..."
if wiki_page.create(attributes)
log " Created page '#{wiki_page.title}' " + "[OK]".green
@ -59,15 +78,15 @@ class WikiToGollumMigrator
end
def create_revisions(project, page, revisions)
log " Creating revisions..."
revisions.each do |revision|
log " Creating revisions..."
# Reinitialize a new GollumWiki instance for each page
# and revision created so the correct User is shown in
# the commit message.
wiki = GollumWiki.new(project, revision.user)
wiki_page = wiki.find_page(page.slug)
attributes = extract_attributes_from_page(revision)
attributes = extract_attributes_from_page(revision, project)
content = attributes[:content]
@ -79,13 +98,15 @@ class WikiToGollumMigrator
end
end
def extract_attributes_from_page(page)
def extract_attributes_from_page(page, project)
attributes = page.attributes
.with_indifferent_access
.slice(:title, :content)
slug = page.slug
# Change 'index' pages to 'home' pages to match Gollum standards
if attributes[:title].downcase == "index"
if slug.downcase == "index"
attributes[:title] = "home" unless home_already_exists?(project)
end
@ -93,7 +114,7 @@ class WikiToGollumMigrator
end
def home_already_exists?(project)
project.wikis.where(title: 'home').any? || project.wikis.where(title: 'Home').any?
project.wikis.where(slug: 'home').any? || project.wikis.where(slug: 'Home').any?
end
def log(message)

View file

@ -108,6 +108,111 @@ describe WikiToGollumMigrator do
end
end
end
context "wikis with pages that have titles that do not match the slugs" do
before do
project = @projects.last
@page = project.wikis.new(title: "test page", content: "Invalid Page")
@page.slug = "totally-incorrect-slug"
@page.user = project.owner
@page.save!
create_revision(@page)
subject.rollback!
subject.migrate!
end
it "has a page with a title differing the slug" do
@page.slug.should_not == @page.title.parameterize
end
it "creates a new revision for each old revision of the page" do
@projects.each do |project|
wiki = GollumWiki.new(project, nil)
wiki.pages.each do |page|
page.versions.count.should == 2
end
end
end
end
context "changing wiki title from index to home" do
before do
@project = @projects.last
@page = @project.wikis.new(title: "Index", content: "Home Page")
@page.slug = "index"
@page.user = @project.owner
@page.save!
create_revision(@page)
subject.rollback!
end
it "creates a page called Home" do
subject.migrate!
wiki = GollumWiki.new(@project, nil)
page = wiki.find_page("home")
page.should be_present
end
context "when a page called Home already exists" do
before do
@index_page = @project.wikis.new(title: "Index", content: "Index Page")
@index_page.slug = "index"
@index_page.user = @project.owner
@index_page.save!
create_revision(@index_page)
@home_page = @project.wikis.new(title: "Home", content: "Home Page")
@home_page.slug = "home"
@home_page.user = @project.owner
@home_page.save!
create_revision(@home_page)
subject.migrate!
end
it "creates the index page" do
wiki = GollumWiki.new(@project, nil)
page = wiki.find_page("index")
page.should be_present
end
it "creates the home page" do
wiki = GollumWiki.new(@project, nil)
page = wiki.find_page("home")
page.should be_present
end
end
end
end
context "#rollback!" do
before do
Gitlab::Shell.any_instance.stub(:add_repository) do |path|
create_temp_repo("#{@repo_path}/#{path}.git")
end
Gitlab::Shell.any_instance.stub(:remove_repository) do |path|
FileUtils.rm_rf "#{@repo_path}/#{path}.git"
end
subject.stub(:log).as_null_object
subject.migrate!
subject.rollback!
end
it "destroys all of the wiki repositories that were created during migrate!" do
@projects.each do |project|
wiki_path = project.path_with_namespace + ".wiki.git"
full_path = @repo_path + "/" + wiki_path
File.exist?(full_path).should be_false
end
end
end