Browse Source

Made migration irreversable, support '$variable' syntax

Dominik Sander 11 years ago
parent
commit
da137aaeb8

+ 5 - 1
db/migrate/20140426202023_migrate_hipchat_and_ef_agent_to_liquid.rb

@@ -1,5 +1,5 @@
 class MigrateHipchatAndEfAgentToLiquid < ActiveRecord::Migration
-  def change
+  def up
     Agent.where(:type => 'Agents::HipchatAgent').each do |agent|
       LiquidMigrator.convert_all_agent_options(agent)
     end
@@ -8,4 +8,8 @@ class MigrateHipchatAndEfAgentToLiquid < ActiveRecord::Migration
       agent.save
     end
   end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration, "Cannot revert migration to Liquid templating"
+  end
 end

+ 5 - 1
db/migrate/20140430215234_migrate_pushbullet_agent_to_liquid.rb

@@ -1,7 +1,11 @@
 class MigratePushbulletAgentToLiquid < ActiveRecord::Migration
-  def change
+  def up
     Agent.where(:type => 'Agents::PushbulletAgent').each do |agent|
       LiquidMigrator.convert_all_agent_options(agent)
     end
   end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration, "Cannot revert migration to Liquid templating"
+  end
 end

+ 5 - 1
db/migrate/20140501183219_migrate_jabber_agent_to_liquid.rb

@@ -1,7 +1,11 @@
 class MigrateJabberAgentToLiquid < ActiveRecord::Migration
-  def change
+  def up
     Agent.where(:type => 'Agents::JabberAgent').each do |agent|
       LiquidMigrator.convert_all_agent_options(agent)
     end
   end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration, "Cannot revert migration to Liquid templating"
+  end
 end

+ 5 - 1
db/migrate/20140501191849_migrate_data_output_agent_to_liquid.rb

@@ -1,7 +1,11 @@
 class MigrateDataOutputAgentToLiquid < ActiveRecord::Migration
-  def change
+  def up
     Agent.where(:type => 'Agents::DataOutputAgent').each do |agent|
       LiquidMigrator.convert_all_agent_options(agent)
     end
   end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration, "Cannot revert migration to Liquid templating"
+  end
 end

+ 5 - 1
db/migrate/20140501200859_migrate_translation_agent_to_liquid.rb

@@ -1,8 +1,12 @@
 class MigrateTranslationAgentToLiquid < ActiveRecord::Migration
-  def change
+  def up
     Agent.where(:type => 'Agents::TranslationAgent').each do |agent|
       agent.options['content'] = LiquidMigrator.convert_hash(agent.options['content'], {:merge_path_attributes => true, :leading_dollarsign_is_jsonpath => true})
       agent.save
     end
   end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration, "Cannot revert migration to Liquid templating"
+  end
 end

+ 5 - 1
db/migrate/20140501203828_migrate_twitter_publish_agent_to_liquid.rb

@@ -1,5 +1,5 @@
 class MigrateTwitterPublishAgentToLiquid < ActiveRecord::Migration
-  def change
+  def up
     Agent.where(:type => 'Agents::TwitterPublishAgent').each do |agent|
       if (message = agent.options.delete('message_path')).present?
         agent.options['message'] = "{{#{message}}}"
@@ -7,4 +7,8 @@ class MigrateTwitterPublishAgentToLiquid < ActiveRecord::Migration
       end
     end
   end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration, "Cannot revert migration to Liquid templating"
+  end
 end

+ 6 - 2
lib/liquid_migrator.rb

@@ -52,11 +52,15 @@ module LiquidMigrator
 
   def self.convert_json_path(string, filter = "")
     check_path(string)
-    "{{#{string[2..-1]}#{filter}}}"
+    if string.start_with? '$.'
+      "{{#{string[2..-1]}#{filter}}}"
+    else
+      "{{#{string[1..-1]}#{filter}}}"
+    end
   end
 
   def self.check_path(string)
-    if string !~ /\A(\$\.)?(\w+\.)*(\w+)\Z/
+    if string !~ /\A(\$\.?)?(\w+\.)*(\w+)\Z/
       raise "JSONPath '#{string}' is too complex, please check your migration."
     end
   end

+ 2 - 0
spec/lib/liquid_migrator_spec.rb

@@ -5,10 +5,12 @@ describe LiquidMigrator do
     it "should work" do
       LiquidMigrator.convert_string("$.data", true).should == "{{data}}"
       LiquidMigrator.convert_string("$.data.test", true).should == "{{data.test}}"
+      LiquidMigrator.convert_string("$first_title", true).should == "{{first_title}}"
     end
 
     it "should ignore strings which just contain a JSONPath" do
       LiquidMigrator.convert_string("$.data").should == "$.data"
+      LiquidMigrator.convert_string("$first_title").should == "$first_title"
       LiquidMigrator.convert_string(" $.data", true).should == " $.data"
       LiquidMigrator.convert_string("lorem $.data", true).should == "lorem $.data"
     end