Class: DataLoader::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/data_loader/migrator.rb

Class Method Summary collapse

Class Method Details

.create_schema(table_name, data_struct) ⇒ Object

takes a column,type data structre and makes a table



14
15
16
17
18
19
20
21
22
# File 'lib/data_loader/migrator.rb', line 14

def self.create_schema(table_name, data_struct)
  ActiveRecord::Schema.define do
    create_table table_name, :force => true, :id => false do |t|
      data_struct.each do |column|
        t.column(column[:name], column[:type])
      end
    end
  end
end

.derive_table_name(file) ⇒ Object

a pretty table name



61
62
63
64
# File 'lib/data_loader/migrator.rb', line 61

def self.derive_table_name(file)
  name = File.basename(file, File.extname(file))  # just file
  name.underscore.sub(/[0-9_]*$/, '')      # remove trailing numbers
end

.load_data(file, table_name, local, separator = ',', row_sep = "\r\n") ⇒ Object

uses MySQL LOAD DATA to import the whole file, ignoring the header line



38
39
40
41
42
43
44
45
46
47
# File 'lib/data_loader/migrator.rb', line 38

def self.load_data(file, table_name, local, separator = ',', row_sep = "\r\n")
  local_txt = local ? "LOCAL" : ''
  sql = "    LOAD DATA \#{local_txt} INFILE '\#{file}' INTO TABLE \#{table_name.to_s}\n      FIELDS TERMINATED BY '\#{separator}' ENCLOSED BY '\"'\n      LINES TERMINATED BY '\#{row_sep}'\n      IGNORE 1 LINES;\n  SQL\n  ActiveRecord::Base.connection.execute(sql)\nend\n"

.migrate(file, columns, table, separator = ',', conn = :root, local = false, row_sep = "\r\n") ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/data_loader/migrator.rb', line 4

def self.migrate(file, columns, table, separator = ',', conn = :root, local = false, row_sep = "\r\n")
  with_connection(conn) do
    create_schema(table, columns)
    puts "-- load_data('#{File.basename(file)}', :#{table.to_s})"
    load_data(file, table, local, separator, row_sep)
    nullify_dates(table, columns)
  end
end

.nullify_dates(table_name, data_struct) ⇒ Object

empty strings import as 0000-00-00 00:00:00, convert to nil



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/data_loader/migrator.rb', line 25

def self.nullify_dates(table_name, data_struct)
  date_columns = data_struct.map {|column| column[:name] if column[:type] == :datetime }.compact!
  date_columns.each do |column|
    sql = "      UPDATE \#{table_name}\n      SET \#{column} = NULL\n      WHERE \#{column} = 0\n    SQL\n    ActiveRecord::Base.connection.execute(sql)\n  end\nend\n"

.with_connection(conn = :root) ⇒ Object

runs a block under a different connection from database.yml



50
51
52
53
54
55
56
57
58
# File 'lib/data_loader/migrator.rb', line 50

def self.with_connection(conn = :root)
  if Rails.env.development?
    yield
  else
    ActiveRecord::Base.establish_connection(conn)
    yield
    ActiveRecord::Base.establish_connection(RAILS_ENV)
  end
end