Module: SafePgMigrations

Defined in:
lib/safe-pg-migrations/base.rb,
lib/safe-pg-migrations/railtie.rb,
lib/safe-pg-migrations/version.rb,
lib/safe-pg-migrations/configuration.rb,
lib/safe-pg-migrations/helpers/logger.rb,
lib/safe-pg-migrations/helpers/batch_over.rb,
lib/safe-pg-migrations/helpers/index_helper.rb,
lib/safe-pg-migrations/helpers/satisfied_helper.rb,
lib/safe-pg-migrations/helpers/statements_helper.rb,
lib/safe-pg-migrations/plugins/statement_insurer.rb,
lib/safe-pg-migrations/plugins/statement_retrier.rb,
lib/safe-pg-migrations/plugins/verbose_sql_logger.rb,
lib/safe-pg-migrations/plugins/idempotent_statements.rb,
lib/safe-pg-migrations/plugins/blocking_activity_logger.rb,
lib/safe-pg-migrations/plugins/useless_statements_logger.rb,
lib/safe-pg-migrations/helpers/blocking_activity_selector.rb,
lib/safe-pg-migrations/helpers/session_setting_management.rb,
lib/safe-pg-migrations/helpers/blocking_activity_formatter.rb,
lib/safe-pg-migrations/polyfills/index_definition_polyfill.rb,
lib/safe-pg-migrations/plugins/statement_insurer/add_column.rb,
lib/safe-pg-migrations/plugins/strong_migrations_integration.rb,
lib/safe-pg-migrations/polyfills/verbose_query_logs_polyfill.rb,
lib/safe-pg-migrations/plugins/statement_insurer/change_column_null.rb,
lib/safe-pg-migrations/plugins/statement_insurer/remove_column_index.rb

Defined Under Namespace

Modules: BlockingActivityLogger, Helpers, IdempotentStatements, Migration, Polyfills, StatementInsurer, StatementRetrier, StrongMigrationsIntegration, UselessStatementsLogger Classes: Configuration, Railtie, VerboseSqlLogger

Constant Summary collapse

PLUGINS =

Order matters: the bottom-most plugin will have precedence

[
  BlockingActivityLogger,
  IdempotentStatements,
  StatementRetrier,
  StatementInsurer,
  UselessStatementsLogger,
  Polyfills::IndexDefinitionPolyfill,
].freeze
VERSION =
'3.1.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_migrationObject (readonly)

Returns the value of attribute current_migration.



35
36
37
# File 'lib/safe-pg-migrations/base.rb', line 35

def current_migration
  @current_migration
end

.pg_version_numObject (readonly)

Returns the value of attribute pg_version_num.



35
36
37
# File 'lib/safe-pg-migrations/base.rb', line 35

def pg_version_num
  @pg_version_num
end

Class Method Details

.alternate_connectionObject



65
66
67
# File 'lib/safe-pg-migrations/base.rb', line 65

def alternate_connection
  @alternate_connection ||= ActiveRecord::Base.connection_pool.send(:new_connection)
end

.close_alternate_connectionObject



69
70
71
72
73
74
# File 'lib/safe-pg-migrations/base.rb', line 69

def close_alternate_connection
  return unless @alternate_connection

  @alternate_connection.disconnect!
  @alternate_connection = nil
end

.configObject



86
87
88
# File 'lib/safe-pg-migrations/base.rb', line 86

def config
  @config ||= Configuration.new
end

.get_pg_version_num(connection) ⇒ Object



90
91
92
# File 'lib/safe-pg-migrations/base.rb', line 90

def get_pg_version_num(connection)
  connection.query_value('SHOW server_version_num').to_i
end

.setup_and_teardown(migration, connection, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/safe-pg-migrations/base.rb', line 37

def setup_and_teardown(migration, connection, &block)
  @pg_version_num = get_pg_version_num(connection)
  @alternate_connection = nil

  with_current_migration(migration) do
    stdout_sql_logger = VerboseSqlLogger.new.setup if verbose?

    VerboseSqlLogger.new.setup if verbose?
    PLUGINS.each { |plugin| connection.extend(plugin) }

    connection.with_setting :lock_timeout, SafePgMigrations.config.pg_lock_timeout do
      connection.with_setting :statement_timeout, SafePgMigrations.config.pg_statement_timeout, &block
    end
  ensure
    stdout_sql_logger&.teardown
  end
ensure
  close_alternate_connection
end

.verbose?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
# File 'lib/safe-pg-migrations/base.rb', line 76

def verbose?
  unless current_migration.class._safe_pg_migrations_verbose.nil?
    return current_migration.class._safe_pg_migrations_verbose
  end
  return ENV['SAFE_PG_MIGRATIONS_VERBOSE'] == '1' if ENV['SAFE_PG_MIGRATIONS_VERBOSE']
  return Rails.env.production? if defined?(Rails)

  false
end

.with_current_migration(migration, &block) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/safe-pg-migrations/base.rb', line 57

def with_current_migration(migration, &block)
  @current_migration = migration

  yield block
ensure
  @current_migration = nil
end