Commit 7b868c61 authored by James Lopez's avatar James Lopez

refactored migration and spec based on feedback

parent ecb174bf
...@@ -4,19 +4,21 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration ...@@ -4,19 +4,21 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration
class ProjectPath class ProjectPath
attr_reader :old_path, :id, :namespace_path attr_reader :old_path, :id, :namespace_path
def initialize(old_path, id, namespace_path) def initialize(old_path, id, namespace_path, namespace_id)
@old_path = old_path @old_path = old_path
@id = id @id = id
@namespace_path = namespace_path @namespace_path = namespace_path
@namespace_id = namespace_id
end end
def clean_path def clean_path
@_clean_path ||= PathCleaner.clean(@old_path) @_clean_path ||= PathCleaner.clean(@old_path, @namespace_id)
end end
end end
class PathCleaner class PathCleaner
def initialize(path) def initialize(path, namespace_id)
@namespace_id = namespace_id
@path = path @path = path
end end
...@@ -34,22 +36,24 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration ...@@ -34,22 +36,24 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration
path path
end end
private
def cleaned_path def cleaned_path
@_cleaned_path ||= @path.gsub(/\.atom\z/, '-atom') @_cleaned_path ||= @path.gsub(/\.atom\z/, '-atom')
end end
def path_exists?(path) def path_exists?(path)
Project.find_by_path(path) Project.find_by_path_and_namespace_id(path, @namespace_id)
end end
end end
def projects_with_dot_atom def projects_with_dot_atom
select_all("SELECT p.id, p.path, n.path as namespace_path FROM projects p inner join namespaces n on n.id = p.namespace_id WHERE lower(p.path) LIKE '%.atom'") select_all("SELECT p.id, p.path, n.path as namespace_path, n.id as namespace_id FROM projects p inner join namespaces n on n.id = p.namespace_id WHERE lower(p.path) LIKE '%.atom'")
end end
def up def up
projects_with_dot_atom.each do |project| projects_with_dot_atom.each do |project|
project_path = ProjectPath.new(project['path'], project['id'], project['namespace_path']) project_path = ProjectPath.new(project['path'], project['id'], project['namespace_path'], project['namespace_id'])
clean_path(project_path) if rename_project_repo(project_path) clean_path(project_path) if rename_project_repo(project_path)
end end
end end
...@@ -66,5 +70,7 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration ...@@ -66,5 +70,7 @@ class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration
gitlab_shell.mv_repository("#{old_path_with_namespace}.wiki", "#{new_path_with_namespace}.wiki") gitlab_shell.mv_repository("#{old_path_with_namespace}.wiki", "#{new_path_with_namespace}.wiki")
gitlab_shell.mv_repository(old_path_with_namespace, new_path_with_namespace) gitlab_shell.mv_repository(old_path_with_namespace, new_path_with_namespace)
rescue
false
end end
end end
...@@ -88,10 +88,10 @@ describe ProjectsController do ...@@ -88,10 +88,10 @@ describe ProjectsController do
end end
context "when the url contains .atom" do context "when the url contains .atom" do
let(:public_project_with_dot_atom) { create(:project, :public, name: 'my.atom', path: 'my.atom') } let(:public_project_with_dot_atom) { build(:project, :public, name: 'my.atom', path: 'my.atom') }
it 'expect an error creating the project' do it 'expect an error creating the project' do
expect { public_project_with_dot_atom }.to raise_error(ActiveRecord::RecordInvalid) expect(public_project_with_dot_atom).not_to be_valid
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment