Class: Tapsoob::Operation
- Inherits:
-
Object
- Object
- Tapsoob::Operation
- Defined in:
- lib/tapsoob/operation.rb
Instance Attribute Summary collapse
-
#database_url ⇒ Object
readonly
Returns the value of attribute database_url.
-
#dump_path ⇒ Object
readonly
Returns the value of attribute dump_path.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_table_filter(tables) ⇒ Object
- #catch_errors(&blk) ⇒ Object
- #completed_tables ⇒ Object
- #data? ⇒ Boolean
- #db ⇒ Object
- #default_chunksize ⇒ Object
- #exclude_tables ⇒ Object
- #exiting? ⇒ Boolean
- #file_prefix ⇒ Object
- #format_number(num) ⇒ Object
- #indexes_first? ⇒ Boolean
-
#initialize(database_url, dump_path = nil, opts = {}) ⇒ Operation
constructor
A new instance of Operation.
- #log ⇒ Object
- #resuming? ⇒ Boolean
- #schema? ⇒ Boolean
- #setup_signal_trap ⇒ Object
- #store_session ⇒ Object
- #stream_state ⇒ Object
- #stream_state=(val) ⇒ Object
- #table_filter ⇒ Object
- #to_hash ⇒ Object
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_url ⇒ Object (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_path ⇒ Object (readonly)
Returns the value of attribute dump_path.
11 12 13 |
# File 'lib/tapsoob/operation.rb', line 11 def dump_path @dump_path end |
#opts ⇒ Object (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
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/tapsoob/operation.rb', line 145 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tapsoob/operation.rb', line 44 def apply_table_filter(tables) return tables if table_filter.empty? && exclude_tables.empty? if tables.kind_of?(Hash) ntables = {} tables.each do |t, d| if !exclude_tables.include?(t.to_s) && (!table_filter.empty? && table_filter.include?(t.to_s)) ntables[t] = d end end ntables else tables.reject { |t| exclude_tables.include?(t.to_s) }.select { |t| table_filter.include?(t.to_s) } end end |
#catch_errors(&blk) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/tapsoob/operation.rb', line 137 def catch_errors(&blk) begin blk.call rescue Exception => e raise e end end |
#completed_tables ⇒ Object
107 108 109 |
# File 'lib/tapsoob/operation.rb', line 107 def completed_tables opts[:completed_tables] ||= [] end |
#data? ⇒ Boolean
24 25 26 |
# File 'lib/tapsoob/operation.rb', line 24 def data? opts[:data] end |
#db ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/tapsoob/operation.rb', line 119 def db @db ||= Sequel.connect(database_url) @db.extension :schema_dumper @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_chunksize ⇒ Object
103 104 105 |
# File 'lib/tapsoob/operation.rb', line 103 def default_chunksize opts[:default_chunksize] end |
#exclude_tables ⇒ Object
40 41 42 |
# File 'lib/tapsoob/operation.rb', line 40 def exclude_tables opts[:exclude_tables] || [] end |
#exiting? ⇒ Boolean
83 84 85 |
# File 'lib/tapsoob/operation.rb', line 83 def exiting? !!@exiting end |
#file_prefix ⇒ Object
20 21 22 |
# File 'lib/tapsoob/operation.rb', line 20 def file_prefix "op" end |
#format_number(num) ⇒ Object
133 134 135 |
# File 'lib/tapsoob/operation.rb', line 133 def format_number(num) num.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,") end |
#indexes_first? ⇒ Boolean
32 33 34 |
# File 'lib/tapsoob/operation.rb', line 32 def indexes_first? !!opts[:indexes_first] end |
#log ⇒ Object
60 61 62 63 |
# File 'lib/tapsoob/operation.rb', line 60 def log Tapsoob.log.level = Logger::DEBUG if opts[:debug] Tapsoob.log end |
#resuming? ⇒ Boolean
99 100 101 |
# File 'lib/tapsoob/operation.rb', line 99 def resuming? opts[:resume] == true end |
#schema? ⇒ Boolean
28 29 30 |
# File 'lib/tapsoob/operation.rb', line 28 def schema? opts[:schema] end |
#setup_signal_trap ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tapsoob/operation.rb', line 87 def setup_signal_trap trap("INT") { puts "\nCompleting current action..." @exiting = true } trap("TERM") { puts "\nCompleting current action..." @exiting = true } end |
#store_session ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/tapsoob/operation.rb', line 65 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_state ⇒ Object
111 112 113 |
# File 'lib/tapsoob/operation.rb', line 111 def stream_state opts[:stream_state] ||= {} end |
#stream_state=(val) ⇒ Object
115 116 117 |
# File 'lib/tapsoob/operation.rb', line 115 def stream_state=(val) opts[:stream_state] = val end |
#table_filter ⇒ Object
36 37 38 |
# File 'lib/tapsoob/operation.rb', line 36 def table_filter opts[:tables] || [] end |
#to_hash ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/tapsoob/operation.rb', line 73 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 |