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(last_executed_script) ⇒ Object
- #drop_all_tables ⇒ Object
- #drop_script_history ⇒ Object
- #drop_table(table_name) ⇒ Object
- #execute ⇒ Object
- #initialize_script_history ⇒ Object
- #retrieve_last_executed_script ⇒ Object
- #table_exists?(table_name) ⇒ Boolean
Class Method Details
.all_tables ⇒ Object
103 104 105 |
# File 'lib/mysql_framework/scripts/manager.rb', line 103 def self.all_tables @all_tables ||= [] end |
Instance Method Details
#all_tables ⇒ Object
99 100 101 |
# File 'lib/mysql_framework/scripts/manager.rb', line 99 def all_tables self.class.all_tables end |
#apply_by_tag(tags) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mysql_framework/scripts/manager.rb', line 25 def apply_by_tag() lock_manager.lock(self.class, 2000) do |locked| raise unless locked initialize_script_history mysql_connector.transaction do pending_scripts = calculate_pending_scripts(0) MysqlFramework.logger.info { "[#{self.class}] - #{pending_scripts.length} pending data store scripts found." } pending_scripts.reject { |script| (script. & ).empty? }.sort_by(&:identifier) .each { |script| apply(script) } end MysqlFramework.logger.info { "[#{self.class}] - Migration script execution complete." } end end |
#calculate_pending_scripts(last_executed_script) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/mysql_framework/scripts/manager.rb', line 75 def calculate_pending_scripts(last_executed_script) MysqlFramework.logger.info { "[#{self.class}] - Calculating pending data store scripts." } MysqlFramework::Scripts::Base.descendants.map(&:new) .select { |script| script.identifier > last_executed_script }.sort_by(&:identifier) end |
#drop_all_tables ⇒ Object
43 44 45 46 |
# File 'lib/mysql_framework/scripts/manager.rb', line 43 def drop_all_tables drop_script_history all_tables.each { |table| drop_table(table) } end |
#drop_script_history ⇒ Object
89 90 91 |
# File 'lib/mysql_framework/scripts/manager.rb', line 89 def drop_script_history drop_table(migration_table_name) end |
#drop_table(table_name) ⇒ Object
93 94 95 96 97 |
# 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 ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mysql_framework/scripts/manager.rb', line 6 def execute lock_manager.lock(self.class, 2000) do |locked| raise unless locked initialize_script_history last_executed_script = retrieve_last_executed_script mysql_connector.transaction do pending_scripts = calculate_pending_scripts(last_executed_script) MysqlFramework.logger.info { "[#{self.class}] - #{pending_scripts.length} pending data store scripts found." } pending_scripts.each { |script| apply(script) } end MysqlFramework.logger.info { "[#{self.class}] - Migration script execution complete." } end end |
#initialize_script_history ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql_framework/scripts/manager.rb', line 62 def initialize_script_history MysqlFramework.logger.info { "[#{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_last_executed_script ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mysql_framework/scripts/manager.rb', line 48 def retrieve_last_executed_script MysqlFramework.logger.info { "[#{self.class}] - Retrieving last executed script from history." } result = mysql_connector.query(<<~SQL) SELECT `identifier` FROM #{migration_table_name} ORDER BY `identifier` DESC SQL if result.each.to_a.length.zero? 0 else Integer(result.first[:identifier]) end end |
#table_exists?(table_name) ⇒ Boolean
82 83 84 85 86 87 |
# File 'lib/mysql_framework/scripts/manager.rb', line 82 def table_exists?(table_name) result = mysql_connector.query(<<~SQL) SHOW TABLES LIKE '#{table_name}' SQL result.count == 1 end |