class Migrate::Storage::Mysql

Public Class Methods

new(*args) click to toggle source
Calls superclass method Migrate::Storage::DB::new
# File lib/migrate/storage/mysql.rb, line 6
def initialize(*args)
  super
  @conn = Mysql2::Client.new(
    :database => @config.database,
    :host => @config.host,
    :port => @config.port,
    :username => @config.user,
    :password => @config.password,
  )
end

Public Instance Methods

create_tables() click to toggle source
# File lib/migrate/storage/mysql.rb, line 17
      def create_tables
        Log.info("Creating version table")
        self.exec_sql <<-eos
        CREATE TABLE #{@config.version_info}
        (
          version INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
          description TEXT,
          created_date TIMESTAMP NOT NULL,
          last_up TIMESTAMP NULL,
          last_down TIMESTAMP NULL
        );
        eos

        self.exec_sql <<-eos
        CREATE TABLE #{@config.version_number} (
          version int(11) not null,
          PRIMARY KEY (version)
        );
        eos

        self.exec_sql <<-eos
        INSERT INTO #{@config.version_number} VALUES(0);
        eos
        Log.success("Version table created")
      end
exec_sql(sql) click to toggle source
# File lib/migrate/storage/mysql.rb, line 50
def exec_sql(sql)
  results = []
  result = @tx.query sql
  return [] if result == nil

  result.each do |row|
    results << row
  end
end
has_tx() click to toggle source
# File lib/migrate/storage/mysql.rb, line 60
def has_tx
  @tx != nil
end
tables_exists?() click to toggle source
# File lib/migrate/storage/mysql.rb, line 43
def tables_exists?
  vi = self.exec_sql("SHOW TABLES LIKE '#{@config.version_info}'")
  vn = self.exec_sql("SHOW TABLES LIKE '#{@config.version_number}'")

  vi.length > 0 && vn.length > 0
end
tx() { || ... } click to toggle source
# File lib/migrate/storage/mysql.rb, line 64
def tx
  if has_tx
    yield
  else
    begin
      @conn.query "BEGIN;"
      @tx = @conn
      yield
      @conn.query "COMMIT;"
    rescue Exception => e
      @conn.query "ROLLBACK;"
      raise e
    ensure
      @tx = nil
    end
  end
end