Skip to content

Commit cd912ad

Browse files
authored
Merge pull request rails#52452 from afn/disable-extension-with-schema
Allow disable_extension to be called with schema-qualified name
2 parents e3ec553 + eb6d170 commit cd912ad

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* PostgreSQLAdapter: Allow `disable_extension` to be called with schema-qualified name.
2+
3+
For parity with `enable_extension`, the `disable_extension` method can be called with a schema-qualified
4+
name (e.g. `disable_extension "myschema.pgcrypto"`). Note that PostgreSQL's `DROP EXTENSION` does not
5+
actually take a schema name (unlike `CREATE EXTENSION`), so the resulting SQL statement will only name
6+
the extension, e.g. `DROP EXTENSION IF EXISTS "pgcrypto"`.
7+
8+
*Tony Novak*
9+
110
* Make `create_schema` / `drop_schema` reversible in migrations.
211

312
Previously, `create_schema` and `drop_schema` were irreversible migration operations.

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ def enable_extension(name, **)
479479
# Set to +:cascade+ to drop dependent objects as well.
480480
# Defaults to false.
481481
def disable_extension(name, force: false)
482+
_schema, name = name.to_s.split(".").values_at(-2, -1)
482483
internal_exec_query("DROP EXTENSION IF EXISTS \"#{name}\"#{' CASCADE' if force == :cascade}").tap {
483484
reload_type_map
484485
}

activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,32 @@ def test_date_decoding_disabled
669669
assert_equal String, date.class
670670
end
671671

672+
def test_disable_extension_with_schema
673+
@connection.execute("CREATE SCHEMA custom_schema")
674+
@connection.execute("CREATE EXTENSION hstore SCHEMA custom_schema")
675+
result = @connection.query("SELECT extname FROM pg_extension WHERE extnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'custom_schema')")
676+
assert_equal [["hstore"]], result.to_a
677+
678+
@connection.disable_extension "custom_schema.hstore"
679+
result = @connection.query("SELECT extname FROM pg_extension WHERE extnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'custom_schema')")
680+
assert_equal [], result.to_a
681+
ensure
682+
@connection.execute("DROP EXTENSION IF EXISTS hstore")
683+
@connection.execute("DROP SCHEMA IF EXISTS custom_schema CASCADE")
684+
end
685+
686+
def test_disable_extension_without_schema
687+
@connection.execute("CREATE EXTENSION hstore")
688+
result = @connection.query("SELECT extname FROM pg_extension")
689+
assert_includes result.to_a, ["hstore"]
690+
691+
@connection.disable_extension "hstore"
692+
result = @connection.query("SELECT extname FROM pg_extension")
693+
assert_not_includes result.to_a, ["hstore"]
694+
ensure
695+
@connection.execute("DROP EXTENSION IF EXISTS hstore")
696+
end
697+
672698
private
673699
def with_postgresql_apdater_decode_dates
674700
PostgreSQLAdapter.decode_dates = true

0 commit comments

Comments
 (0)