diff --git a/lib/generators/data_migration/data_migration_generator.rb b/lib/generators/data_migration/data_migration_generator.rb index 12d7b614..285e5e96 100644 --- a/lib/generators/data_migration/data_migration_generator.rb +++ b/lib/generators/data_migration/data_migration_generator.rb @@ -1,32 +1,34 @@ require 'rails/generators/migration' +module DataMigration + class DataMigrationGenerator < Rails::Generators::NamedBase + namespace "data_migration" + argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" -class DataMigrationGenerator < Rails::Generators::NamedBase - argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" + source_root File.expand_path('../templates', __FILE__) + include Rails::Generators::Migration - source_root File.expand_path('../templates', __FILE__) - include Rails::Generators::Migration + class_option :skip_migration, :desc => 'Dont generate database migration file.', :type => :boolean - class_option :skip_migration, :desc => 'Dont generate database migration file.', :type => :boolean - - def create_data_migration - set_local_assigns! - unless options.skip_migration? - migration_template "migration.rb", "db/migrate/#{file_name}.rb" + def create_data_migration + set_local_assigns! + unless options.skip_migration? + migration_template "migration.rb", "db/migrate/#{file_name}.rb" + end + migration_template "data_migration.rb", "db/data/#{file_name}.rb" end - migration_template "data_migration.rb", "db/data/#{file_name}.rb" - end - protected - attr_reader :migration_action + protected + attr_reader :migration_action - def self.next_migration_number(dirname) - Time.now.utc.strftime("%Y%m%d%H%M%S") - end + def self.next_migration_number(dirname) + Time.now.utc.strftime("%Y%m%d%H%M%S") + end - def set_local_assigns! - if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/ - @migration_action = $1 - @table_name = $2.pluralize + def set_local_assigns! + if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/ + @migration_action = $1 + @table_name = $2.pluralize + end end end end diff --git a/lib/generators/data_migration/install_generator.rb b/lib/generators/data_migration/install_generator.rb new file mode 100644 index 00000000..d4395f06 --- /dev/null +++ b/lib/generators/data_migration/install_generator.rb @@ -0,0 +1,41 @@ +# require 'rails/generators/migration' +# class InstallGenerator < Rails::Generators::Base +# source_root File.expand_path('../templates', __FILE__) +# include Rails::Generators::Migration + +# def data_migrations_table_migration +# generate("migration", "add_data_migrations_table version:text") +# migration_template "install_migration.rb", "db/migrate/create_data_migrations.rb" +# end +# end + + +# protected +# def self.next_migration_number(dirname) +# Time.now.utc.strftime("%Y%m%d%H%M%S") +# end +# end +require 'rails/generators' +require 'rails/generators/migration' +module DataMigration + class InstallGenerator < Rails::Generators::Base + include Rails::Generators::Migration + def self.source_root + @source_root ||= File.join(File.dirname(__FILE__), 'templates') + end + + def self.next_migration_number(dirname) + if ActiveRecord::Base.timestamped_migrations + Time.new.utc.strftime("%Y%m%d%H%M%S") + else + "%.3d" % (current_migration_number(dirname) + 1) + end + end + + def create_migration_file + migration_template "install_migration.rb", "db/migrate/create_data_migrations.rb" + #migration_template 'migration.rb', 'db/migrate/create_pages_table.rb' + end + end + +end diff --git a/lib/generators/data_migration/templates/install_migration.rb b/lib/generators/data_migration/templates/install_migration.rb new file mode 100644 index 00000000..34dd9e7e --- /dev/null +++ b/lib/generators/data_migration/templates/install_migration.rb @@ -0,0 +1,13 @@ +class CreateDataMigrations < ActiveRecord::Migration + def self.up + create_table :data_migrations do |t| + t.string :version + end + + add_index :data_migrations, :version + end + + def self.down + drop_table :data_migrations + end +end