partial_ks 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 642d94cc600de50ae308e592033b37eb76332797
4
- data.tar.gz: 7df7585728a3fe621ed0200d0a83451e3ee0855e
3
+ metadata.gz: d6f2760377220a11179eb98450908820dbfd86ca
4
+ data.tar.gz: 39e16a10891aad2aea68916f57f4e3b8392d88f2
5
5
  SHA512:
6
- metadata.gz: ca7b7e3247cfa18f6ba92aa9de44bb2e52f5cee6ff42a61ff64f9c2e6214b61a78f7bcb29d707f210b7062e998abd09c7dd4e4f224f7a1f27b2ce8e4081e989d
7
- data.tar.gz: eee97be3e6b61d8f3e05a077217ef7d3b6b0659bf663e45c5b06899c7dd58ca8729811c298cef16c09f0c9fa8de01587997fec3e2d52e0cdc972181e4c24c317
6
+ metadata.gz: a039c6bf09d0a69a55cb4f36676ea3e5f92365a009dd5f688d832cc2d82563bb5cfa0ef0725bc4c0e91cacb0543eaa80662e2f53d016d1706951bd72da2055c2
7
+ data.tar.gz: 0c9adb839f994ed6f3d350f526d61faa21d5c15054df366527fd29c3f617f5017a5477583b0c70927496faa457bbcb31c4ad84491cf3955fb1ce1ce2bc2cc830
data/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ # 0.6.0
4
+
5
+ Propogate filters down the tree. If the parent downloads the whole table, do the same for the child.
6
+
3
7
  # 0.5.0
4
8
 
5
9
  * New single entry point, `PartialKs::KitchenSync`. This means that Runner and ConfigurationGenerator are now internal concerns
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.7.1, 4.2.8, 5.0.2, 5.1.0 (on Ruby 2.3.3)
5
+ Tested with activerecord 4.2.10, 5.0.6, 5.1.4 (on Ruby 2.3.5)
6
6
 
7
7
 
8
8
  To install and run tests :
@@ -23,3 +23,7 @@ The building of the filter is done on the target DB.
23
23
 
24
24
  Filters are run on the source DB.
25
25
  - Note that a SQL statement from the target DB will behave differently on the source DB.
26
+
27
+ ## Dependencies
28
+
29
+ KitchenSync -> (ModelList -> Table) -> Runner -> (FilteredTable -> Table)
@@ -1,6 +1,6 @@
1
1
  module PartialKs
2
2
  def self.all_rails_models
3
- if defined?(Rails)
3
+ if defined?(Rails) && Rails.respond_to?(:application)
4
4
  ::Rails.application.eager_load!
5
5
  ::Rails::Engine.subclasses.map(&:eager_load!)
6
6
  end
@@ -1,26 +1,28 @@
1
1
  module PartialKs
2
2
  class FilteredTable
3
- attr_reader :table, :parent_model, :custom_filter_relation
3
+ attr_reader :table, :parent, :custom_filter_relation
4
4
  delegate :table_name, :to => :table
5
5
 
6
- def initialize(model, parent_model, custom_filter_relation: nil)
6
+ def initialize(model, parent, custom_filter_relation: nil)
7
7
  @table = PartialKs::Table.new(model)
8
- @parent_model = parent_model
8
+ @parent = parent
9
9
  @custom_filter_relation = custom_filter_relation
10
10
  end
11
11
 
12
12
  def kitchen_sync_filter
13
13
  if custom_filter_relation
14
14
  {"only" => filter_based_on_custom_filter_relation}
15
- elsif parent_model
16
- {"only" => filter_based_on_parent_model}
15
+ elsif parent && parent.kitchen_sync_filter.nil?
16
+ nil
17
+ elsif parent
18
+ {"only" => filter_based_on_parent_model(parent.table.model)}
17
19
  else
18
20
  nil
19
21
  end
20
22
  end
21
23
 
22
24
  protected
23
- def filter_based_on_parent_model
25
+ def filter_based_on_parent_model(parent_model)
24
26
  table.relation_for_associated_model(parent_model).where_sql.to_s.sub(where_regexp, "")
25
27
  end
26
28
 
@@ -28,7 +30,7 @@ module PartialKs
28
30
  relation = custom_filter_relation.respond_to?(:call) ? custom_filter_relation.call : custom_filter_relation
29
31
 
30
32
  if relation.is_a?(ActiveRecord::Relation) || relation.respond_to?(:where_sql)
31
- relation.where_sql.to_s.sub(where_regexp, "")
33
+ relation.to_sql.to_s.sub(where_regexp, "")
32
34
  elsif relation.is_a?(String)
33
35
  relation.sub(where_regexp, "")
34
36
  end
@@ -24,16 +24,6 @@ module PartialKs
24
24
  end
25
25
  end
26
26
 
27
- def report
28
- result = []
29
- each_generation.with_index do |generation, depth|
30
- generation.each do |table|
31
- result << [table.table_name, table.parent_model, table.custom_filter_relation, depth]
32
- end
33
- end
34
- result
35
- end
36
-
37
27
  def each_generation
38
28
  return enum_for(:each_generation) unless block_given?
39
29
 
@@ -43,9 +33,6 @@ module PartialKs
43
33
  end
44
34
 
45
35
  protected
46
- def filtered_tables
47
- @filtered_tables ||= models_list.map {|model, parent, custom_filter| PartialKs::FilteredTable.new(model, parent, custom_filter_relation: custom_filter)}
48
- end
49
36
 
50
37
  def generations
51
38
  return @generations if defined?(@generations)
@@ -53,8 +40,8 @@ module PartialKs
53
40
  @generations = []
54
41
  q = []
55
42
 
56
- filtered_tables.each do |filtered_table|
57
- q << filtered_table if filtered_table.parent_model.nil?
43
+ models_list.each do |model, parent, filter|
44
+ q << PartialKs::FilteredTable.new(model, nil, custom_filter_relation: filter) if parent.nil?
58
45
  end
59
46
 
60
47
  until q.empty?
@@ -62,8 +49,9 @@ module PartialKs
62
49
 
63
50
  next_generation = []
64
51
  q.each do |table|
65
- filtered_tables.each do |child_table|
66
- next_generation << child_table if child_table.parent_model && child_table.parent_model.table_name == table.table_name
52
+ models_list.each do |child_model, parent, filter|
53
+ # I have access to parent here - link model to child_model
54
+ next_generation << PartialKs::FilteredTable.new(child_model, table, custom_filter_relation: filter) if parent && parent.table_name == table.table_name
67
55
  end
68
56
  end
69
57
 
@@ -1,3 +1,3 @@
1
1
  module PartialKs
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -2,12 +2,13 @@ require 'test_helper'
2
2
 
3
3
  describe "kitchen sync filter" do
4
4
  let(:model) { PostTag }
5
- let(:parent) { Minitest::Mock.new }
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
  relation_mock.expect :where_sql, "WHERE tag_id IN (0)"
11
+ parent = PartialKs::FilteredTable.new(parent, nil, custom_filter_relation: BlogPost.where("1=0"))
11
12
 
12
13
  filtered_table = PartialKs::FilteredTable.new(model, parent)
13
14
  filtered_table.table.stub table_parent_relation_method, relation_mock do
@@ -15,6 +16,12 @@ describe "kitchen sync filter" do
15
16
  end
16
17
  end
17
18
 
19
+ it "short-circuits evaluation if the parent has no filter" do
20
+ parent = PartialKs::FilteredTable.new(parent, nil)
21
+ filtered_table = PartialKs::FilteredTable.new(model, parent)
22
+ filtered_table.kitchen_sync_filter.must_be_nil
23
+ end
24
+
18
25
  it "uses the custom filter if provided" do
19
26
  filter = PostTag.where(:id => [1, 2])
20
27
  filtered_table = PartialKs::FilteredTable.new(model, nil, custom_filter_relation: filter)
@@ -45,4 +52,9 @@ describe "kitchen sync filter" do
45
52
  filtered_table.kitchen_sync_filter.must_be_nil
46
53
  end
47
54
 
55
+ it "generates a complete where fragment from a custom filter" do
56
+ filter = PostTag.where(:id => 2)
57
+ filtered_table = PartialKs::FilteredTable.new(model, nil, custom_filter_relation: filter)
58
+ filtered_table.kitchen_sync_filter.must_equal({"only" => '"post_tags"."id" = 2'})
59
+ end
48
60
  end
data/test/runner_test.rb CHANGED
@@ -26,14 +26,6 @@ describe 'running based on output from generator' do
26
26
 
27
27
  let(:runner) { PartialKs::Runner.new(generator_output) }
28
28
 
29
- it "reports everything" do
30
- runner.report.must_equal [
31
- ["users", nil, User.where(:id => [1]), 0],
32
- ["tags", nil, nil, 0],
33
- ["blog_posts", User, nil, 1],
34
- ]
35
- end
36
-
37
29
  it "yields all table names" do
38
30
  expected_table_names = [User, Tag, BlogPost].map(&:table_name)
39
31
  actual_table_names = []
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.5.0
4
+ version: 0.6.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: 2017-10-11 00:00:00.000000000 Z
11
+ date: 2018-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -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
109
+ rubygems_version: 2.5.2.1
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: Partial KS