heart_seed 0.0.1.beta2 → 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 +4 -4
- data/.gitignore +1 -0
- data/.rspec +1 -1
- data/README.md +16 -2
- data/heart_seed.gemspec +2 -2
- data/lib/heart_seed/db_seed.rb +16 -0
- data/lib/heart_seed/version.rb +1 -1
- data/spec/db/migration.rb +7 -8
- data/spec/db/setup.rb +29 -0
- data/spec/db/test_models.rb +16 -0
- data/spec/fixtures/shard_articles.yml +6 -0
- data/spec/heart_seed/db_seed_spec.rb +19 -0
- data/spec/spec_helper.rb +3 -2
- data/spec/support/models/article.rb +0 -2
- data/spec/support/models/comment.rb +0 -2
- data/spec/support/models/like.rb +0 -2
- metadata +12 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af942643f387893d5ad02467036db83ea556e363
|
4
|
+
data.tar.gz: d3064efe86bba69e05c0da9e6dcf80097012c11e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3d05c45e3888510f44e66eece44a3e5c6893cdc977cd441d0eb9a9a0af04032445ed664b2d8bbcf6585c145a5a14324f1510e4c5ac1de056d6a3d67764fb764
|
7
|
+
data.tar.gz: e276833537f6af0a5bf447865e960ec3d9c1b431098dc8955515a339b921f8497d4f5b8a04461865c410d06c8e31748f483416748c59d9fa0d74241c1150ed37
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# HeartSeed
|
2
2
|
|
3
|
-
seed util (excel
|
3
|
+
seed util (convert excel to yaml and insert yaml to db)
|
4
4
|
|
5
5
|
[](http://badge.fury.io/rb/heart_seed)
|
6
6
|
[](https://travis-ci.org/sue445/heart_seed)
|
@@ -130,6 +130,20 @@ CATALOGS=user bundle exec rake db:seed
|
|
130
130
|
# same to) TABLES=users,user_profiles bundle exec rake db:seed
|
131
131
|
```
|
132
132
|
|
133
|
+
### Shard DB
|
134
|
+
When you use shard DB, write like this to `db/seeds.rb`.
|
135
|
+
|
136
|
+
Rails example
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
SHARD_NAMES = %W(
|
140
|
+
#{Rails.env}
|
141
|
+
shard_#{Rails.env}
|
142
|
+
shard2_#{Rails.env}
|
143
|
+
)
|
144
|
+
HeartSeed::DbSeed.import_all(shard_names: SHARD_NAMES)
|
145
|
+
```
|
146
|
+
|
133
147
|
## Contributing
|
134
148
|
|
135
149
|
1. Fork it ( https://github.com/sue445/heart_seed/fork )
|
data/heart_seed.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = HeartSeed::VERSION
|
9
9
|
spec.authors = ["sue445"]
|
10
10
|
spec.email = ["sue445@sue445.net"]
|
11
|
-
spec.summary = %q{
|
12
|
-
spec.description = %q{
|
11
|
+
spec.summary = %q{seed util (convert excel to yaml and insert yaml to db) }
|
12
|
+
spec.description = %q{seed util (convert excel to yaml and insert yaml to db) }
|
13
13
|
spec.homepage = "https://github.com/sue445/heart_seed"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
data/lib/heart_seed/db_seed.rb
CHANGED
@@ -21,6 +21,7 @@ module HeartSeed
|
|
21
21
|
#
|
22
22
|
# @param seed_dir [String]
|
23
23
|
# @param tables [Array<String>,String] table names array or comma separated table names. if empty, import all seed yaml. if not empty, import only these tables.
|
24
|
+
# @param catalogs [Array<String>,String] catalogs names array or comma separated catalog names. if empty, import all seed yaml. if not empty, import only these tables in catalogs.
|
24
25
|
def self.import_all(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"])
|
25
26
|
# use tables in catalogs
|
26
27
|
target_tables = parse_arg_catalogs(catalogs)
|
@@ -46,6 +47,21 @@ module HeartSeed
|
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
50
|
+
# import all seed yaml to table with specified shards
|
51
|
+
#
|
52
|
+
# @param seed_dir [String]
|
53
|
+
# @param tables [Array<String>,String] table names array or comma separated table names. if empty, import all seed yaml. if not empty, import only these tables.
|
54
|
+
# @param catalogs [Array<String>,String] catalogs names array or comma separated catalog names. if empty, import all seed yaml. if not empty, import only these tables in catalogs.
|
55
|
+
# @param shard_names [Array<String>]
|
56
|
+
def self.import_all_with_shards(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"], shard_names: [])
|
57
|
+
shard_names.each do |shard_name|
|
58
|
+
ActiveRecord::Migration.say_with_time("import to shard: #{shard_name}") do
|
59
|
+
ActiveRecord::Base.establish_connection(shard_name.to_sym)
|
60
|
+
import_all(seed_dir: seed_dir, tables: tables, catalogs: catalogs)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
49
65
|
def self.parse_string_or_array_arg(tables)
|
50
66
|
return [] unless tables
|
51
67
|
return tables if tables.class == Array
|
data/lib/heart_seed/version.rb
CHANGED
data/spec/db/migration.rb
CHANGED
@@ -1,13 +1,5 @@
|
|
1
1
|
ActiveRecord::Schema.verbose = false
|
2
2
|
|
3
|
-
ActiveRecord::Base.configurations["test"] = {
|
4
|
-
adapter: "sqlite3",
|
5
|
-
database: ":memory:",
|
6
|
-
timeout: 500
|
7
|
-
}
|
8
|
-
|
9
|
-
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["test"])
|
10
|
-
|
11
3
|
ActiveRecord::Schema.define(version: 1) do
|
12
4
|
create_table :articles do |t|
|
13
5
|
t.string :title
|
@@ -28,4 +20,11 @@ ActiveRecord::Schema.define(version: 1) do
|
|
28
20
|
|
29
21
|
t.timestamps
|
30
22
|
end
|
23
|
+
|
24
|
+
create_table :shard_articles do |t|
|
25
|
+
t.string :title
|
26
|
+
t.text :description
|
27
|
+
|
28
|
+
t.timestamps
|
29
|
+
end
|
31
30
|
end
|
data/spec/db/setup.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
SHARD_NAMES = %w(test shard_test)
|
2
|
+
MAIN_SHARD = "test"
|
3
|
+
|
4
|
+
SHARD_NAMES.each do |shard_name|
|
5
|
+
db_file = File.join(__dir__, "#{shard_name}.sqlite3")
|
6
|
+
FileUtils.rm(db_file) if File.exists?(db_file)
|
7
|
+
|
8
|
+
ActiveRecord::Base.configurations[shard_name] = {
|
9
|
+
adapter: "sqlite3",
|
10
|
+
# database: ":memory:",
|
11
|
+
database: db_file,
|
12
|
+
timeout: 500
|
13
|
+
}
|
14
|
+
|
15
|
+
DatabaseRewinder.create_cleaner(shard_name)
|
16
|
+
ActiveRecord::Base.establish_connection(shard_name.to_sym)
|
17
|
+
|
18
|
+
load File.join(__dir__, "migration.rb")
|
19
|
+
end
|
20
|
+
|
21
|
+
ActiveRecord::Base.establish_connection(MAIN_SHARD.to_sym)
|
22
|
+
|
23
|
+
def clean_all_shards
|
24
|
+
SHARD_NAMES.each do |shard_name|
|
25
|
+
ActiveRecord::Base.establish_connection(shard_name.to_sym)
|
26
|
+
DatabaseRewinder.clean_all
|
27
|
+
end
|
28
|
+
ActiveRecord::Base.establish_connection(MAIN_SHARD.to_sym)
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Article < ActiveRecord::Base
|
2
|
+
self.establish_connection(:test)
|
3
|
+
end
|
4
|
+
|
5
|
+
class Comment < ActiveRecord::Base
|
6
|
+
self.establish_connection(:test)
|
7
|
+
end
|
8
|
+
|
9
|
+
class Like < ActiveRecord::Base
|
10
|
+
self.establish_connection(:test)
|
11
|
+
end
|
12
|
+
|
13
|
+
class ShardArticle < ActiveRecord::Base
|
14
|
+
self.establish_connection(:shard_test)
|
15
|
+
end
|
16
|
+
|
@@ -54,6 +54,25 @@ describe HeartSeed::DbSeed do
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
+
describe "#import_all_with_shards" do
|
58
|
+
subject{ HeartSeed::DbSeed.import_all_with_shards(seed_dir: seed_dir, tables: tables, catalogs: catalogs, shard_names: shard_names) }
|
59
|
+
|
60
|
+
let(:seed_dir) { FIXTURE_DIR }
|
61
|
+
let(:tables) { [] }
|
62
|
+
let(:catalogs) { [] }
|
63
|
+
let(:shard_names){ %w(test shard_test) }
|
64
|
+
|
65
|
+
after do
|
66
|
+
# FIXME can not clear if using `DatabaseRewinder.clean`
|
67
|
+
clean_all_shards
|
68
|
+
end
|
69
|
+
|
70
|
+
it{ expect{ subject }.to change(Article , :count).from(0).to(2) }
|
71
|
+
it{ expect{ subject }.to change(Comment , :count).from(0).to(2) }
|
72
|
+
it{ expect{ subject }.to change(Like , :count).from(0).to(1) }
|
73
|
+
it{ expect{ subject }.to change(ShardArticle, :count).from(0).to(1) }
|
74
|
+
end
|
75
|
+
|
57
76
|
describe "#parse_string_or_array_arg" do
|
58
77
|
subject{ HeartSeed::DbSeed.parse_string_or_array_arg(tables) }
|
59
78
|
|
data/spec/spec_helper.rb
CHANGED
@@ -29,6 +29,7 @@ require "rspec-parameterized"
|
|
29
29
|
require "rspec/temp_dir"
|
30
30
|
require "pry"
|
31
31
|
require "rake_shared_context"
|
32
|
+
require "database_rewinder"
|
32
33
|
|
33
34
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
34
35
|
# in spec/support/ and its subdirectories.
|
@@ -39,8 +40,8 @@ DATA_DIR = File.join(__dir__, "data")
|
|
39
40
|
FIXTURE_DIR = File.join(__dir__, "fixtures")
|
40
41
|
TASK_DIR = File.join(__dir__, "..", "lib", "heart_seed", "tasks")
|
41
42
|
|
42
|
-
require_relative "./db/
|
43
|
-
|
43
|
+
require_relative "./db/setup"
|
44
|
+
require_relative "./db/test_models"
|
44
45
|
|
45
46
|
RSpec.configure do |config|
|
46
47
|
# The settings below are suggested to provide a good initial experience
|
data/spec/support/models/like.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: heart_seed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sue445
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06
|
11
|
+
date: 2014-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -276,7 +276,7 @@ dependencies:
|
|
276
276
|
- - ">="
|
277
277
|
- !ruby/object:Gem::Version
|
278
278
|
version: '0'
|
279
|
-
description:
|
279
|
+
description: 'seed util (convert excel to yaml and insert yaml to db) '
|
280
280
|
email:
|
281
281
|
- sue445@sue445.net
|
282
282
|
executables: []
|
@@ -304,6 +304,8 @@ files:
|
|
304
304
|
- spec/data/articles.xls
|
305
305
|
- spec/data/articles.xlsx
|
306
306
|
- spec/db/migration.rb
|
307
|
+
- spec/db/setup.rb
|
308
|
+
- spec/db/test_models.rb
|
307
309
|
- spec/dummy/.bundle/config
|
308
310
|
- spec/dummy/.gitignore
|
309
311
|
- spec/dummy/Gemfile
|
@@ -362,6 +364,7 @@ files:
|
|
362
364
|
- spec/fixtures/articles.yml
|
363
365
|
- spec/fixtures/comments.yml
|
364
366
|
- spec/fixtures/likes.yml
|
367
|
+
- spec/fixtures/shard_articles.yml
|
365
368
|
- spec/heart_seed/converter_spec.rb
|
366
369
|
- spec/heart_seed/db_seed_spec.rb
|
367
370
|
- spec/heart_seed/tasks/heart_seed_rake_spec.rb
|
@@ -386,19 +389,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
386
389
|
version: 2.0.0
|
387
390
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
388
391
|
requirements:
|
389
|
-
- - "
|
392
|
+
- - ">="
|
390
393
|
- !ruby/object:Gem::Version
|
391
|
-
version:
|
394
|
+
version: '0'
|
392
395
|
requirements: []
|
393
396
|
rubyforge_project:
|
394
397
|
rubygems_version: 2.2.2
|
395
398
|
signing_key:
|
396
399
|
specification_version: 4
|
397
|
-
summary:
|
400
|
+
summary: seed util (convert excel to yaml and insert yaml to db)
|
398
401
|
test_files:
|
399
402
|
- spec/data/articles.xls
|
400
403
|
- spec/data/articles.xlsx
|
401
404
|
- spec/db/migration.rb
|
405
|
+
- spec/db/setup.rb
|
406
|
+
- spec/db/test_models.rb
|
402
407
|
- spec/dummy/.bundle/config
|
403
408
|
- spec/dummy/.gitignore
|
404
409
|
- spec/dummy/Gemfile
|
@@ -457,6 +462,7 @@ test_files:
|
|
457
462
|
- spec/fixtures/articles.yml
|
458
463
|
- spec/fixtures/comments.yml
|
459
464
|
- spec/fixtures/likes.yml
|
465
|
+
- spec/fixtures/shard_articles.yml
|
460
466
|
- spec/heart_seed/converter_spec.rb
|
461
467
|
- spec/heart_seed/db_seed_spec.rb
|
462
468
|
- spec/heart_seed/tasks/heart_seed_rake_spec.rb
|