Class: MigrationGeneratorCore

Inherits:
Object
  • Object
show all
Defined in:
lib/data_plan/generators/migration/migration_generator_core.rb

Overview

you should set DB_PROJECT_ROOT constant to some path before including this file

Constant Summary collapse

DB_PROJECT_ROOT =
"."

Instance Method Summary collapse

Instance Method Details

#calculate_migration(predefined_migration_name = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/data_plan/generators/migration/migration_generator_core.rb', line 12

def calculate_migration(predefined_migration_name=nil)
  require File.join( File.expand_path(File.dirname(__FILE__)),'lib','auto_migrations.rb')
  require File.join( File.expand_path(File.dirname(__FILE__)),'lib','sexy_statements_hook.rb') 
  ActiveRecord::Migration.send :include, SexyStatementsHook
  ActiveRecord::Migration.send :include, AutoMigrations      

  AutoMigrations.run
  
  print "Migration generated. Self.up is:\n" + ActiveRecord::Migration.sexy_up

  migration_name = predefined_migration_name
  if migration_name.blank?
    name = ActiveRecord::Migration.desired_migration_name 
    name = migration_default_name if name.blank?
    migration_name = input("\nEnter filename [#{name}]:").strip.gsub(' ', '_')
    migration_name = name if migration_name.blank?
  end  

  up = ActiveRecord::Migration.sexy_up + "    "
  down = ActiveRecord::Migration.sexy_down
    
  hints = <<HINT

# Hints on migrations syntax.
#
# add_column(table_name, column_name, type, options = {}) 
#	  Types are: :primary_key, :string, :text, :integer, :float, :datetime, :timestamp, :time, :date, :binary, :boolean. 
#
# Other stuff:
#   change_column(table_name, column_name, type, options = {}) - options as in add_column
#   change_column_default(table_name, column_name, default)
#   remove_column(table_name, column_name) 
#   rename_column(table_name, column_name, new_column_name) 
#
# Sample: Don't add a primary key column
#   create_table(:categories_suppliers, :id => false) do |t|
#     t.column :category_id, :integer
#     t.column :supplier_id, :integer
#   end
#
# Sample: reload columns for a model from DB
#   Person.reset_column_information
#
# More samples here: http://guides.rubyonrails.org/migrations.html
HINT

  #return { :up => up, :down => down, :hints => hints, :migration_name => migration_name.camelize, :migration_file_name => migration_name }
  return up,down,hints,migration_name

end

#input(prompt, options = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/data_plan/generators/migration/migration_generator_core.rb', line 64

def input(prompt, options=nil)
  print(prompt + " ")
  if options
    while !(response = STDIN.readline.strip.downcase).in?(options); 
      print(prompt + " ")
    end
    response
  else
    STDIN.readline
  end
end

#migration_default_nameObject



7
8
9
10
# File 'lib/data_plan/generators/migration/migration_generator_core.rb', line 7

def migration_default_name
  i = Dir["#{DB_PROJECT_ROOT}/db/migrate/*definition_migration*"].length
  "definition_migration_#{i+1}"
end