Class: Dbtools::Import

Inherits:
Thor
  • Object
show all
Defined in:
lib/tasks/import.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Import

Returns a new instance of Import.



17
18
19
20
# File 'lib/tasks/import.rb', line 17

def initialize(*args)
  super
  load_config
end

Instance Method Details

#convert_to_utf8(filename) ⇒ Object



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
# File 'lib/tasks/import.rb', line 38

def convert_to_utf8(filename)
  puts "Converting #{filename} to utf8"

  #filename_utf8_encoded = "#{filename}_utf8"
  #system "iconv -f utf-8 -t utf-8 -c < #{filename} > #{filename_utf8_encoded}"

  filename_tmp = filename + '.tmp'
  filename_old = filename + '.old'

  begin
    File.open(filename_tmp, 'w') do |file_out|
      File.foreach(filename) do |line_input|
        file_out.puts(line_input.encode!('UTF-8', 'UTF-8', :invalid => :replace))
      end
    end
    # Swap filenames
    File.rename(filename, filename_old)
    File.rename(filename_tmp, filename)
    File.rename(filename_old, filename_tmp)
  rescue Exception => e
    raise "Failed to convert encoding of file #{filename} to UTF-8 because #{e.message}. "
  ensure 
    File.delete(filename_tmp)
  end
end

#create_schema(csv_file, table_name) ⇒ Object



65
66
67
68
# File 'lib/tasks/import.rb', line 65

def create_schema(csv_file, table_name)
  csv = Dbtools::Converter::Csv_importer.new(csv_file, tablename: table_name)
  puts csv.to_sql_schema_script
end

#csv_in_postgres(csv_file, database_name, table_name) ⇒ Object



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
# File 'lib/tasks/import.rb', line 71

def csv_in_postgres(csv_file, database_name, table_name)
  # Converts file to utf8
  convert_to_utf8(csv_file)
  # Sanitize table name
  table_name = table_name.downcase.gsub(/[^0-9a-zA-Z_]/,'_')

  # Creates a table schema based on the csv.
  csv = Dbtools::Converter::Csv_importer.new(csv_file, tablename: table_name)
  csv_schema_command = csv.to_sql_schema_script
  #copy_csv_command = "\\COPY \"#{table_name}\" FROM '#{csv_file}' DELIMITER '#{csv.delimiter}' CSV HEADER;"

  #system "createdb #{@postgres_connection_options} #{database_name}"

  puts "Importing #{csv_file} into #{database_name}.#{table_name}"
  # Create the table and copy the csv contents into the table. 
  begin
    psql = Dbtools::Database::PostgresqlConnection.new("#{@postgres_connection_url}")
    psql.create_database(database_name)
    psql.close 
  # Create the database

    psql = Dbtools::Database::PostgresqlConnection.new("#{@postgres_connection_url}#{database_name}")
    psql.execute_query(csv_schema_command)
    psql.copy_from_file(csv_file, table_name, csv.delimiter)
    psql.execute_files(Dbtools::Constants::PLSQL_FUNCTIONS_DIR)
  ensure
    psql.close
  end
  return "#{@postgres_connection_url}#{database_name}"
end

#excel(database_name, file) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tasks/import.rb', line 103

def excel(database_name, file)
  folder = File.join("/tmp", database_name)
  # Convert spreadsheets to Excel. 
  files = excel2csv(file, folder)
  result = ''
  files.each do |sheet_name, csv_file|
    begin
      # Load every file in Postgres
      table_name = "#{File.basename(csv_file, File.extname(csv_file))}_#{sheet_name}"
      result = csv_in_postgres(csv_file, database_name, table_name)
    rescue Exception => e
      puts "Could not load #{csv_file} into #{database_name} because #{e.message}. "
    end
  end
  FileUtils.remove_entry_secure(folder)
  return result
end

#excel2csv(file, target_dir) ⇒ Object



122
123
124
125
126
# File 'lib/tasks/import.rb', line 122

def excel2csv(file, target_dir)
  converter = Dbtools::Converter::Excel2csv_converter.new(file)
  files = converter.output(target_dir)
  return files
end

#mysql_dump(database_name, file) ⇒ Object



31
32
33
34
35
# File 'lib/tasks/import.rb', line 31

def mysql_dump(database_name, file)
  system "#{@mysql_connection_command} -e 'CREATE DATABASE #{database_name}'"
  system "#{@mysql_connection_command} < #{file}"
  return "#{@mysql_connection_url}#{database_name}"
end

#postgres_dump(database_name, file) ⇒ Object



24
25
26
27
28
# File 'lib/tasks/import.rb', line 24

def postgres_dump(database_name, file)
  system "createdb #{@postgres_connection_options} #{database_name}"
  system "#{@postgres_connection_command} --dbname=#{database_name} --file=#{file}"
  return "#{@postgres_connection_url}#{database_name}"
end