Skip to content

Commit f521559

Browse files
authored
Add check_current_protected_environment! to Tasks. (#222)
1 parent b409f19 commit f521559

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

lib/clickhouse-activerecord/tasks.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,34 @@ def migrate
8888
ActiveRecord::Migration.verbose = verbose_was
8989
end
9090

91+
def check_current_protected_environment!(db_config, migration_class = ActiveRecord::Migration)
92+
with_temporary_pool(db_config, migration_class) do |pool|
93+
migration_context = pool.migration_context
94+
current = migration_context.current_environment
95+
stored = migration_context.last_stored_environment
96+
97+
if migration_context.protected_environment?
98+
raise ActiveRecord::ProtectedEnvironmentError.new(stored)
99+
end
100+
101+
if stored && stored != current
102+
raise ActiveRecord::EnvironmentMismatchError.new(current: current, stored: stored)
103+
end
104+
rescue ActiveRecord::NoDatabaseError
105+
end
106+
end
107+
91108
private
92109

110+
def with_temporary_pool(db_config, migration_class, clobber: false)
111+
original_db_config = migration_class.connection_db_config
112+
pool = migration_class.connection_handler.establish_connection(db_config, clobber: clobber)
113+
114+
yield pool
115+
ensure
116+
migration_class.connection_handler.establish_connection(original_db_config, clobber: clobber)
117+
end
118+
93119
def establish_master_connection
94120
establish_connection @configuration
95121
end

0 commit comments

Comments
 (0)