@@ -1,17 +1,17 | |||
|
1 | 1 | begin |
|
2 | 2 | require 'rails/version' |
|
3 | 3 | unless Rails::VERSION::MAJOR >= 2 && Rails::VERSION::MINOR >= 3 && Rails::VERSION::TINY >= 2 |
|
4 | 4 | raise "This version of the engines plugin requires Rails 2.3.2 or later!" |
|
5 | 5 | end |
|
6 | 6 | end |
|
7 | 7 | |
|
8 | 8 | require File.join(File.dirname(__FILE__), 'lib/engines') |
|
9 | 9 | |
|
10 | 10 | # initialize Rails::Configuration with our own default values to spare users |
|
11 | 11 | # some hassle with the installation and keep the environment cleaner |
|
12 | 12 | |
|
13 |
{ :default_plugin_locators => [Engines::Plugin::FileSystemLocator |
|
|
13 | { :default_plugin_locators => (defined?(Gem) ? [Rails::Plugin::GemLocator] : []).push(Engines::Plugin::FileSystemLocator), | |
|
14 | 14 | :default_plugin_loader => Engines::Plugin::Loader, |
|
15 | 15 | :default_plugins => [:engines, :all] }.each do |name, default| |
|
16 | 16 | Rails::Configuration.send(:define_method, name) { default } |
|
17 | 17 | end No newline at end of file |
@@ -1,98 +1,98 | |||
|
1 | 1 | # Generates a migration which migrates all plugins to their latest versions |
|
2 | 2 | # within the database. |
|
3 | 3 | class PluginMigrationGenerator < Rails::Generator::Base |
|
4 | 4 | |
|
5 | 5 | # 255 characters max for Windows NTFS (http://en.wikipedia.org/wiki/Filename) |
|
6 | 6 | # minus 14 for timestamp, minus some extra chars for dot, underscore, file |
|
7 | 7 | # extension. So let's have 230. |
|
8 | 8 | MAX_FILENAME_LENGTH = 230 |
|
9 | 9 | |
|
10 | 10 | def initialize(runtime_args, runtime_options={}) |
|
11 | 11 | super |
|
12 | 12 | @options = {:assigns => {}} |
|
13 | 13 | ensure_schema_table_exists |
|
14 | 14 | get_plugins_to_migrate(runtime_args) |
|
15 | 15 | |
|
16 | 16 | if @plugins_to_migrate.empty? |
|
17 | 17 | puts "All plugins are migrated to their latest versions" |
|
18 | 18 | exit(0) |
|
19 | 19 | end |
|
20 | 20 | |
|
21 | 21 | @options[:migration_file_name] = build_migration_name |
|
22 | 22 | @options[:assigns][:class_name] = build_migration_name.classify |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | def manifest |
|
26 | 26 | record do |m| |
|
27 | 27 | m.migration_template 'plugin_migration.erb', 'db/migrate', @options |
|
28 | 28 | end |
|
29 | 29 | end |
|
30 | 30 | |
|
31 | 31 | protected |
|
32 | 32 | |
|
33 | 33 | # Create the schema table if it doesn't already exist. |
|
34 | 34 | def ensure_schema_table_exists |
|
35 | 35 | ActiveRecord::Base.connection.initialize_schema_migrations_table |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | # Determine all the plugins which have migrations that aren't present |
|
39 | 39 | # according to the plugin schema information from the database. |
|
40 | 40 | def get_plugins_to_migrate(plugin_names) |
|
41 | 41 | |
|
42 | 42 | # First, grab all the plugins which exist and have migrations |
|
43 | 43 | @plugins_to_migrate = if plugin_names.empty? |
|
44 | 44 | Engines.plugins |
|
45 | 45 | else |
|
46 | 46 | plugin_names.map do |name| |
|
47 | 47 | Engines.plugins[name] ? Engines.plugins[name] : raise("Cannot find the plugin '#{name}'") |
|
48 | 48 | end |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | @plugins_to_migrate.reject! { |p| p.latest_migration.nil? } | |
|
51 | @plugins_to_migrate.reject! { |p| !p.respond_to?(:latest_migration) || p.latest_migration.nil? } | |
|
52 | 52 | |
|
53 | 53 | # Then find the current versions from the database |
|
54 | 54 | @current_versions = {} |
|
55 | 55 | @plugins_to_migrate.each do |plugin| |
|
56 | 56 | @current_versions[plugin.name] = Engines::Plugin::Migrator.current_version(plugin) |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | # Then find the latest versions from their migration directories |
|
60 | 60 | @new_versions = {} |
|
61 | 61 | @plugins_to_migrate.each do |plugin| |
|
62 | 62 | @new_versions[plugin.name] = plugin.latest_migration |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | # Remove any plugins that don't need migration |
|
66 | 66 | @plugins_to_migrate.map { |p| p.name }.each do |name| |
|
67 | 67 | @plugins_to_migrate.delete(Engines.plugins[name]) if @current_versions[name] == @new_versions[name] |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | @options[:assigns][:plugins] = @plugins_to_migrate |
|
71 | 71 | @options[:assigns][:new_versions] = @new_versions |
|
72 | 72 | @options[:assigns][:current_versions] = @current_versions |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | # Returns a migration name. If the descriptive migration name based on the |
|
76 | 76 | # plugin names involved is shorter than 230 characters that one will be |
|
77 | 77 | # used. Otherwise a shorter name will be returned. |
|
78 | 78 | def build_migration_name |
|
79 | 79 | returning descriptive_migration_name do |name| |
|
80 | 80 | name.replace short_migration_name if name.length > MAX_FILENAME_LENGTH |
|
81 | 81 | end |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | # Construct a unique migration name based on the plugins involved and the |
|
85 | 85 | # versions they should reach after this migration is run. The name constructed |
|
86 | 86 | # needs to be lowercase |
|
87 | 87 | def descriptive_migration_name |
|
88 | 88 | @plugins_to_migrate.map do |plugin| |
|
89 | 89 | "#{plugin.name}_to_version_#{@new_versions[plugin.name]}" |
|
90 | 90 | end.join("_and_").downcase |
|
91 | 91 | end |
|
92 | 92 | |
|
93 | 93 | # Short migration name that will be used if the descriptive_migration_name |
|
94 | 94 | # exceeds 230 characters |
|
95 | 95 | def short_migration_name |
|
96 | 96 | 'plugin_migrations' |
|
97 | 97 | end |
|
98 | 98 | end No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now