Skip to content

Commit 39c43fa

Browse files
BuonOmorafiss
authored andcommitted
fix(#schema_names): Do not show crdb_internal schema
The `schema_names` method is used to list the schemas in the database. It should not list internal schemas. Implementation detail: we use the less performant ruby implementation rather than rewritting the method for maintainance costs. Fixes #314
1 parent 0fe2427 commit 39c43fa

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Ongoing
44

55
- Support arbitrary max identifier length ([#317](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/317)).
6+
- Fix `#schema_names` not to return `crdb_internal` ([#316](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/316))
67

78
## 7.1.0 - 2024-01-30
89

lib/active_record/connection_adapters/cockroachdb/database_tasks.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def structure_dump(filename, extra_flags=nil)
2626
old_search_path = conn.schema_search_path
2727
conn.schema_search_path = search_path
2828
File.open(filename, "w") do |file|
29+
# NOTE: There is no issue with the crdb_internal schema, it is ignored by SHOW CREATE.
2930
%w(SCHEMAS TYPES).each do |object_kind|
3031
ActiveRecord::Base.connection.execute("SHOW CREATE ALL #{object_kind}").each_row { file.puts _1 }
3132
end
@@ -44,7 +45,7 @@ def structure_dump(filename, extra_flags=nil)
4445

4546
file.puts sql
4647
end
47-
file.puts "SET seach_path TO #{conn.schema_search_path};\n\n"
48+
file.puts "SET search_path TO #{conn.schema_search_path};\n\n"
4849
end
4950
ensure
5051
conn.schema_search_path = old_search_path

lib/active_record/connection_adapters/cockroachdb/schema_statements.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ module CockroachDB
44
module SchemaStatements
55
include ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements
66

7+
# OVERRIDE: We do not want to see the crdb_internal schema in the names.
8+
#
9+
# Returns an array of schema names.
10+
def schema_names
11+
super - ["crdb_internal"]
12+
end
13+
714
def add_index(table_name, column_name, **options)
815
super
916
rescue ActiveRecord::StatementInvalid => error

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "rgeo/active_record"
24

35
require_relative "../../arel/nodes/join_source_ext"
@@ -101,7 +103,7 @@ def initialize(pool_config)
101103
ConnectionPool.prepend(CockroachDBConnectionPool)
102104

103105
class CockroachDBAdapter < PostgreSQLAdapter
104-
ADAPTER_NAME = "CockroachDB".freeze
106+
ADAPTER_NAME = "CockroachDB"
105107
DEFAULT_PRIMARY_KEY = "rowid"
106108

107109
SPATIAL_COLUMN_OPTIONS =

test/cases/adapters/postgresql/schema_statements_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
require 'models/building'
55

66
class SchemaStatementsTest < ActiveRecord::PostgreSQLTestCase
7+
def test_no_crdb_internal_in_schema_names
8+
assert_not_includes Building.connection.schema_names, "crdb_internal"
9+
end
10+
711
def test_initialize_type_map
812
initialized_types = Building.connection.send(:type_map).keys
913

0 commit comments

Comments
 (0)