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
-
#host ⇒ Object
Returns the value of attribute host.
-
#name ⇒ Object
Returns the value of attribute name.
-
#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
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) ⇒ Object
loads the source, runs make install and removes the source afterwards.
-
#make_uninstall(source, ext_name) ⇒ Object
loads the source and runs make uninstall.
- #transaction(&block) ⇒ Object
- #transaction_rollback(&block) ⇒ Object
Constructor Details
#initialize(name, opts = {}) ⇒ Database
Returns a new instance of Database.
10 11 12 13 14 15 16 |
# File 'lib/pgbundle/database.rb', line 10 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' end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
9 10 11 |
# File 'lib/pgbundle/database.rb', line 9 def host @host end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/pgbundle/database.rb', line 9 def name @name end |
#system_user ⇒ Object
Returns the value of attribute system_user.
9 10 11 |
# File 'lib/pgbundle/database.rb', line 9 def system_user @system_user end |
#use_sudo ⇒ Object
Returns the value of attribute use_sudo.
9 10 11 |
# File 'lib/pgbundle/database.rb', line 9 def use_sudo @use_sudo end |
#user ⇒ Object
Returns the value of attribute user.
9 10 11 |
# File 'lib/pgbundle/database.rb', line 9 def user @user end |
Instance Method Details
#connection ⇒ Object
18 19 20 21 22 |
# File 'lib/pgbundle/database.rb', line 18 def connection @connection ||= begin PG.connect(dbname: name, user: user, host: host) end end |
#current_definition ⇒ Object
returns currently installed extensions
74 75 76 |
# File 'lib/pgbundle/database.rb', line 74 def current_definition result = execute('SELECT name, version, requires FROM pg_available_extension_versions WHERE installed').to_a end |
#drop_extension(name) ⇒ Object
65 66 67 |
# File 'lib/pgbundle/database.rb', line 65 def drop_extension(name) execute "DROP EXTENSION IF EXISTS #{name}" end |
#execute(sql) ⇒ Object
executes the given sql on the database connections redirects all noise to /dev/null
26 27 28 29 30 |
# File 'lib/pgbundle/database.rb', line 26 def execute(sql) silence do connection.exec sql end end |
#load_destination(ext_name) ⇒ Object
69 70 71 |
# File 'lib/pgbundle/database.rb', line 69 def load_destination(ext_name) "/tmp/#{ext_name}" end |
#make_install(source, ext_name) ⇒ Object
loads the source, runs make install and removes the source afterwards
50 51 52 53 54 55 |
# File 'lib/pgbundle/database.rb', line 50 def make_install(source, ext_name) remove_source(ext_name) source.load(host, system_user, load_destination(ext_name)) run(make_install_cmd(ext_name)) remove_source(ext_name) end |
#make_uninstall(source, ext_name) ⇒ Object
loads the source and runs make uninstall
58 59 60 61 62 63 |
# File 'lib/pgbundle/database.rb', line 58 def make_uninstall(source, ext_name) remove_source(ext_name) source.load(host, system_user, load_destination(ext_name)) run(make_uninstall_cmd(ext_name)) remove_source(ext_name) end |
#transaction(&block) ⇒ Object
32 33 34 35 36 |
# File 'lib/pgbundle/database.rb', line 32 def transaction(&block) silence do connection.transaction(&block) end end |
#transaction_rollback(&block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pgbundle/database.rb', line 38 def transaction_rollback(&block) silence do connection.transaction do |con| yield con fail TransactionRollback end end rescue TransactionRollback end |