Last active
September 11, 2018 20:16
-
-
Save belt/c8f7b1c45834ce6fa485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://gist.github.com/belt/c8f7b1c45834ce6fa485 | |
desc 'Create a db/extracted_data.sql file for a given entity' | |
namespace :db do | |
namespace :grep do | |
task associations: :environment do | |
obfuscate_sql if obfuscate | |
Rails.application.eager_load! | |
if dump_file_path.blank? | |
puts grep_associations_to_sql | |
else | |
dump_to_sql_file | |
end | |
end | |
def grep_associations_to_sql | |
Polo.explore(klass, id, model_reflections[entity.classify]) | |
end | |
def dump_to_sql_file | |
puts "Writing #{dump_file_path.sub(Rails.root.to_s + '/', '')}" | |
File.open dump_file_path, 'wb' do |file| | |
file.write grep_associations_to_sql | |
end | |
end | |
def model_reflections | |
descendants = ActiveRecord::Base.descendants.map(&:to_s) | |
@model_reflections ||= descendants.each_with_object({}) do |ar_class, result| | |
reflections = ar_class.constantize.reflections.symbolize_keys.keys.sort | |
reflections -= [:versions] unless capture_versions | |
result[ar_class] = reflections | |
end | |
end | |
# ------------------------------------------------------------------------ | |
# require parameters and validation there-of | |
def fail_usage_message | |
required = 'ENTITY=<table> ID=<integer>' | |
optionals = '[FILE=[path]] [OBFUSCATE=<bool>] [VERSIONS=<bool>]' | |
"try `rake #{ARGV[0]} #{required} #{optionals}`" | |
end | |
def invalid_class_message | |
"#{entity} is not a descendant of ActiveRecord::Base" | |
end | |
def entity | |
fail fail_usage_message unless env[:entity] || env[:ENTITY] | |
(ENV['entity'] || ENV['ENTITY']).try(:classify) | |
end | |
def id | |
fail fail_usage_message unless env[:id] || env[:ID] | |
ENV['id'] || ENV['ID'] | |
end | |
def klass | |
fail fail_usage_msg unless entity | |
@klass ||= entity.try(:constantize) | |
fail invalid_class_message unless @klass && model_reflections.key?(entity) | |
@klass | |
end | |
# ------------------------------------------------------------------------ | |
# optional parameters | |
def dump_file_path | |
@dump_file_path ||= ENV['file'] || ENV['FILE'] || | |
Rails.root.join('db', 'extracted_data.sql') | |
end | |
def obfuscate | |
@obfuscate = true | |
obfuscate = ENV['obfuscate'] || ENV['OBFUSCATE'] | |
@obfuscate = false if %w(0 no false off).include? obfuscate.try :downcase | |
@obfuscate | |
end | |
def capture_versions | |
@capture_versions = true | |
versions = ENV['versions'] || ENV['VERSIONS'] | |
@capture_versions = false if %w(0 no false off).include? versions.try :downcase | |
@capture_versions | |
end | |
private | |
def env | |
@env ||= ENV.to_hash.symbolize_keys | |
end | |
def obfuscate_sql | |
Polo.configure do | |
obfuscate password_digest: ->(_) {'changeme'}, | |
password_reset_digest: ->(_) {'changeme'}, | |
reset_password_token: ->(_) {'changeme'} | |
end | |
end | |
end | |
end | |
# The MIT License (MIT) | |
# | |
# Copyright (c) 2015 Paul Belt | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment