csv_db 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +5 -0
- data/README.rdoc +21 -0
- data/Rakefile +15 -0
- data/csv_db.gemspec +31 -0
- data/lib/csv_db.rb +9 -0
- data/lib/tasks/csv_db.rake +39 -0
- data.tar.gz.sig +0 -0
- metadata +87 -0
- metadata.gz.sig +0 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= csv_db
|
2
|
+
is a gem for rails apps that adds to rake db tasks. The command rake db:csv_to_db pushes csv content to a db table
|
3
|
+
and the command rake db:db_to_csv pulls a db table into a csv
|
4
|
+
|
5
|
+
== Install
|
6
|
+
gem install csv_db
|
7
|
+
|
8
|
+
== Usage
|
9
|
+
If you have a table you want to backup into a csv in the rails db with a model: Model
|
10
|
+
|
11
|
+
just call: rake db:csv_to_db model=Model
|
12
|
+
|
13
|
+
the call writes into a csv file Model.csv located in the app root directory
|
14
|
+
|
15
|
+
If you have a data you want to push from a csv in the rails db with a model: Model
|
16
|
+
|
17
|
+
just call: rake db:db_to_csv csv=Model
|
18
|
+
|
19
|
+
the call reads from a csv file Model.csv located in the app root directory
|
20
|
+
|
21
|
+
remember to place files in the right place. also primary keys cannot be written.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('csv_db', '0.0.4') do |p|
|
6
|
+
p.description = "Backup a db table to a csv or push csv content to a db table"
|
7
|
+
p.url = "http://github.com/phongsi/csv_db"
|
8
|
+
p.author = "Phong Si"
|
9
|
+
p.email = "phong.si@gmail.com"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
p.require_signed = true
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/csv_db.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "csv_db"
|
5
|
+
s.version = "0.0.4"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Phong Si"]
|
9
|
+
s.cert_chain = ["/media/share/gem-public_cert.pem"]
|
10
|
+
s.date = "2011-10-07"
|
11
|
+
s.description = "Backup a db table to a csv or push csv content to a db table"
|
12
|
+
s.email = "phong.si@gmail.com"
|
13
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/csv_db.rb", "lib/tasks/csv_db.rake"]
|
14
|
+
s.files = ["Manifest", "README.rdoc", "Rakefile", "lib/csv_db.rb", "lib/tasks/csv_db.rake", "csv_db.gemspec"]
|
15
|
+
s.homepage = "http://github.com/phongsi/csv_db"
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Csv_db", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = "csv_db"
|
19
|
+
s.rubygems_version = "1.8.11"
|
20
|
+
s.signing_key = "/media/share/gem-private_key.pem"
|
21
|
+
s.summary = "Backup a db table to a csv or push csv content to a db table"
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
s.specification_version = 3
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
27
|
+
else
|
28
|
+
end
|
29
|
+
else
|
30
|
+
end
|
31
|
+
end
|
data/lib/csv_db.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
namespace :db do
|
4
|
+
task :db_to_csv => :environment do
|
5
|
+
model_dir = Dir['**/models/**/*.rb'].detect {|f| ENV['model'] == File.basename(f, '.*').camelize}
|
6
|
+
if !model_dir.eql?(nil)
|
7
|
+
table = File.basename(model_dir, '.*').camelize.constantize
|
8
|
+
objects = table.all
|
9
|
+
CSV.open("#{table}.csv", "wb") do |csv|
|
10
|
+
csv << table.column_names
|
11
|
+
row = Array.new
|
12
|
+
objects.each do |obj|
|
13
|
+
table.column_names.each do |col|
|
14
|
+
row << obj[col]
|
15
|
+
end
|
16
|
+
csv << row
|
17
|
+
row.clear
|
18
|
+
end
|
19
|
+
end
|
20
|
+
else
|
21
|
+
puts "Table #{ENV['model']} could not be found"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
task :csv_to_db => :environment do
|
25
|
+
model = ENV['csv'].constantize
|
26
|
+
columns = Array.new
|
27
|
+
CSV.foreach("#{ENV['csv']}.csv", :headers => :first_row, :return_headers => true) do |row|
|
28
|
+
if row.header_row?()
|
29
|
+
columns = row
|
30
|
+
else
|
31
|
+
temp = model.new
|
32
|
+
(0...row.size).each do |col|
|
33
|
+
temp[columns[col]] = row[col]
|
34
|
+
end
|
35
|
+
temp.save!
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_db
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.4
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Phong Si
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain:
|
12
|
+
- |
|
13
|
+
-----BEGIN CERTIFICATE-----
|
14
|
+
MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MREwDwYDVQQDDAhwaG9u
|
15
|
+
Zy5zaTEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPyLGQBGRYDY29t
|
16
|
+
MB4XDTExMTAwNDA1NDgwOFoXDTEyMTAwMzA1NDgwOFowPzERMA8GA1UEAwwIcGhv
|
17
|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33
|
+
|
34
|
+
date: 2011-10-07 00:00:00 Z
|
35
|
+
dependencies: []
|
36
|
+
|
37
|
+
description: Backup a db table to a csv or push csv content to a db table
|
38
|
+
email: phong.si@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- README.rdoc
|
45
|
+
- lib/csv_db.rb
|
46
|
+
- lib/tasks/csv_db.rake
|
47
|
+
files:
|
48
|
+
- Manifest
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- lib/csv_db.rb
|
52
|
+
- lib/tasks/csv_db.rake
|
53
|
+
- csv_db.gemspec
|
54
|
+
homepage: http://github.com/phongsi/csv_db
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --line-numbers
|
60
|
+
- --inline-source
|
61
|
+
- --title
|
62
|
+
- Csv_db
|
63
|
+
- --main
|
64
|
+
- README.rdoc
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "1.2"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project: csv_db
|
82
|
+
rubygems_version: 1.8.11
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Backup a db table to a csv or push csv content to a db table
|
86
|
+
test_files: []
|
87
|
+
|
metadata.gz.sig
ADDED
Binary file
|