database_rewinder icon indicating copy to clipboard operation
database_rewinder copied to clipboard

Tables created in complex queries are not cleaned

Open joshleaves opened this issue 8 years ago • 2 comments

This is an example of a query we have, using PostGreSQL's WITH statement:

WITH updated_records AS (
  UPDATE records SET updated_at = NOW(), foo = 'bar'
  WHERE type = 'custom_type'
  RETURNING id
)
INSERT INTO record_histories (record_id, action)
SELECT
  id AS record_id,
  'set_to_bar' AS action
FROM updated_records

Breaking it down into one UPDATE records... and one INSERT INTO record_histories.. queries would solve the issue but we'd like to avoid this solution if possible and keep our current codebase as is.

For minimal break of codebase, possible solutions rely on modifying the matcher regexp to either remove the leading \A (but then every test query that includes INSERT INTO xxxxx will add xxxxx to the tables to clean) or adding support for WITH xxxxx AS (...) INSERT INTO...-style queries to the regexp (which sounds dauting enough).

What would be your thoughts?

joshleaves avatar Jan 23 '17 15:01 joshleaves

Though I didn't know WITH statement, I think both solutions are OK. You can send your Pull request.

deeeki avatar Jan 24 '17 08:01 deeeki

Possibly related, I have a table being populated by raw SQL that isn't being picked up by DatabaseRewinder. The SQL goes something like

delete from table_name where model_id = 1;

insert into table_name(
  column_a,
  column_b,
  ...
)
select
  column_a,
  column_b,
  ...
from 
  complicated_sql

dillonwelch avatar Jan 31 '17 04:01 dillonwelch