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

Add custom checks for TableDefinitions #264

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
52 changes: 29 additions & 23 deletions lib/strong_migrations.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
# dependencies
require "active_support"
require 'active_support'

# adapters
require_relative "strong_migrations/adapters/abstract_adapter"
require_relative "strong_migrations/adapters/mysql_adapter"
require_relative "strong_migrations/adapters/mariadb_adapter"
require_relative "strong_migrations/adapters/postgresql_adapter"
require_relative 'strong_migrations/adapters/abstract_adapter'
require_relative 'strong_migrations/adapters/mysql_adapter'
require_relative 'strong_migrations/adapters/mariadb_adapter'
require_relative 'strong_migrations/adapters/postgresql_adapter'

# modules
require_relative "strong_migrations/checks"
require_relative "strong_migrations/safe_methods"
require_relative "strong_migrations/checker"
require_relative "strong_migrations/database_tasks"
require_relative "strong_migrations/migration"
require_relative "strong_migrations/migrator"
require_relative "strong_migrations/version"
require_relative 'strong_migrations/checks'
require_relative 'strong_migrations/safe_methods'
require_relative 'strong_migrations/checker'
require_relative 'strong_migrations/database_tasks'
require_relative 'strong_migrations/migration'
require_relative 'strong_migrations/migrator'
require_relative 'strong_migrations/version'
require_relative 'strong_migrations/table_definition'

# integrations
require_relative "strong_migrations/railtie" if defined?(Rails)
require_relative 'strong_migrations/railtie' if defined?(Rails)

module StrongMigrations
class Error < StandardError; end
class UnsafeMigration < Error; end
class UnsupportedVersion < Error; end

class << self
attr_accessor :auto_analyze, :start_after, :checks, :error_messages,
:target_postgresql_version, :target_mysql_version, :target_mariadb_version,
:enabled_checks, :lock_timeout, :statement_timeout, :check_down, :target_version,
:safe_by_default, :target_sql_mode, :lock_timeout_retries, :lock_timeout_retry_delay,
:alphabetize_schema
attr_accessor :auto_analyze, :start_after, :checks, :table_checks, :error_messages,
:target_postgresql_version, :target_mysql_version, :target_mariadb_version,
:enabled_checks, :lock_timeout, :statement_timeout, :check_down, :target_version,
:safe_by_default, :target_sql_mode, :lock_timeout_retries, :lock_timeout_retry_delay,
:alphabetize_schema
attr_writer :lock_timeout_limit
end
self.auto_analyze = false
self.start_after = 0
self.lock_timeout_retries = 0
self.lock_timeout_retry_delay = 10 # seconds
self.checks = []
self.table_checks = []
self.safe_by_default = false
self.check_down = false
self.alphabetize_schema = false

# private
def self.developer_env?
env == "development" || env == "test"
env == 'development' || env == 'test'
end

# private
Expand All @@ -52,7 +54,7 @@ def self.env
Rails.env
else
# default to production for safety
ENV["RACK_ENV"] || "production"
ENV['RACK_ENV'] || 'production'
end
end

Expand All @@ -63,12 +65,16 @@ def self.lock_timeout_limit
@lock_timeout_limit
end

def self.add_table_check(&block)
table_checks << block
end

def self.add_check(&block)
checks << block
end

def self.enable_check(check, start_after: nil)
enabled_checks[check] = {start_after: start_after}
enabled_checks[check] = { start_after: start_after }
end

def self.disable_check(check)
Expand All @@ -86,7 +92,7 @@ def self.check_enabled?(check, version: nil)
end

# load error messages
require_relative "strong_migrations/error_messages"
require_relative 'strong_migrations/error_messages'

ActiveSupport.on_load(:active_record) do
ActiveRecord::Migration.prepend(StrongMigrations::Migration)
Expand All @@ -96,6 +102,6 @@ def self.check_enabled?(check, version: nil)
ActiveRecord::Tasks::DatabaseTasks.singleton_class.prepend(StrongMigrations::DatabaseTasks)
end

require_relative "strong_migrations/schema_dumper"
require_relative 'strong_migrations/schema_dumper'
ActiveRecord::SchemaDumper.prepend(StrongMigrations::SchemaDumper)
end
41 changes: 16 additions & 25 deletions lib/strong_migrations/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ def self.safety_assured
end
end

def perform(method, *args)
def perform(method, *args, &block)
check_version_supported
set_timeouts
check_lock_timeout

if !safe? || safe_by_default_method?(method)
# TODO better pattern
# TODO: better pattern
# see checks.rb for methods
case method
when :add_check_constraint
Expand Down Expand Up @@ -83,7 +83,7 @@ def perform(method, *args)
@committed = true
end

if !safe?
unless safe?
# custom checks
StrongMigrations.checks.each do |check|
@migration.instance_exec(method, args, &check)
Expand All @@ -93,18 +93,16 @@ def perform(method, *args)

result =
if retry_lock_timeouts?(method)
# TODO figure out how to handle methods that generate multiple statements
# TODO: figure out how to handle methods that generate multiple statements
# like add_reference(table, ref, index: {algorithm: :concurrently})
# lock timeout after first statement will cause retry to fail
retry_lock_timeouts { yield }
retry_lock_timeouts(&block)
else
yield
end

# outdated statistics + a new index can hurt performance of existing queries
if StrongMigrations.auto_analyze && direction == :up && method == :add_index
adapter.analyze_table(args[0])
end
adapter.analyze_table(args[0]) if StrongMigrations.auto_analyze && direction == :up && method == :add_index

result
end
Expand All @@ -129,6 +127,10 @@ def version_safe?
version && version <= StrongMigrations.start_after
end

def safe?
self.class.safe || ENV['SAFETY_ASSURED'] || (direction == :down && !StrongMigrations.check_down) || version_safe? || @migration.reverting?
end

private

def check_version_supported
Expand All @@ -138,7 +140,8 @@ def check_version_supported
if min_version
version = adapter.server_version
if version < Gem::Version.new(min_version)
raise UnsupportedVersion, "#{adapter.name} version (#{version}) not supported in this version of Strong Migrations (#{StrongMigrations::VERSION})"
raise UnsupportedVersion,
"#{adapter.name} version (#{version}) not supported in this version of Strong Migrations (#{StrongMigrations::VERSION})"
end
end

Expand All @@ -148,30 +151,20 @@ def check_version_supported
def set_timeouts
return if @timeouts_set

if StrongMigrations.statement_timeout
adapter.set_statement_timeout(StrongMigrations.statement_timeout)
end
if StrongMigrations.lock_timeout
adapter.set_lock_timeout(StrongMigrations.lock_timeout)
end
adapter.set_statement_timeout(StrongMigrations.statement_timeout) if StrongMigrations.statement_timeout
adapter.set_lock_timeout(StrongMigrations.lock_timeout) if StrongMigrations.lock_timeout

@timeouts_set = true
end

def check_lock_timeout
return if defined?(@lock_timeout_checked)

if StrongMigrations.lock_timeout_limit
adapter.check_lock_timeout(StrongMigrations.lock_timeout_limit)
end
adapter.check_lock_timeout(StrongMigrations.lock_timeout_limit) if StrongMigrations.lock_timeout_limit

@lock_timeout_checked = true
end

def safe?
self.class.safe || ENV["SAFETY_ASSURED"] || (direction == :down && !StrongMigrations.check_down) || version_safe? || @migration.reverting?
end

def version
@migration.version
end
Expand Down Expand Up @@ -201,11 +194,9 @@ def connection
end

def retry_lock_timeouts?(method)
(
StrongMigrations.lock_timeout_retries > 0 &&
StrongMigrations.lock_timeout_retries > 0 &&
!in_transaction? &&
method != :transaction
)
end
end
end
Loading