Adds engines 2.1.0 plugin.
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1654 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
Description:
|
||||
The plugin migration generator assists in working with schema additions
|
||||
required by plugins. Instead of running migrations from plugins directly,
|
||||
the generator creates a regular Rails migration which will be responsible
|
||||
for migrating the plugins from their current version to the latest version
|
||||
installed.
|
||||
|
||||
This is important because the set of application migrations remains an
|
||||
accurate record of the state of the database, even as plugins are installed
|
||||
and removed during the development process.
|
||||
|
||||
Example:
|
||||
./script/generate plugin_migration [<plugin_name> <another_plugin_name> ...]
|
||||
|
||||
This will generate:
|
||||
|
||||
RAILS_ROOT
|
||||
|- db
|
||||
|-migrate
|
||||
|- xxx_plugin_migrations.rb
|
||||
|
||||
which contains the migrations for the given plugin(s).
|
||||
|
||||
|
||||
Advanced Usage:
|
||||
|
||||
There may be situations where you need *complete* control over the migrations
|
||||
of plugins in your application, migrating a certainly plugin down to X, and
|
||||
another plugin up to Y, where neither X or Y are the latest migrations for those
|
||||
plugins.
|
||||
|
||||
For those unfortunate few, I have two pieces of advice:
|
||||
|
||||
1. Why? This is a code smell [http://c2.com/xp/CodeSmell.html].
|
||||
|
||||
2. Well, OK. Don't panic. You can completely control plugin migrations by
|
||||
creating your own migrations. To manually migrate a plugin to a specific
|
||||
version, simply use
|
||||
|
||||
Engines.plugins[:your_plugin_name].migrate(version)
|
||||
|
||||
where version is the integer of the migration this plugin should end
|
||||
up at.
|
||||
|
||||
With great power comes great responsibility. Use this wisely.
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
# Generates a migration which migrates all plugins to their latest versions
|
||||
# within the database.
|
||||
class PluginMigrationGenerator < Rails::Generator::Base
|
||||
|
||||
def initialize(runtime_args, runtime_options={})
|
||||
super
|
||||
@options = {:assigns => {}}
|
||||
|
||||
ensure_plugin_schema_table_exists
|
||||
get_plugins_to_migrate(runtime_args)
|
||||
|
||||
if @plugins_to_migrate.empty?
|
||||
puts "All plugins are migrated to their latest versions"
|
||||
exit(0)
|
||||
end
|
||||
|
||||
@options[:migration_file_name] = build_migration_name
|
||||
@options[:assigns][:class_name] = build_migration_name.classify
|
||||
end
|
||||
|
||||
def manifest
|
||||
record do |m|
|
||||
m.migration_template 'plugin_migration.erb', 'db/migrate', @options
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Create the plugin schema table if it doesn't already exist. See
|
||||
# Engines::RailsExtensions::Migrations#initialize_schema_migrations_table_with_engine_additions
|
||||
def ensure_plugin_schema_table_exists
|
||||
ActiveRecord::Base.connection.initialize_schema_migrations_table
|
||||
end
|
||||
|
||||
# Determine all the plugins which have migrations that aren't present
|
||||
# according to the plugin schema information from the database.
|
||||
def get_plugins_to_migrate(plugin_names)
|
||||
|
||||
# First, grab all the plugins which exist and have migrations
|
||||
@plugins_to_migrate = if plugin_names.empty?
|
||||
Engines.plugins
|
||||
else
|
||||
plugin_names.map do |name|
|
||||
Engines.plugins[name] ? Engines.plugins[name] : raise("Cannot find the plugin '#{name}'")
|
||||
end
|
||||
end
|
||||
|
||||
@plugins_to_migrate.reject! { |p| p.latest_migration.nil? }
|
||||
|
||||
# Then find the current versions from the database
|
||||
@current_versions = {}
|
||||
@plugins_to_migrate.each do |plugin|
|
||||
@current_versions[plugin.name] = Engines::Plugin::Migrator.current_version(plugin)
|
||||
end
|
||||
|
||||
# Then find the latest versions from their migration directories
|
||||
@new_versions = {}
|
||||
@plugins_to_migrate.each do |plugin|
|
||||
@new_versions[plugin.name] = plugin.latest_migration
|
||||
end
|
||||
|
||||
# Remove any plugins that don't need migration
|
||||
@plugins_to_migrate.map { |p| p.name }.each do |name|
|
||||
@plugins_to_migrate.delete(Engines.plugins[name]) if @current_versions[name] == @new_versions[name]
|
||||
end
|
||||
|
||||
@options[:assigns][:plugins] = @plugins_to_migrate
|
||||
@options[:assigns][:new_versions] = @new_versions
|
||||
@options[:assigns][:current_versions] = @current_versions
|
||||
end
|
||||
|
||||
# Construct a unique migration name based on the plugins involved and the
|
||||
# versions they should reach after this migration is run.
|
||||
def build_migration_name
|
||||
@plugins_to_migrate.map do |plugin|
|
||||
"#{plugin.name}_to_version_#{@new_versions[plugin.name]}"
|
||||
end.join("_and_")
|
||||
end
|
||||
end
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
class <%= class_name %> < ActiveRecord::Migration
|
||||
def self.up
|
||||
<%- plugins.each do |plugin| -%>
|
||||
Engines.plugins["<%= plugin.name %>"].migrate(<%= new_versions[plugin.name] %>)
|
||||
<%- end -%>
|
||||
end
|
||||
|
||||
def self.down
|
||||
<%- plugins.each do |plugin| -%>
|
||||
Engines.plugins["<%= plugin.name %>"].migrate(<%= current_versions[plugin.name] %>)
|
||||
<%- end -%>
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user