Sha256: 9ce1a487ffd3cc8f473882ceac4e1f6140bc7a68a21d6debbd75ae5b6103d7dd

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

#
#  Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias
#  Schmidt
#
#  Apply a change to the database.
#

module Lhm
  module Command
    def self.included(base)
      base.send :attr_reader, :connection
    end

    #
    # Command Interface
    #

    def validate; end

    def revert
      raise NotImplementedError.new(self.class.name)
    end

    def run(&block)
      validate

      if(block_given?)
        before
        block.call(self)
        after
      else
        execute
      end
    end

  private

    def execute
      raise NotImplementedError.new(self.class.name)
    end

    def before
      raise NotImplementedError.new(self.class.name)
    end

    def after
      raise NotImplementedError.new(self.class.name)
    end

    def table?(table_name)
      @connection.table_exists?(table_name)
    end

    def error(msg)
      raise Exception.new("#{ self.class }: #{ msg }")
    end

    def sql(statements)
      [*statements].each do |statement|
        begin
          @connection.execute(statement)
        rescue Mysql::Error => e
          revert
          error "#{ statement } failed: #{ e.inspect }"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lhm-1.0.0.rc.1 lib/lhm/command.rb