Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #24 from alphagov/artefact-slug-migrator

Add artefact slug migrator task
  • Loading branch information...
commit 52fce0c321a15b551e5dcec2c37a19b4a69a4f08 2 parents d0a2572 + cbfee22
@KushalP KushalP authored
View
3  data/slugs_to_migrate.json
@@ -0,0 +1,3 @@
+{
+
+}
View
34 lib/artefact_slug_migrator.rb
@@ -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
View
8 lib/tasks/artefact_slug_migrator.rake
@@ -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
View
40 test/unit/artefact_slug_migrator_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.