class GroongaSchema::Diff::GroongaCommandListConverter

Public Class Methods

new(diff, options={}) click to toggle source
# File lib/groonga-schema/diff.rb, line 72
def initialize(diff, options={})
  @diff = diff
  @options = options
  @grouped_list = []
end

Public Instance Methods

convert() click to toggle source
# File lib/groonga-schema/diff.rb, line 78
def convert
  @grouped_list.clear

  convert_added_plugins
  convert_added_tables
  convert_removed_columns
  convert_removed_tables
  convert_removed_plugins
  convert_changed_tables

  meaningful_grouped_list = @grouped_list.reject do |group|
    group.empty?
  end
  formatted_grouped_list = meaningful_grouped_list.collect do |group|
    command_list = ""
    group.each do |command|
      command_list << "#{format_command(command)}\n"
    end
    command_list
  end
  formatted_grouped_list.join("\n")
end

Private Instance Methods

convert_added_columns(name, target_is_reference_type) click to toggle source
# File lib/groonga-schema/diff.rb, line 140
def convert_added_columns(name, target_is_reference_type)
  columns = @diff.added_columns[name]
  return [] if columns.nil?

  sorted_columns = columns.sort_by do |column_name,|
    column_name
  end

  group = []
  sorted_columns.each do |column_name, column|
    if target_is_reference_type
      next unless column.reference_value_type?
    else
      next if column.reference_value_type?
    end
    group << column.to_create_groonga_command
  end
  group
end
convert_added_plugins() click to toggle source
# File lib/groonga-schema/diff.rb, line 102
def convert_added_plugins
  sorted_plugins = @diff.added_plugins.sort_by(&:name)
  @grouped_list << sorted_plugins.collect(&:to_register_groonga_command)
end
convert_added_tables() click to toggle source
# File lib/groonga-schema/diff.rb, line 107
def convert_added_tables
  reference_table_names = []
  no_reference_table_names = []
  @diff.added_tables.each do |name, table|
    if table.reference_key_type?
      reference_table_names << name
    else
      no_reference_table_names << name
    end
  end
  no_reference_table_names |=
    (@diff.added_columns.keys - reference_table_names)

  sorted_reference_table_names = reference_table_names.sort
  sorted_no_reference_table_names = no_reference_table_names.sort

  sorted_table_names =
    sorted_no_reference_table_names +
    sorted_reference_table_names

  sorted_table_names.each do |name|
    group = []
    table = @diff.added_tables[name]
    group << table.to_create_groonga_command if table
    group.concat(convert_added_columns(name, false))
    @grouped_list << group
  end

  sorted_table_names.each do |name|
    @grouped_list << convert_added_columns(name, true)
  end
end
convert_changed_columns() click to toggle source
# File lib/groonga-schema/diff.rb, line 229
def convert_changed_columns
  all_columns = []
  @diff.changed_columns.each do |table_name, columns|
    all_columns.concat(columns.values)
  end

  sorted_columns = all_columns.sort_by do |column|
    [
      (column.type == :index) ? 1 : 0,
      column.table_name,
      column.name,
    ]
  end
  sorted_columns.each do |column|
    @grouped_list << column.to_migrate_start_groonga_commands
  end

  sorted_columns = all_columns.sort_by do |column|
    [
      (column.type == :index) ? 0 : 1,
      column.table_name,
      column.name,
    ]
  end
  sorted_columns.each do |column|
    @grouped_list << column.to_migrate_finish_groonga_commands
  end
end
convert_changed_tables() click to toggle source
# File lib/groonga-schema/diff.rb, line 212
def convert_changed_tables
  sorted_tables = @diff.changed_tables.sort_by do |name, table|
    [
      table.reference_key_type? ? 1 : 0,
      table.name,
    ]
  end

  sorted_tables.each do |name, table|
    @grouped_list << table.to_migrate_start_groonga_commands
  end
  convert_changed_columns
  sorted_tables.each do |name, table|
    @grouped_list << table.to_migrate_finish_groonga_commands
  end
end
convert_removed_columns() click to toggle source
# File lib/groonga-schema/diff.rb, line 160
def convert_removed_columns
  sorted_removed_columns = @diff.removed_columns.sort_by do |table_name,|
    table_name
  end

  column_groups = []
  sorted_removed_columns.each do |table_name, columns|
    group = []
    columns.each do |column_name, column|
      group << column unless column.sources.empty?
    end
    next if group.empty?
    column_groups << group
  end
  sorted_removed_columns.each do |table_name, columns|
    group = []
    columns.each do |column_name, column|
      group << column if column.sources.empty?
    end
    next if group.empty?
    column_groups << group
  end

  column_groups.each do |columns|
    sorted_columns = columns.sort_by do |column|
      column.name
    end
    group = sorted_columns.collect do |column|
      column.to_remove_groonga_command
    end
    @grouped_list << group
  end
end
convert_removed_plugins() click to toggle source
# File lib/groonga-schema/diff.rb, line 207
def convert_removed_plugins
  sorted_plugins = @diff.removed_plugins.sort_by(&:name)
  @grouped_list << sorted_plugins.collect(&:to_unregister_groonga_command)
end
convert_removed_tables() click to toggle source
# File lib/groonga-schema/diff.rb, line 194
def convert_removed_tables
  sorted_tables = @diff.removed_tables.sort_by do |name, table|
    [
      table.reference_key_type? ? 0 : 1,
      table.name,
    ]
  end

  sorted_tables.each do |name, table|
    @grouped_list << [table.to_remove_groonga_command]
  end
end
format_command(command) click to toggle source
# File lib/groonga-schema/diff.rb, line 258
def format_command(command)
  case @options[:format]
  when :uri
    command.to_uri_format
  else
    command.to_command_format
  end
end