How can I get commands in Rails Migration file using regex?

I’m trying to get commands from a Rails migration file as an array based on a specific migration command, using regex. My code works well on most cases, but when there is a command with multiline code, it broke and I couldn’t fix.

Example

class AddMissingUniqueIndices < ActiveRecord::Migration
  def self.up
    add_index :tags, :name, unique: true

    remove_index :taggings, :tag_id
    remove_index :taggings, [:taggable_id, :taggable_type, :context]
    add_index :taggings,
              [:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
              unique: true, name: 'taggings_idx'
  end

  def self.down
    remove_index :tags, :name

    remove_index :taggings, name: 'taggings_idx'
    add_index :taggings, :tag_id
    add_index :taggings, [:taggable_id, :taggable_type, :context]
  end
end

My objective is return an array with the separated commands as string. What I expect:

[
  "add_index :tags, :name, unique: true", 
  "remove_index :taggings, :tag_id", 
  "remove_index :taggings, [:taggable_id, :taggable_type, :context]", 
  "add_index :taggings, [:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type], unique: true, name: 'taggings_idx'"
]

First, I separe the block of change or self.up (for old migrations), and then try to use the above regex code to collect each add/remove index commands into an array:

migration_content = 'migration file in txt'
@table_name = 'taggings'
regex_pattern = /(add|remove)_indexs+:#{@table_name}.*w+:s+?w+/m
columns_to_process = migration_content.to_enum(:scan, regex_pattern).map { Regexp.last_match.to_s.squish }
puts columns_to_process
=> ["remove_index :taggings, :tag_id remove_index :taggings, [:taggable_id, :taggable_type, :context] add_index :taggings, [:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type], unique: true"]

As you can see, didn’t work, returning just 2 commands, and both in same string.
This works fine for inline code, my problem starts when the user can use a block like the last self.up action, specially this case where has much elements, I couldn’t adapt the regex to all cases, also tried to get all content between add_index/remove_index or end, but didn’t work. Can anyone help me?

1

I think before scanning the file content you could replace all the line breaks that come after coma with space:

migration_content = migration_content.gsub(/,s*R/, ', ')

Maybe can also use gsub(/(s*R/, '(') to replace multiline function calls where the code line ends with (

This will get all the commands correctly:

@table_name = 'taggings'
commands = []
current_command = ""
inside_method = false

migration_content.each_line do |line|
  stripped_line = line.strip

  if stripped_line =~ /^defs+self.(up|down)/
    inside_method = true
  elsif stripped_line =~ /^end$/
    inside_method = false
    if current_command.include?(":#{@table_name}")
      commands << current_command.strip unless current_command.empty?
    end
    current_command = ""
  elsif inside_method && stripped_line =~ /^(add|remove)_index/
    if current_command.include?(":#{@table_name}")
      commands << current_command.strip unless current_command.empty?
    end
    current_command = stripped_line
  elsif inside_method && !current_command.empty?
    current_command += " #{stripped_line}"
  end
end

if current_command.include?(":#{@table_name}")
  commands << current_command.strip unless current_command.empty?
end

puts commands

Output:

["remove_index :taggings, :tag_id", "remove_index :taggings, [:taggable_id, :taggable_type, :context]", "add_index :taggings, [:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type], unique: true, name: 'taggings_idx'", "remove_index :taggings, name: 'taggings_idx'", "add_index :taggings, :tag_id", "add_index :taggings, [:taggable_id, :taggable_type, :context]"]

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật