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