Skip to content

Commit c6b4b4a

Browse files
kamipojeremy
authored andcommitted
Schema dumping support for PostgreSQL oid type
Closes rails#27980
1 parent 42ed16a commit c6b4b4a

File tree

8 files changed

+33
-13
lines changed

8 files changed

+33
-13
lines changed

activerecord/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
* PostgreSQL: schema dumping support for PostgreSQL interval type.
1+
* PostgreSQL: schema dumping support for interval and OID columns.
22

33
*Ryuta Kamizono*
44

activerecord/lib/active_record/connection_adapters/postgresql/oid.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require "active_record/connection_adapters/postgresql/oid/json"
1212
require "active_record/connection_adapters/postgresql/oid/jsonb"
1313
require "active_record/connection_adapters/postgresql/oid/money"
14+
require "active_record/connection_adapters/postgresql/oid/oid"
1415
require "active_record/connection_adapters/postgresql/oid/point"
1516
require "active_record/connection_adapters/postgresql/oid/legacy_point"
1617
require "active_record/connection_adapters/postgresql/oid/range"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ActiveRecord
2+
module ConnectionAdapters
3+
module PostgreSQL
4+
module OID # :nodoc:
5+
class Oid < Type::Integer # :nodoc:
6+
def type
7+
:oid
8+
end
9+
end
10+
end
11+
end
12+
end
13+
end

activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ def numrange(*args, **options)
124124
args.each { |name| column(name, :numrange, options) }
125125
end
126126

127+
def oid(*args, **options)
128+
args.each { |name| column(name, :oid, options) }
129+
end
130+
127131
def point(*args, **options)
128132
args.each { |name| column(name, :point, options) }
129133
end

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class PostgreSQLAdapter < AbstractAdapter
110110
bit_varying: { name: "bit varying" },
111111
money: { name: "money" },
112112
interval: { name: "interval" },
113+
oid: { name: "oid" },
113114
}
114115

115116
OID = PostgreSQL::OID #:nodoc:
@@ -452,7 +453,7 @@ def initialize_type_map(m)
452453
register_class_with_limit m, "int2", Type::Integer
453454
register_class_with_limit m, "int4", Type::Integer
454455
register_class_with_limit m, "int8", Type::Integer
455-
m.alias_type "oid", "int2"
456+
m.register_type "oid", OID::Oid.new
456457
m.register_type "float4", Type::Float.new
457458
m.alias_type "float8", "float4"
458459
m.register_type "text", Type::Text.new

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_data_type_of_time_types
3333
end
3434

3535
def test_data_type_of_oid_types
36-
assert_equal :integer, @first_oid.column_for_attribute(:obj_id).type
36+
assert_equal :oid, @first_oid.column_for_attribute(:obj_id).type
3737
end
3838

3939
def test_time_values

activerecord/test/cases/schema_dumper_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ def test_schema_dump_interval_type
286286
assert_match %r{t\.interval\s+"scaled_time_interval",\s+precision: 6$}, output
287287
end
288288

289+
def test_schema_dump_oid_type
290+
output = dump_table_schema "postgresql_oids"
291+
assert_match %r{t\.oid\s+"obj_id"$}, output
292+
end
293+
289294
if ActiveRecord::Base.connection.supports_extensions?
290295
def test_schema_dump_includes_extensions
291296
connection = ActiveRecord::Base.connection

activerecord/test/schema/postgresql_specific_schema.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
t.interval :scaled_time_interval, precision: 6
3434
end
3535

36-
%w(postgresql_oids postgresql_timestamp_with_zones
37-
postgresql_partitioned_table postgresql_partitioned_table_parent).each do |table_name|
38-
drop_table table_name, if_exists: true
36+
create_table :postgresql_oids, force: true do |t|
37+
t.oid :obj_id
3938
end
4039

40+
drop_table 'postgresql_timestamp_with_zones', if_exists: true
41+
drop_table 'postgresql_partitioned_table', if_exists: true
42+
drop_table 'postgresql_partitioned_table_parent', if_exists: true
43+
4144
execute "DROP SEQUENCE IF EXISTS companies_nonstd_seq CASCADE"
4245
execute "CREATE SEQUENCE companies_nonstd_seq START 101 OWNED BY companies.id"
4346
execute "ALTER TABLE companies ALTER COLUMN id SET DEFAULT nextval('companies_nonstd_seq')"
@@ -49,13 +52,6 @@
4952
execute "SELECT setval('#{seq_name}', 100)"
5053
end
5154

52-
execute <<_SQL
53-
CREATE TABLE postgresql_oids (
54-
id SERIAL PRIMARY KEY,
55-
obj_id OID
56-
);
57-
_SQL
58-
5955
execute <<_SQL
6056
CREATE TABLE postgresql_timestamp_with_zones (
6157
id SERIAL PRIMARY KEY,

0 commit comments

Comments
 (0)