Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depollute rake global scope #7

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading