From db577200848ae9c936763a58129354db9e46fbfc Mon Sep 17 00:00:00 2001 From: Dan Knox Date: Wed, 24 Apr 2013 13:29:40 -0700 Subject: [PATCH 1/2] Add a safe migration mode to the wiki migrator. The safe migrate mode can be invoked by setting the safe_migrate environment variable to 'true' on the command line when running the task. RAILS_ENV=production rake gitlab:wiki:migrate --- lib/tasks/gitlab/migrate_wiki.rake | 8 +- lib/wiki_to_gollum_migrator.rb | 12 ++- spec/lib/wiki_to_gollum_migrator_spec.rb | 94 ++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) diff --git a/lib/tasks/gitlab/migrate_wiki.rake b/lib/tasks/gitlab/migrate_wiki.rake index 5d9881e45db..9198a398c82 100644 --- a/lib/tasks/gitlab/migrate_wiki.rake +++ b/lib/tasks/gitlab/migrate_wiki.rake @@ -11,7 +11,13 @@ namespace :gitlab do # Notes: # * The existing Wiki content will remain in your # database in-tact. - desc "GITLAB | Migrate Wiki content from database to Gollum repositories." + # * If the migration does not work the first time, + # run the `RAILS_ENV=production rake gitlab:wiki:rollback` + # command and then execute the migration again with + # the safe_migrate=true environment variable: + # + # `RAILS_ENV=production rake gitlab:wiki:migrate safe_migrate=true` + desc "GITLAB | Migrate Wiki content from database to Gollum repositories. Use the safe_migrate=true argument if initial migration fails." task :migrate => :environment do wiki_migrator = WikiToGollumMigrator.new wiki_migrator.migrate! diff --git a/lib/wiki_to_gollum_migrator.rb b/lib/wiki_to_gollum_migrator.rb index ed6a1700631..f99b96e125e 100644 --- a/lib/wiki_to_gollum_migrator.rb +++ b/lib/wiki_to_gollum_migrator.rb @@ -1,3 +1,5 @@ +# encoding: UTF-8 + class WikiToGollumMigrator attr_reader :projects @@ -84,7 +86,7 @@ class WikiToGollumMigrator # 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) + wiki_page = wiki.find_page(page.title) attributes = extract_attributes_from_page(revision, project) @@ -103,6 +105,10 @@ class WikiToGollumMigrator .with_indifferent_access .slice(:title, :content) + if ENV["safe_migrate"] == "true" + attributes[:title] = gollum_safe_title(attributes[:title]) + end + slug = page.slug # Change 'index' pages to 'home' pages to match Gollum standards @@ -113,6 +119,10 @@ class WikiToGollumMigrator attributes end + def gollum_safe_title(title) + title.parameterize.titleize + end + def home_already_exists?(project) project.wikis.where(slug: 'home').any? || project.wikis.where(slug: 'Home').any? end diff --git a/spec/lib/wiki_to_gollum_migrator_spec.rb b/spec/lib/wiki_to_gollum_migrator_spec.rb index f9b9c78ea01..fac7339d615 100644 --- a/spec/lib/wiki_to_gollum_migrator_spec.rb +++ b/spec/lib/wiki_to_gollum_migrator_spec.rb @@ -1,3 +1,5 @@ +# encoding: UTF-8 + require "spec_helper" describe WikiToGollumMigrator do @@ -137,6 +139,98 @@ describe WikiToGollumMigrator do end end + context "when migrating wiki's with extra whitespace in the title" do + before do + subject.rollback! + ENV['safe_migrate'] = 'true' + @project = @projects.last + @page = @project.wikis.new(title: "2012-06-16 ", content: "Page with funky title") + @page.slug = @page.title.parameterize + @page.user = @project.owner + @page.save! + + create_revision(@page) + + subject.migrate! + end + + it "creates the wiki page correctly" do + wiki = GollumWiki.new(@project, nil) + page = wiki.find_page("2012 06 16") + page.should be_present + page.versions.count.should == 2 + end + end + + context "when migrating wiki's with slashes in the title" do + before do + subject.rollback! + ENV['safe_migrate'] = 'true' + @project = @projects.last + @page = @project.wikis.new(title: "Awesome 1337 /bin/badass ", content: "Page with funky title") + @page.slug = @page.title.parameterize + @page.user = @project.owner + @page.save! + + create_revision(@page) + + subject.migrate! + end + + it "creates the wiki page correctly" do + wiki = GollumWiki.new(@project, nil) + page = wiki.find_page("Awesome 1337 Bin Badass") + page.should be_present + page.versions.count.should == 2 + end + end + + context "when migrating wiki's with non alphanumeric characters in the title" do + before do + subject.rollback! + ENV['safe_migrate'] = 'true' + @project = @projects.last + @page = @project.wikis.new(title: "Awes@me-1337 #!/bin/badass? ", content: "Page with funky title") + @page.slug = @page.title.parameterize + @page.user = @project.owner + @page.save! + + create_revision(@page) + + subject.migrate! + end + + it "creates the wiki page correctly" do + wiki = GollumWiki.new(@project, nil) + page = wiki.find_page("Awes Me 1337 Bin Badass") + page.should be_present + page.versions.count.should == 2 + end + end + + context "when migrating wiki's with non-english characters in the title" do + before do + subject.rollback! + ENV['safe_migrate'] = 'true' + @project = @projects.last + @page = @project.wikis.new(title: "Mögliche Aufteilung", content: "Page with funky title") + @page.slug = @page.title.parameterize + @page.user = @project.owner + @page.save! + + create_revision(@page) + + subject.migrate! + end + + it "creates the wiki page correctly" do + wiki = GollumWiki.new(@project, nil) + page = wiki.find_page("Mogliche Aufteilung") + page.should be_present + page.versions.count.should == 2 + end + end + context "changing wiki title from index to home" do before do @project = @projects.last -- GitLab From 58ca7553a46ec7e1c089877f1dce8f3af42f449a Mon Sep 17 00:00:00 2001 From: Dan Knox Date: Wed, 24 Apr 2013 17:20:43 -0700 Subject: [PATCH 2/2] Ensure old revisions are retrieved from the DB in the correct order. --- lib/wiki_to_gollum_migrator.rb | 2 +- spec/lib/wiki_to_gollum_migrator_spec.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/wiki_to_gollum_migrator.rb b/lib/wiki_to_gollum_migrator.rb index f99b96e125e..8f9218bbc91 100644 --- a/lib/wiki_to_gollum_migrator.rb +++ b/lib/wiki_to_gollum_migrator.rb @@ -55,7 +55,7 @@ class WikiToGollumMigrator def create_page_and_revisions(project, page) # Grab all revisions of the page - revisions = project.wikis.where(slug: page.slug).ordered.all + revisions = project.wikis.where(slug: page.slug).order('id desc').all # Remove the first revision created from the array # and use it to create the Gollum page. Each successive revision diff --git a/spec/lib/wiki_to_gollum_migrator_spec.rb b/spec/lib/wiki_to_gollum_migrator_spec.rb index fac7339d615..4740839bc77 100644 --- a/spec/lib/wiki_to_gollum_migrator_spec.rb +++ b/spec/lib/wiki_to_gollum_migrator_spec.rb @@ -149,7 +149,7 @@ describe WikiToGollumMigrator do @page.user = @project.owner @page.save! - create_revision(@page) + 3.times { create_revision(@page) } subject.migrate! end @@ -158,6 +158,7 @@ describe WikiToGollumMigrator do wiki = GollumWiki.new(@project, nil) page = wiki.find_page("2012 06 16") page.should be_present + page.content.should == "Updated Content" page.versions.count.should == 2 end end -- GitLab