class MysqlFramework::Scripts::Manager
Attributes
mysql_connector[R]
Public Class Methods
all_tables()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 103 def self.all_tables @all_tables ||= [] end
new(mysql_connector)
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 6 def initialize(mysql_connector) @mysql_connector = mysql_connector end
Public Instance Methods
all_tables()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 99 def all_tables self.class.all_tables end
apply_by_tag(tags)
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 29 def apply_by_tag(tags) lock_manager.with_lock(key: self.class) do initialize_script_history mysql_connector.transaction do |client| pending_scripts = calculate_pending_scripts MysqlFramework.logger.info do "[#{self.class}] - #{pending_scripts.length} pending data store scripts found." end pending_scripts.reject { |script| (script.tags & tags).empty? }.sort_by(&:identifier) .each { |script| apply(script, client) } end MysqlFramework.logger.debug { "[#{self.class}] - Migration script execution complete." } end end
calculate_pending_scripts(executed_scripts = [])
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 70 def calculate_pending_scripts(executed_scripts = []) MysqlFramework.logger.debug { "[#{self.class}] - Calculating pending data store scripts." } migrations.map(&:new).reject { |script| executed_scripts.include?(script.identifier) }.sort_by(&:identifier) end
drop_all_tables()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 84 def drop_all_tables drop_script_history all_tables.each { |table| drop_table(table) } end
drop_script_history()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 89 def drop_script_history drop_table(migration_table_name) end
drop_table(table_name)
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 93 def drop_table(table_name) mysql_connector.query(<<~SQL) DROP TABLE IF EXISTS `#{table_name}` SQL end
execute()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 10 def execute lock_manager.with_lock(key: self.class) do initialize_script_history executed_scripts = retrieve_executed_scripts mysql_connector.transaction do |client| pending_scripts = calculate_pending_scripts(executed_scripts) MysqlFramework.logger.info do "[#{self.class}] - #{pending_scripts.length} pending data store scripts found." end pending_scripts.each { |script| apply(script, client) } end MysqlFramework.logger.debug { "[#{self.class}] - Migration script execution complete." } end end
initialize_script_history()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 57 def initialize_script_history MysqlFramework.logger.debug { "[#{self.class}] - Initializing script history." } mysql_connector.query(<<~SQL) CREATE TABLE IF NOT EXISTS `#{migration_table_name}` ( `identifier` CHAR(15) NOT NULL, `timestamp` DATETIME NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`identifier`), UNIQUE INDEX `identifier_UNIQUE` (`identifier` ASC) ) SQL end
retrieve_executed_scripts()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 47 def retrieve_executed_scripts MysqlFramework.logger.debug { "[#{self.class}] - Retrieving last executed script from history." } results = mysql_connector.query(<<~SQL) SELECT `identifier` FROM `#{migration_table_name}` ORDER BY `identifier` DESC SQL results.to_a.map { |result| result[:identifier]&.to_i } end
table_exists?(table_name)
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 76 def table_exists?(table_name) result = mysql_connector.query(<<~SQL) SHOW TABLES LIKE '#{table_name}' SQL result.count == 1 end
Private Instance Methods
apply(script, client)
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 123 def apply(script, client) MysqlFramework.logger.info { "[#{self.class}] - Applying script: #{script}." } script.apply(client) client.query(<<~SQL) INSERT INTO `#{migration_table_name}` (`identifier`, `timestamp`) VALUES ('#{script.identifier}', NOW()) SQL end
lock_manager()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 111 def lock_manager @lock_manager ||= MysqlFramework::Scripts::LockManager.new end
migration_table_name()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 115 def migration_table_name @migration_table_name ||= ENV.fetch('MYSQL_MIGRATION_TABLE', 'migration_script_history') end
migrations()
click to toggle source
# File lib/mysql_framework/scripts/manager.rb, line 119 def migrations @migrations ||= MysqlFramework::Scripts::Base.descendants end