Class: PgBundle::Extension
- Inherits:
-
Object
- Object
- PgBundle::Extension
- Defined in:
- lib/pgbundle/extension.rb
Overview
The Extension class provides the api for defining an Extension it installation source, and dependencies example:
define an extension named 'foo' at version '0.1.1', with source on github depending on hstore
Extension.new('foo', '0.1.1', github: 'me/foo', requires: 'hstore')
you can then check if the Extension is available on a given database
extension.available?(database)
or install it along with it's dependencies
extension.install(database)
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#resolving_dependencies ⇒ Object
Returns the value of attribute resolving_dependencies.
-
#source ⇒ Object
Returns the value of attribute source.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#available?(database) ⇒ Boolean
returns true if extension is available for installation on a given database.
-
#created?(database) ⇒ Boolean
returns true if extension is already created with the correct version in the given database.
- #dependencies ⇒ Object
-
#dependencies=(obj = nil) ⇒ Object
set dependency hash with different options dependencies= Extension.new(‘foo’), bar: Extension.new(‘bar’) => => Extension.new(‘foo’), ‘bar’ Extension.new(‘bar’) dependencies= ‘foo’ => Extension.new(‘foo’) dependencies= Extension.new(‘foo’) => Extension.new(‘foo’) dependencies= [‘foo’, ‘bar’] => => Extension.new(‘foo’), ‘bar’ Extension.new(‘bar’).
-
#dependencies_installed?(database) ⇒ Boolean
checks that all dependencies are installed on a given database.
-
#initialize(*args) ⇒ Extension
constructor
A new instance of Extension.
-
#install(database) ⇒ Object
installs extension and all dependencies using make install returns true if Extension can successfully be created using CREATE EXTENSION.
-
#installed?(database) ⇒ Boolean
returns if the extension is already installed on the database system if it is also already created it returns the installed version.
-
#uninstall!(database) ⇒ Object
completely removes extension be running make uninstall and DROP EXTENSION.
Constructor Details
#initialize(*args) ⇒ Extension
Returns a new instance of Extension.
14 15 16 17 18 19 |
# File 'lib/pgbundle/extension.rb', line 14 def initialize(*args) opts = args.last.is_a?(Hash) ? args.pop : {} @name, @version = args self.dependencies = opts[:requires] set_source(opts) end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/pgbundle/extension.rb', line 13 def name @name end |
#resolving_dependencies ⇒ Object
Returns the value of attribute resolving_dependencies.
13 14 15 |
# File 'lib/pgbundle/extension.rb', line 13 def resolving_dependencies @resolving_dependencies end |
#source ⇒ Object
Returns the value of attribute source.
13 14 15 |
# File 'lib/pgbundle/extension.rb', line 13 def source @source end |
#version ⇒ Object
Returns the value of attribute version.
13 14 15 |
# File 'lib/pgbundle/extension.rb', line 13 def version @version end |
Instance Method Details
#available?(database) ⇒ Boolean
returns true if extension is available for installation on a given database
58 59 60 61 62 63 64 65 |
# File 'lib/pgbundle/extension.rb', line 58 def available?(database) return false unless installed?(database) return true if created?(database) created_any_version?(database) ? updatable?(database) : creatable?(database) rescue ExtensionCreateError false end |
#created?(database) ⇒ Boolean
returns true if extension is already created with the correct version in the given database
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pgbundle/extension.rb', line 68 def created?(database) if version result = database.execute("SELECT * FROM pg_available_extension_versions WHERE name ='#{name}' AND version = '#{version}' AND installed").to_a else result = database.execute("SELECT * FROM pg_available_extension_versions WHERE name ='#{name}' AND installed").to_a end if result.empty? false else true end end |
#dependencies ⇒ Object
21 22 23 |
# File 'lib/pgbundle/extension.rb', line 21 def dependencies @dependencies end |
#dependencies=(obj = nil) ⇒ Object
set dependency hash with different options dependencies= Extension.new(‘foo’), bar: Extension.new(‘bar’)
> => Extension.new(‘foo’), ‘bar’ Extension.new(‘bar’)
dependencies= ‘foo’
> Extension.new(‘foo’)
dependencies= Extension.new(‘foo’)
> Extension.new(‘foo’)
dependencies= [‘foo’, ‘bar’]
> => Extension.new(‘foo’), ‘bar’ Extension.new(‘bar’)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pgbundle/extension.rb', line 34 def dependencies=(obj = nil) @dependencies = case obj when nil {} when Hash Hash[obj.map { |k, v| [k.to_s, v] }] when String, Symbol { obj.to_s => Extension.new(obj.to_s) } when Extension { obj.name => obj } when Array hashable = obj.map do |o| case o when String, Symbol [o.to_s, Extension.new(obj.to_s)] when Extension [o.name, o] end end Hash[hashable] end end |
#dependencies_installed?(database) ⇒ Boolean
checks that all dependencies are installed on a given database
99 100 101 |
# File 'lib/pgbundle/extension.rb', line 99 def dependencies_installed?(database) dependencies.all?{|_, d| d.installed?(database)} end |
#install(database) ⇒ Object
installs extension and all dependencies using make install returns true if Extension can successfully be created using CREATE EXTENSION
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/pgbundle/extension.rb', line 105 def install(database) unless dependencies.empty? install_dependencies(database) end make_install(database) raise ExtensionNotFound.new(name, version) unless installed?(database) add_missing_required_dependencies(database) creatable?(database) end |
#installed?(database) ⇒ Boolean
returns if the extension is already installed on the database system if it is also already created it returns the installed version
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/pgbundle/extension.rb', line 84 def installed?(database) if version result = database.execute("SELECT * FROM pg_available_extension_versions WHERE name ='#{name}' AND version = '#{version}'").to_a else result = database.execute("SELECT * FROM pg_available_extension_versions WHERE name ='#{name}'").to_a end if result.empty? false else true end end |
#uninstall!(database) ⇒ Object
completely removes extension be running make uninstall and DROP EXTENSION
119 120 121 122 |
# File 'lib/pgbundle/extension.rb', line 119 def uninstall!(database) drop_extension(database) make_uninstall(database) end |