polo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3cc4c8cc8d6e2ecfb65d39c7ec4c49192c643388
4
+ data.tar.gz: efdec09f03b61b966650144674799234e2c10369
5
+ SHA512:
6
+ metadata.gz: 10a1d5a28d71cfda25e83d108231dd2daa068075bdff5f356c37270039c090ddb3652246c5223a4bd3e1a3d30d2ce71a6e47961e7d73eecce4892ecf6fc55e5b
7
+ data.tar.gz: af6ebc5a5f4f08cf7106ad53a24244e5b8cc1b0dab4c7cd988d88134e8357a888bee464627b589095e0d08f0b218f091e59af02cd7ef592a1c7f43196fee6a4a
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.5
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ gemfile:
5
+ - gemfiles/rails32.gemfile
6
+ - gemfiles/rails40.gemfile
7
+ - gemfiles/rails41.gemfile
8
+ - gemfiles/rails42.gemfile
9
+ before_install: gem install bundler -v 1.10.6
data/Appraisals ADDED
@@ -0,0 +1,15 @@
1
+ appraise "rails32" do
2
+ gem "activerecord", "3.2.22"
3
+ end
4
+
5
+ appraise "rails40" do
6
+ gem "activerecord", "4.0.13"
7
+ end
8
+
9
+ appraise "rails41" do
10
+ gem "activerecord", "4.1.12"
11
+ end
12
+
13
+ appraise "rails42" do
14
+ gem "activerecord", "4.2.3"
15
+ end
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 IFTTT
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,169 @@
1
+ [![Open Source at IFTTT](http://ifttt.github.io/images/open-source-ifttt.svg)](http://ifttt.github.io)
2
+
3
+ ![Polo](https://raw.githubusercontent.com/IFTTT/polo/images/images/polo.png "Polo")
4
+
5
+ # Polo
6
+ Polo travels through your database and creates sample snapshots so you can work with real world data in any environment.
7
+
8
+ Polo takes an `ActiveRecord::Base` seed object and traverses every whitelisted `ActiveRecord::Association` generating SQL `INSERTs` along the way.
9
+
10
+ You can then save those SQL `INSERTS` to .sql file and import the data to your favorite environment.
11
+
12
+ ## Usage
13
+ Given the following data model:
14
+ ```ruby
15
+ class Chef < ActiveRecord::Base
16
+ has_many :recipes
17
+ has_many :ingredients, through: :recipes
18
+ end
19
+
20
+ class Recipe < ActiveRecord::Base
21
+ has_many :recipes_ingredients
22
+ has_many :ingredients, through: :recipes_ingredients
23
+ end
24
+
25
+ class Ingredient < ActiveRecord::Base
26
+ end
27
+
28
+ class RecipesIngredient < ActiveRecord::Base
29
+ belongs_to :recipe
30
+ belongs_to :ingredient
31
+ end
32
+ ```
33
+
34
+ ### Simple ActiveRecord Objects
35
+ ```ruby
36
+ inserts = Polo.explore(Chef, 1)
37
+ ```
38
+ ```sql
39
+ INSERT INTO `chefs` (`id`, `name`) VALUES (1, 'Netto')
40
+ ```
41
+
42
+ Where `Chef` is the seed object class, and `1` is the seed object id.
43
+
44
+ ### Simple Associations
45
+ ```ruby
46
+ inserts = Polo.explore(Chef, 1, :recipes)
47
+ ```
48
+ ```sql
49
+ INSERT INTO `chefs` (`id`, `name`) VALUES (1, 'Netto')
50
+ INSERT INTO `recipes` (`id`, `title`, `num_steps`, `chef_id`) VALUES (1, 'Turkey Sandwich', NULL, 1)
51
+ INSERT INTO `recipes` (`id`, `title`, `num_steps`, `chef_id`) VALUES (2, 'Cheese Burger', NULL, 1)
52
+ ```
53
+
54
+ ### Complex nested associations
55
+ ```ruby
56
+ inserts = Polo.explore(Chef, 1, :recipes => :ingredients)
57
+ ```
58
+
59
+ ```sql
60
+ INSERT INTO `chefs` (`id`, `name`) VALUES (1, 'Netto')
61
+ INSERT INTO `recipes` (`id`, `title`, `num_steps`, `chef_id`) VALUES (1, 'Turkey Sandwich', NULL, 1)
62
+ INSERT INTO `recipes` (`id`, `title`, `num_steps`, `chef_id`) VALUES (2, 'Cheese Burger', NULL, 1)
63
+ INSERT INTO `recipes_ingredients` (`id`, `recipe_id`, `ingredient_id`) VALUES (1, 1, 1)
64
+ INSERT INTO `recipes_ingredients` (`id`, `recipe_id`, `ingredient_id`) VALUES (2, 1, 2)
65
+ INSERT INTO `recipes_ingredients` (`id`, `recipe_id`, `ingredient_id`) VALUES (3, 2, 3)
66
+ INSERT INTO `recipes_ingredients` (`id`, `recipe_id`, `ingredient_id`) VALUES (4, 2, 4)
67
+ INSERT INTO `ingredients` (`id`, `name`, `quantity`) VALUES (1, 'Turkey', 'a lot')
68
+ INSERT INTO `ingredients` (`id`, `name`, `quantity`) VALUES (2, 'Cheese', '1 slice')
69
+ INSERT INTO `ingredients` (`id`, `name`, `quantity`) VALUES (3, 'Patty', '1')
70
+ INSERT INTO `ingredients` (`id`, `name`, `quantity`) VALUES (4, 'Cheese', '2 slices')
71
+ ```
72
+
73
+ ## Advanced Usage
74
+
75
+ Occasionally, you might have a dataset that you want to refresh. A production database that has data that might be useful on your local copy of the database. Polo doesn't have an opinion about your data; if you try to import data with a key that's already in your local database, Polo doesn't necessarily know how you want to handle that conflict.
76
+
77
+ Advanced users will find the `on_duplicate` option to be helpful in this context. It gives Polo instructions on how to handle collisions. *Note: This feature is currently only supported for MySQL databases. (PRs for other databases are welcome!)*
78
+
79
+ There are two possible values for the `on_duplicate` key: `:ignore` and `:override`. Ignore keeps the old data. Override keeps the new data. If there's a collision and the on_duplicate param is not set, Polo will simpy stop importing the data.
80
+
81
+ ### Ignore
82
+ A.K.A the Ostrich Approach: stick your head in the sand and pretend nothing happened.
83
+
84
+ ```ruby
85
+ Polo.configure do
86
+ on_duplicate :ignore
87
+ end
88
+
89
+ Polo::Traveler.explore(Chef, 1, :recipes)
90
+ ```
91
+
92
+ ```sql
93
+ INSERT IGNORE INTO `chefs` (`id`, `name`) VALUES (1, 'Netto')
94
+ INSERT IGNORE INTO `recipes` (`id`, `title`, `num_steps`, `chef_id`) VALUES (1, 'Turkey Sandwich', NULL, 1)
95
+ INSERT IGNORE INTO `recipes` (`id`, `title`, `num_steps`, `chef_id`) VALUES (2, 'Cheese Burger', NULL, 1)
96
+ ```
97
+
98
+ ### Override
99
+ Use the option `on_duplicate: :override` to override your local data with new data from your Polo script.
100
+
101
+ ```ruby
102
+ Polo.configure do
103
+ on_duplicate :override
104
+ end
105
+
106
+ Polo::Traveler.explore(Chef, 1, :recipes)
107
+ ```
108
+
109
+ ```sql
110
+ INSERT INTO `chefs` (`id`, `name`) VALUES (1, 'Netto')
111
+ ON DUPLICATE KEY UPDATE id = VALUES(id), name = VALUES(name)
112
+ ...
113
+ ```
114
+
115
+ ### Sensitive Fields
116
+ You can use the `obfuscate` option to obfuscate sensitive fields like emails or
117
+ user logins.
118
+
119
+ ```ruby
120
+ Polo.configure do
121
+ obfuscate :email, :credit_card
122
+ end
123
+
124
+ Polo::Traveler.explore(AR::Chef, 1)
125
+ ```
126
+
127
+ ```sql
128
+ INSERT INTO `chefs` (`id`, `name`, `email`) VALUES (1, 'Netto', 'eahorctmaagfo.nitm@l')
129
+ ```
130
+
131
+ Warning: This is not a security feature. Fields can still easily be rearranged back to their original format. Polo will simply scramble the order of strings so you don't accidentaly end up causing side effects when using production data in development.
132
+
133
+ It is not a good practice to use highly sensitive data in development.
134
+
135
+ ## Installation
136
+
137
+ Add this line to your application's Gemfile:
138
+
139
+ ```ruby
140
+ gem 'polo'
141
+ ```
142
+
143
+ And then execute:
144
+ ```bash
145
+ $ bundle
146
+ ```
147
+
148
+ Or install it yourself as:
149
+ ```bash
150
+ $ gem install polo
151
+ ```
152
+
153
+ ## Contributing
154
+
155
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/polo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
156
+
157
+ To run the specs across all supported version of Rails, check out the repo and
158
+ follow these steps:
159
+
160
+ ```bash
161
+ $ bundle install
162
+ $ bundle exec appraisal install
163
+ $ bundle exec appraisal rake
164
+ ```
165
+
166
+ ## License
167
+
168
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
169
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "3.2.22"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ polo (0.1.0)
5
+ activerecord (>= 3.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (3.2.22)
11
+ activesupport (= 3.2.22)
12
+ builder (~> 3.0.0)
13
+ activerecord (3.2.22)
14
+ activemodel (= 3.2.22)
15
+ activesupport (= 3.2.22)
16
+ arel (~> 3.0.2)
17
+ tzinfo (~> 0.3.29)
18
+ activesupport (3.2.22)
19
+ i18n (~> 0.6, >= 0.6.4)
20
+ multi_json (~> 1.0)
21
+ appraisal (2.1.0)
22
+ bundler
23
+ rake
24
+ thor (>= 0.14.0)
25
+ arel (3.0.3)
26
+ builder (3.0.4)
27
+ diff-lcs (1.2.5)
28
+ i18n (0.7.0)
29
+ multi_json (1.11.2)
30
+ rake (10.4.2)
31
+ rspec (3.3.0)
32
+ rspec-core (~> 3.3.0)
33
+ rspec-expectations (~> 3.3.0)
34
+ rspec-mocks (~> 3.3.0)
35
+ rspec-core (3.3.2)
36
+ rspec-support (~> 3.3.0)
37
+ rspec-expectations (3.3.1)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.3.0)
40
+ rspec-mocks (3.3.2)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.3.0)
43
+ rspec-support (3.3.0)
44
+ sqlite3 (1.3.10)
45
+ thor (0.19.1)
46
+ tzinfo (0.3.44)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ activerecord (= 3.2.22)
53
+ appraisal
54
+ bundler (~> 1.10)
55
+ polo!
56
+ rake (~> 10.0)
57
+ rspec
58
+ sqlite3 (= 1.3.10)
59
+
60
+ BUNDLED WITH
61
+ 1.10.6
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "4.0.13"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ polo (0.1.0)
5
+ activerecord (>= 3.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.0.13)
11
+ activesupport (= 4.0.13)
12
+ builder (~> 3.1.0)
13
+ activerecord (4.0.13)
14
+ activemodel (= 4.0.13)
15
+ activerecord-deprecated_finders (~> 1.0.2)
16
+ activesupport (= 4.0.13)
17
+ arel (~> 4.0.0)
18
+ activerecord-deprecated_finders (1.0.4)
19
+ activesupport (4.0.13)
20
+ i18n (~> 0.6, >= 0.6.9)
21
+ minitest (~> 4.2)
22
+ multi_json (~> 1.3)
23
+ thread_safe (~> 0.1)
24
+ tzinfo (~> 0.3.37)
25
+ appraisal (2.1.0)
26
+ bundler
27
+ rake
28
+ thor (>= 0.14.0)
29
+ arel (4.0.2)
30
+ builder (3.1.4)
31
+ diff-lcs (1.2.5)
32
+ i18n (0.7.0)
33
+ minitest (4.7.5)
34
+ multi_json (1.11.2)
35
+ rake (10.4.2)
36
+ rspec (3.3.0)
37
+ rspec-core (~> 3.3.0)
38
+ rspec-expectations (~> 3.3.0)
39
+ rspec-mocks (~> 3.3.0)
40
+ rspec-core (3.3.2)
41
+ rspec-support (~> 3.3.0)
42
+ rspec-expectations (3.3.1)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.3.0)
45
+ rspec-mocks (3.3.2)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.3.0)
48
+ rspec-support (3.3.0)
49
+ sqlite3 (1.3.10)
50
+ thor (0.19.1)
51
+ thread_safe (0.3.5)
52
+ tzinfo (0.3.44)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ activerecord (= 4.0.13)
59
+ appraisal
60
+ bundler (~> 1.10)
61
+ polo!
62
+ rake (~> 10.0)
63
+ rspec
64
+ sqlite3 (= 1.3.10)
65
+
66
+ BUNDLED WITH
67
+ 1.10.6
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "4.1.12"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ polo (0.1.0)
5
+ activerecord (>= 3.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.1.12)
11
+ activesupport (= 4.1.12)
12
+ builder (~> 3.1)
13
+ activerecord (4.1.12)
14
+ activemodel (= 4.1.12)
15
+ activesupport (= 4.1.12)
16
+ arel (~> 5.0.0)
17
+ activesupport (4.1.12)
18
+ i18n (~> 0.6, >= 0.6.9)
19
+ json (~> 1.7, >= 1.7.7)
20
+ minitest (~> 5.1)
21
+ thread_safe (~> 0.1)
22
+ tzinfo (~> 1.1)
23
+ appraisal (2.1.0)
24
+ bundler
25
+ rake
26
+ thor (>= 0.14.0)
27
+ arel (5.0.1.20140414130214)
28
+ builder (3.2.2)
29
+ diff-lcs (1.2.5)
30
+ i18n (0.7.0)
31
+ json (1.8.3)
32
+ minitest (5.8.0)
33
+ rake (10.4.2)
34
+ rspec (3.3.0)
35
+ rspec-core (~> 3.3.0)
36
+ rspec-expectations (~> 3.3.0)
37
+ rspec-mocks (~> 3.3.0)
38
+ rspec-core (3.3.2)
39
+ rspec-support (~> 3.3.0)
40
+ rspec-expectations (3.3.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.3.0)
43
+ rspec-mocks (3.3.2)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.3.0)
46
+ rspec-support (3.3.0)
47
+ sqlite3 (1.3.10)
48
+ thor (0.19.1)
49
+ thread_safe (0.3.5)
50
+ tzinfo (1.2.2)
51
+ thread_safe (~> 0.1)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ activerecord (= 4.1.12)
58
+ appraisal
59
+ bundler (~> 1.10)
60
+ polo!
61
+ rake (~> 10.0)
62
+ rspec
63
+ sqlite3 (= 1.3.10)
64
+
65
+ BUNDLED WITH
66
+ 1.10.6
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "4.2.3"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ polo (0.1.0)
5
+ activerecord (>= 3.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.3)
11
+ activesupport (= 4.2.3)
12
+ builder (~> 3.1)
13
+ activerecord (4.2.3)
14
+ activemodel (= 4.2.3)
15
+ activesupport (= 4.2.3)
16
+ arel (~> 6.0)
17
+ activesupport (4.2.3)
18
+ i18n (~> 0.7)
19
+ json (~> 1.7, >= 1.7.7)
20
+ minitest (~> 5.1)
21
+ thread_safe (~> 0.3, >= 0.3.4)
22
+ tzinfo (~> 1.1)
23
+ appraisal (2.1.0)
24
+ bundler
25
+ rake
26
+ thor (>= 0.14.0)
27
+ arel (6.0.3)
28
+ builder (3.2.2)
29
+ diff-lcs (1.2.5)
30
+ i18n (0.7.0)
31
+ json (1.8.3)
32
+ minitest (5.8.0)
33
+ rake (10.4.2)
34
+ rspec (3.3.0)
35
+ rspec-core (~> 3.3.0)
36
+ rspec-expectations (~> 3.3.0)
37
+ rspec-mocks (~> 3.3.0)
38
+ rspec-core (3.3.2)
39
+ rspec-support (~> 3.3.0)
40
+ rspec-expectations (3.3.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.3.0)
43
+ rspec-mocks (3.3.2)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.3.0)
46
+ rspec-support (3.3.0)
47
+ sqlite3 (1.3.10)
48
+ thor (0.19.1)
49
+ thread_safe (0.3.5)
50
+ tzinfo (1.2.2)
51
+ thread_safe (~> 0.1)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ activerecord (= 4.2.3)
58
+ appraisal
59
+ bundler (~> 1.10)
60
+ polo!
61
+ rake (~> 10.0)
62
+ rspec
63
+ sqlite3 (= 1.3.10)
64
+
65
+ BUNDLED WITH
66
+ 1.10.6
data/lib/polo.rb ADDED
@@ -0,0 +1,87 @@
1
+ require "polo/version"
2
+ require "polo/collector"
3
+ require "polo/translator"
4
+ require "polo/configuration"
5
+
6
+ module Polo
7
+
8
+ # Public: Traverses a dependency graph based on a seed ActiveRecord object
9
+ # and generates all the necessary INSERT queries for each one of the records
10
+ # it finds along the way.
11
+ #
12
+ # base_class - An ActiveRecord::Base class for the seed record.
13
+ # id - An ID used to find the desired seed record.
14
+ #
15
+ # dependency_tree - An ActiveRecord::Associations::Preloader compliant that
16
+ # will define the path Polo will traverse.
17
+ #
18
+ # (from ActiveRecord::Associations::Preloader docs)
19
+ # It may be:
20
+ # - a Symbol or a String which specifies a single association name. For
21
+ # example, specifying +:books+ allows this method to preload all books
22
+ # for an Author.
23
+ # - an Array which specifies multiple association names. This array
24
+ # is processed recursively. For example, specifying <tt>[:avatar, :books]</tt>
25
+ # allows this method to preload an author's avatar as well as all of his
26
+ # books.
27
+ # - a Hash which specifies multiple association names, as well as
28
+ # association names for the to-be-preloaded association objects. For
29
+ # example, specifying <tt>{ author: :avatar }</tt> will preload a
30
+ # book's author, as well as that author's avatar.
31
+ #
32
+ # +:associations+ has the same format as the +:include+ option for
33
+ # <tt>ActiveRecord::Base.find</tt>. So +associations+ could look like this:
34
+ #
35
+ # :books
36
+ # [ :books, :author ]
37
+ # { author: :avatar }
38
+ # [ :books, { author: :avatar } ]
39
+ #
40
+ def self.explore(base_class, id, dependencies={})
41
+ Traveler.collect(base_class, id, dependencies).translate(defaults)
42
+ end
43
+
44
+
45
+ # Public: Sets up global settings for Polo
46
+ #
47
+ # block - Takes a block with the settings you decide to use
48
+ #
49
+ # obfuscate - Takes a blacklist with sensitive fields you wish to scramble
50
+ # on_duplicate - Defines the on_duplicate strategy for your INSERTS
51
+ # e.g. :override, :ignore
52
+ #
53
+ # usage:
54
+ # Polo.configure do
55
+ # obfuscate(:email, :password, :credit_card)
56
+ # on_duplicate(:override)
57
+ # end
58
+ #
59
+ def self.configure(&block)
60
+ @configuration = Configuration.new
61
+ @configuration.instance_eval(&block) if block_given?
62
+ @configuration
63
+ end
64
+
65
+ # Public: Returns the default settings
66
+ #
67
+ def self.defaults
68
+ @configuration || configure
69
+ end
70
+
71
+
72
+ class Traveler
73
+
74
+ def self.collect(base_class, id, dependencies={})
75
+ selects = Collector.new(base_class, id, dependencies).collect
76
+ new(selects)
77
+ end
78
+
79
+ def initialize(selects)
80
+ @selects = selects
81
+ end
82
+
83
+ def translate(configuration=Configuration.new)
84
+ Translator.new(@selects, configuration).translate
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,53 @@
1
+ module Polo
2
+ class Collector
3
+
4
+ def initialize(base_class, id, dependency_tree={})
5
+ @base_class = base_class
6
+ @id = id
7
+ @dependency_tree = dependency_tree
8
+ @selects = []
9
+ end
10
+
11
+ # Public: Traverses the dependency tree and collects every SQL query.
12
+ #
13
+ # This is done by wrapping a top level call to includes(...) with a
14
+ # ActiveSupport::Notifications block and collecting every generate SQL query.
15
+ #
16
+ def collect
17
+ ActiveSupport::Notifications.subscribed(collector, 'sql.active_record') do
18
+ base_finder = @base_class.includes(@dependency_tree).where(id: @id)
19
+ collect_sql(@base_class, base_finder.to_sql)
20
+ base_finder.to_a
21
+ end
22
+
23
+ @selects.compact.uniq
24
+ end
25
+
26
+ private
27
+
28
+ # Internal: Store ActiveRecord queries in @selects
29
+ #
30
+ # Collector will intersect every ActiveRecord query performed within the
31
+ # ActiveSupport::Notifications.subscribed block defined in #run and store
32
+ # the resulting SQL query in @selects
33
+ #
34
+ def collector
35
+ lambda do |name, start, finish, id, payload|
36
+ begin
37
+ class_name = payload[:name].gsub(' Load', '').constantize
38
+ sql = payload[:sql]
39
+ collect_sql(class_name, sql)
40
+ rescue ActiveRecord::StatementInvalid, NameError
41
+ # invalid table name (common when prefetching schemas)
42
+ end
43
+ end
44
+ end
45
+
46
+ def collect_sql(klass, sql)
47
+ @selects << {
48
+ klass: klass,
49
+ sql: sql
50
+ }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,20 @@
1
+ module Polo
2
+
3
+ class Configuration
4
+ attr_reader :on_duplicate_strategy, :blacklist
5
+
6
+ def initialize(options={})
7
+ options = { on_duplicate: nil, obfuscate: [] }.merge(options)
8
+ @on_duplicate_strategy = options[:on_duplicate]
9
+ @blacklist = options[:obfuscate]
10
+ end
11
+
12
+ def obfuscate(*fields)
13
+ @blacklist = fields
14
+ end
15
+
16
+ def on_duplicate(strategy)
17
+ @on_duplicate_strategy = strategy
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,93 @@
1
+ require 'active_record'
2
+ require 'polo/configuration'
3
+
4
+ module Polo
5
+ class SqlTranslator
6
+
7
+ def initialize(object, configuration=Configuration.new)
8
+ @record = object
9
+ @configuration = configuration
10
+ end
11
+
12
+ def to_sql
13
+ records = Array.wrap(@record)
14
+
15
+ sqls = records.map do |record|
16
+ raw_sql(record)
17
+ end
18
+
19
+ if @configuration.on_duplicate_strategy == :ignore
20
+ sqls = ignore_transform(sqls)
21
+ end
22
+
23
+ if @configuration.on_duplicate_strategy == :override
24
+ sqls = on_duplicate_key_update(sqls, records)
25
+ end
26
+
27
+ sqls
28
+ end
29
+
30
+ private
31
+
32
+ def on_duplicate_key_update(sqls, records)
33
+ insert_and_record = sqls.zip(records)
34
+ insert_and_record.map do |insert, record|
35
+ values_syntax = record.attributes.keys.map do |key|
36
+ "#{key} = VALUES(#{key})"
37
+ end
38
+
39
+ on_dup_syntax = "ON DUPLICATE KEY UPDATE #{values_syntax.join(', ')}"
40
+
41
+ "#{insert} #{on_dup_syntax}"
42
+ end
43
+ end
44
+
45
+ def ignore_transform(inserts)
46
+ inserts.map do |insert|
47
+ insert.gsub("INSERT", "INSERT IGNORE")
48
+ end
49
+ end
50
+
51
+ def raw_sql(record)
52
+ connection = ActiveRecord::Base.connection
53
+ attributes = record.attributes
54
+
55
+ keys = attributes.keys.map do |key|
56
+ "`#{key}`"
57
+ end
58
+
59
+ values = attributes.map do |key, value|
60
+ column = record.column_for_attribute(key)
61
+ connection.quote(cast_attribute(record, column, value))
62
+ end
63
+
64
+ "INSERT INTO `#{record.class.table_name}` (#{keys.join(', ')}) VALUES (#{values.join(', ')})"
65
+ end
66
+
67
+ module ActiveRecordLessThanFourPointTwo
68
+ def cast_attribute(record, column, value)
69
+ attribute = record.send(:type_cast_attribute_for_write, column, value)
70
+
71
+ if record.class.serialized_attributes.include?(column.name)
72
+ attribute.serialize
73
+ else
74
+ attribute
75
+ end
76
+ end
77
+ end
78
+
79
+ module ActiveRecordFourPointTwoOrGreater
80
+ def cast_attribute(record, column, value)
81
+ column.type_cast_for_database(value)
82
+ end
83
+ end
84
+
85
+ if ActiveRecord::VERSION::STRING.start_with?('3.2') ||
86
+ ActiveRecord::VERSION::STRING.start_with?('4.0') ||
87
+ ActiveRecord::VERSION::STRING.start_with?('4.1')
88
+ include ActiveRecordLessThanFourPointTwo
89
+ else
90
+ include ActiveRecordFourPointTwoOrGreater
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,46 @@
1
+ require "polo/sql_translator"
2
+ require "polo/configuration"
3
+
4
+ module Polo
5
+ class Translator
6
+
7
+ # Public: Creates a new Polo::Collector
8
+ #
9
+ # selects - An array of SELECT queries
10
+ #
11
+ def initialize(selects, configuration=Configuration.new)
12
+ @selects = selects
13
+ @configuration = configuration
14
+ end
15
+
16
+ # Public: Translates SELECT queries into INSERTS.
17
+ #
18
+ def translate
19
+ SqlTranslator.new(instances, @configuration).to_sql.uniq
20
+ end
21
+
22
+ def instances
23
+ active_record_instances = @selects.flat_map do |select|
24
+ select[:klass].find_by_sql(select[:sql]).to_a
25
+ end
26
+
27
+ if fields = @configuration.blacklist
28
+ obfuscate!(active_record_instances, fields.map(&:to_s))
29
+ end
30
+
31
+ active_record_instances
32
+ end
33
+
34
+ private
35
+
36
+ def obfuscate!(instances, fields)
37
+ instances.each do |instance|
38
+ next if (instance.attributes.keys & fields).empty?
39
+ fields.each do |field|
40
+ value = instance.attributes[field] || ''
41
+ instance.send("#{field}=", value.split('').shuffle.join)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Polo
2
+ VERSION = "0.1.0"
3
+ end
data/polo.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'polo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "polo"
8
+ spec.version = Polo::VERSION
9
+ spec.authors = ["Netto Farah"]
10
+ spec.email = ["nettofarah@gmail.com"]
11
+
12
+ spec.summary = %q{Bring life back to your development environment with samples from production data.}
13
+ spec.description = %q{Polo travels through your database and creates sample snapshots so you can work with real world data in development.}
14
+ spec.homepage = "http://ifttt.github.io"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "activerecord", ">= 3.2"
23
+
24
+ spec.add_development_dependency "appraisal"
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+
29
+ spec.add_development_dependency "sqlite3", "1.3.10"
30
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Netto Farah
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-14 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: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: appraisal
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.10
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.10
97
+ description: Polo travels through your database and creates sample snapshots so you
98
+ can work with real world data in development.
99
+ email:
100
+ - nettofarah@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - Appraisals
110
+ - CODE_OF_CONDUCT.md
111
+ - Gemfile
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - gemfiles/rails32.gemfile
116
+ - gemfiles/rails32.gemfile.lock
117
+ - gemfiles/rails40.gemfile
118
+ - gemfiles/rails40.gemfile.lock
119
+ - gemfiles/rails41.gemfile
120
+ - gemfiles/rails41.gemfile.lock
121
+ - gemfiles/rails42.gemfile
122
+ - gemfiles/rails42.gemfile.lock
123
+ - lib/polo.rb
124
+ - lib/polo/collector.rb
125
+ - lib/polo/configuration.rb
126
+ - lib/polo/sql_translator.rb
127
+ - lib/polo/translator.rb
128
+ - lib/polo/version.rb
129
+ - polo.gemspec
130
+ homepage: http://ifttt.github.io
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.4.6
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Bring life back to your development environment with samples from production
154
+ data.
155
+ test_files: []