Class: Trains::Visitor::Migration

Inherits:
Base
  • Object
show all
Includes:
Utils::Args
Defined in:
lib/trains/visitor/migration.rb

Overview

Visitor that parses DB migration and associates them with Rails models

Constant Summary collapse

ALLOWED_METHOD_NAMES =
%i[change up].freeze
ALLOWED_TABLE_MODIFIERS =
%i[
  create_table
  create_join_table
  change_table
  safety_assured
  update_column
].freeze
COLUMN_MODIFIERS =
%i[
  add_column
  add_column_with_default
  change_column
  add_reference
  rename_column
  remove_column
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Args

#parse_args, #parse_hash, #parse_value

Constructor Details

#initializeMigration

skipcq: RB-LI1087



30
31
32
33
34
# File 'lib/trains/visitor/migration.rb', line 30

def initialize
  @result = []
  @migration_class = nil
  @migration_version = nil
end

Instance Attribute Details

#is_migrationObject (readonly)

Returns the value of attribute is_migration.



10
11
12
# File 'lib/trains/visitor/migration.rb', line 10

def is_migration
  @is_migration
end

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/trains/visitor/migration.rb', line 10

def model
  @model
end

#resultObject (readonly)

Returns the value of attribute result.



10
11
12
# File 'lib/trains/visitor/migration.rb', line 10

def result
  @result
end

Instance Method Details

#on_class(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/trains/visitor/migration.rb', line 36

def on_class(node)
  unless node.parent_class.source.include? 'ActiveRecord::Migration'
    return
  end

  @migration_class = node.children.first.source
  @migration_version = extract_version(node.parent_class.source)

  node.each_descendant(:def) do |child_node|
    next if child_node.body.nil?
    next unless ALLOWED_METHOD_NAMES.include?(child_node.method_name)

    process_migration(child_node)
  end
end