Class: Cbratools::ComponentNameChangeMigrations

Inherits:
Object
  • Object
show all
Defined in:
lib/cbratools/component_name_change_migrations.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_name, new_name, migrations_path, db_state_file) ⇒ ComponentNameChangeMigrations

Returns a new instance of ComponentNameChangeMigrations.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/cbratools/component_name_change_migrations.rb', line 3

def initialize(current_name, new_name, migrations_path, db_state_file)
  @current_name = current_name
  @new_name = new_name
  @migrations_path = migrations_path
  @db_state_file = db_state_file

  @name_changes = [
      [current_name, new_name],
      [Cbratools::String.underscore(current_name),
       Cbratools::String.underscore(new_name)]
  ]
end

Instance Method Details

#addObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cbratools/component_name_change_migrations.rb', line 16

def add
  file = File.read(@db_state_file)
  p file
  tables = []
  file.each_line do |line|
    tables << find_all_potential_table_names(line)
  end
  component_tables = tables.compact.select do |table|
    table.start_with?(@name_changes.last.first)
  end
  component_tables.each do |component_table|
    p component_table
    table_name = component_table.gsub("#{@name_changes.last.first}_", "")
    migration_name = Time.now.strftime("%Y%m%d%H%M%S") + migration_title(table_name)
    filename = File.join(@migrations_path, Cbratools::String.underscore(migration_name)) + ".rb"
    File.open(filename, "w") do |f|
      f.write(migration_content(table_name))
    end
  end
end

#find_all_potential_table_names(line) ⇒ Object



37
38
39
40
# File 'lib/cbratools/component_name_change_migrations.rb', line 37

def find_all_potential_table_names(line)
  result = /create_table (?:"|')([^"']+)(?:"|')/.match line
  result && result[1]
end