Class: DeepTest::Database::PostgresqlSetupListener

Inherits:
SetupListener show all
Defined in:
lib/deep_test/database/postgresql_setup_listener.rb

Overview

SetupListener implementation for PostgreSQL.

Constant Summary

Constants inherited from SetupListener

SetupListener::DUMPED_SCHEMAS

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SetupListener

#before_starting_workers, #before_sync, #connect_to_database, #disconnect_from_database, #dump_schema_once, #master_database_config, #starting, #worker_database, #worker_database_config

Methods inherited from NullWorkerListener

#before_starting_workers, #before_sync, #finished_work, #starting, #starting_work

Class Attribute Details

.admin_configurationObject

ActiveRecord configuration to use when connecting to MySQL to create databases, drop database, and grant privileges. By default, connects to information_schema on localhost as root with no password.



14
15
16
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 14

def admin_configuration
  @admin_configuration
end

Instance Method Details

#admin_connectionObject

:nodoc:



107
108
109
110
111
112
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 107

def admin_connection # :nodoc:
  conn = ActiveRecord::Base.postgresql_connection(self.class.admin_configuration)
  yield conn
ensure
  conn.disconnect! if conn
end

#command_line_config(config) ⇒ Object

:nodoc:



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 94

def command_line_config(config) # :nodoc:
  command =  ['-U', config[:username]]
  command += ["-W#{config[:password]}"] if config[:password]
  command += ['-h', config[:host]] if config[:host]
  command += ['-p', config[:port]] if config[:port]
  # TODO: socket issue with psql from the command line - it is passed to host switch,
  # not a separate switch like mysql
  # command += ['-S', config[:socket]] if config[:socket]
  command += [config[:database]]
  command += [config[:schema]]
  command.join(' ')
end

#create_databaseObject

Creates database and grants privileges (via grant_privileges) on it via ActiveRecord connection based on admin_configuration.



28
29
30
31
32
33
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 28

def create_database
  admin_connection do |connection|
    connection.create_database worker_database
    grant_privileges connection
  end
end

#drop_databaseObject

Drops database via ActiveRecord connection based on admin_configuration



52
53
54
55
56
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 52

def drop_database
  admin_connection do |connection|
    connection.drop_database worker_database
  end
end

#dump_file_nameObject

Location to store dumpfile. The default assumes you are testing a Rails project. You should override this if you are not using Rails or would like the dump file to be something other than the default



85
86
87
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 85

def dump_file_name
  "#{RAILS_ROOT}/db/deep_test_schema.sql"
end

#dump_schemaObject

Dumps schema from master database using mysqldump command



61
62
63
64
65
66
67
68
69
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 61

def dump_schema
  config = command_line_config(master_database_config)

  # TODO: the -R switch on the mysql version of this will dump stored procs, etc. we'll
  # need to do the same with pg before we push our mods back
  # system "mysqldump -R #{config} > #{dump_file_name}"
  system "pg_dump #{config} > #{dump_file_name}"
  raise "Error Dumping schema" unless $?.success?
end

#grant_privileges(connection) ⇒ Object

Grants ‘all’ privilege on worker database to username and password specified by worker database config. If your application has special database privilege needs beyond ‘all’, you should override this method and grant them.



41
42
43
44
45
46
47
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 41

def grant_privileges(connection)
  sql = "GRANT ALL PRIVILEGES ON DATABASE #{worker_database} TO #{worker_database_config[:username]};" # % [
   #          connection.quote(worker_database_config[:username])
   #        ]

  connection.execute sql
end

#load_schemaObject

Loads dumpfile into worker database using mysql command



74
75
76
77
78
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 74

def load_schema
  config = command_line_config(worker_database_config)
  system "psql -q #{config} < #{dump_file_name}"
  raise "Error Loading schema" unless $?.success?
end

#system(command) ⇒ Object

:nodoc:



89
90
91
92
# File 'lib/deep_test/database/postgresql_setup_listener.rb', line 89

def system(command) # :nodoc:
  DeepTest.logger.info { command }
  super command
end