Class: DataLoader::Migrator
- Inherits:
-
Object
- Object
- DataLoader::Migrator
- Defined in:
- lib/data_loader/migrator.rb
Class Method Summary collapse
-
.create_schema(table_name, data_struct) ⇒ Object
takes a column,type data structre and makes a table.
-
.derive_table_name(file) ⇒ Object
a pretty table name.
-
.load_data(file, table_name, separator = ',') ⇒ Object
uses MySQL LOAD DATA to import the whole file, ignoring the header line.
- .migrate(file, columns, table, separator = ',', conn = :root) ⇒ Object
-
.with_connection(conn = :root) ⇒ Object
runs a block under a different connection from database.yml.
Class Method Details
.create_schema(table_name, data_struct) ⇒ Object
takes a column,type data structre and makes a table
13 14 15 16 17 18 19 20 21 |
# File 'lib/data_loader/migrator.rb', line 13 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
46 47 48 49 |
# File 'lib/data_loader/migrator.rb', line 46 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, separator = ',') ⇒ Object
uses MySQL LOAD DATA to import the whole file, ignoring the header line
24 25 26 27 28 29 30 31 32 |
# File 'lib/data_loader/migrator.rb', line 24 def self.load_data(file, table_name, separator = ',') sql = <<-SQL LOAD DATA LOCAL INFILE '#{file}' INTO TABLE #{table_name.to_s} FIELDS TERMINATED BY '#{separator}' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES; SQL ActiveRecord::Base.connection.execute(sql) end |
.migrate(file, columns, table, separator = ',', conn = :root) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/data_loader/migrator.rb', line 4 def self.migrate(file, columns, table, separator = ',', conn = :root) with_connection(conn) do create_schema(table, columns) puts "-- load_data('#{File.basename(file)}', :#{table.to_s})" load_data(file, table, separator) end end |
.with_connection(conn = :root) ⇒ Object
runs a block under a different connection from database.yml
35 36 37 38 39 40 41 42 43 |
# File 'lib/data_loader/migrator.rb', line 35 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 |