seed_ext 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3d54c24cb98da30d29eb987977d7087da23a4f8
4
+ data.tar.gz: 586f68c47a4373128d12d101c180834712a39b1c
5
+ SHA512:
6
+ metadata.gz: 7390116aa149317a360a2b2bb2d0273a221a56661fc59af564dc0fbb46e7a6819412afad23c49d22c64c6b6bdf6b9407fa95e1fe2474d926f63e0b115544282c
7
+ data.tar.gz: c9d39add8fbd6cc75960447e7a8721c51303e04667261f58cb5b407e5c14e3c9e4279c723cd702a45029fd87b97a93848bb0958a99bb5f1a949cfa2f2234a94e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ #gem 'rails', "~> 4.0.0.beta1"
4
+ #gem 'sqlite3'
5
+ #gem 'mysql2'
6
+ gem 'pg'
7
+ # Specify your gem's dependencies in seed_ext.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 RAWHIDE. Co., Ltd.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # SeedExt
2
+
3
+ SeedExt can truncate records and seed data from csv/yml file.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'seed_ext', github: rawhide/seed_ext
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ ### In db/seeds/users.yml
18
+
19
+ user1:
20
+ id: 1
21
+ name: user1
22
+ sex: 0
23
+
24
+ user2:
25
+ id: 2
26
+ name: user2
27
+ sex: 1
28
+
29
+ ### In db/migrate/xxxx_create_users.rb
30
+ class CreateUsers < ActiveRecord::Migration
31
+ def change
32
+ create_table :users do |t|
33
+ t.string :name
34
+ t.integer :sex
35
+ t.timestamps
36
+ end
37
+ end
38
+ end
39
+
40
+ ### In app/models/user.rb
41
+
42
+ class User < ActiveRecord::Base
43
+ end
44
+
45
+ ### In db/seeds.rb
46
+
47
+ User.truncation
48
+
49
+ ### To load data:
50
+
51
+ rake db:create
52
+ rake db:migrate
53
+ rake db:seed
54
+
55
+ Users table will be truncated and be loaded data from yml file.
56
+
57
+ ## To load from CSV file
58
+
59
+ ### In db/seeds/users.csv
60
+
61
+ id,name,sex
62
+ 1,user1,0
63
+ 2,user2,1
64
+
65
+ ### In db/seeds.rb
66
+
67
+ User.truncation(:csv)
68
+
69
+ ## Customizing
70
+
71
+ ### In db/fixures/users.csv
72
+
73
+ id,name,sex
74
+ 1,user1,0
75
+ 2,user2,1
76
+
77
+ ### In db/seeds.rb
78
+
79
+ User.truncation(:csv, 'db/fixures')
80
+
81
+ ### You can just truncate
82
+
83
+ User.truncation!
84
+
85
+ Users table will be truncated.(supporting for sqlite3/mysql2/postgresql/sqlserver)
86
+
87
+ ## Contributing
88
+
89
+ 1. Fork it
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/seed_ext.rb ADDED
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ require 'active_record'
3
+ require 'csv'
4
+ module Railstar
5
+ module ActiveRecordExt
6
+ module ClassMethods
7
+ def truncation(sym=:yml, file_dir='db/seeds')
8
+ table_name = self.table_name || self.to_s.underscore.pluralize
9
+ file_name = "#{table_name}.#{sym.to_s}"
10
+ file_path = File.join(file_dir, file_name)
11
+ raise "#{file_path} file not found." unless File.exist?(file_path)
12
+ self.transaction do
13
+ self.truncation!
14
+ self.send("create_from_#{sym.to_s}", file_path)
15
+ end
16
+ end
17
+
18
+ def truncation!
19
+ case self.connection.adapter_name
20
+ when "SQLite"
21
+ self.connection.execute("DELETE FROM `#{self.table_name}`")
22
+ else
23
+ self.connection.execute("TRUNCATE TABLE #{self.table_name}")
24
+ end
25
+ end
26
+
27
+ def create_from_yml(file_path)
28
+ YAML.load_file(file_path).each do |value|
29
+ self.create value.is_a?(Array) ? value.last : value
30
+ end
31
+ end
32
+
33
+ def create_from_csv(file_path)
34
+ CSV.foreach(file_path, :headers => true) {|row| self.create Hash[*row.to_a.flatten] }
35
+ end
36
+
37
+ end
38
+
39
+ module InstanceMethods
40
+ end
41
+
42
+ def self.included(base)
43
+ base.extend ClassMethods
44
+ base.class_eval do
45
+ include InstanceMethods
46
+ end
47
+ end
48
+ end
49
+ end
50
+ ActiveRecord::Base.send(:include, Railstar::ActiveRecordExt)
@@ -0,0 +1,3 @@
1
+ module SeedExt
2
+ VERSION = "0.0.1"
3
+ end
data/seed_ext.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'seed_ext/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "seed_ext"
8
+ spec.version = SeedExt::VERSION
9
+ spec.authors = ["RAWHIDE. Co., Ltd."]
10
+ spec.email = ["info@raw-hide.co.jp"]
11
+ spec.description = %q{extract active_record to truncate and seed}
12
+ spec.summary = %q{extract active_record to truncate and seed}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "activerecord", "~> 4.0.0.beta1"
25
+ end
data/spec/.gitignore ADDED
@@ -0,0 +1 @@
1
+ test.sqlite3
data/spec/README ADDED
@@ -0,0 +1,22 @@
1
+ To run the specs:
2
+
3
+ gem install bundler # If not already installed
4
+ bundle install # Install the dependencies
5
+ rspec spec/ # Run the specs
6
+
7
+ By default an sqlite3 database is used.
8
+
9
+ To test others:
10
+ mysql2
11
+ switch `gem 'mysql2'` # In Gemfile
12
+ bundle install
13
+ DB=mysql2 rspec spec/
14
+
15
+ postgresql
16
+ switch `gem 'pg'` # In Gemfile
17
+ bundle install
18
+ DB=postgresql rspec spec/
19
+
20
+ In each of these cases, you'll need to create a database named "seed_ext_test".
21
+
22
+ The connection paramaters for each of these are specified in spec/connections/, which you can edit if necessary (for example to change the username/password).
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => "mysql2",
3
+ :database => "seed_ext_test",
4
+ :username => "root"
5
+ )
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => "mysql2",
3
+ :database => "seed_ext_test",
4
+ :username => "root"
5
+ )
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => "postgresql",
3
+ :database => "seed_ext_test",
4
+ :username => "postgres"
5
+ )
@@ -0,0 +1,4 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => "sqlite3",
3
+ :database => File.dirname(__FILE__) + "/test.sqlite3"
4
+ )
@@ -0,0 +1,3 @@
1
+ id,name,sex
2
+ 1,担当者1,1
3
+ 2,担当者2,0
@@ -0,0 +1,9 @@
1
+ user3:
2
+ id: 3
3
+ name: 担当者3
4
+ sex: 0
5
+
6
+ user4:
7
+ id: 4
8
+ name: 担当者4
9
+ sex: 1
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'SeedExt' do
4
+
5
+ context "when truncation!" do
6
+ before { User.truncation! }
7
+ it "record's count should == 0" do
8
+ User.count.should == 0
9
+ end
10
+ end
11
+
12
+ describe "truncation" do
13
+ context "when load from csv" do
14
+ before { User.truncation(:csv, "spec/db/seeds") }
15
+ it "should seed data" do
16
+ User.count.should == 2
17
+ User.find(1).name == '$BC4Ev<T(B1'
18
+ User.find(2).sex == 0
19
+ end
20
+ end
21
+ context "when load from yml" do
22
+ before { User.truncation(:yml, "spec/db/seeds") }
23
+ it "should seed data" do
24
+ User.count.should == 2
25
+ User.find(3).name == '$BC4Ev<T(B3'
26
+ User.find(4).sex == 1
27
+ end
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,18 @@
1
+ require 'rake'
2
+ require './lib/seed_ext'
3
+
4
+ ENV["DB"] ||= 'mysql2'
5
+ puts "Using #{ENV["DB"]} to run the tests."
6
+ require File.dirname(__FILE__) + "/connections/#{ENV["DB"]}.rb"
7
+
8
+ ActiveRecord::Schema.define :version => 0 do
9
+ create_table :users, :force => true do |t|
10
+ t.column :name, :string
11
+ t.column :sex, :integer
12
+ t.timestamp
13
+ end
14
+ end
15
+
16
+ class User < ActiveRecord::Base
17
+ end
18
+
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seed_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - RAWHIDE. Co., Ltd.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0.beta1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0.beta1
69
+ description: extract active_record to truncate and seed
70
+ email:
71
+ - info@raw-hide.co.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/seed_ext.rb
82
+ - lib/seed_ext/version.rb
83
+ - seed_ext.gemspec
84
+ - spec/.gitignore
85
+ - spec/README
86
+ - spec/connections/mysql
87
+ - spec/connections/mysql2.rb
88
+ - spec/connections/postgresql.rb
89
+ - spec/connections/sqlite3.rb
90
+ - spec/db/seeds/users.csv
91
+ - spec/db/seeds/users.yml
92
+ - spec/seed_ext_spec.rb
93
+ - spec/spec_helper.rb
94
+ homepage: ''
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.0
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: extract active_record to truncate and seed
118
+ test_files:
119
+ - spec/.gitignore
120
+ - spec/README
121
+ - spec/connections/mysql
122
+ - spec/connections/mysql2.rb
123
+ - spec/connections/postgresql.rb
124
+ - spec/connections/sqlite3.rb
125
+ - spec/db/seeds/users.csv
126
+ - spec/db/seeds/users.yml
127
+ - spec/seed_ext_spec.rb
128
+ - spec/spec_helper.rb