yogthos / migratus

MIGRATE ALL THE THINGS!
643 stars 93 forks source link

Introduce :expect-results? config that allows comparing rows #151

Closed snoe closed 5 years ago

snoe commented 5 years ago

This PR might be a bit too specific to to our needs but we thought we'd contribute it if people are interested or if you think it could be useful to others.

We ran into a production bug where we accidentally deleted too much data because of a missing where clause on a DELETE statement. So we decided we wanted better logging of migrations in two ways:

An example

-- expect 0;;
-- 20180928110216-foobartest.up.sql
create table foobar (thing text);

--;;
-- expect 18;;
insert into foobar values ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('a'), ('b');

--;;

-- expect 17;;

update foobar set thing = 'c' where thing = 'a';

--;;
-- expect 1;;
delete from foobar where thing = 'c';

--;;
-- expect 100000;;
delete from svc.incident_attributes;
2018-10-01 20:27:47.046 migratus.core  INFO   Running up for [20180928110216 20181001103554]
2018-10-01 20:27:47.047 migratus.core  INFO   Up 20180928110216-foobartest
2018-10-01 20:27:47.061 migratus.migration.sql  DEBUG  found 5 up migrations
2018-10-01 20:27:47.070 migratus.migration.sql  INFO   CREATE 0
2018-10-01 20:27:47.076 migratus.migration.sql  ERROR  INSERT 19 Expected 18
2018-10-01 20:27:47.079 migratus.migration.sql  ERROR  UPDATE 18 Expected 17
2018-10-01 20:27:47.081 migratus.migration.sql  ERROR  DELETE 18 Expected 1
2018-10-01 20:27:47.084 migratus.migration.sql  ERROR  DELETE 0 Expected 100000
2018-10-01 20:27:47.085 migratus.database  DEBUG  marking 20180928110216 complete
yogthos commented 5 years ago

Thanks, this looks like a helpful sanity check.

yogthos commented 5 years ago

And I just published version 1.1.1 to Clojars with the updates.