Update PostgreSQLAdapter#extensions to include schema name

This allows the schema dumper's generated `enable_extension` statements
to include the schema name, if different from `current_schema`.

[Fix #52312]
This commit is contained in:
Tony Novak 2024-07-11 15:32:59 -04:00
parent eaa74eedba
commit 2ea8e89fd1
3 changed files with 52 additions and 1 deletions

View File

@ -1,3 +1,24 @@
* Include schema name in `enable_extension` statements in `db/schema.rb`.
The schema dumper will now include the schema name in generated
`enable_extension` statements if they differ from the current schema.
For example, if you have a migration:
```ruby
enable_extension "heroku_ext.pgcrypto"
enable_extension "pg_stat_statements"
```
then the generated schema dump will also contain:
```ruby
enable_extension "heroku_ext.pgcrypto"
enable_extension "pg_stat_statements"
```
*Tony Novak*
* Fix `ActiveRecord::Encryption::EncryptedAttributeType#type` to return
actual cast type.

View File

@ -494,7 +494,19 @@ module ActiveRecord
end
def extensions
internal_exec_query("SELECT extname FROM pg_extension", "SCHEMA", allow_retry: true, materialize_transactions: false).cast_values
query = <<~SQL
SELECT
pg_extension.extname,
n.nspname AS schema
FROM pg_extension
JOIN pg_namespace n ON pg_extension.extnamespace = n.oid
SQL
internal_exec_query(query, "SCHEMA", allow_retry: true, materialize_transactions: false).cast_values.map do |row|
name, schema = row[0], row[1]
schema = nil if schema == current_schema
[schema, name].compact.join(".")
end
end
# Returns a list of defined enum types, and their values.

View File

@ -561,6 +561,24 @@ module ActiveRecord
@connection.execute("DROP DOMAIN example_type")
end
def test_extensions_omits_current_schema_name
@connection.execute("DROP EXTENSION IF EXISTS hstore")
@connection.execute("CREATE SCHEMA customschema")
@connection.execute("CREATE EXTENSION hstore SCHEMA customschema")
assert_includes @connection.extensions, "customschema.hstore"
ensure
@connection.execute("DROP SCHEMA IF EXISTS customschema CASCADE")
@connection.execute("DROP EXTENSION IF EXISTS hstore")
end
def test_extensions_includes_non_current_schema_name
@connection.execute("DROP EXTENSION IF EXISTS hstore")
@connection.execute("CREATE EXTENSION hstore")
assert_includes @connection.extensions, "hstore"
ensure
@connection.execute("DROP EXTENSION IF EXISTS hstore")
end
def test_ignores_warnings_when_behaviour_ignore
with_db_warnings_action(:ignore) do
# libpq prints a warning to stderr from C, so we need to stub