Module: PgSaurus::ConnectionAdapters::PostgreSQLAdapter::ExtensionMethods
- Included in:
- PgSaurus::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/pg_saurus/connection_adapters/postgresql_adapter/extension_methods.rb
Overview
Provides methods to extend ActiveRecord::ConnectionAdapters::PostgreSQLAdapter to support extensions feature.
Constant Summary collapse
- CREATE_EXTENSION_DEFAULTS =
Default options for #create_extension method.
{ if_not_exists: true, schema_name: nil, version: nil, old_version: nil }
- DROP_EXTENSION_DEFAULTS =
Default options for #drop_extension method.
{ if_exists: true, mode: :restrict }
- AVAILABLE_DROP_MODES =
The modes which determine postgresql behavior on DROP EXTENSION operation.
:restrict refuse to drop the extension if any objects depend on it :cascade automatically drop objects that depend on the extension
{ restrict: 'RESTRICT', cascade: 'CASCADE' }
Instance Method Summary collapse
-
#create_extension(extension_name, options = {}) ⇒ Object
Execute SQL to load a postgresql extension module into the current database.
-
#drop_extension(extension_name, options = {}) ⇒ Object
Execute SQL to remove a postgresql extension module from the current database.
-
#enable_extension(extension_name, options = {}) ⇒ Object
Execute SQL to load a postgresql extension module into the current database if it does not already exist.
-
#pg_extensions ⇒ Hash{String => Hash{Symbol => String}}
Query the pg_catalog for all extension modules loaded to the current database.
-
#supports_extensions? ⇒ Boolean
If adapter supports postgresql extension manipulation.
Instance Method Details
#create_extension(extension_name, options = {}) ⇒ Object
Execute SQL to load a postgresql extension module into the current database.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/extension_methods.rb', line 41 def create_extension(extension_name, = {}) = CREATE_EXTENSION_DEFAULTS.merge(.symbolize_keys) sql = ['CREATE EXTENSION'] sql << 'IF NOT EXISTS' if [:if_not_exists] sql << %Q{"#{extension_name.to_s}"} sql << "SCHEMA #{[:schema_name]}" if [:schema_name].present? sql << "VERSION '#{[:version]}'" if [:version].present? sql << "FROM #{[:old_version]}" if [:old_version].present? sql = sql.join(' ') execute(sql) end |
#drop_extension(extension_name, options = {}) ⇒ Object
Execute SQL to remove a postgresql extension module from the current database.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/extension_methods.rb', line 75 def drop_extension(extension_name, = {}) = DROP_EXTENSION_DEFAULTS.merge(.symbolize_keys) sql = ['DROP EXTENSION'] sql << 'IF EXISTS' if [:if_exists] sql << %Q{"#{extension_name.to_s}"} mode = [:mode] if mode.present? mode = mode.to_sym unless AVAILABLE_DROP_MODES.include?(mode) raise ArgumentError, "Expected one of #{AVAILABLE_DROP_MODES.keys.inspect} drop modes, " \ "but #{mode} received" end sql << AVAILABLE_DROP_MODES[mode] end sql = sql.join(' ') execute(sql) end |
#enable_extension(extension_name, options = {}) ⇒ Object
Execute SQL to load a postgresql extension module into the current database if it does not already exist. Then reload the type map.
64 65 66 67 |
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/extension_methods.rb', line 64 def enable_extension(extension_name, = {}) [:if_not_exists] = true create_extension(extension_name, ).tap { reload_type_map } end |
#pg_extensions ⇒ Hash{String => Hash{Symbol => String}}
Since Rails 4 connection has method extensions
that returns array of extensions. This method is slightly different, since it returns also additional options.
Query the pg_catalog for all extension modules loaded to the current database.
Please note all extensions which belong to pg_catalog schema are omitted
Example
extension # => {
"fuzzystrmatch" => {:schema_name => "public", :version => "1.0" }
}
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/extension_methods.rb', line 113 def pg_extensions # Check postgresql version to not break on Postgresql < 9.1 during schema dump return {} if (::PgSaurus::Engine.pg_server_version <=> [9, 1]) < 0 sql = <<-SQL SELECT pge.extname AS ext_name, pgn.nspname AS schema_name, pge.extversion AS ext_version FROM pg_extension pge INNER JOIN pg_namespace pgn on pge.extnamespace = pgn.oid WHERE pgn.nspname <> 'pg_catalog' SQL result = select_all(sql) formatted_result = result.map do |row| [ row['ext_name'], { schema_name: row['schema_name'], version: row['ext_version'] } ] end Hash[formatted_result] end |
#supports_extensions? ⇒ Boolean
Returns if adapter supports postgresql extension manipulation.
28 29 30 |
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/extension_methods.rb', line 28 def supports_extensions? true end |