loadat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in loadat.gemspec
4
+ gemspec
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ module Loadat
2
+ require 'loadat/railtie' if defined?(Rails)
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'loadat'
2
+ require 'rails'
3
+ module Loadat
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load 'tasks/loadat.rake'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Loadat
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ namespace :loadat do
2
+ desc "Load data in <directory>/*.dat into MySQL database"
3
+ task :dir, :directory, :needs => :environment do |task, args|
4
+ total_size = 0
5
+ dir = args[:directory] ? [args[:directory]] : [Rails.root, "tmp"]
6
+ files = (dir + ["*.dat"]).flatten
7
+ Dir.glob(File.join(files)) do |file|
8
+ table_name = File.basename(file, ".dat")
9
+ quoted_table_name = quote_table_name(table_name)
10
+ file_size = File.size(file)
11
+ total_size += file_size
12
+ if file_size > 0
13
+ progress("Loading #{file_size} byte data into #{table_name}")
14
+ execute("ALTER TABLE #{quoted_table_name} ENGINE = MyISAM")
15
+ execute("TRUNCATE TABLE #{quoted_table_name}")
16
+ execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS")
17
+ execute("LOAD DATA LOCAL INFILE '#{file}' INTO TABLE #{quoted_table_name}")
18
+ progress("adding index to #{quoted_table_name}")
19
+ execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS")
20
+ end
21
+ end
22
+ puts "[#{Time.now.to_s}] #{total_size} byte data loaded"
23
+ end
24
+
25
+ def progress(message)
26
+ puts "[#{Time.now.to_s}] #{message}"
27
+ end
28
+
29
+ def execute(sql)
30
+ ActiveRecord::Base.connection.execute(sql)
31
+ end
32
+
33
+ def quote_table_name(table_name)
34
+ ActiveRecord::Base.connection.quote_table_name(table_name)
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "loadat/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "loadat"
7
+ s.version = Loadat::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Joon Lee"]
10
+ s.email = ["seouri@gmail.com"]
11
+ s.homepage = "https://github.com/seouri/loadat"
12
+ s.summary = %q{Ruby on Rails Rake task for loading data in text files into MySQL database.}
13
+ s.description = %q{load_dat is a Ruby on Rails Rake task that loads data in <tablename>.dat into MySQL database that is defined in the database.yml file.}
14
+
15
+ s.rubyforge_project = "loadat"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: loadat
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Joon Lee
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-07 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: load_dat is a Ruby on Rails Rake task that loads data in <tablename>.dat into MySQL database that is defined in the database.yml file.
22
+ email:
23
+ - seouri@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - lib/loadat.rb
35
+ - lib/loadat/railtie.rb
36
+ - lib/loadat/version.rb
37
+ - lib/tasks/loadat.rake
38
+ - loadat.gemspec
39
+ has_rdoc: true
40
+ homepage: https://github.com/seouri/loadat
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project: loadat
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Ruby on Rails Rake task for loading data in text files into MySQL database.
71
+ test_files: []
72
+