Class: MysqlFramework::Scripts::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_framework/scripts/manager.rb

Class Method Summary collapse

Instance Method Summary collapse

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_tablesObject



103
104
105
# File 'lib/mysql_framework/scripts/manager.rb', line 103

def self.all_tables
  @all_tables ||= []
end

Instance Method Details

#all_tablesObject



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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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 = []) ⇒ Object



70
71
72
73
74
# 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_tablesObject



84
85
86
87
# 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_historyObject



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

#executeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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_historyObject



57
58
59
60
61
62
63
64
65
66
67
68
# 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_scriptsObject



47
48
49
50
51
52
53
54
55
# 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) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# 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