From 29561bb09e4bc59f5e56ca8edb4b9faedfc1c059 Mon Sep 17 00:00:00 2001 From: Denis Titusov Date: Tue, 2 May 2023 02:24:18 +0300 Subject: [PATCH] feat: add option to disable DB cleanup (#161) --- testfixtures.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/testfixtures.go b/testfixtures.go index 9480805..ee53827 100644 --- a/testfixtures.go +++ b/testfixtures.go @@ -23,6 +23,7 @@ type Loader struct { helper helper fixturesFiles []*fixtureFile + skipCleanup bool skipTestDatabaseCheck bool location *time.Location @@ -218,6 +219,16 @@ func DangerousSkipTestDatabaseCheck() func(*Loader) error { } } +// DangerousSkipCleanupFixtureTables will make Loader not wipe data from fixture tables. +// This may lead to dirty data in the test database. +// Use with caution! +func DangerousSkipCleanupFixtureTables() func(*Loader) error { + return func(l *Loader) error { + l.skipCleanup = true + return nil + } +} + // Directory informs Loader to load YAML files from a given directory. func Directory(dir string) func(*Loader) error { return func(l *Loader) error { @@ -406,13 +417,15 @@ func (l *Loader) Load() error { // Delete existing table data for specified fixtures before populating the data. This helps avoid // DELETE CASCADE constraints when using the `UseAlterConstraint()` option. - for _, file := range l.fixturesFiles { - modified := modifiedTables[file.fileNameWithoutExtension()] - if !modified { - continue - } - if err := file.delete(tx, l.helper); err != nil { - return err + if !l.skipCleanup { + for _, file := range l.fixturesFiles { + modified := modifiedTables[file.fileNameWithoutExtension()] + if !modified { + continue + } + if err := file.delete(tx, l.helper); err != nil { + return err + } } }