Class: Tapsoob::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/tapsoob/operation.rb

Direct Known Subclasses

Pull, Push

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database_url, dump_path = nil, opts = {}) ⇒ Operation

Returns a new instance of Operation.



13
14
15
16
17
18
# File 'lib/tapsoob/operation.rb', line 13

def initialize(database_url, dump_path = nil, opts={})
  @database_url = database_url
  @dump_path    = dump_path
  @opts         = opts
  @exiting      = false
end

Instance Attribute Details

#database_urlObject (readonly)

Returns the value of attribute database_url.



11
12
13
# File 'lib/tapsoob/operation.rb', line 11

def database_url
  @database_url
end

#dump_pathObject (readonly)

Returns the value of attribute dump_path.



11
12
13
# File 'lib/tapsoob/operation.rb', line 11

def dump_path
  @dump_path
end

#optsObject (readonly)

Returns the value of attribute opts.



11
12
13
# File 'lib/tapsoob/operation.rb', line 11

def opts
  @opts
end

Class Method Details

.factory(type, database_url, dump_path, opts) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/tapsoob/operation.rb', line 141

def self.factory(type, database_url, dump_path, opts)
  type = :resume if opts[:resume]
  klass = case type
    when :pull   then Tapsoob::Pull
    when :push   then Tapsoob::Push
    when :resume then eval(opts[:klass])
    else raise "Unknown Operation Type -> #{type}"
  end

  klass.new(database_url, dump_path, opts)
end

Instance Method Details

#apply_table_filter(tables) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tapsoob/operation.rb', line 40

def apply_table_filter(tables)
  return tables unless table_filter || exclude_tables

  re = table_filter ? Regexp.new(table_filter) : nil
  if tables.kind_of?(Hash)
    ntables = {}
    tables.each do |t, d|
      if !exclude_tables.include?(t.to_s) && (!re || !re.match(t.to_s).nil?)
        ntables[t] = d
      end
    end
    ntables
  else
    tables.reject { |t| exclude_tables.include?(t.to_s) || (re && re.match(t.to_s).nil?) }
  end
end

#catch_errors(&blk) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/tapsoob/operation.rb', line 133

def catch_errors(&blk)
  begin
    blk.call
  rescue Exception => e
    raise e
  end
end

#completed_tablesObject



104
105
106
# File 'lib/tapsoob/operation.rb', line 104

def completed_tables
  opts[:completed_tables] ||= []
end

#dbObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/tapsoob/operation.rb', line 116

def db
  @db ||= Sequel.connect(database_url)
  @db.loggers << Tapsoob.log if opts[:debug]

  # Set parameters
  if @db.uri =~ /oracle/i
    @db << "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'"
    @db << "ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS:FF6'"
  end

  @db
end

#default_chunksizeObject



100
101
102
# File 'lib/tapsoob/operation.rb', line 100

def default_chunksize
  opts[:default_chunksize]
end

#exclude_tablesObject



36
37
38
# File 'lib/tapsoob/operation.rb', line 36

def exclude_tables
  opts[:exclude_tables] || []
end

#exiting?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/tapsoob/operation.rb', line 80

def exiting?
  !!@exiting
end

#file_prefixObject



20
21
22
# File 'lib/tapsoob/operation.rb', line 20

def file_prefix
  "op"
end

#format_number(num) ⇒ Object



129
130
131
# File 'lib/tapsoob/operation.rb', line 129

def format_number(num)
  num.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
end

#indexes_first?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/tapsoob/operation.rb', line 28

def indexes_first?
  !!opts[:indexes_first]
end

#logObject



57
58
59
60
# File 'lib/tapsoob/operation.rb', line 57

def log
  Tapsoob.log.level = Logger::DEBUG if opts[:debug]
  Tapsoob.log
end

#resuming?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/tapsoob/operation.rb', line 96

def resuming?
  opts[:resume] == true
end

#setup_signal_trapObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tapsoob/operation.rb', line 84

def setup_signal_trap
  trap("INT") {
    puts "\nCompleting current action..."
    @exiting = true
  }

  trap("TERM") {
    puts "\nCompleting current action..."
    @exiting = true
  }
end

#skip_schema?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/tapsoob/operation.rb', line 24

def skip_schema?
  !!opts[:skip_schema]
end

#store_sessionObject



62
63
64
65
66
67
68
# File 'lib/tapsoob/operation.rb', line 62

def store_session
  file = "#{file_prefix}_#{Time.now.strftime("%Y%m%d%H%M")}.dat"
  log.info "\nSaving session to #{file}..."
  File.open(file, 'w') do |f|
    f.write(JSON.generate(to_hash))
  end
end

#stream_stateObject



108
109
110
# File 'lib/tapsoob/operation.rb', line 108

def stream_state
  opts[:stream_state] ||= {}
end

#stream_state=(val) ⇒ Object



112
113
114
# File 'lib/tapsoob/operation.rb', line 112

def stream_state=(val)
  opts[:stream_state] = val
end

#table_filterObject



32
33
34
# File 'lib/tapsoob/operation.rb', line 32

def table_filter
  opts[:table_filter]
end

#to_hashObject



70
71
72
73
74
75
76
77
78
# File 'lib/tapsoob/operation.rb', line 70

def to_hash
  {
    :klass            => self.class.to_s,
    :database_url     => database_url,
    :stream_state     => stream_state,
    :completed_tables => completed_tables,
    :table_filter     => table_filter,
  }
end