Class: Tableflip::ArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/tableflip/argument_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArgumentParser

Instance Methods =====================================================



14
15
# File 'lib/tableflip/argument_parser.rb', line 14

def initialize
end

Class Method Details

.default_env(env = nil) ⇒ Object

Class Methods ========================================================



8
9
10
# File 'lib/tableflip/argument_parser.rb', line 8

def self.default_env(env = nil)
  (env || ENV)['RAILS_ENV'] || 'development'
end

Instance Method Details

#parse(args, env = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tableflip/argument_parser.rb', line 17

def parse(args, env = nil)
  strategy = Tableflip::Strategy.new

  strategy.source_env = self.class.default_env(env)

  _parser = parser(strategy)

  tables = _parser.parse!(args)

  tables.each do |table|
    strategy.tables << table
  end

  if (strategy.tables.empty? or strategy.actions.empty?)
    strategy.message = _parser.to_s
  end

  strategy
end

#parser(strategy) ⇒ Object



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tableflip/argument_parser.rb', line 37

def parser(strategy)
  OptionParser.new do |parser|
    parser.banner = "Usage: tableflip [options] [table_name [table_name [...]]]"

    parser.separator("")
    parser.separator("Options:")

    parser.on("-a", "--all", "Track all tables") do |s|
      strategy.tables << :__all__
    end
    parser.on("-i", "--insert", "Use INSERT IGNORE instead of REPLACE INTO") do
      strategy.migrate_method = :insert
    end
    parser.on("-b", "--block=s", "Transfer data in blocks of N rows") do |s|
      strategy.block_size = s.to_i
    end
    parser.on("-f", "--config=s") do |path|
      strategy.config_path = path
    end
    parser.on("-t", "--track", "Add tracking triggers on tables") do
      strategy.actions << :tracking_add
    end
    parser.on("-d", "--seed", "Seed the tracking table with entries from the source table") do
      strategy.actions << :tracking_seed
    end
    parser.on("-r", "--remove", "Remove tracking triggers from tables") do
      strategy.actions << :tracking_remove
    end
    parser.on("-o", "--target=s", "Set target environment") do |s|
      strategy.target_env = s
    end
    parser.on("-m", "--migrate", "Migrate data from source to target") do
      strategy.actions << :table_migrate
    end
    parser.on("-c", "--count", "Count number of records in source table") do
      strategy.actions << :table_count
    end
    parser.on("-s", "--status", "Show current status") do
      strategy.actions << :table_report_status
    end
    parser.on("-e", "--env=s", "Establish primary environment") do |s|
      strategy.source_env = s
    end
    parser.on("-x", "--exclude=s", "Exclude column(s) from migration") do |s|
      s.split(/,/).each do |column|
        strategy.exclude_columns << column
      end
    end
    parser.on("-n", "--encoding=s", "Set connection encoding") do |s|
      strategy.encoding = s
    end
    parser.on("-k", "--create-test", "Creates a test table") do
      strategy.actions << :table_create_test
    end
    parser.on("-z", "--fuzz[=d]", "Inserts and alters records on test table") do |d|
      strategy.actions << :table_fuzz

      if (d)
        strategy.fuzz_intensity = d.to_i
      end
    end
    parser.on("-p", "--persist", "Keep running perpetually") do
      strategy.persist = true
    end
    parser.on("-w","--where=s", "Add conditions to selecting") do |s|
      strategy.where = s
    end
    parser.on("-q", "--debug", "Show the queries as they're executed") do
      strategy.debug_queries = true
    end
    parser.on("-h", "--help", "Display this help") do
      strategy.message = parser.to_s
    end
  end
end