mirror of https://github.com/rails/rails
Adds schema parameter into enable_extension
This patch tries to solve Heroku's new [PostgreSQL extension policy](https://devcenter.heroku.com/changelog-items/2446) while keeping the migration and schema code idiomatic. PostgreSQL adapter method `enable_extension` now allows to add an schema in its name. The extension must be installed on another schema. Usage: `enable_extension('other_schema.hstore')` The `enable_extension` can work with `schema` only if the given schema already exists in the database.
This commit is contained in:
parent
d192ba1098
commit
7e95d3e653
|
@ -1,3 +1,11 @@
|
|||
* PostgreSQL adapter method `enable_extension` now allows parameter to be `[schema_name.]<extension_name>`
|
||||
if the extension must be installed on another schema.
|
||||
|
||||
Example: `enable_extension('heroku_ext.hstore')`
|
||||
|
||||
*Leonardo Luarte*
|
||||
|
||||
|
||||
* `ActiveRecord::Relation`’s `#any?`, `#none?`, and `#one?` methods take an optional pattern
|
||||
argument, more closely matching their `Enumerable` equivalents.
|
||||
|
||||
|
|
|
@ -442,9 +442,11 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def enable_extension(name, **)
|
||||
exec_query("CREATE EXTENSION IF NOT EXISTS \"#{name}\"").tap {
|
||||
reload_type_map
|
||||
}
|
||||
schema, name = name.to_s.split(".").values_at(-2, -1)
|
||||
sql = +"CREATE EXTENSION IF NOT EXISTS \"#{name}\""
|
||||
sql << " SCHEMA #{schema}" if schema
|
||||
|
||||
exec_query(sql).tap { reload_type_map }
|
||||
end
|
||||
|
||||
# Removes an extension from the database.
|
||||
|
|
|
@ -17,6 +17,12 @@ class PostgresqlExtensionMigrationTest < ActiveRecord::PostgreSQLTestCase
|
|||
end
|
||||
end
|
||||
|
||||
class EnableHstoreInSchema < ActiveRecord::Migration::Current
|
||||
def change
|
||||
enable_extension "other_schema.hstore"
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
super
|
||||
|
||||
|
@ -50,6 +56,19 @@ class PostgresqlExtensionMigrationTest < ActiveRecord::PostgreSQLTestCase
|
|||
assert @connection.extension_enabled?("hstore"), "extension hstore should be enabled"
|
||||
end
|
||||
|
||||
def test_enable_extension_migration_with_schema
|
||||
@connection.disable_extension("hstore")
|
||||
@connection.create_schema "other_schema"
|
||||
|
||||
migrations = [EnableHstoreInSchema.new(nil, 1)]
|
||||
ActiveRecord::Migrator.new(:up, migrations, @connection.schema_migration, @connection.internal_metadata).migrate
|
||||
|
||||
assert @connection.extension_enabled?("hstore"), "extension hstore should be enabled"
|
||||
ensure
|
||||
@connection.drop_schema "other_schema", if_exists: true
|
||||
end
|
||||
|
||||
|
||||
def test_disable_extension_migration_ignores_prefix_and_suffix
|
||||
@connection.enable_extension("hstore")
|
||||
|
||||
|
|
Loading…
Reference in New Issue