Skip to content

Commit

Permalink
Merge pull request #7 from trusted/depollute-global-scope
Browse files Browse the repository at this point in the history
Depollute rake global scope
  • Loading branch information
andrepiske authored Dec 6, 2024
2 parents d26ebe5 + 557ccb3 commit e331153
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions lib/tasks/tracking.rake
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
# frozen_string_literal: true

module IronTrail::RakeHelper
class << self
def db_functions
IronTrail::DbFunctions.new(ActiveRecord::Base.connection)
end

def abort_when_unsafe!
run_unsafe = %w[true 1 yes].include?(ENV['IRONTRAIL_RUN_UNSAFE'])
return unless Rails.env.production? && !run_unsafe

puts "Aborting: operation is dangerous in a production environment. " + \
"Override this behavior by setting the IRONTRAIL_RUN_UNSAFE=1 env var."

exit(1)
end
end
end

namespace :iron_trail do
namespace :tracking do
desc 'Enables tracking for all missing tables.'
task enable: :environment do
tables = db_functions.collect_tables_tracking_status[:missing]
tables = IronTrail::RakeHelper.db_functions.collect_tables_tracking_status[:missing]
unless tables.length > 0
puts "All tables are being tracked already (no missing tables found)."
puts "If you think this is wrong, check your ignored_tables list."
Expand All @@ -13,21 +31,21 @@ namespace :iron_trail do

puts "Will start tracking #{tables.length} tables."
tables.each do |table_name|
db_functions.enable_tracking_for_table(table_name)
IronTrail::RakeHelper.db_functions.enable_tracking_for_table(table_name)
end
end

desc 'Disables tracking all tables. Dangerous!'
task disable: :environment do
abort_when_unsafe!
IronTrail::RakeHelper.abort_when_unsafe!

tables = db_functions.collect_tables_tracking_status[:tracked]
tables = IronTrail::RakeHelper.db_functions.collect_tables_tracking_status[:tracked]
puts "Will stop tracking #{tables.length} tables."
tables.each do |table_name|
db_functions.disable_tracking_for_table(table_name)
IronTrail::RakeHelper.db_functions.disable_tracking_for_table(table_name)
end

tables = db_functions.collect_tables_tracking_status[:tracked]
tables = IronTrail::RakeHelper.db_functions.collect_tables_tracking_status[:tracked]
if tables.length > 0
puts "WARNING: Something went wrong. There are still #{tables.length}" + \
" tables being tracked."
Expand All @@ -38,7 +56,7 @@ namespace :iron_trail do

desc 'Shows which tables are tracking, missing and ignored.'
task status: :environment do
status = db_functions.collect_tables_tracking_status
status = IronTrail::RakeHelper.db_functions.collect_tables_tracking_status
ignored = (IronTrail.config.ignored_tables || [])

# We likely want to keep this structure of text untouched as someone
Expand All @@ -62,21 +80,5 @@ namespace :iron_trail do
puts "\t#{table_name}"
end
end

private

def db_functions
@db_functions ||= IronTrail::DbFunctions.new(ActiveRecord::Base.connection)
end

def abort_when_unsafe!
run_unsafe = %w[true 1 yes].include?(ENV['IRONTRAIL_RUN_UNSAFE'])
return unless Rails.env.production? && !run_unsafe

puts "Aborting: operation is dangerous in a production environment. " + \
"Override this behavior by setting the IRONTRAIL_RUN_UNSAFE=1 env var."

exit(1)
end
end
end

0 comments on commit e331153

Please sign in to comment.