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



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/after_migrate/executor.rb', line 77

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
# File 'lib/after_migrate/executor.rb', line 14

def call(schema: nil)
  return reset_store unless maintenance_enabled?

  tables = target_tables
  return reset_store if tables.blank?

  run_optimizations(schema:, tables:)
  reset_store
end

.maintenance_enabled?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/after_migrate/executor.rb', line 28

def maintenance_enabled?
  AfterMigrate.configuration.vacuum || AfterMigrate.configuration.analyze != 'none'
end

.optimize_tables(schema:, table_names:) ⇒ Object



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

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

.reset_storeObject



32
33
34
# File 'lib/after_migrate/executor.rb', line 32

def reset_store
  AfterMigrate.reset!
end

.run_optimizations(schema:, tables:) ⇒ Object



36
37
38
39
40
# File 'lib/after_migrate/executor.rb', line 36

def run_optimizations(schema:, tables:)
  return run_optimize(schema:, tables: tables[schema]) if schema.present? && tables[schema].present?

  tables.each { |s, t| run_optimize(schema: s, tables: t) } unless schema.present?
end

.run_optimize(schema:, tables:) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/after_migrate/executor.rb', line 42

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

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

.target_tablesObject



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

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
  else
    # 'only_affected_tables' or 'none' — vacuum still needs the affected list
    AfterMigrate.affected_tables
  end
end

Instance Method Details

#call(schema: nil) ⇒ Object



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

def call(schema: nil)
  return reset_store unless maintenance_enabled?

  tables = target_tables
  return reset_store if tables.blank?

  run_optimizations(schema:, tables:)
  reset_store
end