Sha256: bacd80ea7acb3ee20ba40d73a502fc94397f46f4a02a4b92a1b87927448257ed

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module PgMigrate

  class DbUtility

    DEFAULT_OPTIONS = {
            :dbtestname => "pg_migrate_test",
            :dbsuperuser => "postgres",
            :dbsuperpass => "postgres",
            :dbhost => "localhost",
            :dbport => 5432
    }

    def initialize(options=DEFAULT_OPTIONS)

      options = DEFAULT_OPTIONS.merge(options)

      @dbtestname = options[:dbtestname]
      @dbsuperuser = options[:dbsuperuser]
      @dbsuperpass = options[:dbsuperpass]
      @dbhost = options[:dbhost]
      @dbport = options[:dbport]
    end

    def pg_connection_hasher()
      return {
              :port => @dbport,
              :user => @dbsuperuser,
              :password => @dbsuperpass,
              :host => @dbhost
      }
    end


    def create_new_test_database()

      # this will presumably do the right default thing,
      # to get us into a 'default' database where we can execute 'create database' from
      conn_properties = pg_connection_hasher

      conn_properties.delete(:dbname)

      conn = PG::Connection.new(conn_properties)

      conn.exec("DROP DATABASE IF EXISTS #{@dbtestname}").clear
      conn.exec("CREATE DATABASE #{@dbtestname}").clear

      conn.close

    end

    def connect_test_database(&block)
      conn = nil

      begin
        conn_properties = pg_connection_hasher

        conn_properties[:dbname] = @dbtestname
        conn = PG::Connection.open(conn_properties)

        yield conn

      ensure
        if !conn.nil?
          conn.close
        end
      end


    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pg_migrate-0.1.11 spec/pg_migrate/db_utility.rb