Skip to content

All AR::ConnectionAdapters#tables return only tables(exclude views)#21601

Merged
senny merged 1 commit intorails:masterfrom
yui-knk:fix/ar_tables_without_view2
Nov 12, 2015
Merged

All AR::ConnectionAdapters#tables return only tables(exclude views)#21601
senny merged 1 commit intorails:masterfrom
yui-knk:fix/ar_tables_without_view2

Conversation

@yui-knk
Copy link
Contributor

@yui-knk yui-knk commented Sep 12, 2015

As reported on #21509,
tables method of adapters for mysql and sqlite include view
but for postgresql does not include. This commit fix all of them
to return only tables(exclude views). Close #21509.

This commit includes these fixes:

Implement tables_and_views for mysql and sqlite

tables method of adapters for mysql and sqlite has two
functions.

  1. Return all tables names
  2. Return all tables names which matches to database name or table name
    (specified by argument)

Move no. 2 function to tables_and_views method.

Change arguments of tables method

Now the responsibility of tables is to return all tables names,
so change this method to be argument-less method.

Change some tests

  • test_tables_quoting in activerecord/test/cases/adapters/mysql2/schema_test.rb
    This test asserts database name is quoted internally. tables_and_views accepts
    database name. So change from @connection.tables to @connection.tables_and_views.
  • test_tables_logs_name in activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
    This test asserts what SQL is executed. SQL created by tables is changed, so change sql
    used to assertion.

@rails-bot
Copy link

r? @arthurnn

(@rails-bot has picked a reviewer for you, use r? to override)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think name is being used anywhere so lets remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can we make this method private? It is not being used anywhere besides test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is my mistakes, I will fix them :)

@rafaelfranca
Copy link
Member

I'm not sure. This is a huge change on behavior. tables is public API and I bet someone is depending in the fact that it returns also view. I vote to just document that it returns views.

@sgrif @senny @matthewd thoughts?

@sgrif
Copy link
Contributor

sgrif commented Sep 12, 2015

This will break apps I have worked on in the past for sure.

@sgrif
Copy link
Contributor

sgrif commented Sep 12, 2015

The inconsistency between PG and the rest bothers me as well, though.

@senny
Copy link
Member

senny commented Sep 13, 2015

We should deal with that inconsistency somehow. It's quite the difference in behavior and client code for all adapters can never be sure what they'll get. I think there's a handful of issues on GitHub about that behavior.

One approach that comes to mind would be deprecating tables() for Rails 5 in favor of tables(views: true) and tables(views: false). This would force callers to be specific about what they want and we can maintain the current inconsistent behavior of tables(). At a later stage (Rails 5.1), we can then move tables() to the new behavior. Either returning both views and tables or just tables. Whichever is preferable.

@yui-knk
Copy link
Contributor Author

yui-knk commented Sep 13, 2015

@rafaelfranca

bet someone is depending in the fact that it returns also view

Sure. But at the same time, I think someone is depending in the fact that it does not return view (postgresql users). I am not sure changing all adapters to returns table + view is better than only return table.

@senny
Now #21609 PR is posted. So user can views + talbes to get both of them. I think tables should return only table in the future. Is it better add views option to tables (tables(views: true)) and waning for Rails 5.0?

@senny
Copy link
Member

senny commented Sep 13, 2015

I need to have a better look where tables is used but my first reaction is that tables should return both tables and views. My thinking is that at it's core a view is a virtual table. Many features that work with a table can also work with a view. That's just a guess though, we need to look through our current usage to determine what's better.

I like the idea of passing in view: true and view: false because this let's us make the change in a completely backwards compatible manner. In the first step we can deprecate calling tables without any options and describing the future behavior change. After that it's save to change tables without any arguments.

@senny senny assigned senny and unassigned arthurnn Sep 13, 2015
@senny
Copy link
Member

senny commented Sep 13, 2015

/cc @matthewd

@senny
Copy link
Member

senny commented Sep 22, 2015

@yui-knk #21609 was merged, is there anything that you'd like to update?

@senny
Copy link
Member

senny commented Sep 22, 2015

@yui-knk please hold changes to this PR, I'm currently working on a patch that should make the situation for tables more clear. I'll ping you on the PR when it's ready.

@senny
Copy link
Member

senny commented Sep 22, 2015

@yui-knk I merged my patch. We can now put in place the necessary deprecations to change the behavior of #tables. I suggest the following:

  • Deprecate #tables on all the adapters, where views and tables are returned in favor of data_sources but keep the current implementation.
  • Keep #tables as is on adapters that only return tables.

Then after 5.0 lands no-one should be relying on #tables behavior with views any longer and we can safely change the implementations and remove the deprecations.

This means that in the transition phase. Basically 5.0 some adapters won't have a method to only get tables. But that shouldn't be too much of a problem as that is the same situation that we have right now.

@yui-knk yui-knk force-pushed the fix/ar_tables_without_view2 branch 2 times, most recently from 03db04d to 308406e Compare September 23, 2015 15:36
@yui-knk
Copy link
Contributor Author

yui-knk commented Sep 23, 2015

@senny I updated to only warning!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like no need And.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank, I fixed it. Copy & paste is evil 👿

@yui-knk yui-knk force-pushed the fix/ar_tables_without_view2 branch from 308406e to ddb3eae Compare September 23, 2015 15:59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the following wording:

    #tables currently returns both tables and views.
    This behavior is deprecated and will be changed with Rails 5.1 to only return tables.
    Use #data_sources instead.

@senny
Copy link
Member

senny commented Sep 30, 2015

@yui-knk running the update produces tons of warnings. We should change our internals to no longer depend on any of the deprecated behavior. This means to also touch on the behavior of table_eixsts? and look into deprecations on that end as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SchemaMigration#table_exists? is used for SchemaMigration#create_table and SchemaMigration#drop_table. It seems that should use table_exists? rather than data_source_exists?.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto :)

Reported on rails#21509, how views is treated by `#tables` are differ
by each adapters. To fix this different behavior, after Rails 5.0
is released, deprecate `#tables`.

And `#table_exists?` would check both tables and views.
To make their behavior consistent with `#tables`, after Rails 5.0
is released, deprecate `#table_exists?`.
@yui-knk yui-knk force-pushed the fix/ar_tables_without_view2 branch from 94f0440 to 7429633 Compare November 9, 2015 14:13
senny added a commit that referenced this pull request Nov 12, 2015
All `AR::ConnectionAdapters#tables` return only tables(exclude views)
@senny senny merged commit 19398ab into rails:master Nov 12, 2015
@senny
Copy link
Member

senny commented Nov 12, 2015

🎉 @yui-knk thank you for working on this! 💛

@yui-knk
Copy link
Contributor Author

yui-knk commented Nov 12, 2015

@senny 🎉 thanks for your review !!!! 😍

@yui-knk yui-knk deleted the fix/ar_tables_without_view2 branch November 12, 2015 15:26
kamipo added a commit to kamipo/rails that referenced this pull request Nov 19, 2015
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request May 20, 2016
also deprecate `#table_exists?`, `#tables` and passing arguments to `#tables`

Refer rails/rails#21601
rails/rails#21509
kamipo added a commit to kamipo/rails that referenced this pull request Jan 3, 2017
Passing `name` to `tables` is already deprecated at rails#21601.
Passing `name` to `indexes` is also unused.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Behavior of AR::ConnectionAdapters handling VIEW differ

7 participants