class Scheman::Diff
Public Class Methods
@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
@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
@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
@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
@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
@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
@return [Scheman::Schema]
# File lib/scheman/diff.rb, line 149 def after_schema @after_schema ||= after_parser.parse(@after) end
@return [String]
# File lib/scheman/diff.rb, line 177 def after_type @after_type || @type end
@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
@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
@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
@return [Scheman::Schema]
# File lib/scheman/diff.rb, line 144 def before_schema @before_schema ||= before_parser.parse(@before) end
@return [String]
# File lib/scheman/diff.rb, line 172 def before_type @before_type || @type end
@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
@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
@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
@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
@return [String]
# File lib/scheman/diff.rb, line 182 def output_type @output_type || @type end
@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
@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
@raise [Scheman::Errors::ParserNotFound]
# File lib/scheman/diff.rb, line 166 def validate! before_parser after_parser end
@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