DEV: allows to run up/down migrations of a plugin (#9241)

This is mostly useful while developing a plugin, to avoid manual actions of deleting tables and schema_migrations rows.

Usage:
bundle exec rake plugin:migrate:down[discourse-calendar]
This commit is contained in:
Joffrey JAFFEUX 2020-03-19 19:30:08 +01:00 committed by GitHub
parent 080960a15e
commit 86eb03af85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 0 deletions

View File

@ -146,3 +146,32 @@ task 'plugin:qunit', [:plugin, :timeout] do |t, args|
sh cmd
end
desc 'run all migrations of a plugin'
namespace 'plugin:migrate' do
def list_migrations(plugin_name)
plugin_root = File.join(Rails.root, "plugins", plugin_name)
migrations_root = File.join(plugin_root, "db", "{post_migrate,migrate}", "*.rb")
Dir[migrations_root]
.map do |migration_filename|
File.basename(migration_filename)[/(^.*?)_/, 1].to_i
end
.sort
end
def cmd(operation, migration_number)
"rails db:migrate:#{operation} LOAD_PLUGINS=1 VERSION=#{migration_number}"
end
task :down, [:plugin] do |t, args|
list_migrations(args[:plugin]).reverse.each do |migration_number|
sh cmd(:down, migration_number)
end
end
task :up, [:plugin] do |t, args|
list_migrations(args[:plugin]).each do |migration_number|
sh cmd(:up, migration_number)
end
end
end