Class: MysqlFramework::Scripts::Manager
- Inherits:
-
Object
- Object
- MysqlFramework::Scripts::Manager
- Defined in:
- lib/mysql_framework/scripts/manager.rb
Class Method Summary collapse
Instance Method Summary collapse
- #all_tables ⇒ Object
- #apply_by_tag(tags) ⇒ Object
- #calculate_pending_scripts(executed_scripts = []) ⇒ Object
- #drop_all_tables ⇒ Object
- #drop_script_history ⇒ Object
- #drop_table(table_name) ⇒ Object
- #execute ⇒ Object
-
#initialize(mysql_connector) ⇒ Manager
constructor
A new instance of Manager.
- #initialize_script_history ⇒ Object
- #retrieve_executed_scripts ⇒ Object
- #table_exists?(table_name) ⇒ Boolean
Constructor Details
#initialize(mysql_connector) ⇒ Manager
Returns a new instance of Manager.
6 7 8 |
# File 'lib/mysql_framework/scripts/manager.rb', line 6 def initialize(mysql_connector) @mysql_connector = mysql_connector end |
Class Method Details
.all_tables ⇒ Object
107 108 109 |
# File 'lib/mysql_framework/scripts/manager.rb', line 107 def self.all_tables @all_tables ||= [] end |
Instance Method Details
#all_tables ⇒ Object
103 104 105 |
# File 'lib/mysql_framework/scripts/manager.rb', line 103 def all_tables self.class.all_tables end |
#apply_by_tag(tags) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mysql_framework/scripts/manager.rb', line 31 def apply_by_tag() lock_manager.lock(self.class, migration_ttl) do |locked| raise unless locked 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. & ).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 = []) ⇒ Object
74 75 76 77 78 |
# File 'lib/mysql_framework/scripts/manager.rb', line 74 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 ⇒ Object
88 89 90 91 |
# File 'lib/mysql_framework/scripts/manager.rb', line 88 def drop_all_tables drop_script_history all_tables.each { |table| drop_table(table) } end |
#drop_script_history ⇒ Object
93 94 95 |
# File 'lib/mysql_framework/scripts/manager.rb', line 93 def drop_script_history drop_table(migration_table_name) end |
#drop_table(table_name) ⇒ Object
97 98 99 100 101 |
# File 'lib/mysql_framework/scripts/manager.rb', line 97 def drop_table(table_name) mysql_connector.query(<<~SQL) DROP TABLE IF EXISTS `#{table_name}` SQL end |
#execute ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mysql_framework/scripts/manager.rb', line 10 def execute lock_manager.lock(self.class, migration_ttl) do |locked| raise unless locked 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 ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mysql_framework/scripts/manager.rb', line 61 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 ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/mysql_framework/scripts/manager.rb', line 51 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) ⇒ Boolean
80 81 82 83 84 85 86 |
# File 'lib/mysql_framework/scripts/manager.rb', line 80 def table_exists?(table_name) result = mysql_connector.query(<<~SQL) SHOW TABLES LIKE '#{table_name}' SQL result.count == 1 end |