File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
lib/clickhouse-activerecord Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments