Skip to content

Commit c44d7ae

Browse files
authored
Merge pull request rails#52313 from afn/include-schema-name-in-enable-extension
Update PostgreSQLAdapter#extensions to include schema name
2 parents 05e9bd2 + 2ea8e89 commit c44d7ae

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
* Include schema name in `enable_extension` statements in `db/schema.rb`.
2+
3+
The schema dumper will now include the schema name in generated
4+
`enable_extension` statements if they differ from the current schema.
5+
6+
For example, if you have a migration:
7+
8+
```ruby
9+
enable_extension "heroku_ext.pgcrypto"
10+
enable_extension "pg_stat_statements"
11+
```
12+
13+
then the generated schema dump will also contain:
14+
15+
```ruby
16+
enable_extension "heroku_ext.pgcrypto"
17+
enable_extension "pg_stat_statements"
18+
```
19+
20+
*Tony Novak*
21+
122
* Fix `ActiveRecord::Encryption::EncryptedAttributeType#type` to return
223
actual cast type.
324

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,19 @@ def extension_enabled?(name)
494494
end
495495

496496
def extensions
497-
internal_exec_query("SELECT extname FROM pg_extension", "SCHEMA", allow_retry: true, materialize_transactions: false).cast_values
497+
query = <<~SQL
498+
SELECT
499+
pg_extension.extname,
500+
n.nspname AS schema
501+
FROM pg_extension
502+
JOIN pg_namespace n ON pg_extension.extnamespace = n.oid
503+
SQL
504+
505+
internal_exec_query(query, "SCHEMA", allow_retry: true, materialize_transactions: false).cast_values.map do |row|
506+
name, schema = row[0], row[1]
507+
schema = nil if schema == current_schema
508+
[schema, name].compact.join(".")
509+
end
498510
end
499511

500512
# Returns a list of defined enum types, and their values.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,24 @@ def test_only_check_for_insensitive_comparison_capability_once
561561
@connection.execute("DROP DOMAIN example_type")
562562
end
563563

564+
def test_extensions_omits_current_schema_name
565+
@connection.execute("DROP EXTENSION IF EXISTS hstore")
566+
@connection.execute("CREATE SCHEMA customschema")
567+
@connection.execute("CREATE EXTENSION hstore SCHEMA customschema")
568+
assert_includes @connection.extensions, "customschema.hstore"
569+
ensure
570+
@connection.execute("DROP SCHEMA IF EXISTS customschema CASCADE")
571+
@connection.execute("DROP EXTENSION IF EXISTS hstore")
572+
end
573+
574+
def test_extensions_includes_non_current_schema_name
575+
@connection.execute("DROP EXTENSION IF EXISTS hstore")
576+
@connection.execute("CREATE EXTENSION hstore")
577+
assert_includes @connection.extensions, "hstore"
578+
ensure
579+
@connection.execute("DROP EXTENSION IF EXISTS hstore")
580+
end
581+
564582
def test_ignores_warnings_when_behaviour_ignore
565583
with_db_warnings_action(:ignore) do
566584
# libpq prints a warning to stderr from C, so we need to stub

0 commit comments

Comments
 (0)