Skip to content

Commit

Permalink
feat: add option to disable DB cleanup (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
titusjaka authored May 1, 2023
1 parent d70fdc7 commit 29561bb
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions testfixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Loader struct {
helper helper
fixturesFiles []*fixtureFile

skipCleanup bool
skipTestDatabaseCheck bool
location *time.Location

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit 29561bb

Please sign in to comment.