Class: JunglePath::DBAccess::IO::Copy

Inherits:
Object
  • Object
show all
Includes:
InitDB
Defined in:
lib/jungle_path/db_access/io/copy.rb

Instance Method Summary collapse

Methods included from InitDB

#handle_json_columns, #initialize

Instance Method Details

#from_table(model_class) ⇒ Object



12
13
14
# File 'lib/jungle_path/db_access/io/copy.rb', line 12

def from_table model_class
	nil
end

#into_table(model_class, data_file) ⇒ Object



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
# File 'lib/jungle_path/db_access/io/copy.rb', line 16

def into_table model_class, data_file
	done = false
	loops = 0
	until done
		loops +=1
		puts "loops: #{loops}."
		error = nil
		file_reader = JunglePath::DBAccess::IO::ChunkedFileReader.new data_file unless file_reader # must work with chunks of lines since we won't know which exact line was bad if there is a postgresql error.
		begin
			@db.copy_into(model_class.table_name, :data => file_reader)
		rescue Sequel::ForeignKeyConstraintViolation => e
			error = e
		rescue Sequel::UniqueConstraintViolation => e
			error = e
		rescue Sequel::DatabaseError => e
			error = e
		rescue Exception => e
			error = e
		end
		if error
			if file_reader.chunk_size == 1
				log_error "bad data: exception: #{error.class}."
				log_error "  message: #{error.message.gsub("\n", '\n').gsub("\r", '\r')}."
				log_error "  was_bad_chunk: start_at: #{file_reader.start_at}, chunk_size: #{file_reader.chunk_size}."
				line = file_reader.line
				log_error "  line: #{line.gsub("\n", '\n').gsub("\r", '\r')}."
				parts = line.split("\t")
				parts.each_with_index do |part, index|
					log_error "    #{index}: part: #{part}."
				end
			end
			file_reader.was_bad_chunk
		else
			puts "was_good_chunk: start_at: #{file_reader.start_at}, chunk_size: #{file_reader.chunk_size}."
			file_reader.was_good_chunk
		end
		done = file_reader.done
		if done
			log "done with #{model_class.table_name}."
		end
	end
end

#log(msg) ⇒ Object



65
66
67
68
69
# File 'lib/jungle_path/db_access/io/copy.rb', line 65

def log(msg)
	#binding.pry
	puts "log info: #{msg}"
	@logger.info(msg) if @logger
end

#log_error(msg) ⇒ Object



59
60
61
62
63
# File 'lib/jungle_path/db_access/io/copy.rb', line 59

def log_error(msg)
	#binding.pry
	puts "log error: #{msg}"
	@logger.error(msg)
end