Skip to content

Commit 5973a98

Browse files
committed
#tables and #table_exists? and returns only tables and not views
1 parent d5be101 commit 5973a98

19 files changed

Lines changed: 131 additions & 166 deletions

File tree

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* `#tables` and `#table_exists?` and returns only tables and not views.
2+
3+
All the deprecations on those methods were removed.
4+
5+
*Rafael Mendonça França*
6+
17
* Remove deprecated `name` argument from `#tables`.
28

39
*Rafael Mendonça França*

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -311,38 +311,35 @@ def collation
311311
end
312312

313313
def tables # :nodoc:
314-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
315-
#tables currently returns both tables and views.
316-
This behavior is deprecated and will be changed with Rails 5.1 to only return tables.
317-
Use #data_sources instead.
318-
MSG
314+
sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE'"
315+
sql << " AND table_schema = #{quote(@config[:database])}"
319316

320-
data_sources
317+
select_values(sql, "SCHEMA")
318+
end
319+
320+
def views # :nodoc:
321+
select_values("SHOW FULL TABLES WHERE table_type = 'VIEW'", "SCHEMA")
321322
end
322323

323-
def data_sources
324+
def data_sources # :nodoc:
324325
sql = "SELECT table_name FROM information_schema.tables "
325326
sql << "WHERE table_schema = #{quote(@config[:database])}"
326327

327328
select_values(sql, "SCHEMA")
328329
end
329330

330-
def truncate(table_name, name = nil)
331-
execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name
332-
end
331+
def table_exists?(table_name) # :nodoc:
332+
return false unless table_name.present?
333333

334-
def table_exists?(table_name)
335-
# Update lib/active_record/internal_metadata.rb when this gets removed
336-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
337-
#table_exists? currently checks both tables and views.
338-
This behavior is deprecated and will be changed with Rails 5.1 to only check tables.
339-
Use #data_source_exists? instead.
340-
MSG
334+
schema, name = extract_schema_qualified_name(table_name)
341335

342-
data_source_exists?(table_name)
336+
sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE'"
337+
sql << " AND table_schema = #{quote(schema)} AND table_name = #{quote(name)}"
338+
339+
select_values(sql, "SCHEMA").any?
343340
end
344341

345-
def data_source_exists?(table_name)
342+
def data_source_exists?(table_name) # :nodoc:
346343
return false unless table_name.present?
347344

348345
schema, name = extract_schema_qualified_name(table_name)
@@ -353,10 +350,6 @@ def data_source_exists?(table_name)
353350
select_values(sql, "SCHEMA").any?
354351
end
355352

356-
def views # :nodoc:
357-
select_values("SHOW FULL TABLES WHERE table_type = 'VIEW'", "SCHEMA")
358-
end
359-
360353
def view_exists?(view_name) # :nodoc:
361354
return false unless view_name.present?
362355

@@ -368,6 +361,10 @@ def view_exists?(view_name) # :nodoc:
368361
select_values(sql, "SCHEMA").any?
369362
end
370363

364+
def truncate(table_name, name = nil)
365+
execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name
366+
end
367+
371368
# Returns an array of indexes for the given table.
372369
def indexes(table_name, name = nil) #:nodoc:
373370
indexes = []

activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,40 +85,42 @@ def data_sources # :nodoc
8585
SQL
8686
end
8787

88+
def views # :nodoc:
89+
select_values(<<-SQL, "SCHEMA")
90+
SELECT c.relname
91+
FROM pg_class c
92+
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
93+
WHERE c.relkind IN ('v','m') -- (v)iew, (m)aterialized view
94+
AND n.nspname = ANY (current_schemas(false))
95+
SQL
96+
end
97+
8898
# Returns true if table exists.
8999
# If the schema is not specified as part of +name+ then it will only find tables within
90100
# the current schema search path (regardless of permissions to access tables in other schemas)
91101
def table_exists?(name)
92-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
93-
#table_exists? currently checks both tables and views.
94-
This behavior is deprecated and will be changed with Rails 5.1 to only check tables.
95-
Use #data_source_exists? instead.
96-
MSG
97-
98-
data_source_exists?(name)
99-
end
100-
101-
def data_source_exists?(name)
102102
name = Utils.extract_schema_qualified_name(name.to_s)
103103
return false unless name.identifier
104104

105105
select_values(<<-SQL, "SCHEMA").any?
106-
SELECT c.relname
107-
FROM pg_class c
108-
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
109-
WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view
110-
AND c.relname = #{quote(name.identifier)}
111-
AND n.nspname = #{name.schema ? quote(name.schema) : "ANY (current_schemas(false))"}
106+
SELECT tablename
107+
FROM pg_tables
108+
WHERE tablename = #{quote(name.identifier)}
109+
AND schemaname = #{name.schema ? quote(name.schema) : "ANY (current_schemas(false))"}
112110
SQL
113111
end
114112

115-
def views # :nodoc:
116-
select_values(<<-SQL, "SCHEMA")
113+
def data_source_exists?(name) # :nodoc:
114+
name = Utils.extract_schema_qualified_name(name.to_s)
115+
return false unless name.identifier
116+
117+
select_values(<<-SQL, "SCHEMA").any?
117118
SELECT c.relname
118119
FROM pg_class c
119120
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
120-
WHERE c.relkind IN ('v','m') -- (v)iew, (m)aterialized view
121-
AND n.nspname = ANY (current_schemas(false))
121+
WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view
122+
AND c.relname = #{quote(name.identifier)}
123+
AND n.nspname = #{name.schema ? quote(name.schema) : "ANY (current_schemas(false))"}
122124
SQL
123125
end
124126

activerecord/lib/active_record/connection_adapters/schema_cache.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ def data_source_exists?(name)
4848

4949
@data_sources[name] = connection.data_source_exists?(name)
5050
end
51-
alias table_exists? data_source_exists?
52-
deprecate table_exists?: "use #data_source_exists? instead"
5351

5452
# Add internal cache for table with +table_name+.
5553
def add(table_name)
@@ -63,8 +61,6 @@ def add(table_name)
6361
def data_sources(name)
6462
@data_sources[name]
6563
end
66-
alias tables data_sources
67-
deprecate tables: "use #data_sources instead"
6864

6965
# Get the columns for a table
7066
def columns(table_name)
@@ -99,8 +95,6 @@ def clear_data_source_cache!(name)
9995
@primary_keys.delete name
10096
@data_sources.delete name
10197
end
102-
alias clear_table_cache! clear_data_source_cache!
103-
deprecate clear_table_cache!: "use #clear_data_source_cache! instead"
10498

10599
def marshal_dump
106100
# if we get current version during initialization, it happens stack over flow.

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -260,40 +260,33 @@ def exec_rollback_db_transaction #:nodoc:
260260
# SCHEMA STATEMENTS ========================================
261261

262262
def tables # :nodoc:
263-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
264-
#tables currently returns both tables and views.
265-
This behavior is deprecated and will be changed with Rails 5.1 to only return tables.
266-
Use #data_sources instead.
267-
MSG
268-
269-
data_sources
263+
select_values("SELECT name FROM sqlite_master WHERE type = 'table' AND name <> 'sqlite_sequence'", "SCHEMA")
270264
end
271265

272-
def data_sources
266+
def data_sources # :nodoc:
273267
select_values("SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name <> 'sqlite_sequence'", "SCHEMA")
274268
end
275269

276-
def table_exists?(table_name)
277-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
278-
#table_exists? currently checks both tables and views.
279-
This behavior is deprecated and will be changed with Rails 5.1 to only check tables.
280-
Use #data_source_exists? instead.
281-
MSG
282-
283-
data_source_exists?(table_name)
270+
def views # :nodoc:
271+
select_values("SELECT name FROM sqlite_master WHERE type = 'view' AND name <> 'sqlite_sequence'", "SCHEMA")
284272
end
285273

286-
def data_source_exists?(table_name)
274+
def table_exists?(table_name) # :nodoc:
287275
return false unless table_name.present?
288276

289-
sql = "SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name <> 'sqlite_sequence'"
277+
sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name <> 'sqlite_sequence'"
290278
sql << " AND name = #{quote(table_name)}"
291279

292280
select_values(sql, "SCHEMA").any?
293281
end
294282

295-
def views # :nodoc:
296-
select_values("SELECT name FROM sqlite_master WHERE type = 'view' AND name <> 'sqlite_sequence'", "SCHEMA")
283+
def data_source_exists?(table_name) # :nodoc:
284+
return false unless table_name.present?
285+
286+
sql = "SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name <> 'sqlite_sequence'"
287+
sql << " AND name = #{quote(table_name)}"
288+
289+
select_values(sql, "SCHEMA").any?
297290
end
298291

299292
def view_exists?(view_name) # :nodoc:

activerecord/lib/active_record/internal_metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def [](key)
2323
end
2424

2525
def table_exists?
26-
ActiveSupport::Deprecation.silence { connection.table_exists?(table_name) }
26+
connection.table_exists?(table_name)
2727
end
2828

2929
# Creates an internal metadata table with columns +key+ and +value+

activerecord/lib/active_record/migration.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,12 +1026,10 @@ def schema_migrations_table_name
10261026
end
10271027

10281028
def get_all_versions(connection = Base.connection)
1029-
ActiveSupport::Deprecation.silence do
1030-
if connection.table_exists?(schema_migrations_table_name)
1031-
SchemaMigration.all.map { |x| x.version.to_i }.sort
1032-
else
1033-
[]
1034-
end
1029+
if connection.table_exists?(schema_migrations_table_name)
1030+
SchemaMigration.all.map { |x| x.version.to_i }.sort
1031+
else
1032+
[]
10351033
end
10361034
end
10371035

activerecord/lib/active_record/schema_migration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def table_name
1717
end
1818

1919
def table_exists?
20-
ActiveSupport::Deprecation.silence { connection.table_exists?(table_name) }
20+
connection.table_exists?(table_name)
2121
end
2222

2323
def create_table

activerecord/test/cases/adapter_test.rb

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,19 @@ def test_create_record_with_pk_as_zero
3232

3333
def test_tables
3434
tables = nil
35-
ActiveSupport::Deprecation.silence { tables = @connection.tables }
35+
tables = @connection.tables
3636
assert_includes tables, "accounts"
3737
assert_includes tables, "authors"
3838
assert_includes tables, "tasks"
3939
assert_includes tables, "topics"
4040
end
4141

4242
def test_table_exists?
43-
ActiveSupport::Deprecation.silence do
44-
assert @connection.table_exists?("accounts")
45-
assert @connection.table_exists?(:accounts)
46-
assert_not @connection.table_exists?("nonexistingtable")
47-
assert_not @connection.table_exists?("'")
48-
assert_not @connection.table_exists?(nil)
49-
end
50-
end
51-
52-
def test_table_exists_checking_both_tables_and_views_is_deprecated
53-
assert_deprecated { @connection.table_exists?("accounts") }
43+
assert @connection.table_exists?("accounts")
44+
assert @connection.table_exists?(:accounts)
45+
assert_not @connection.table_exists?("nonexistingtable")
46+
assert_not @connection.table_exists?("'")
47+
assert_not @connection.table_exists?(nil)
5448
end
5549

5650
def test_data_sources
@@ -294,12 +288,6 @@ def test_log_invalid_encoding
294288
assert_not_nil error.message
295289
end
296290
end
297-
298-
if current_adapter?(:Mysql2Adapter, :SQLite3Adapter)
299-
def test_tables_returning_both_tables_and_views_is_deprecated
300-
assert_deprecated { @connection.tables }
301-
end
302-
end
303291
end
304292

305293
class AdapterTestWithoutTransaction < ActiveRecord::TestCase

activerecord/test/cases/adapters/postgresql/connection_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_indexes_logs_name
100100
end
101101

102102
def test_table_exists_logs_name
103-
ActiveSupport::Deprecation.silence { @connection.table_exists?("items") }
103+
@connection.table_exists?("items")
104104
assert_equal "SCHEMA", @subscriber.logged[0][1]
105105
end
106106

0 commit comments

Comments
 (0)