Module: AfterMigrate::Collector

Extended by:
Collector
Included in:
Collector
Defined in:
lib/after_migrate/collector.rb

Class Method Summary collapse

Class Method Details

.callObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/after_migrate/collector.rb', line 9

def call(*)
  event = ActiveSupport::Notifications::Event.new(*)
  sql = event.payload[:sql]&.strip
  return unless sql
  return unless sql.match?(/\A\s*(CREATE|ALTER|DROP|INSERT|UPDATE|DELETE|RENAME\s+TABLE|TRUNCATE)/i)

  table_names = parse_tables(sql)
  schema = fetch_schema
  AfterMigrate.merge_tables(schema, table_names)
end

.fetch_schemaObject



22
23
24
25
26
27
28
29
# File 'lib/after_migrate/collector.rb', line 22

def fetch_schema
  connection = ActiveRecord::Base.connection
  case connection.adapter_name
  when 'PostgreSQL'
    quoted = connection.schema_search_path.split(',').first
    quoted&.delete('"')
  end
end

.parse_tables(sql) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/after_migrate/collector.rb', line 31

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