Module: AfterMigrate::Executor

Extended by:
Executor
Included in:
Executor
Defined in:
lib/after_migrate/executor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_tables(schema:) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/after_migrate/executor.rb', line 66

def all_tables(schema:)
  connection = ActiveRecord::Base.connection
  case connection.adapter_name
  when 'PostgreSQL'
    AfterMigrate::Postgresql.all_tables(schema:)
  when 'SQLite'
    AfterMigrate::Sqlite.all_tables(schema:)
  when 'Mysql2', 'Trilogy'
    AfterMigrate::Mysql.all_tables(schema:)
  else
    []
  end
end

.call(schema: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/after_migrate/executor.rb', line 14

def call(schema: nil)
  tables = target_tables
  return if tables.blank?

  if schema.present?
    run_optimize(schema:, tables: tables[schema]) if tables[schema].present?
  else
    tables.each { |s, t| run_optimize(schema: s, tables: t) }
  end
ensure
  AfterMigrate.reset!
end

.optimize_tables(schema:, table_names:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/after_migrate/executor.rb', line 39

def optimize_tables(schema:, table_names:)
  connection = ActiveRecord::Base.connection
  case connection.adapter_name
  when 'PostgreSQL'
    AfterMigrate::Postgresql.optimize_tables(schema:, table_names:, connection:)
  when 'SQLite'
    AfterMigrate::Sqlite.optimize_tables(schema:, table_names:, connection:)
  when 'Mysql2', 'Trilogy'
    AfterMigrate::Mysql.optimize_tables(schema:, table_names:, connection:)
  else
    AfterMigrate.log("No maintenance implemented for #{connection.adapter_name}")
  end
end

.run_optimize(schema:, tables:) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/after_migrate/executor.rb', line 31

def run_optimize(schema:, tables:)
  table_names = tables.to_a.sort
  return if table_names.empty?

  AfterMigrate.log("Migration touched #{table_names.size} table(s) in schema #{schema.inspect}: #{table_names.join(', ')}")
  optimize_tables(schema:, table_names:)
end

.target_tablesObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/after_migrate/executor.rb', line 53

def target_tables
  case AfterMigrate.configuration.analyze
  when 'all_tables'
    AfterMigrate.affected_tables.keys.each_with_object({}) do |schema, hash|
      hash[schema] = all_tables(schema:)
    end
  when 'only_affected_tables'
    AfterMigrate.affected_tables
  else
    {}
  end
end

Instance Method Details

#call(schema: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/after_migrate/executor.rb', line 14

def call(schema: nil)
  tables = target_tables
  return if tables.blank?

  if schema.present?
    run_optimize(schema:, tables: tables[schema]) if tables[schema].present?
  else
    tables.each { |s, t| run_optimize(schema: s, tables: t) }
  end
ensure
  AfterMigrate.reset!
end