Class: PgBundle::Database
- Inherits:
-
Object
- Object
- PgBundle::Database
- Defined in:
- lib/pgbundle/database.rb
Overview
The Database class defines on which database the extensions should be installed Note to install an extension the code must be compiled on the database server on a typical environment ssh access is needed if the database host differs from the Pgfile host
Instance Attribute Summary collapse
-
#force_ssh ⇒ Object
Returns the value of attribute force_ssh.
-
#host ⇒ Object
Returns the value of attribute host.
-
#name ⇒ Object
Returns the value of attribute name.
-
#port ⇒ Object
Returns the value of attribute port.
-
#system_user ⇒ Object
Returns the value of attribute system_user.
-
#use_sudo ⇒ Object
Returns the value of attribute use_sudo.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #connection ⇒ Object
-
#current_definition ⇒ Object
returns currently installed extensions.
- #drop_extension(name) ⇒ Object
-
#execute(sql) ⇒ Object
(also: #exec)
executes the given sql on the database connections redirects all noise to /dev/null.
-
#initialize(name, opts = {}) ⇒ Database
constructor
A new instance of Database.
- #load_destination(ext_name) ⇒ Object
-
#make_install(source, ext_name, flags) ⇒ Object
loads the source, runs make install and removes the source afterwards.
-
#make_uninstall(source, ext_name, flags) ⇒ Object
loads the source and runs make uninstall.
- #slave? ⇒ Boolean
- #to_s ⇒ Object
- #transaction(&block) ⇒ Object
- #transaction_rollback(&block) ⇒ Object
Constructor Details
#initialize(name, opts = {}) ⇒ Database
Returns a new instance of Database.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/pgbundle/database.rb', line 11 def initialize(name, opts = {}) @name = name @user = opts[:user] || 'postgres' @host = opts[:host] || 'localhost' @use_sudo = opts[:use_sudo] || false @system_user = opts[:system_user] || 'postgres' @port = opts[:port] || 5432 @force_ssh = opts[:force_ssh] || false @slave = opts[:slave] || false end |
Instance Attribute Details
#force_ssh ⇒ Object
Returns the value of attribute force_ssh.
10 11 12 |
# File 'lib/pgbundle/database.rb', line 10 def force_ssh @force_ssh end |
#host ⇒ Object
Returns the value of attribute host.
10 11 12 |
# File 'lib/pgbundle/database.rb', line 10 def host @host end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/pgbundle/database.rb', line 10 def name @name end |
#port ⇒ Object
Returns the value of attribute port.
10 11 12 |
# File 'lib/pgbundle/database.rb', line 10 def port @port end |
#system_user ⇒ Object
Returns the value of attribute system_user.
10 11 12 |
# File 'lib/pgbundle/database.rb', line 10 def system_user @system_user end |
#use_sudo ⇒ Object
Returns the value of attribute use_sudo.
10 11 12 |
# File 'lib/pgbundle/database.rb', line 10 def use_sudo @use_sudo end |
#user ⇒ Object
Returns the value of attribute user.
10 11 12 |
# File 'lib/pgbundle/database.rb', line 10 def user @user end |
Instance Method Details
#connection ⇒ Object
22 23 24 25 26 |
# File 'lib/pgbundle/database.rb', line 22 def connection @connection ||= begin PG.connect(dbname: name, user: user, host: host, port: port) end end |
#current_definition ⇒ Object
returns currently installed extensions
90 91 92 |
# File 'lib/pgbundle/database.rb', line 90 def current_definition result = execute('SELECT name, version, requires FROM pg_available_extension_versions WHERE installed').to_a end |
#drop_extension(name) ⇒ Object
81 82 83 |
# File 'lib/pgbundle/database.rb', line 81 def drop_extension(name) execute "DROP EXTENSION IF EXISTS #{name}" end |
#execute(sql) ⇒ Object Also known as: exec
executes the given sql on the database connections redirects all noise to /dev/null
37 38 39 40 41 |
# File 'lib/pgbundle/database.rb', line 37 def execute(sql) silence do connection.exec sql end end |
#load_destination(ext_name) ⇒ Object
85 86 87 |
# File 'lib/pgbundle/database.rb', line 85 def load_destination(ext_name) "/tmp/pgbundle/#{ext_name}" end |
#make_install(source, ext_name, flags) ⇒ Object
loads the source, runs make install and removes the source afterwards
63 64 65 66 67 68 69 70 |
# File 'lib/pgbundle/database.rb', line 63 def make_install(source, ext_name, flags) run("mkdir -p -m 0777 /tmp/pgbundle/") remove_source(ext_name) source.load(host, system_user, load_destination(ext_name)) run(make_install_cmd(ext_name, flags)) remove_source(ext_name) source.clean end |
#make_uninstall(source, ext_name, flags) ⇒ Object
loads the source and runs make uninstall
73 74 75 76 77 78 79 |
# File 'lib/pgbundle/database.rb', line 73 def make_uninstall(source, ext_name, flags) remove_source(ext_name) source.load(host, system_user, load_destination(ext_name)) run(make_uninstall_cmd(ext_name, flags)) remove_source(ext_name) source.clean end |
#slave? ⇒ Boolean
32 33 34 |
# File 'lib/pgbundle/database.rb', line 32 def slave? @slave end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/pgbundle/database.rb', line 28 def to_s "host: #{@host}:#{port} db: #{@name}" end |
#transaction(&block) ⇒ Object
45 46 47 48 49 |
# File 'lib/pgbundle/database.rb', line 45 def transaction(&block) silence do connection.transaction(&block) end end |
#transaction_rollback(&block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pgbundle/database.rb', line 51 def transaction_rollback(&block) silence do connection.transaction do |con| yield con fail TransactionRollback end end rescue TransactionRollback end |