module FakePipe::Commenter

Provides helper migration method to set a valid fake_pipe comment. Currently this only supports the follwing DB adapter types:

postgres

To support more implement aa private `execute_comment_<dialect>` method.

Public Instance Methods

anonymize_comment(table, column, mutator) click to toggle source

Generates an fake_pipe comment for the give table and column. This is uses `reversible` so it should be safe to use within a `change` style migration.

@params [String] table to apply the comment @params [String] column to apply the comment @params [String] mutator strategy to apply

# File lib/fake_pipe/commenter.rb, line 15
def anonymize_comment(table, column, mutator)
  validate_mutator!(mutator)
  comment_updater.call(table: table, column: column, mutator: mutator)
end

Private Instance Methods

comment_updater() click to toggle source
# File lib/fake_pipe/commenter.rb, line 27
def comment_updater
  meth = "execute_comment_#{dialect}"
  if respond_to?(meth, true)
    method(meth)
  else
    raise NotImplementedError,
          "DB dialect `#{dialect}` not supported. Try one of: #{supported_dialects}"
  end
end
dialect() click to toggle source

TODO: There's got be a better way

# File lib/fake_pipe/commenter.rb, line 61
def dialect
  connection.adapter_name.downcase.to_s
end
escape_string(text) click to toggle source
# File lib/fake_pipe/commenter.rb, line 52
def escape_string(text)
  connection.quote(text)
end
execute_comment_postgresql(table:, column:, mutator:) click to toggle source
# File lib/fake_pipe/commenter.rb, line 37
def execute_comment_postgresql(table:, column:, mutator:)
  reversible do |dir|
    dir.up do
      execute "COMMENT ON COLUMN #{table}.#{column} IS #{formatted_comment(mutator)};"
    end
    dir.down do
      execute "COMMENT ON COLUMN #{table}.#{column} IS NULL;"
    end
  end
end
formatted_comment(mutator) click to toggle source
# File lib/fake_pipe/commenter.rb, line 48
def formatted_comment(mutator)
  escape_string("anon: #{mutator}")
end
supported_dialects() click to toggle source
# File lib/fake_pipe/commenter.rb, line 56
def supported_dialects
  methods.map { |m| m[/^execute_comment_(.*)$/, 1] }.compact
end
validate_mutator!(mutator) click to toggle source
# File lib/fake_pipe/commenter.rb, line 22
def validate_mutator!(mutator)
  Mutator.list.include?(mutator) or
    raise "Mutator #{mutator} is not valid. Try one of: #{Mutator.list}"
end