Browse files

Quitoting the table name before querying.

  • Loading branch information...
1 parent 988061d commit 8aa7b8695dd90989de699e050b61d755e3a61b19 @tenderlove tenderlove committed Dec 5, 2011
View
2 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -24,7 +24,7 @@ def table_alias_for(table_name)
# table_exists?(:developers)
def table_exists?(table_name)
begin
- select_value("SELECT 1 FROM #{table_name.to_s} where 1=0")
+ select_value("SELECT 1 FROM #{quote_table_name(table_name)} where 1=0")
true
rescue
false

4 comments on commit 8aa7b86

@sobrinho

I don't know if it's cross-database or if more faster but query could be:

SELECT 1 FROM "users" LIMIT 0

This works at least on PostgreSQL.

Could you check that @tenderlove?

@jonleighton
Ruby on Rails member

The build is broken after this: http://travis-ci.org/#!/rails/rails/builds/377045

@sobrinho

Also, each database has its way to check if a table exists.

For PostgreSQL, could be more faster using this:

SELECT COUNT(*) FROM "pg_tables" WHERE "pg_tables"."tablename" = 'users';

I didn't checked if it's more faster because I don't have a database with more than 100 tables ;)

@sobrinho

I ran the 3 queries on small database (171 tables) and all took ~0m0.019s using the following script:

time psql database < query1.sql > /dev/null
time psql database < query2.sql > /dev/null
time psql database < query3.sql > /dev/null

@jadeforrest, could you check that in your database?

Please sign in to comment.