Merge pull request #282 from billywatson/do_not_truncate_schema_migrations_table_in_postgresql

always return Postgres table names with schema to avoid awfulness
This commit is contained in:
Jon Rowe 2014-12-12 11:45:36 +11:00
commit 9e8522cfb3
2 changed files with 25 additions and 0 deletions

View file

@ -24,6 +24,7 @@ module DatabaseCleaner
def database_cleaner_view_cache
@views ||= select_values("select table_name from information_schema.views where table_schema = '#{current_database}'") rescue []
end
def database_cleaner_table_cache
# the adapters don't do caching (#130) but we make the assumption that the list stays the same in tests
@database_cleaner_tables ||= tables
@ -155,6 +156,15 @@ module DatabaseCleaner
truncate_tables(tables.select(&filter))
end
def database_cleaner_table_cache
# AR returns a list of tables without schema but then returns a
# migrations table with the schema. There are other problems, too,
# with using the base list. If a table exists in multiple schemas
# within the search path, truncation without the schema name could
# result in confusing, if not unexpected results.
@database_cleaner_tables ||= tables_with_schema
end
private
# Returns a boolean indicating if the given table has an auto-inc number higher than 0.
@ -169,6 +179,15 @@ module DatabaseCleaner
def has_rows?(table)
select_value("SELECT true FROM #{table} LIMIT 1;")
end
def tables_with_schema
rows = select_rows <<-_SQL
SELECT schemaname || '.' || tablename
FROM pg_tables
WHERE tablename !~ '_prt_' AND schemaname = ANY (current_schemas(false))
_SQL
rows.collect { |result| result.first }
end
end
end
end

View file

@ -35,6 +35,12 @@ module ActiveRecord
end
end
describe '#database_cleaner_table_cache' do
it 'should default to the list of tables with their schema' do
connection.database_cleaner_table_cache.first.should match(/^public\./)
end
end
it_behaves_like "an adapter with pre-count truncation" do
let(:connection) { active_record_pg_connection }
end