Class: Squixtures::PostgresHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/squixtures/postgres_helper.rb

Overview

A class to contain elements specific to the Postgres system.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_connection_url(settings) ⇒ Object

This method converts a typical set of database connection settings, such as those in a Rails database.yml file, into the URL to be used to connect to a Postgres database using the Sequel library.

Parameters

settings

A Hash of the settings that will be used to generate the connection URL.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/squixtures/postgres_helper.rb', line 61

def self.get_connection_url(settings)
   url = StringIO.new
   url << "postgres://"
   if settings.include?("username")
      url << settings["username"]
      url << ":#{settings['password']}" if settings.include?("password")
   end
   url << "@"
   if settings.include?("host")
      url << "#{settings['host']}"
      url << ":#{settings['port']}" if settings.include?("port")
   end
   url << "/#{settings['database']}" if settings.include?("database")
   url.string
end

Instance Method Details

#after_load(fixtures, connection) ⇒ Object

This method is invoked immediately after a fixture load completes and performs all clean up necessary.

Parameters

fixtures

A collection of the fixture objects to be loaded.

connection

The database connection that the load will be performed through.



37
38
39
40
41
42
43
44
45
46
# File 'lib/squixtures/postgres_helper.rb', line 37

def after_load(fixtures, connection)
   PostgresHelper.log.debug "PostgresHelper.after_load called."
   fixtures.each do |fixture|
     begin
       connection.run("alter table #{fixture.table_name} enable trigger all")
     rescue
       connection.run("alter table #{fixture.table_name} enable trigger user")
     end
   end
end

#before_load(fixtures, connection) ⇒ Object

This method is invoked immediately before a fixture load starts and performs all work necessary to facilitate the load.

Parameters

fixtures

A collection of the fixture objects to be loaded.

connection

The database connection that the load will be performed through.



19
20
21
22
23
24
25
26
27
28
# File 'lib/squixtures/postgres_helper.rb', line 19

def before_load(fixtures, connection)
   PostgresHelper.log.debug "PostgresHelper.before_load called."
   fixtures.each do |fixture|
     begin
       connection.run("alter table #{fixture.table_name} disable trigger all")
     rescue
       connection.run("alter table #{fixture.table_name} disable trigger user")
     end
   end
end

#get_connection_url(settings) ⇒ Object

This method is an instance level version of the get_connection_url method declared at the class level.



50
51
52
# File 'lib/squixtures/postgres_helper.rb', line 50

def get_connection_url(settings)
   PostgresHelper.get_connection_url(settings)
end