forked from ilyakatz/data-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name space generators; add install generator
- Loading branch information
Andrew J Vargo
committed
Apr 12, 2011
1 parent
0a53ab7
commit 5613830
Showing
3 changed files
with
77 additions
and
21 deletions.
There are no files selected for viewing
44 changes: 23 additions & 21 deletions
44
lib/generators/data_migration/data_migration_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
13 changes: 13 additions & 0 deletions
13
lib/generators/data_migration/templates/install_migration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |