Skip to content

Commit

Permalink
Schema dumping support for PostgreSQL oid type
Browse files Browse the repository at this point in the history
Closes #27980
  • Loading branch information
kamipo authored and jeremy committed Feb 13, 2017
1 parent 42ed16a commit c6b4b4a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* PostgreSQL: schema dumping support for PostgreSQL interval type.
* PostgreSQL: schema dumping support for interval and OID columns.

*Ryuta Kamizono*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require "active_record/connection_adapters/postgresql/oid/json"
require "active_record/connection_adapters/postgresql/oid/jsonb"
require "active_record/connection_adapters/postgresql/oid/money"
require "active_record/connection_adapters/postgresql/oid/oid"
require "active_record/connection_adapters/postgresql/oid/point"
require "active_record/connection_adapters/postgresql/oid/legacy_point"
require "active_record/connection_adapters/postgresql/oid/range"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module OID # :nodoc:
class Oid < Type::Integer # :nodoc:
def type
:oid
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def numrange(*args, **options)
args.each { |name| column(name, :numrange, options) }
end

def oid(*args, **options)
args.each { |name| column(name, :oid, options) }
end

def point(*args, **options)
args.each { |name| column(name, :point, options) }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class PostgreSQLAdapter < AbstractAdapter
bit_varying: { name: "bit varying" },
money: { name: "money" },
interval: { name: "interval" },
oid: { name: "oid" },
}

OID = PostgreSQL::OID #:nodoc:
Expand Down Expand Up @@ -452,7 +453,7 @@ def initialize_type_map(m)
register_class_with_limit m, "int2", Type::Integer
register_class_with_limit m, "int4", Type::Integer
register_class_with_limit m, "int8", Type::Integer
m.alias_type "oid", "int2"
m.register_type "oid", OID::Oid.new
m.register_type "float4", Type::Float.new
m.alias_type "float8", "float4"
m.register_type "text", Type::Text.new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_data_type_of_time_types
end

def test_data_type_of_oid_types
assert_equal :integer, @first_oid.column_for_attribute(:obj_id).type
assert_equal :oid, @first_oid.column_for_attribute(:obj_id).type
end

def test_time_values
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ def test_schema_dump_interval_type
assert_match %r{t\.interval\s+"scaled_time_interval",\s+precision: 6$}, output
end

def test_schema_dump_oid_type
output = dump_table_schema "postgresql_oids"
assert_match %r{t\.oid\s+"obj_id"$}, output
end

if ActiveRecord::Base.connection.supports_extensions?
def test_schema_dump_includes_extensions
connection = ActiveRecord::Base.connection
Expand Down
16 changes: 6 additions & 10 deletions activerecord/test/schema/postgresql_specific_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
t.interval :scaled_time_interval, precision: 6
end

%w(postgresql_oids postgresql_timestamp_with_zones
postgresql_partitioned_table postgresql_partitioned_table_parent).each do |table_name|
drop_table table_name, if_exists: true
create_table :postgresql_oids, force: true do |t|
t.oid :obj_id
end

drop_table 'postgresql_timestamp_with_zones', if_exists: true
drop_table 'postgresql_partitioned_table', if_exists: true
drop_table 'postgresql_partitioned_table_parent', if_exists: true

execute "DROP SEQUENCE IF EXISTS companies_nonstd_seq CASCADE"
execute "CREATE SEQUENCE companies_nonstd_seq START 101 OWNED BY companies.id"
execute "ALTER TABLE companies ALTER COLUMN id SET DEFAULT nextval('companies_nonstd_seq')"
Expand All @@ -49,13 +52,6 @@
execute "SELECT setval('#{seq_name}', 100)"
end

execute <<_SQL
CREATE TABLE postgresql_oids (
id SERIAL PRIMARY KEY,
obj_id OID
);
_SQL

execute <<_SQL
CREATE TABLE postgresql_timestamp_with_zones (
id SERIAL PRIMARY KEY,
Expand Down

0 comments on commit c6b4b4a

Please sign in to comment.