Skip to content

Commit

Permalink
Remove SafePgMigrations from module calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Hareau committed Jul 4, 2023
1 parent e467696 commit d3d9a0d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
14 changes: 7 additions & 7 deletions lib/safe-pg-migrations/helpers/blocking_activity_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ module Helpers
module BlockingActivityFormatter
def log_queries(queries)
if queries.empty?
SafePgMigrations::Helpers::Logger.say 'Could not find any blocking query.', sub_item: true
Logger.say 'Could not find any blocking query.', sub_item: true
else
SafePgMigrations::Helpers::Logger.say <<~MESSAGE.rstrip, sub_item: true
Logger.say <<~MESSAGE.rstrip, sub_item: true
Statement was being blocked by the following #{'query'.pluralize(queries.size)}:
MESSAGE

SafePgMigrations::Helpers::Logger.say '', sub_item: true
Logger.say '', sub_item: true
output_blocking_queries(queries)
SafePgMigrations::Helpers::Logger.say <<~MESSAGE, sub_item: true
Logger.say <<~MESSAGE, sub_item: true
Beware, some of those queries might run in a transaction. In this case the locking query might be located
elsewhere in the transaction
MESSAGE

SafePgMigrations::Helpers::Logger.say '', sub_item: true
Logger.say '', sub_item: true
end
end

Expand All @@ -27,7 +27,7 @@ def log_queries(queries)
def output_blocking_queries(queries)
if SafePgMigrations.config.blocking_activity_logger_verbose
queries.each do |pid, query, start_time|
SafePgMigrations::Helpers::Logger.say(
Logger.say(
"Query with pid #{pid || 'null'} started #{format_start_time start_time}: #{query}",
sub_item: true
)
Expand All @@ -39,7 +39,7 @@ def output_blocking_queries(queries)

def output_confidentially_blocking_queries(queries)
queries.each do |start_time, locktype, mode, pid, transactionid|
SafePgMigrations::Helpers::Logger.say(
Logger.say(
"Query with pid #{pid || 'null'} " \
"started #{format_start_time(start_time)}: " \
"lock type: #{locktype || 'null'}, " \
Expand Down
8 changes: 4 additions & 4 deletions lib/safe-pg-migrations/plugins/blocking_activity_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

module SafePgMigrations
module BlockingActivityLogger
include ::SafePgMigrations::Helpers::BlockingActivityFormatter
include ::SafePgMigrations::Helpers::BlockingActivitySelector
include Helpers::BlockingActivityFormatter
include Helpers::BlockingActivitySelector

%i[
add_column
Expand Down Expand Up @@ -63,12 +63,12 @@ def log_blocking_queries_after_lock

blocking_queries_retriever_thread.kill
rescue ActiveRecord::LockWaitTimeout
SafePgMigrations::Helpers::Logger.say 'Lock timeout.', sub_item: true
Helpers::Logger.say 'Lock timeout.', sub_item: true
queries =
begin
blocking_queries_retriever_thread.value
rescue StandardError => e
SafePgMigrations::Helpers::Logger.say(
Helpers::Logger.say(
"Error while retrieving blocking queries: #{e}",
sub_item: true
)
Expand Down
28 changes: 14 additions & 14 deletions lib/safe-pg-migrations/plugins/idempotent_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module SafePgMigrations
module IdempotentStatements
include SafePgMigrations::Helpers::IndexHelper
include Helpers::IndexHelper

ruby2_keywords def add_index(table_name, column_name, *args)
options = args.last.is_a?(Hash) ? args.last : {}
Expand All @@ -12,7 +12,7 @@ module IdempotentStatements
return super unless index_name_exists?(index_definition.table, index_definition.name)

if index_valid?(index_definition.name)
SafePgMigrations::Helpers::Logger.say(
Helpers::Logger.say(
"/!\\ Index '#{index_definition.name}' already exists in '#{table_name}'. Skipping statement.",
sub_item: true
)
Expand All @@ -26,7 +26,7 @@ module IdempotentStatements
ruby2_keywords def add_column(table_name, column_name, type, *)
return super unless column_exists?(table_name, column_name)

SafePgMigrations::Helpers::Logger.say(
Helpers::Logger.say(
"/!\\ Column '#{column_name}' already exists in '#{table_name}'. Skipping statement.",
sub_item: true
)
Expand All @@ -35,7 +35,7 @@ module IdempotentStatements
ruby2_keywords def remove_column(table_name, column_name, type = nil, *)
return super if column_exists?(table_name, column_name)

SafePgMigrations::Helpers::Logger.say(
Helpers::Logger.say(
"/!\\ Column '#{column_name}' not found on table '#{table_name}'. Skipping statement.", sub_item: true
)
end
Expand All @@ -46,7 +46,7 @@ module IdempotentStatements

return super if index_name_exists?(table_name, index_name)

SafePgMigrations::Helpers::Logger.say(
Helpers::Logger.say(
"/!\\ Index '#{index_name}' not found on table '#{table_name}'. Skipping statement.", sub_item: true
)
end
Expand All @@ -56,7 +56,7 @@ module IdempotentStatements
suboptions = options.slice(:name, :column)
return super unless foreign_key_exists?(from_table, suboptions.present? ? nil : to_table, **suboptions)

SafePgMigrations::Helpers::Logger.say(
Helpers::Logger.say(
"/!\\ Foreign key '#{from_table}' -> '#{to_table}' already exists. Skipping statement.",
sub_item: true
)
Expand All @@ -66,7 +66,7 @@ def remove_foreign_key(from_table, to_table = nil, **options)
return super if foreign_key_exists?(from_table, to_table, **options)

reference_name = to_table || options[:to_table] || options[:column] || options[:name]
SafePgMigrations::Helpers::Logger.say(
Helpers::Logger.say(
"/!\\ Foreign key '#{from_table}' -> '#{reference_name}' does not exist. Skipping statement.",
sub_item: true
)
Expand All @@ -76,14 +76,14 @@ def remove_foreign_key(from_table, to_table = nil, **options)
options = args.last.is_a?(Hash) ? args.last : {}
return super if options[:force] || !table_exists?(table_name)

SafePgMigrations::Helpers::Logger.say "/!\\ Table '#{table_name}' already exists.", sub_item: true
Helpers::Logger.say "/!\\ Table '#{table_name}' already exists.", sub_item: true

td = create_table_definition(table_name, *args)

yield td if block_given?

SafePgMigrations::Helpers::Logger.say(td.indexes.empty? ? '-- Skipping statement' : '-- Creating indexes',
sub_item: true)
Helpers::Logger.say(td.indexes.empty? ? '-- Skipping statement' : '-- Creating indexes',
sub_item: true)

td.indexes.each do |column_name, index_options|
add_index(table_name, column_name, **index_options)
Expand All @@ -96,7 +96,7 @@ def add_check_constraint(table_name, expression, **options)

return super if constraint_definition.nil?

SafePgMigrations::Helpers::Logger.say <<~MESSAGE, sub_item: true
Helpers::Logger.say <<~MESSAGE, sub_item: true
/!\\ Constraint '#{constraint_definition.name}' already exists. Skipping statement.
MESSAGE
end
Expand All @@ -106,7 +106,7 @@ def change_column_null(table_name, column_name, null, *)

return super if column.null != null

SafePgMigrations::Helpers::Logger.say <<~MESSAGE, sub_item: true
Helpers::Logger.say <<~MESSAGE, sub_item: true
/!\\ Column '#{table_name}.#{column.name}' is already set to 'null: #{null}'. Skipping statement.
MESSAGE
end
Expand All @@ -116,7 +116,7 @@ def validate_check_constraint(table_name, **options)

return super unless constraint_definition.validated?

SafePgMigrations::Helpers::Logger.say <<~MESSAGE, sub_item: true
Helpers::Logger.say <<~MESSAGE, sub_item: true
/!\\ Constraint '#{constraint_definition.name}' already validated. Skipping statement.
MESSAGE
end
Expand All @@ -132,7 +132,7 @@ def change_column_default(table_name, column_name, default_or_changes)

return super if new_alter_statement != previous_alter_statement

SafePgMigrations::Helpers::Logger.say <<~MESSAGE, sub_item: true
Helpers::Logger.say <<~MESSAGE, sub_item: true
/!\\ Column '#{table_name}.#{column.name}' is already set to 'default: #{column.default}'. Skipping statement.
MESSAGE
end
Expand Down
14 changes: 7 additions & 7 deletions lib/safe-pg-migrations/plugins/statement_insurer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def add_check_constraint(table_name, expression, **options)

options = check_constraint_options(table_name, expression, options)

SafePgMigrations::Helpers::Logger.say_method_call :add_check_constraint, table_name, expression, **options,
Helpers::Logger.say_method_call :add_check_constraint, table_name, expression, **options,
validate: false
super table_name, expression, **options, validate: false

return unless options.fetch(:validate, true)

SafePgMigrations::Helpers::Logger.say_method_call :validate_check_constraint, table_name, name: options[:name]
Helpers::Logger.say_method_call :validate_check_constraint, table_name, name: options[:name]
validate_check_constraint table_name, name: options[:name]
end

Expand Down Expand Up @@ -68,15 +68,15 @@ def add_check_constraint(table_name, expression, **options)
options[:algorithm] = :concurrently
end

SafePgMigrations::Helpers::Logger.say_method_call(:add_index, table_name, column_name, **options)
Helpers::Logger.say_method_call(:add_index, table_name, column_name, **options)

without_timeout { super(table_name, column_name, **options) }
end

ruby2_keywords def remove_index(table_name, *args)
options = args.last.is_a?(Hash) ? args.last : { column: args.last }
options[:algorithm] = :concurrently unless options.key?(:algorithm)
SafePgMigrations::Helpers::Logger.say_method_call(:remove_index, table_name, **options)
Helpers::Logger.say_method_call(:remove_index, table_name, **options)

without_timeout { super(table_name, **options) }
end
Expand All @@ -88,13 +88,13 @@ def change_column_null(table_name, column_name, null, default = nil)

add_check_constraint table_name, "#{column_name} IS NOT NULL"

SafePgMigrations::Helpers::Logger.say_method_call :change_column_null, table_name, column_name, false
Helpers::Logger.say_method_call :change_column_null, table_name, column_name, false
with_setting(:statement_timeout, SafePgMigrations.config.pg_statement_timeout) do
super table_name, column_name, false
end

SafePgMigrations::Helpers::Logger.say_method_call :remove_check_constraint, table_name,
"#{column_name} IS NOT NULL"
Helpers::Logger.say_method_call :remove_check_constraint, table_name,
"#{column_name} IS NOT NULL"
remove_check_constraint table_name, "#{column_name} IS NOT NULL"
end

Expand Down
10 changes: 5 additions & 5 deletions lib/safe-pg-migrations/plugins/statement_insurer/add_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ module AddColumn
null = options.delete(:null)

with_setting(:statement_timeout, SafePgMigrations.config.pg_statement_timeout) do
SafePgMigrations::Helpers::Logger.say_method_call(:add_column, table_name, column_name, type, options)
Helpers::Logger.say_method_call(:add_column, table_name, column_name, type, options)
super table_name, column_name, type, **options
end

SafePgMigrations::Helpers::Logger.say_method_call(:change_column_default, table_name, column_name, default)
Helpers::Logger.say_method_call(:change_column_default, table_name, column_name, default)
change_column_default(table_name, column_name, default)

SafePgMigrations::Helpers::Logger.say_method_call(:backfill_column_default, table_name, column_name)
Helpers::Logger.say_method_call(:backfill_column_default, table_name, column_name)
without_statement_timeout do
backfill_column_default(table_name, column_name)
end
Expand All @@ -34,14 +34,14 @@ module AddColumn

def should_keep_default_implementation?(default: nil, default_value_backfill: :auto, **)
default_value_backfill != :update_in_batches || !default ||
!SafePgMigrations::Helpers::SatisfiedHelper.satisfies_add_column_update_rows_backfill?
!Helpers::SatisfiedHelper.satisfies_add_column_update_rows_backfill?
end

def backfill_column_default(table_name, column_name)
model = Class.new(ActiveRecord::Base) { self.table_name = table_name }
quoted_column_name = quote_column_name(column_name)

SafePgMigrations::Helpers::BatchOver.new(model).each_batch do |batch|
Helpers::BatchOver.new(model).each_batch do |batch|
batch
.update_all("#{quoted_column_name} = DEFAULT")
sleep SafePgMigrations.config.backfill_pause
Expand Down
4 changes: 2 additions & 2 deletions lib/safe-pg-migrations/plugins/statement_retrier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def retry_if_lock_timeout
raise unless remaining_tries > 0

retry_delay = SafePgMigrations.config.retry_delay
SafePgMigrations::Helpers::Logger.say "Retrying in #{retry_delay} seconds...", sub_item: true
Helpers::Logger.say "Retrying in #{retry_delay} seconds...", sub_item: true
sleep retry_delay
SafePgMigrations::Helpers::Logger.say 'Retrying now.', sub_item: true
Helpers::Logger.say 'Retrying now.', sub_item: true
retry
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/safe-pg-migrations/plugins/useless_statements_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module SafePgMigrations
module UselessStatementsLogger
class << self
ruby2_keywords def warn_useless(action, link = nil, *args)
SafePgMigrations::Helpers::Logger.say(
Helpers::Logger.say(
"/!\\ No need to explicitly use #{action}, safe-pg-migrations does it for you", *args
)
SafePgMigrations::Helpers::Logger.say "\t see #{link} for more details", *args if link
Helpers::Logger.say "\t see #{link} for more details", *args if link
end
end

Expand Down

0 comments on commit d3d9a0d

Please sign in to comment.