partial_ks 0.0.4
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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +32 -0
- data/lib/partial_ks/configuration_generator.rb +61 -0
- data/lib/partial_ks/runner.rb +81 -0
- data/lib/partial_ks/table.rb +47 -0
- data/lib/partial_ks/version.rb +3 -0
- data/lib/partial_ks.rb +6 -0
- data/partial_ks.gemspec +22 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b14cf3641f82c08ea9ea16536510c59b950dd0f6
|
4
|
+
data.tar.gz: ee41b04d67e616c72787e953ca341da267b15317
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5b20e05c16bd828f7c9d0f1797fd95b9a5281f49f251f65cb4353caf176b0ef8891ad9f4aa56225935d2a890d28c223e39c827fa080e85b6713838bf96f177f5
|
7
|
+
data.tar.gz: 0b9d556d6419799d9f05649198fc3b4b40e4658ecd2d08f59ad2a6193af4d27a0d8afbc4b82424cb1e236fbe9b2564bd50cb99a65ded4f8bbd56e2de192fab2c
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Powershop New Zealand Limited
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PartialKs
|
2
|
+
- ConfigurationGenerator
|
3
|
+
- Runner
|
4
|
+
- runs
|
5
|
+
- reports (mostly for debugging)
|
6
|
+
|
7
|
+
So how does it work ?
|
8
|
+
|
9
|
+
`brew install kitchen-sync`
|
10
|
+
|
11
|
+
```
|
12
|
+
manual_configuration = []
|
13
|
+
config = PartialKs::ConfigurationGenerator.new().call
|
14
|
+
PartialKs::Runner.new([config]).run!
|
15
|
+
```
|
16
|
+
|
17
|
+
You can specify manual configurations if needed.
|
18
|
+
|
19
|
+
```
|
20
|
+
manual_configuration = [
|
21
|
+
["users", nil, User.where(:id => [1])], # specify a subset of users. as users have no parent, specify `nil`
|
22
|
+
["blog_posts", User] # filter blog_posts by User
|
23
|
+
]
|
24
|
+
```
|
25
|
+
|
26
|
+
TODO :
|
27
|
+
|
28
|
+
Rename PartialKs::ConfigurationGenerator#call to something better
|
29
|
+
What in the world is the tuples in `manual_configuration` meant to be ?
|
30
|
+
Minimize Public API
|
31
|
+
|
32
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Given an initial table graph
|
2
|
+
# goes through each table not already in the table graph,
|
3
|
+
# and attempts to automatically populate the table into the table graph
|
4
|
+
class PartialKs::ConfigurationGenerator
|
5
|
+
attr_reader :table_graph, :ignored_table_names
|
6
|
+
|
7
|
+
def initialize(table_graph, ignored_table_names: [])
|
8
|
+
@table_graph = table_graph
|
9
|
+
@ignored_table_names = ignored_table_names
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
@auto_constructed_table_graph ||= auto_construct_table_graph
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
def all_tables
|
18
|
+
@all_tables ||= (ActiveRecord::Base.connection.tables - ignored_table_names).map {|table_name| PartialKs::Table.new(table_name) }.select(&:model?).index_by(&:table_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def auto_construct_table_graph
|
22
|
+
synced_tables = {}
|
23
|
+
|
24
|
+
table_graph.each do |table_name, parent_table, table_graph|
|
25
|
+
next unless all_tables[table_name]
|
26
|
+
|
27
|
+
synced_tables[table_name] = [table_name, parent_table, table_graph]
|
28
|
+
end
|
29
|
+
|
30
|
+
all_tables.each do |table_name, table|
|
31
|
+
next if synced_tables[table_name]
|
32
|
+
next unless table.top_level_table?
|
33
|
+
|
34
|
+
synced_tables[table_name] = [table_name, nil]
|
35
|
+
end
|
36
|
+
|
37
|
+
all_tables.each do |table_name, table|
|
38
|
+
next if synced_tables[table_name]
|
39
|
+
next unless table.non_nullable_parent_tables.size == 1
|
40
|
+
|
41
|
+
parent_table_name, _, _ = synced_tables[table.non_nullable_parent_tables.only]
|
42
|
+
parent_table = all_tables[parent_table_name] if parent_table_name
|
43
|
+
next unless parent_table
|
44
|
+
|
45
|
+
synced_tables[table.table_name] = [table_name, parent_table.model]
|
46
|
+
end
|
47
|
+
|
48
|
+
puts "***************"
|
49
|
+
remaining_size = 0
|
50
|
+
all_tables.each do |table_name, table|
|
51
|
+
next if synced_tables[table_name]
|
52
|
+
|
53
|
+
puts "#{table.table_name} - #{table.parent_tables.join(',')}"
|
54
|
+
remaining_size += 1
|
55
|
+
end
|
56
|
+
puts "WARNING: #{remaining_size} tables has no configuration"
|
57
|
+
|
58
|
+
synced_tables.values
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
class PartialKs::Runner
|
2
|
+
attr_reader :table_graphs
|
3
|
+
|
4
|
+
def initialize(table_graphs)
|
5
|
+
@table_graphs = table_graphs
|
6
|
+
end
|
7
|
+
|
8
|
+
def run!
|
9
|
+
each_generation do |generation|
|
10
|
+
tables_to_filter = {}
|
11
|
+
table_names = []
|
12
|
+
|
13
|
+
generation.each do |table_name, filter_config, depth|
|
14
|
+
table_names << table_name
|
15
|
+
|
16
|
+
if !filter_config.nil?
|
17
|
+
if filter_config.is_a?(ActiveRecord::Relation) || filter_config.respond_to?(:where_sql)
|
18
|
+
only_filter = filter_config.where_sql.to_s.sub("WHERE", "")
|
19
|
+
elsif filter_config.is_a?(String)
|
20
|
+
only_filter = filter_config
|
21
|
+
else
|
22
|
+
only_filter = "#{filter_config.to_s.foreign_key} IN (#{[0, *filter_config.pluck(:id)].join(',')})"
|
23
|
+
end
|
24
|
+
|
25
|
+
tables_to_filter[table_name] = {"only" => only_filter}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
yield tables_to_filter, table_names
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def report
|
34
|
+
each_generation.with_index do |generation, depth|
|
35
|
+
generation.each do |table_name, table_graph|
|
36
|
+
print " " * depth
|
37
|
+
puts [table_name, table_graph.try!(:to_s), depth].inspect
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Yields the following
|
43
|
+
# 1. table name
|
44
|
+
# 2. the foreign key columns used to filter this table, and a lambda that will return the foreign key ids
|
45
|
+
def each_generation
|
46
|
+
return enum_for(:each_generation) unless block_given?
|
47
|
+
|
48
|
+
generations.each do |generation|
|
49
|
+
yield generation
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
def generations
|
55
|
+
return @generations if @generations
|
56
|
+
|
57
|
+
@generations = []
|
58
|
+
table_graphs.each do |table_graph|
|
59
|
+
q = []
|
60
|
+
|
61
|
+
table_graph.each do |table_name, parent_table, configuration_for_table|
|
62
|
+
q << [table_name, configuration_for_table || parent_table] if parent_table.nil?
|
63
|
+
end
|
64
|
+
|
65
|
+
until q.empty?
|
66
|
+
@generations << q
|
67
|
+
|
68
|
+
next_generation = []
|
69
|
+
q.each do |table_name, _|
|
70
|
+
table_graph.each do |child_table_name, parent_table, configuration_for_table|
|
71
|
+
next_generation << [child_table_name, configuration_for_table || parent_table] if parent_table && parent_table.table_name == table_name
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
q = next_generation
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
@generations
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module PartialKs
|
2
|
+
class Table
|
3
|
+
attr_reader :table_name
|
4
|
+
|
5
|
+
def initialize(table_name)
|
6
|
+
@table_name = table_name
|
7
|
+
end
|
8
|
+
|
9
|
+
def model
|
10
|
+
@model ||= table_name.classify.constantize
|
11
|
+
rescue NameError
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
# sometimes the table is present, but the model is not defined
|
16
|
+
# e.g. in market specific tables
|
17
|
+
def model?
|
18
|
+
model && model.respond_to?(:table_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def top_level_table?
|
22
|
+
non_nullable_reflections.empty?
|
23
|
+
end
|
24
|
+
|
25
|
+
def non_nullable_parent_tables
|
26
|
+
non_nullable_reflections.map(&:plural_name)
|
27
|
+
end
|
28
|
+
|
29
|
+
def parent_tables
|
30
|
+
belongs_to_reflections.map(&:plural_name)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def belongs_to_reflections
|
35
|
+
@belongs_to_reflections ||= model.reflect_on_all_associations(:belongs_to)
|
36
|
+
end
|
37
|
+
|
38
|
+
def non_nullable_reflections
|
39
|
+
# We have to consider the situation where the column does not exist on
|
40
|
+
# the remote end, which results in a discrepancy between model.columns and the current schema
|
41
|
+
belongs_to_reflections.reject do |reflection|
|
42
|
+
fk_column = model.columns.find{|column| column.name.to_s == reflection.foreign_key.to_s }
|
43
|
+
fk_column.nil? || fk_column.null
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/partial_ks.rb
ADDED
data/partial_ks.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path('../lib/partial_ks/version', __FILE__)
|
2
|
+
|
3
|
+
spec = Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'partial_ks'
|
5
|
+
gem.version = PartialKs::VERSION
|
6
|
+
gem.summary = "Partial KS"
|
7
|
+
gem.description = <<-EOF
|
8
|
+
A library to use kitchen-sync to sync a subset of your database
|
9
|
+
EOF
|
10
|
+
gem.has_rdoc = false
|
11
|
+
gem.author = "Thong Kuah"
|
12
|
+
gem.email = "kuahyeow@gmail.com"
|
13
|
+
gem.homepage = "https://github.com/powershop/partial_ks"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
gem.files = `git ls-files`.split("\n")
|
18
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
gem.require_path = "lib"
|
20
|
+
|
21
|
+
gem.add_dependency "activerecord"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: partial_ks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thong Kuah
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: |
|
28
|
+
A library to use kitchen-sync to sync a subset of your database
|
29
|
+
email: kuahyeow@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- lib/partial_ks.rb
|
37
|
+
- lib/partial_ks/configuration_generator.rb
|
38
|
+
- lib/partial_ks/runner.rb
|
39
|
+
- lib/partial_ks/table.rb
|
40
|
+
- lib/partial_ks/version.rb
|
41
|
+
- partial_ks.gemspec
|
42
|
+
homepage: https://github.com/powershop/partial_ks
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata: {}
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.2.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Partial KS
|
66
|
+
test_files: []
|