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.
-
#password ⇒ Object
Returns the value of attribute password.
-
#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
- #connection_opts ⇒ 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.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/pgbundle/database.rb', line 12 def initialize(name, opts = {}) @name = name @user = opts[:user] || 'postgres' @password = opts[:password] @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 |
#password ⇒ Object
Returns the value of attribute password.
10 11 12 |
# File 'lib/pgbundle/database.rb', line 10 def password @password 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
24 25 26 27 28 |
# File 'lib/pgbundle/database.rb', line 24 def connection @connection ||= begin PG.connect(connection_opts) end end |
#connection_opts ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/pgbundle/database.rb', line 30 def connection_opts { dbname: name, user: user, password: password, host: host, port: port }.compact end |
#current_definition ⇒ Object
returns currently installed extensions
102 103 104 |
# File 'lib/pgbundle/database.rb', line 102 def current_definition result = execute('SELECT name, version, requires FROM pg_available_extension_versions WHERE installed').to_a end |
#drop_extension(name) ⇒ Object
93 94 95 |
# File 'lib/pgbundle/database.rb', line 93 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
49 50 51 52 53 |
# File 'lib/pgbundle/database.rb', line 49 def execute(sql) silence do connection.exec sql end end |
#load_destination(ext_name) ⇒ Object
97 98 99 |
# File 'lib/pgbundle/database.rb', line 97 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
75 76 77 78 79 80 81 82 |
# File 'lib/pgbundle/database.rb', line 75 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
85 86 87 88 89 90 91 |
# File 'lib/pgbundle/database.rb', line 85 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
44 45 46 |
# File 'lib/pgbundle/database.rb', line 44 def slave? @slave end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/pgbundle/database.rb', line 40 def to_s "host: #{@host}:#{port} db: #{@name}" end |
#transaction(&block) ⇒ Object
57 58 59 60 61 |
# File 'lib/pgbundle/database.rb', line 57 def transaction(&block) silence do connection.transaction(&block) end end |
#transaction_rollback(&block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pgbundle/database.rb', line 63 def transaction_rollback(&block) silence do connection.transaction do |con| yield con fail TransactionRollback end end rescue TransactionRollback end |