@@ -242,6 +242,7 @@ impl FinishCheckerHandle {
242242pub struct MasWriter {
243243 conn : LockedMasDatabase ,
244244 writer_pool : WriterConnectionPool ,
245+ dry_run : bool ,
245246
246247 indices_to_restore : Vec < IndexDescription > ,
247248 constraints_to_restore : Vec < ConstraintDescription > ,
@@ -793,6 +794,7 @@ impl MasWriter {
793794 pub async fn new (
794795 mut conn : LockedMasDatabase ,
795796 mut writer_connections : Vec < PgConnection > ,
797+ dry_run : bool ,
796798 ) -> Result < Self , Error > {
797799 // Given that we don't have any concurrent transactions here,
798800 // the READ COMMITTED isolation level is sufficient.
@@ -902,7 +904,7 @@ impl MasWriter {
902904
903905 Ok ( Self {
904906 conn,
905-
907+ dry_run ,
906908 writer_pool : WriterConnectionPool :: new ( writer_connections) ,
907909 indices_to_restore,
908910 constraints_to_restore,
@@ -987,7 +989,6 @@ impl MasWriter {
987989
988990 // Now all the data has been migrated, finish off by restoring indices and
989991 // constraints!
990-
991992 query ( "BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED;" )
992993 . execute ( self . conn . as_mut ( ) )
993994 . await
@@ -1009,6 +1010,28 @@ impl MasWriter {
10091010 . await
10101011 . into_database ( "could not revert temporary tables" ) ?;
10111012
1013+ // If we're in dry-run mode, truncate all the tables we've written to
1014+ if self . dry_run {
1015+ warn ! ( "Migration ran in dry-run mode, deleting all imported data" ) ;
1016+ let tables = MAS_TABLES_AFFECTED_BY_MIGRATION
1017+ . iter ( )
1018+ . map ( |table| format ! ( "\" {table}\" " ) )
1019+ . collect :: < Vec < _ > > ( )
1020+ . join ( ", " ) ;
1021+
1022+ // Note that we do that with CASCADE, because we do that *after*
1023+ // restoring the FK constraints.
1024+ //
1025+ // The alternative would be to list all the tables we have FK to
1026+ // those tables, which would be a hassle, or to do that after
1027+ // restoring the constraints, which would mean we wouldn't validate
1028+ // that we've done valid FKs in dry-run mode.
1029+ query ( & format ! ( "TRUNCATE TABLE {tables} CASCADE;" ) )
1030+ . execute ( self . conn . as_mut ( ) )
1031+ . await
1032+ . into_database_with ( || "failed to truncate all tables" ) ?;
1033+ }
1034+
10121035 query ( "COMMIT;" )
10131036 . execute ( self . conn . as_mut ( ) )
10141037 . await
@@ -1193,7 +1216,7 @@ mod test {
11931216 . await
11941217 . expect ( "failed to lock MAS database" )
11951218 . expect_left ( "MAS database is already locked" ) ;
1196- MasWriter :: new ( locked_main_conn, writer_conns)
1219+ MasWriter :: new ( locked_main_conn, writer_conns, false )
11971220 . await
11981221 . expect ( "failed to construct MasWriter" )
11991222 }
0 commit comments