Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for PostgreSQL foreign tables #31549

Merged
merged 1 commit into from Jan 22, 2018
Merged

Conversation

@fatkodima
Copy link
Contributor

@fatkodima fatkodima commented Dec 23, 2017

The change is pretty minimal, the query used in #table_exists?/#tables has been expanded to include foreign tables in the kinds of relations it includes in its search.

Related #28886.

@rails-bot
Copy link

@rails-bot rails-bot commented Dec 23, 2017

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@kaspth
Copy link
Member

@kaspth kaspth commented Jan 20, 2018

r? @kamipo

I’m a little out of my element here, thanks 😊

@kamipo kamipo assigned kamipo and unassigned kaspth Jan 22, 2018
@kamipo
Copy link
Member

@kamipo kamipo commented Jan 22, 2018

Looks fine that finding foreign tables in data_source_exists?/data_sources because it is used for SchemaCache.

def data_source_exists?(name)
prepare_data_sources if @data_sources.empty?
return @data_sources[name] if @data_sources.key? name
@data_sources[name] = connection.data_source_exists?(name)
end

But I'd not like to find that in table_exists?/tables because connection.tables is used in SchemaDumper and SchemaDumper does not have the ability to dump foreign tables properly.

def tables(stream)
sorted_tables = @connection.tables.sort
sorted_tables.each do |table_name|
table(table_name, stream) unless ignored?(table_name)
end

@fatkodima fatkodima force-pushed the fatkodima:foreign_tables branch Jan 22, 2018
@fatkodima
Copy link
Contributor Author

@fatkodima fatkodima commented Jan 22, 2018

@kamipo Thanks for review! Updated according to your comments.

@matthewd
Copy link
Member

@matthewd matthewd commented Jan 22, 2018

Hmm.. instead of hiding it completely, maybe we should treat it as a special type of a view? Our definition of view is pretty much "table-like thing that you can query etc, but isn't really a table".

OTOH, data_sources is clear that it can return things that are neither a table nor a view. So this is also quite reasonable as is.

Just a thought; I'll defer to @kamipo.

@kamipo
Copy link
Member

@kamipo kamipo commented Jan 22, 2018

Treating foreign tables as a view may break scenic gem.
At least we have one method to work that gem.

# Keep it for indexing materialized views
def indexes(table, stream)
if (indexes = @connection.indexes(table)).any?
add_index_statements = indexes.map do |index|
table_name = remove_prefix_and_suffix(index.table).inspect
" add_index #{([table_name] + index_parts(index)).join(', ')}"
end
stream.puts add_index_statements.sort.join("\n")
stream.puts
end
end

Probably we should add foreign_table_exists?/foreign_tables to not break any other functionality.

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 c6e5122daf..bdccd53ff0 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -527,6 +527,14 @@ def foreign_keys(table_name)
           end
         end
 
+      def foreign_tables
+        query_values(data_source_sql(type: "FOREIGN TABLE"), "SCHEMA")
+      end
+
+      def foreign_table_exists?(table_name)
+        query_values(data_source_sql(table_name, type: "FOREIGN TABLE"), "SCHEMA").any? if table_name.present?
+      end
+
         # Maps logical Rails types to PostgreSQL-specific data types.
         def type_to_sql(type, limit: nil, precision: nil, scale: nil, array: nil, **) # :nodoc:
           sql = \
@@ -756,6 +764,8 @@ def quoted_scope(name = nil, type: nil)
                 "'r'"
               when "VIEW"
                 "'v','m'"
+              when "FOREIGN TABLE"
+                "'f'"
               end
             scope = {}
             scope[:schema] = schema ? quote(schema) : "ANY (current_schemas(false))"
activerecord/test/cases/adapters/postgresql/foreign_table_test.rb Outdated

if ActiveRecord::Base.connection.supports_foreign_tables?
class ForeignTableTest < ActiveRecord::TestCase
fixtures :professors

This comment has been minimized.

@kamipo

kamipo Jan 22, 2018
Member

Since this fixture is used only in the tests and have only one record, can you setup in setup in the tests directly?

# frozen_string_literal: true

require "cases/helper"
require "models/professor"

This comment has been minimized.

@kamipo

kamipo Jan 22, 2018
Member

Professor model is unused.

This comment has been minimized.

@fatkodima

fatkodima Jan 22, 2018
Author Contributor

This is now used.

@fatkodima fatkodima force-pushed the fatkodima:foreign_tables branch Jan 22, 2018
@fatkodima
Copy link
Contributor Author

@fatkodima fatkodima commented Jan 22, 2018

@kamipo fixed your notes.

@kamipo
kamipo approved these changes Jan 22, 2018
Copy link
Member

@kamipo kamipo left a comment

Looks good to me. Can you squash your commits into one?

activerecord/test/cases/adapters/postgresql/foreign_table_test.rb Outdated

def test_attributes
professor = ForeignProfessorWithPk.find(@professor.id)
assert_equal({ "id" => @professor.id, "name" => "Nicola" }, professor.attributes)

This comment has been minimized.

@kamipo

kamipo Jan 22, 2018
Member

How about assert_equal @professor.attributes, professor.attributes?

@fatkodima fatkodima force-pushed the fatkodima:foreign_tables branch to 3ad2f99 Jan 22, 2018
@fatkodima
Copy link
Contributor Author

@fatkodima fatkodima commented Jan 22, 2018

Fixed last comment, squashed and rebased.

@kamipo kamipo merged commit 742c9ba into rails:master Jan 22, 2018
2 checks passed
2 checks passed
codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@fatkodima fatkodima deleted the fatkodima:foreign_tables branch Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants