diff options
Diffstat (limited to 'activerecord')
5 files changed, 27 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 1d439acb07..02a6da2f71 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -344,13 +344,17 @@ module ActiveRecord          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 diff --git a/activerecord/test/cases/adapters/postgresql/uuid_test.rb b/activerecord/test/cases/adapters/postgresql/uuid_test.rb index 6aa6a79705..52e4a38cae 100644 --- a/activerecord/test/cases/adapters/postgresql/uuid_test.rb +++ b/activerecord/test/cases/adapters/postgresql/uuid_test.rb @@ -13,6 +13,10 @@ module PostgresqlUUIDHelper    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 @@ -178,7 +182,7 @@ class PostgresqlUUIDGenerationTest < ActiveRecord::PostgreSQLTestCase        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 @@ -320,10 +324,10 @@ class PostgresqlUUIDTestInverseOf < ActiveRecord::PostgreSQLTestCase    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 diff --git a/activerecord/test/cases/migration/rename_table_test.rb b/activerecord/test/cases/migration/rename_table_test.rb index 19588d28a2..7bcabd0cc6 100644 --- a/activerecord/test/cases/migration/rename_table_test.rb +++ b/activerecord/test/cases/migration/rename_table_test.rb @@ -80,7 +80,7 @@ module ActiveRecord          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 diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb index 07288568e8..1d21a2454f 100644 --- a/activerecord/test/cases/view_test.rb +++ b/activerecord/test/cases/view_test.rb @@ -154,7 +154,9 @@ if ActiveRecord::Base.connection.supports_views?    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 diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index 860c63b27c..e56e8fa36a 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -3,11 +3,13 @@ ActiveRecord::Schema.define do    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 @@ -102,7 +104,7 @@ _SQL    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  | 
