class Scheman::Diff

Public Class Methods

new(before: nil, after: nil, type: nil, before_type: nil, after_type: nil, output_type: nil) click to toggle source

@param before [String] The previous schema @param after [String] The next schema @param type [String] The type of schema syntax, default types of the both schemata (e.g. “mysql”) @param before_type [String] Specify the type of the previous schema if needed @param after_type [String] Specify the type of the next schema if needed @param output_type [String] Specify the type of the output schema if needed @raise [Scheman::Errors::ParserNotFound]

# File lib/scheman/diff.rb, line 10
def initialize(before: nil, after: nil, type: nil, before_type: nil, after_type: nil, output_type: nil)
  @before = before
  @after = after
  @type = type
  @before_type = before_type
  @after_type = after_type
  @output_type = output_type
  validate!
end

Public Instance Methods

to_hash() click to toggle source

@note Passed to Parslet::Transform to convert into SQL string @return [Hash] A hash representation of this diff

# File lib/scheman/diff.rb, line 27
def to_hash
  {
    alter_tables: alter_tables,
    create_tables: create_tables,
    drop_tables: drop_tables,
  }
end
to_s() click to toggle source

@return [String] A string representation of this diff

# File lib/scheman/diff.rb, line 21
def to_s
  view_class.new(to_hash).to_s
end

Private Instance Methods

add_fields() click to toggle source

@return [Array<Hash>] ALTER TABLE statements for adding new fields

# File lib/scheman/diff.rb, line 43
def add_fields
  after_schema.tables.each_with_object([]) do |after_table, result|
    if before_table = before_schema.tables_indexed_by_name[after_table.name]
      after_table.fields.each do |after_field|
        unless before_table.fields_indexed_by_name[after_field.name]
          result << {
            add_field: after_field.to_hash.merge(table_name: after_table.name),
          }
        end
      end
    end
  end
end
add_indices() click to toggle source

@return [Array<Hash>] ALTER TABLE statements for adding indices

# File lib/scheman/diff.rb, line 90
def add_indices
  after_schema.tables.each_with_object([]) do |after_table, result|
    if before_table = before_schema.tables_indexed_by_name[after_table.name]
      (after_table.indices - before_table.indices).each do |index|
        result << {
          add_index: index.merge(table_name: after_table.name),
        }
      end
    end
  end
end
after_parser() click to toggle source

@return [Scheman::Parsers::Base] @raise [Scheman::Errors::ParserNotFound]

# File lib/scheman/diff.rb, line 161
def after_parser
  @after_parser ||= ParserBuilder.build(after_type)
end
after_schema() click to toggle source

@return [Scheman::Schema]

# File lib/scheman/diff.rb, line 149
def after_schema
  @after_schema ||= after_parser.parse(@after)
end
after_type() click to toggle source

@return [String]

# File lib/scheman/diff.rb, line 177
def after_type
  @after_type || @type
end
alter_fields() click to toggle source

@return [Array<Hash>] ALTER TABLE statements for altering fields

# File lib/scheman/diff.rb, line 73
def alter_fields
  after_schema.tables.each_with_object([]) do |after_table, result|
    if before_table = before_schema.tables_indexed_by_name[after_table.name]
      after_table.fields.each do |after_field|
        if before_field = before_table.fields_indexed_by_name[after_field.name]
          if after_field != before_field
            result << {
              alter_field: after_field.to_hash.merge(table_name: after_table.name),
            }
          end
        end
      end
    end
  end
end
alter_tables() click to toggle source

@return [Array<Hash>] ALTER TABLE statements we need to apply

# File lib/scheman/diff.rb, line 38
def alter_tables
  drop_fields + add_fields + alter_fields + drop_indices + add_indices
end
before_parser() click to toggle source

@return [Scheman::Parsers::Base] @raise [Scheman::Errors::ParserNotFound]

# File lib/scheman/diff.rb, line 155
def before_parser
  @before_parser ||= ParserBuilder.build(before_type)
end
before_schema() click to toggle source

@return [Scheman::Schema]

# File lib/scheman/diff.rb, line 144
def before_schema
  @before_schema ||= before_parser.parse(@before)
end
before_type() click to toggle source

@return [String]

# File lib/scheman/diff.rb, line 172
def before_type
  @before_type || @type
end
create_tables() click to toggle source

@return [Array<Hash>] CREATE TABLE statements we need to apply

# File lib/scheman/diff.rb, line 127
def create_tables
  after_schema.create_tables.select do |statement|
    table_names_to_create.include?(statement[:create_table][:name])
  end
end
drop_fields() click to toggle source

@return [Array<Hash>] ALTER TABLE statements for dropping fields

# File lib/scheman/diff.rb, line 58
def drop_fields
  after_schema.tables.each_with_object([]) do |after_table, result|
    if before_table = before_schema.tables_indexed_by_name[after_table.name]
      before_table.fields.each do |before_field|
        unless after_table.fields_indexed_by_name[before_field.name]
          result << {
            drop_field: before_field.to_hash.merge(table_name: after_table.name),
          }
        end
      end
    end
  end
end
drop_indices() click to toggle source

@return [Array<Hash>] ALTER TABLE statements for dropping indices

# File lib/scheman/diff.rb, line 103
def drop_indices
  after_schema.tables.each_with_object([]) do |after_table, result|
    if before_table = before_schema.tables_indexed_by_name[after_table.name]
      (before_table.indices - after_table.indices).each do |index|
        result << {
          drop_index: index.merge(table_name: after_table.name),
        }
      end
    end
  end
end
drop_tables() click to toggle source

@return [Array<Hash>] DROP TABLE statements we need to apply

# File lib/scheman/diff.rb, line 116
def drop_tables
  table_names_to_drop.map do |name|
    {
      drop_table: {
        name: name,
      },
    }
  end
end
output_type() click to toggle source

@return [String]

# File lib/scheman/diff.rb, line 182
def output_type
  @output_type || @type
end
table_names_to_create() click to toggle source

@return [Array<String>]

# File lib/scheman/diff.rb, line 134
def table_names_to_create
  @table_names_to_create ||= after_schema.table_names - before_schema.table_names
end
table_names_to_drop() click to toggle source

@return [Array<String>]

# File lib/scheman/diff.rb, line 139
def table_names_to_drop
  @table_names_to_drop ||= before_schema.table_names - after_schema.table_names
end
validate!() click to toggle source

@raise [Scheman::Errors::ParserNotFound]

# File lib/scheman/diff.rb, line 166
def validate!
  before_parser
  after_parser
end
view_class() click to toggle source

@return [Class]

# File lib/scheman/diff.rb, line 187
def view_class
  case output_type
  when "mysql"
    Views::Mysql
  else
    raise Errors::ViewNotFound
  end
end