Commit 0415566b3796f500e7f694a0aee4882752d150c1

Authored by Dmitriy Zaporozhets
1 parent 86e368a8

Remove wiki migrator. We dont need it any more. Users should migrate to 5.0 first

lib/tasks/migrate/migrate_wiki.rake
... ... @@ -1,42 +0,0 @@
1   -namespace :gitlab do
2   - namespace :wiki do
3   -
4   - # This task will migrate all of the existing Wiki
5   - # content stored in your database into the new
6   - # Gollum Wiki system. A new repository named
7   - # namespace/project.wiki.git will be created for
8   - # each project that currently has Wiki pages in
9   - # the database.
10   - #
11   - # Notes:
12   - # * The existing Wiki content will remain in your
13   - # database in-tact.
14   - desc "GITLAB | Migrate Wiki content from database to Gollum repositories."
15   - task :migrate => :environment do
16   - wiki_migrator = WikiToGollumMigrator.new
17   - wiki_migrator.migrate!
18   - end
19   -
20   - # This task will destroy all of the Wiki repos
21   - # that the Wiki migration task created. Run this
22   - # to clean up your environment if you experienced
23   - # problems during the original migration. After
24   - # executing this task, you can attempt the original
25   - # migration again.
26   - #
27   - # Notes:
28   - # * This will not affect Wikis that have been created
29   - # as Gollum Wikis only. It will only remove the wikis
30   - # for the repositories that have old Wiki data in the
31   - # dataabase.
32   - # * If you have any repositories already named
33   - # namespace/project.wiki that you do not wish
34   - # to be removed you may want to perform a manual
35   - # cleanup instead.
36   - desc "GITLAB | Remove the Wiki repositories created by the `gitlab:wiki:migrate` task."
37   - task :rollback => :environment do
38   - wiki_migrator = WikiToGollumMigrator.new
39   - wiki_migrator.rollback!
40   - end
41   - end
42   -end
lib/wiki_to_gollum_migrator.rb
... ... @@ -1,124 +0,0 @@
1   -class WikiToGollumMigrator
2   -
3   - attr_reader :projects
4   -
5   - def initialize
6   - @projects = []
7   -
8   - Project.find_in_batches(batch_size: 50) do |batch|
9   - batch.each { |p| @projects << p if p.wikis.any? }
10   - end
11   - end
12   -
13   - def migrate!
14   - projects.each do |project|
15   - log "\nMigrating Wiki for '#{project.path_with_namespace}'"
16   - wiki = create_gollum_repo(project)
17   - create_pages project, wiki
18   - log "Project '#{project.path_with_namespace}' migrated. " + "[OK]".green
19   - end
20   - end
21   -
22   - def rollback!
23   - log "\nBeginning Wiki Migration Rollback..."
24   - projects.each do |project|
25   - destroy_gollum_repo project
26   - end
27   - log "\nWiki Rollback Complete."
28   - end
29   -
30   - private
31   -
32   - def create_gollum_repo(project)
33   - GollumWiki.new(project, nil).wiki
34   - end
35   -
36   - def destroy_gollum_repo(project)
37   - log " Removing Wiki repo for project: #{project.path_with_namespace}"
38   - path = GollumWiki.new(project, nil).path_with_namespace
39   - if Gitlab::Shell.new.remove_repository(path)
40   - log " Wiki destroyed successfully. " + "[OK}".green
41   - else
42   - log " Problem destroying wiki. Please remove it manually. " + "[FAILED]".red
43   - end
44   - end
45   -
46   - def create_pages(project, wiki)
47   - pages = project.wikis.group(:slug).all
48   -
49   - pages.each do |page|
50   - create_page_and_revisions(project, page)
51   - end
52   - end
53   -
54   - def create_page_and_revisions(project, page)
55   - # Grab all revisions of the page
56   - revisions = project.wikis.where(slug: page.slug).ordered.all
57   -
58   - # Remove the first revision created from the array
59   - # and use it to create the Gollum page. Each successive revision
60   - # will then be applied to the new Gollum page as an update.
61   - first_rev = revisions.pop
62   -
63   - wiki = GollumWiki.new(project, page.user)
64   - wiki_page = WikiPage.new(wiki)
65   -
66   - attributes = extract_attributes_from_page(first_rev, project)
67   -
68   - log " Creating page '#{first_rev.title}'..."
69   - if wiki_page.create(attributes)
70   - log " Created page '#{wiki_page.title}' " + "[OK]".green
71   -
72   - # Reverse the revisions to create them in the correct
73   - # chronological order.
74   - create_revisions(project, wiki_page, revisions.reverse)
75   - else
76   - log " Failed to create page '#{wiki_page.title}' " + "[FAILED]".red
77   - end
78   - end
79   -
80   - def create_revisions(project, page, revisions)
81   - log " Creating revisions..."
82   - revisions.each do |revision|
83   - # Reinitialize a new GollumWiki instance for each page
84   - # and revision created so the correct User is shown in
85   - # the commit message.
86   - wiki = GollumWiki.new(project, revision.user)
87   - wiki_page = wiki.find_page(page.slug)
88   -
89   - attributes = extract_attributes_from_page(revision, project)
90   -
91   - content = attributes[:content]
92   -
93   - if wiki_page.update(content)
94   - log " Created revision " + "[OK]".green
95   - else
96   - log " Failed to create revision " + "[FAILED]".red
97   - end
98   - end
99   - end
100   -
101   - def extract_attributes_from_page(page, project)
102   - attributes = page.attributes
103   - .with_indifferent_access
104   - .slice(:title, :content)
105   -
106   - slug = page.slug
107   -
108   - # Change 'index' pages to 'home' pages to match Gollum standards
109   - if slug.downcase == "index"
110   - attributes[:title] = "home" unless home_already_exists?(project)
111   - end
112   -
113   - attributes
114   - end
115   -
116   - def home_already_exists?(project)
117   - project.wikis.where(slug: 'home').any? || project.wikis.where(slug: 'Home').any?
118   - end
119   -
120   - def log(message)
121   - puts message
122   - end
123   -
124   -end
spec/lib/wiki_to_gollum_migrator_spec.rb
... ... @@ -1,219 +0,0 @@
1   -require "spec_helper"
2   -
3   -describe WikiToGollumMigrator do
4   -
5   - def create_wiki_for(project)
6   - 3.times { @pages[project.id] << create_page(project) }
7   - end
8   -
9   - def create_revisions_for(project)
10   - @pages[project.id].each do |page|
11   - create_revision(page)
12   - end
13   - end
14   -
15   - def create_page(project)
16   - page = project.wikis.new(title: "Page #{rand(1000)}", content: "Content")
17   - page.user = project.owner
18   - page.slug = page.title.parameterize
19   - page.save!
20   - page
21   - end
22   -
23   - def create_revision(page)
24   - revision = page.dup
25   - revision.content = "Updated Content"
26   - revision.save!
27   - end
28   -
29   - def create_temp_repo(path)
30   - FileUtils.mkdir_p path
31   - command = "git init --quiet --bare #{path};"
32   - system(command)
33   - end
34   -
35   - before do
36   - @repo_path = "#{Rails.root}/tmp/test-git-base-path"
37   - @projects = []
38   - @pages = Hash.new {|h,k| h[k] = Array.new }
39   -
40   - @projects << create(:project)
41   - @projects << create(:project)
42   -
43   - @projects.each do |project|
44   - create_wiki_for project
45   - create_revisions_for project
46   - end
47   -
48   - @project_without_wiki = create(:project)
49   - end
50   -
51   - context "Before the migration" do
52   - it "has two projects with valid wikis" do
53   - @projects.each do |project|
54   - pages = project.wikis.group(:slug).all
55   - pages.count.should == 3
56   - end
57   - end
58   -
59   - it "has two revision for each page" do
60   - @projects.each do |project|
61   - @pages[project.id].each do |page|
62   - revisions = project.wikis.where(slug: page.slug)
63   - revisions.count.should == 2
64   - end
65   - end
66   - end
67   - end
68   -
69   - describe "#initialize" do
70   - it "finds all projects that have existing wiki pages" do
71   - Project.count.should == 3
72   - subject.projects.count.should == 2
73   - end
74   - end
75   -
76   - context "#migrate!" do
77   - before do
78   - Gitlab::Shell.any_instance.stub(:add_repository) do |path|
79   - create_temp_repo("#{@repo_path}/#{path}.git")
80   - end
81   -
82   - subject.stub(:log).as_null_object
83   -
84   - subject.migrate!
85   - end
86   -
87   - it "creates a new Gollum Wiki for each project" do
88   - @projects.each do |project|
89   - wiki_path = project.path_with_namespace + ".wiki.git"
90   - full_path = @repo_path + "/" + wiki_path
91   - File.exist?(full_path).should be_true
92   - File.directory?(full_path).should be_true
93   - end
94   - end
95   -
96   - it "creates a gollum page for each unique Wiki page" do
97   - @projects.each do |project|
98   - wiki = GollumWiki.new(project, nil)
99   - wiki.pages.count.should == 3
100   - end
101   - end
102   -
103   - it "creates a new revision for each old revision of the page" do
104   - @projects.each do |project|
105   - wiki = GollumWiki.new(project, nil)
106   - wiki.pages.each do |page|
107   - page.versions.count.should == 2
108   - end
109   - end
110   - end
111   -
112   - context "wikis with pages that have titles that do not match the slugs" do
113   - before do
114   - project = @projects.last
115   - @page = project.wikis.new(title: "test page", content: "Invalid Page")
116   - @page.slug = "totally-incorrect-slug"
117   - @page.user = project.owner
118   - @page.save!
119   -
120   - create_revision(@page)
121   -
122   - subject.rollback!
123   - subject.migrate!
124   - end
125   -
126   - it "has a page with a title differing the slug" do
127   - @page.slug.should_not == @page.title.parameterize
128   - end
129   -
130   - it "creates a new revision for each old revision of the page" do
131   - @projects.each do |project|
132   - wiki = GollumWiki.new(project, nil)
133   - wiki.pages.each do |page|
134   - page.versions.count.should == 2
135   - end
136   - end
137   - end
138   - end
139   -
140   - context "changing wiki title from index to home" do
141   - before do
142   - @project = @projects.last
143   - @page = @project.wikis.new(title: "Index", content: "Home Page")
144   - @page.slug = "index"
145   - @page.user = @project.owner
146   - @page.save!
147   -
148   - create_revision(@page)
149   -
150   - subject.rollback!
151   - end
152   -
153   - it "creates a page called Home" do
154   - subject.migrate!
155   - wiki = GollumWiki.new(@project, nil)
156   - page = wiki.find_page("home")
157   - page.should be_present
158   - end
159   -
160   - context "when a page called Home already exists" do
161   - before do
162   - @index_page = @project.wikis.new(title: "Index", content: "Index Page")
163   - @index_page.slug = "index"
164   - @index_page.user = @project.owner
165   - @index_page.save!
166   -
167   - create_revision(@index_page)
168   -
169   - @home_page = @project.wikis.new(title: "Home", content: "Home Page")
170   - @home_page.slug = "home"
171   - @home_page.user = @project.owner
172   - @home_page.save!
173   -
174   - create_revision(@home_page)
175   - subject.migrate!
176   - end
177   -
178   - it "creates the index page" do
179   - wiki = GollumWiki.new(@project, nil)
180   - page = wiki.find_page("index")
181   - page.should be_present
182   - end
183   -
184   - it "creates the home page" do
185   - wiki = GollumWiki.new(@project, nil)
186   - page = wiki.find_page("home")
187   - page.should be_present
188   - end
189   - end
190   - end
191   - end
192   -
193   - context "#rollback!" do
194   - before do
195   - Gitlab::Shell.any_instance.stub(:add_repository) do |path|
196   - create_temp_repo("#{@repo_path}/#{path}.git")
197   - end
198   -
199   - Gitlab::Shell.any_instance.stub(:remove_repository) do |path|
200   - FileUtils.rm_rf "#{@repo_path}/#{path}.git"
201   - end
202   -
203   - subject.stub(:log).as_null_object
204   -
205   - subject.migrate!
206   - subject.rollback!
207   - end
208   -
209   - it "destroys all of the wiki repositories that were created during migrate!" do
210   - @projects.each do |project|
211   - wiki_path = project.path_with_namespace + ".wiki.git"
212   - full_path = @repo_path + "/" + wiki_path
213   - File.exist?(full_path).should be_false
214   - end
215   - end
216   - end
217   -
218   -
219   -end