Skip to content

Commit

Permalink
Merge pull request rails#28740 from matthewd/old-postgres
Browse files Browse the repository at this point in the history
Use a query that's compatible with PostgreSQL 9.2
  • Loading branch information
matthewd committed Apr 12, 2017
2 parents 2b4583f + 7384771 commit 0eb2187
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
13 changes: 6 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ matrix:
- "GEM=ar:mysql2 MYSQL=mariadb"
addons:
mariadb: 10.0
- rvm: 2.4.1
- rvm: 2.3.4
env:
- "GEM=ar:sqlite3_mem"
- rvm: 2.3.4
env:
- "GEM=ar:postgresql POSTGRES=9.2"
addons:
postgresql: "9.2"
- rvm: jruby-9.1.8.0
jdk: oraclejdk8
env:
Expand All @@ -104,12 +109,6 @@ matrix:
jdk: oraclejdk8
env:
- "GEM=am,amo,aj"
# Test with old (< 9.4.2) postgresql
- rvm: 2.4.1
env:
- "GEM=ar:postgresql"
addons:
postgresql: "9.4"
allow_failures:
- rvm: ruby-head
- rvm: jruby-9.1.8.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,17 @@ def pk_and_sequence_for(table) #:nodoc:

def primary_keys(table_name) # :nodoc:
select_values(<<-SQL.strip_heredoc, "SCHEMA")
SELECT a.attname FROM pg_index i
CROSS JOIN generate_subscripts(i.indkey, 1) k
JOIN pg_attribute a
ON a.attrelid = i.indrelid
AND a.attnum = i.indkey[k]
WHERE i.indrelid = #{quote(quote_table_name(table_name))}::regclass
AND i.indisprimary
SELECT a.attname
FROM (
SELECT indrelid, indkey, generate_subscripts(indkey, 1) idx
FROM pg_index
WHERE indrelid = #{quote(quote_table_name(table_name))}::regclass
AND indisprimary
) i
JOIN pg_attribute a
ON a.attrelid = i.indrelid
AND a.attnum = i.indkey[i.idx]
ORDER BY i.idx
SQL
end

Expand Down
10 changes: 7 additions & 3 deletions activerecord/test/cases/adapters/postgresql/uuid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def drop_table(name)
def uuid_function
connection.supports_pgcrypto_uuid? ? "gen_random_uuid()" : "uuid_generate_v4()"
end

def uuid_default
connection.supports_pgcrypto_uuid? ? {} : { default: uuid_function }
end
end

class PostgresqlUUIDTest < ActiveRecord::PostgreSQLTestCase
Expand Down Expand Up @@ -178,7 +182,7 @@ class UUID < ActiveRecord::Base
t.uuid "other_uuid_2", default: "my_uuid_generator()"
end

connection.create_table("pg_uuids_3", id: :uuid) do |t|
connection.create_table("pg_uuids_3", id: :uuid, **uuid_default) do |t|
t.string "name"
end
end
Expand Down Expand Up @@ -320,10 +324,10 @@ class UuidComment < ActiveRecord::Base

setup do
connection.transaction do
connection.create_table("pg_uuid_posts", id: :uuid) do |t|
connection.create_table("pg_uuid_posts", id: :uuid, **uuid_default) do |t|
t.string "title"
end
connection.create_table("pg_uuid_comments", id: :uuid) do |t|
connection.create_table("pg_uuid_comments", id: :uuid, **uuid_default) do |t|
t.references :uuid_post, type: :uuid
t.string "content"
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/migration/rename_table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_rename_table_for_postgresql_should_also_rename_default_sequence
end

def test_renaming_table_doesnt_attempt_to_rename_non_existent_sequences
connection.create_table :cats, id: :uuid
connection.create_table :cats, id: :uuid, default: "uuid_generate_v4()"
assert_nothing_raised { rename_table :cats, :felines }
assert connection.table_exists? :felines
ensure
Expand Down
4 changes: 3 additions & 1 deletion activerecord/test/cases/view_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ def test_does_not_dump_view_as_table
end

# sqlite dose not support CREATE, INSERT, and DELETE for VIEW
if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter, :SQLServerAdapter)
if current_adapter?(:Mysql2Adapter, :SQLServerAdapter) ||
current_adapter?(:PostgreSQLAdapter) && ActiveRecord::Base.connection.postgresql_version >= 90300

class UpdateableViewTest < ActiveRecord::TestCase
self.use_transactional_tests = false
fixtures :books
Expand Down
8 changes: 5 additions & 3 deletions activerecord/test/schema/postgresql_specific_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
enable_extension!("uuid-ossp", ActiveRecord::Base.connection)
enable_extension!("pgcrypto", ActiveRecord::Base.connection) if ActiveRecord::Base.connection.supports_pgcrypto_uuid?

create_table :uuid_parents, id: :uuid, force: true do |t|
uuid_default = connection.supports_pgcrypto_uuid? ? {} : { default: "uuid_generate_v4()" }

create_table :uuid_parents, id: :uuid, force: true, **uuid_default do |t|
t.string :name
end

create_table :uuid_children, id: :uuid, force: true do |t|
create_table :uuid_children, id: :uuid, force: true, **uuid_default do |t|
t.string :name
t.uuid :uuid_parent_id
end
Expand Down Expand Up @@ -102,7 +104,7 @@
end

create_table :uuid_items, force: true, id: false do |t|
t.uuid :uuid, primary_key: true
t.uuid :uuid, primary_key: true, **uuid_default
t.string :title
end
end

0 comments on commit 0eb2187

Please sign in to comment.