Skip to content
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
26 changes: 26 additions & 0 deletions lib/clickhouse-activerecord/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,34 @@ def migrate
ActiveRecord::Migration.verbose = verbose_was
end

def check_current_protected_environment!(db_config, migration_class = ActiveRecord::Migration)
with_temporary_pool(db_config, migration_class) do |pool|
migration_context = pool.migration_context
current = migration_context.current_environment
stored = migration_context.last_stored_environment

if migration_context.protected_environment?
raise ActiveRecord::ProtectedEnvironmentError.new(stored)
end

if stored && stored != current
raise ActiveRecord::EnvironmentMismatchError.new(current: current, stored: stored)
end
rescue ActiveRecord::NoDatabaseError
end
end

private

def with_temporary_pool(db_config, migration_class, clobber: false)
original_db_config = migration_class.connection_db_config
pool = migration_class.connection_handler.establish_connection(db_config, clobber: clobber)

yield pool
ensure
migration_class.connection_handler.establish_connection(original_db_config, clobber: clobber)
end

def establish_master_connection
establish_connection @configuration
end
Expand Down