From a614718e29e7121f63e1c62a318555e4ebacc4a3 Mon Sep 17 00:00:00 2001 From: Sukhbir Singh Date: Mon, 6 Aug 2018 16:21:52 +0530 Subject: [PATCH] fixed migrations bug of plugins (#221) great job in drilling down this bug! --- app/models/plugin.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/models/plugin.rb b/app/models/plugin.rb index f9642635..014ef873 100644 --- a/app/models/plugin.rb +++ b/app/models/plugin.rb @@ -56,8 +56,7 @@ def before_destroy base = File.basename path destination = File.join(Rails.root, "plugins", "#{1000+id}-#{base}") - Rails.application.paths["db/migrate"] << "#{destination}/db/migrate" - ActiveRecord::Migration.new.migrate(:down) + Plugin.run_migration(destination, :down) FileUtils.rm_rf destination # restart the rails stack -- FIXME: this is too much a restart would be best @@ -81,8 +80,7 @@ def dir2plugin(source, config) FileUtils.rm_rf destination FileUtils.mv source, destination - Rails.application.paths["db/migrate"] << "#{destination}/db/migrate" - ActiveRecord::Migration.new.migrate(:up) + self.run_migration(destination, :up) # restart the rails stack -- FIXME: this is too much a restart would be best c = Command.new "touch /var/hda/platform/html/tmp/restart.txt" @@ -90,6 +88,19 @@ def dir2plugin(source, config) # return the plugin we just created plugin end + + def run_migration(destination, type) + migration_files = Dir["#{destination}/db/migrate/*.rb"] + migration_files.each do |migration_file| + start_index = migration_file.index("_")+1 + last_index = migration_file.rindex(".")-1 + require "#{migration_file}" + + class_name = migration_file[start_index..last_index].camelize.constantize + class_name.new.migrate(type) + end + end + end end