Class: ActualDbSchema::SchemaDiff

Inherits:
Object
  • Object
show all
Includes:
OutputFormatter
Defined in:
lib/actual_db_schema/schema_diff.rb

Overview

Generates a diff of schema changes between the current schema file and the last committed version, annotated with the migrations responsible for each change.

Direct Known Subclasses

SchemaDiffHtml

Constant Summary collapse

SIGN_COLORS =
{
  "+" => :green,
  "-" => :red
}.freeze
CHANGE_PATTERNS =
{
  /t\.(\w+)\s+["']([^"']+)["']/ => :column,
  /t\.index\s+.*name:\s*["']([^"']+)["']/ => :index,
  /create_table\s+["']([^"']+)["']/ => :table
}.freeze
SQL_CHANGE_PATTERNS =
{
  /CREATE (?:UNIQUE\s+)?INDEX\s+["']?([^"'\s]+)["']?\s+ON\s+([\w.]+)/i => :index,
  /CREATE TABLE\s+(\S+)\s+\(/i => :table,
  /CREATE SEQUENCE\s+(\S+)/i => :table,
  /ALTER SEQUENCE\s+(\S+)\s+OWNED BY\s+([\w.]+)/i => :table,
  /ALTER TABLE\s+ONLY\s+(\S+)\s+/i => :table
}.freeze

Constants included from OutputFormatter

OutputFormatter::UNICODE_COLORS

Instance Method Summary collapse

Methods included from OutputFormatter

#colorize

Constructor Details

#initialize(schema_path, migrations_path) ⇒ SchemaDiff

Returns a new instance of SchemaDiff.



30
31
32
33
# File 'lib/actual_db_schema/schema_diff.rb', line 30

def initialize(schema_path, migrations_path)
  @schema_path = schema_path
  @migrations_path = migrations_path
end

Instance Method Details

#renderObject



35
36
37
38
39
40
41
42
43
# File 'lib/actual_db_schema/schema_diff.rb', line 35

def render
  if old_schema_content.nil? || old_schema_content.strip.empty?
    puts colorize("Could not retrieve old schema from git.", :red)
    return
  end

  diff_output = generate_diff(old_schema_content, new_schema_content)
  process_diff_output(diff_output)
end