partial_ks 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGES.md +21 -0
- data/CONTRIBUTING.md +2 -2
- data/README.md +28 -17
- data/lib/partial_ks.rb +1 -1
- data/lib/partial_ks/filtered_table.rb +7 -11
- data/lib/partial_ks/runner.rb +1 -1
- data/lib/partial_ks/{kitchen_sync.rb → sync.rb} +1 -1
- data/lib/partial_ks/version.rb +1 -1
- data/partial_ks.gemspec +1 -1
- data/test/filtered_table_test.rb +12 -10
- data/test/runner_test.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fbc1cd0a2fa2a07636eb8343189227655545d592546ae4271f2c7a2525fefd93
|
4
|
+
data.tar.gz: f60d0fd9bcf8a9409e53cb272aa70dcf1f3009aafaa3513e3bf6e82ceae15b61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d667eab2216765f872360fb9a11748a2e334192e9f28558bca8b81547843dbe75c57503be7ca03faa23b7ce16d075dbc01ba5832339c9eb099f5de2434f895b9
|
7
|
+
data.tar.gz: 1086325707f98099a5fc815d029e63cf91eba347c9702befe0e69ea05ea9f61fb1c0682c27c37ffb28036d9aa4b3ccdfddb21bc2a889e1c33a20228999845f43
|
data/CHANGES.md
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
# 0.7.0
|
4
|
+
|
5
|
+
* Removed Kitchen Sync specific behaviour - now yields full SQL statements (and no longer kitchen sync specific filter format)
|
6
|
+
|
7
|
+
To maintain previous behaviour, use the folllowing code snippet:
|
8
|
+
|
9
|
+
```
|
10
|
+
PartialKs::Sync.new(manual_configuration).run! do |tables_to_filter, tables|
|
11
|
+
kitchen_sync_filter = {}
|
12
|
+
tables_to_filter.each do |table_name, filter|
|
13
|
+
kitchen_sync_filter[table_name] = { "only" => filter }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
* Rename main class to PartialKs::Sync
|
19
|
+
|
20
|
+
* Remove FilteredTable#where_fragment
|
21
|
+
|
22
|
+
* Introduces FilteredTable#to_sql
|
23
|
+
|
3
24
|
# 0.6.0
|
4
25
|
|
5
26
|
Propogate filters down the tree. If the parent downloads the whole table, do the same for the child.
|
data/CONTRIBUTING.md
CHANGED
@@ -2,7 +2,7 @@ This document should tell all you need to know for contributing to this project.
|
|
2
2
|
|
3
3
|
# Testing
|
4
4
|
|
5
|
-
Tested with activerecord 4.2.10, 5.0.
|
5
|
+
Tested with activerecord 4.2.10, 5.0.7, 5.1.6, 5.2.0 (on Ruby 2.3.5)
|
6
6
|
|
7
7
|
|
8
8
|
To install and run tests :
|
@@ -26,4 +26,4 @@ Filters are run on the source DB.
|
|
26
26
|
|
27
27
|
## Dependencies
|
28
28
|
|
29
|
-
|
29
|
+
Sync -> (ModelList -> Table) -> Runner -> (FilteredTable -> Table)
|
data/README.md
CHANGED
@@ -1,42 +1,53 @@
|
|
1
1
|
# PartialKs
|
2
2
|
|
3
|
-
A library to
|
3
|
+
A library to sync a subset of your database
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
So how does it work ?
|
5
|
+
## Usage
|
8
6
|
|
7
|
+
See the following example:
|
9
8
|
|
10
9
|
```
|
11
|
-
|
10
|
+
configuration = [
|
11
|
+
[User, nil, -> { User.where(:id => [1]) }], # specify a subset of users. as users have no "parent", specify `nil`
|
12
|
+
[BlogPost, User] # filter blog_posts by User
|
13
|
+
]
|
14
|
+
|
15
|
+
PartialKs::Sync.new(configuration).run! do |tables_to_filter, tables|
|
12
16
|
puts tables_to_filter.inspect
|
13
17
|
puts tables.inspect
|
14
18
|
end
|
15
19
|
```
|
16
20
|
|
17
|
-
|
21
|
+
## So how does it work ?
|
22
|
+
|
23
|
+
The idea is that PartialKS builds a tree of models and uses
|
24
|
+
the parent of a model to sync a subset of the database records for
|
25
|
+
the model. E.g.
|
18
26
|
|
19
27
|
```
|
20
|
-
|
21
|
-
|
22
|
-
[BlogPost, User] # filter blog_posts by User
|
23
|
-
]
|
28
|
+
User id 1, 3, 5, 7
|
29
|
+
User -> BlogPost
|
24
30
|
```
|
25
31
|
|
26
|
-
|
32
|
+
PartialKS will only download blog_posts records for users 1, 3, 5, and 7.
|
27
33
|
|
28
|
-
|
34
|
+
Most of the time PartialKS will be able to deduce the parent automatically.
|
29
35
|
|
30
|
-
|
36
|
+
If a model has no "parent", PartialKS will download the whole table.
|
31
37
|
|
32
|
-
|
38
|
+
If a model has "multiple parents", then PartialKS will not choose, and hence
|
39
|
+
will download the whole table. If it is not a problem to download the whole
|
40
|
+
table, then you can address the warning at your leisure.
|
33
41
|
|
42
|
+
## Public API
|
34
43
|
|
35
|
-
|
44
|
+
It currently consists of :
|
45
|
+
|
46
|
+
- PartialKs::Sync
|
47
|
+
|
48
|
+
## Not supported
|
36
49
|
|
37
50
|
Things that are not supported in this version.
|
38
51
|
|
39
52
|
* Polymorphic relationships
|
40
53
|
* Tables with STI
|
41
|
-
|
42
|
-
|
data/lib/partial_ks.rb
CHANGED
@@ -9,13 +9,13 @@ module PartialKs
|
|
9
9
|
@custom_filter_relation = custom_filter_relation
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
12
|
+
def to_sql
|
13
13
|
if custom_filter_relation
|
14
|
-
|
15
|
-
elsif parent && parent.
|
14
|
+
filter_based_on_custom_filter_relation
|
15
|
+
elsif parent && parent.to_sql.nil?
|
16
16
|
nil
|
17
17
|
elsif parent
|
18
|
-
|
18
|
+
filter_based_on_parent_model(parent.table.model)
|
19
19
|
else
|
20
20
|
nil
|
21
21
|
end
|
@@ -23,21 +23,17 @@ module PartialKs
|
|
23
23
|
|
24
24
|
protected
|
25
25
|
def filter_based_on_parent_model(parent_model)
|
26
|
-
table.relation_for_associated_model(parent_model).
|
26
|
+
table.relation_for_associated_model(parent_model).to_sql.to_s
|
27
27
|
end
|
28
28
|
|
29
29
|
def filter_based_on_custom_filter_relation
|
30
30
|
relation = custom_filter_relation.respond_to?(:call) ? custom_filter_relation.call : custom_filter_relation
|
31
31
|
|
32
32
|
if relation.is_a?(ActiveRecord::Relation) || relation.respond_to?(:where_sql)
|
33
|
-
relation.to_sql.to_s
|
33
|
+
relation.to_sql.to_s
|
34
34
|
elsif relation.is_a?(String)
|
35
|
-
relation
|
35
|
+
relation
|
36
36
|
end
|
37
37
|
end
|
38
|
-
|
39
|
-
def where_regexp
|
40
|
-
/\A.*WHERE\s*/i
|
41
|
-
end
|
42
38
|
end
|
43
39
|
end
|
data/lib/partial_ks/runner.rb
CHANGED
data/lib/partial_ks/version.rb
CHANGED
data/partial_ks.gemspec
CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
|
|
5
5
|
gem.version = PartialKs::VERSION
|
6
6
|
gem.summary = "Partial KS"
|
7
7
|
gem.description = <<-EOF
|
8
|
-
A library to
|
8
|
+
A library to sync a subset of your database
|
9
9
|
EOF
|
10
10
|
gem.has_rdoc = false
|
11
11
|
gem.author = "Thong Kuah"
|
data/test/filtered_table_test.rb
CHANGED
@@ -1,60 +1,62 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "filtered table" do
|
4
4
|
let(:model) { PostTag }
|
5
5
|
let(:parent_model) { BlogPost }
|
6
6
|
|
7
7
|
it "proxies to Table if there's parent only" do
|
8
8
|
table_parent_relation_method = :relation_for_associated_model
|
9
9
|
relation_mock = Minitest::Mock.new
|
10
|
-
|
10
|
+
statement = "SELECT * FROM #{model.table_name} WHERE tag_id IN (0)"
|
11
|
+
relation_mock.expect :to_sql, statement
|
12
|
+
relation_mock.expect :to_sql, statement
|
11
13
|
parent = PartialKs::FilteredTable.new(parent, nil, custom_filter_relation: BlogPost.where("1=0"))
|
12
14
|
|
13
15
|
filtered_table = PartialKs::FilteredTable.new(model, parent)
|
14
16
|
filtered_table.table.stub table_parent_relation_method, relation_mock do
|
15
|
-
filtered_table.
|
17
|
+
filtered_table.to_sql.must_equal(statement)
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
21
|
it "short-circuits evaluation if the parent has no filter" do
|
20
22
|
parent = PartialKs::FilteredTable.new(parent, nil)
|
21
23
|
filtered_table = PartialKs::FilteredTable.new(model, parent)
|
22
|
-
filtered_table.
|
24
|
+
filtered_table.to_sql.must_be_nil
|
23
25
|
end
|
24
26
|
|
25
27
|
it "uses the custom filter if provided" do
|
26
28
|
filter = PostTag.where(:id => [1, 2])
|
27
29
|
filtered_table = PartialKs::FilteredTable.new(model, nil, custom_filter_relation: filter)
|
28
|
-
filtered_table.
|
30
|
+
filtered_table.to_sql.must_equal('SELECT "post_tags".* FROM "post_tags" WHERE "post_tags"."id" IN (1, 2)')
|
29
31
|
end
|
30
32
|
|
31
33
|
it "uses the filter inside a lambda" do
|
32
34
|
filter = -> { PostTag.where(:id => [1, 2]) }
|
33
35
|
filtered_table = PartialKs::FilteredTable.new(model, nil, custom_filter_relation: filter)
|
34
|
-
filtered_table.
|
36
|
+
filtered_table.to_sql.must_equal('SELECT "post_tags".* FROM "post_tags" WHERE "post_tags"."id" IN (1, 2)')
|
35
37
|
end
|
36
38
|
|
37
39
|
it "uses a SQL where fragment as a filter if provided" do
|
38
40
|
string_filter = "1=0"
|
39
41
|
filtered_table = PartialKs::FilteredTable.new(model, nil, custom_filter_relation: string_filter)
|
40
|
-
filtered_table.
|
42
|
+
filtered_table.to_sql.must_equal(string_filter)
|
41
43
|
end
|
42
44
|
|
43
45
|
it "uses a SQL statement as a filter if provided" do
|
44
46
|
string_filter = "1=0"
|
45
47
|
sql_statement = "select * from #{model.table_name} where #{string_filter}"
|
46
48
|
filtered_table = PartialKs::FilteredTable.new(model, nil, custom_filter_relation: sql_statement)
|
47
|
-
filtered_table.
|
49
|
+
filtered_table.to_sql.must_equal(sql_statement)
|
48
50
|
end
|
49
51
|
|
50
52
|
it "returns nil if parent is nil" do
|
51
53
|
filtered_table = PartialKs::FilteredTable.new(model, nil)
|
52
|
-
filtered_table.
|
54
|
+
filtered_table.to_sql.must_be_nil
|
53
55
|
end
|
54
56
|
|
55
57
|
it "generates a complete where fragment from a custom filter" do
|
56
58
|
filter = PostTag.where(:id => 2)
|
57
59
|
filtered_table = PartialKs::FilteredTable.new(model, nil, custom_filter_relation: filter)
|
58
|
-
filtered_table.
|
60
|
+
filtered_table.to_sql.must_equal('SELECT "post_tags".* FROM "post_tags" WHERE "post_tags"."id" = 2')
|
59
61
|
end
|
60
62
|
end
|
data/test/runner_test.rb
CHANGED
@@ -47,7 +47,7 @@ describe 'running based on output from generator' do
|
|
47
47
|
actual_filters.size.must_equal expected_filters.size
|
48
48
|
actual_filters.keys.must_equal expected_filters
|
49
49
|
expected_filters.each do |table_name|
|
50
|
-
actual_filters[table_name]
|
50
|
+
actual_filters[table_name].must_be_kind_of String
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: partial_ks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thong Kuah
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: 'A library to
|
55
|
+
description: 'A library to sync a subset of your database
|
56
56
|
|
57
57
|
'
|
58
58
|
email: kuahyeow@gmail.com
|
@@ -70,9 +70,9 @@ files:
|
|
70
70
|
- lib/partial_ks.rb
|
71
71
|
- lib/partial_ks/all_rails_models.rb
|
72
72
|
- lib/partial_ks/filtered_table.rb
|
73
|
-
- lib/partial_ks/kitchen_sync.rb
|
74
73
|
- lib/partial_ks/models_list.rb
|
75
74
|
- lib/partial_ks/runner.rb
|
75
|
+
- lib/partial_ks/sync.rb
|
76
76
|
- lib/partial_ks/table.rb
|
77
77
|
- lib/partial_ks/version.rb
|
78
78
|
- partial_ks.gemspec
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
requirements: []
|
108
108
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.7.6
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: Partial KS
|