partial_ks 0.6.0 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d6f2760377220a11179eb98450908820dbfd86ca
4
- data.tar.gz: 39e16a10891aad2aea68916f57f4e3b8392d88f2
2
+ SHA256:
3
+ metadata.gz: fbc1cd0a2fa2a07636eb8343189227655545d592546ae4271f2c7a2525fefd93
4
+ data.tar.gz: f60d0fd9bcf8a9409e53cb272aa70dcf1f3009aafaa3513e3bf6e82ceae15b61
5
5
  SHA512:
6
- metadata.gz: a039c6bf09d0a69a55cb4f36676ea3e5f92365a009dd5f688d832cc2d82563bb5cfa0ef0725bc4c0e91cacb0543eaa80662e2f53d016d1706951bd72da2055c2
7
- data.tar.gz: 0c9adb839f994ed6f3d350f526d61faa21d5c15054df366527fd29c3f617f5017a5477583b0c70927496faa457bbcb31c4ad84491cf3955fb1ce1ce2bc2cc830
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.
@@ -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.6, 5.1.4 (on Ruby 2.3.5)
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
- KitchenSync -> (ModelList -> Table) -> Runner -> (FilteredTable -> Table)
29
+ Sync -> (ModelList -> Table) -> Runner -> (FilteredTable -> Table)
data/README.md CHANGED
@@ -1,42 +1,53 @@
1
1
  # PartialKs
2
2
 
3
- A library to use kitchen-sync to sync a subset of your database
3
+ A library to sync a subset of your database
4
4
 
5
- # Usage
6
-
7
- So how does it work ?
5
+ ## Usage
8
6
 
7
+ See the following example:
9
8
 
10
9
  ```
11
- PartialKs::KitchenSync.new(manual_configuration).run! do |tables_to_filter, tables|
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
- You can specify manual configurations if needed.
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
- manual_configuration = [
21
- [User, nil, -> { User.where(:id => [1]) }], # specify a subset of users. as users have no parent, specify `nil`
22
- [BlogPost, User] # filter blog_posts by User
23
- ]
28
+ User id 1, 3, 5, 7
29
+ User -> BlogPost
24
30
  ```
25
31
 
26
- NB: The first use case for this gem is to be run in conjuction with [Kitchen Sync](https://github.com/willbryant/kitchen_sync). On OSX, one can install Kitchen Sync using `brew install kitchen-sync`
32
+ PartialKS will only download blog_posts records for users 1, 3, 5, and 7.
27
33
 
28
- # Public API
34
+ Most of the time PartialKS will be able to deduce the parent automatically.
29
35
 
30
- It currently consists of :
36
+ If a model has no "parent", PartialKS will download the whole table.
31
37
 
32
- - PartialKs::KitchenSync
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
- # Not supported
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
-
@@ -5,4 +5,4 @@ require_relative 'partial_ks/filtered_table'
5
5
  require_relative 'partial_ks/runner'
6
6
  require_relative 'partial_ks/table'
7
7
  require_relative 'partial_ks/models_list'
8
- require_relative 'partial_ks/kitchen_sync'
8
+ require_relative 'partial_ks/sync'
@@ -9,13 +9,13 @@ module PartialKs
9
9
  @custom_filter_relation = custom_filter_relation
10
10
  end
11
11
 
12
- def kitchen_sync_filter
12
+ def to_sql
13
13
  if custom_filter_relation
14
- {"only" => filter_based_on_custom_filter_relation}
15
- elsif parent && parent.kitchen_sync_filter.nil?
14
+ filter_based_on_custom_filter_relation
15
+ elsif parent && parent.to_sql.nil?
16
16
  nil
17
17
  elsif parent
18
- {"only" => filter_based_on_parent_model(parent.table.model)}
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).where_sql.to_s.sub(where_regexp, "")
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.sub(where_regexp, "")
33
+ relation.to_sql.to_s
34
34
  elsif relation.is_a?(String)
35
- relation.sub(where_regexp, "")
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
@@ -13,7 +13,7 @@ module PartialKs
13
13
 
14
14
  generation.each do |table|
15
15
  table_names << table.table_name
16
- filter_config = table.kitchen_sync_filter
16
+ filter_config = table.to_sql
17
17
 
18
18
  if !filter_config.nil?
19
19
  tables_to_filter[table.table_name] = filter_config
@@ -1,5 +1,5 @@
1
1
  module PartialKs
2
- class KitchenSync
2
+ class Sync
3
3
  attr_reader :models_list
4
4
  delegate :issues, to: :models_list
5
5
 
@@ -1,3 +1,3 @@
1
1
  module PartialKs
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -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 use kitchen-sync to sync a subset of your database
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"
@@ -1,60 +1,62 @@
1
1
  require 'test_helper'
2
2
 
3
- describe "kitchen sync filter" do
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
- relation_mock.expect :where_sql, "WHERE tag_id IN (0)"
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.kitchen_sync_filter.must_equal({"only" => 'tag_id IN (0)'})
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.kitchen_sync_filter.must_be_nil
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.kitchen_sync_filter.must_equal({"only" => '"post_tags"."id" IN (1, 2)'})
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.kitchen_sync_filter.must_equal({"only" => '"post_tags"."id" IN (1, 2)'})
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.kitchen_sync_filter.must_equal({"only" => string_filter})
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.kitchen_sync_filter.must_equal({"only" => string_filter})
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.kitchen_sync_filter.must_be_nil
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.kitchen_sync_filter.must_equal({"only" => '"post_tags"."id" = 2'})
60
+ filtered_table.to_sql.must_equal('SELECT "post_tags".* FROM "post_tags" WHERE "post_tags"."id" = 2')
59
61
  end
60
62
  end
@@ -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]["only"].must_be_kind_of String
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.6.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-01-26 00:00:00.000000000 Z
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 use kitchen-sync to sync a subset of your database
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.5.2.1
109
+ rubygems_version: 2.7.6
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: Partial KS