This repository has been archived by the owner on Oct 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from alphagov/artefact-slug-migrator
Add artefact slug migrator task
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class ArtefactSlugMigrator | ||
|
||
attr_reader :logger | ||
|
||
def initialize(logger = nil) | ||
@logger = logger || Logger.new(STDOUT) | ||
end | ||
|
||
def run | ||
logger.info "Migrating slugs for #{slugs.size} artefacts" | ||
slugs.each do |slug, new_slug| | ||
artefact = Artefact.where(slug: slug).first | ||
raise "Artefact not found with slug #{slug}" if artefact.nil? | ||
|
||
rummageable_artefact = RummageableArtefact.new(artefact) | ||
rummageable_artefact.delete | ||
|
||
artefact.update_attribute(:slug, new_slug) | ||
|
||
logger.info " #{slug} -> #{new_slug}" | ||
end | ||
logger.info "Sequence complete." | ||
end | ||
|
||
private | ||
def slugs | ||
@slugs ||= load_slugs | ||
end | ||
|
||
def load_slugs | ||
json = File.open(Rails.root.join('data','slugs_to_migrate.json')).read | ||
JSON.parse(json) || [ ] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'artefact_slug_migrator' | ||
|
||
namespace :artefact_slug_migrator do | ||
desc "Migrate the artefact slugs specified in data/slugs_to_migrate.json" | ||
task :run => :environment do | ||
ArtefactSlugMigrator.new.run | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'test_helper' | ||
require 'artefact_slug_migrator' | ||
|
||
class ArtefactSlugMigratorTest < ActiveSupport::TestCase | ||
|
||
setup do | ||
# stub the observers for creating artefacts | ||
UpdateRouterObserver.any_instance.stubs(:after_save) | ||
UpdateSearchObserver.any_instance.stubs(:after_save) | ||
|
||
@artefacts = [ | ||
FactoryGirl.create(:artefact, :slug => "first-original-slug"), | ||
FactoryGirl.create(:artefact, :slug => "second-original-slug", :state => 'live'), | ||
FactoryGirl.create(:artefact, :slug => "third-original-slug", :state => 'archived') | ||
] | ||
|
||
@it = ArtefactSlugMigrator.new( Logger.new("/dev/null") ) | ||
|
||
RummageableArtefact.any_instance.stubs(:delete).returns(true) | ||
ArtefactSlugMigrator.any_instance.stubs(:slugs).returns({ | ||
"first-original-slug" => "first-new-slug", | ||
"second-original-slug" => "second-new-slug", | ||
"third-original-slug" => "third-new-slug" | ||
}) | ||
end | ||
|
||
should "remove artefact from search" do | ||
RummageableArtefact.any_instance.expects(:delete).at_least_once.returns(true) | ||
|
||
@it.run | ||
end | ||
|
||
should "update the slug in the artefact" do | ||
@it.run | ||
|
||
@artefacts.each(&:reload) | ||
assert_equal ["first-new-slug", "second-new-slug", "third-new-slug"], @artefacts.map(&:slug) | ||
end | ||
|
||
end |