These are the code examples referenced in our blog post Our Plan for Zero-Downtime Database Transitions with Rails: Part 2.
Last active
March 5, 2019 17:02
-
-
Save jason-o-matic/6676c3409f7d25db3a43097e0333b5b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
after_commit :write_active_migrator_destination_model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def write_active_migrator_destination_model! | |
if ActiveMigrator.config.enabled_checker.call(id) | |
ActiveMigrator.config.metric_incrementer.call("active_migrator.#{active_migrator_source_klass.name}.write_active_migrator_destination_model") | |
original_record = find_original_model | |
if original_record | |
active_migrator_destination_model = original_record.find_active_migrator_destination_model | |
original_record.transformed_attributes_for_active_migrator.each do |name, value| | |
active_migrator_destination_model.send(:write_attribute, name, value) | |
end | |
active_migrator_destination_model.save! | |
else | |
active_migrator_destination_model = find_active_migrator_destination_model | |
active_migrator_destination_model.destroy unless active_migrator_destination_model.new_record? | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveMigrator | |
class ActiveMigratorDestinationModel < ActiveRecord::Base | |
self.abstract_class = true | |
# disable STI for this "copy" model | |
self.inheritance_column = :_type_disabled | |
establish_connection DB_ACTIVE_MIGRATOR_CONFIGURATION[:destination] | |
end | |
end | |
module ActiveMigrator | |
module ActiveMigratorSource | |
extend ActiveSupport::Concern | |
included do | |
@active_migrator_destination_klass_name = "ActiveMigrator::#{self.name}ActiveMigratorDestinationModel" | |
eval <<-STR, nil, __FILE__, __LINE__ | |
ActiveMigrator.send(:remove_const, :#{self.name}ActiveMigratorDestinationModel) if defined?(#{@active_migrator_destination_klass_name}) | |
class #{@active_migrator_destination_klass_name} < ActiveMigrator::ActiveMigratorDestinationModel | |
self.table_name = '#{table_name}' | |
self.record_timestamps = false | |
self.lock_optimistically = false | |
end | |
STR | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
after_commit :write_active_migrator_destination_model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def write_active_migrator_destination_model | |
begin | |
write_active_migrator_destination_model! | |
rescue Exception => e | |
ActiveMigrator.config.exception_handler.call(e, active_migrator_source_class: active_migrator_source_klass.name, active_migrator_source_id: id) | |
end | |
true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment