Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for deleting the data after Load() #110

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 48 additions & 20 deletions testfixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,21 @@ func (l *Loader) EnsureTestDatabase() error {
return nil
}

func setModifiedTables(l *Loader, tx *sql.Tx) (map[string]bool, error) {
modifiedTables := make(map[string]bool, len(l.fixturesFiles))

for _, file := range l.fixturesFiles {
tableName := file.fileNameWithoutExtension()
modified, err := l.helper.isTableModified(tx, tableName)
if err != nil {
return nil, err
}
modifiedTables[tableName] = modified
}

return modifiedTables, nil
}

// Load wipes and after load all fixtures in the database.
// if err := fixtures.Load(); err != nil {
// ...
Expand All @@ -346,27 +361,14 @@ func (l *Loader) Load() error {
}
}

err := l.helper.disableReferentialIntegrity(l.db, func(tx *sql.Tx) error {
modifiedTables := make(map[string]bool, len(l.fixturesFiles))
for _, file := range l.fixturesFiles {
tableName := file.fileNameWithoutExtension()
modified, err := l.helper.isTableModified(tx, tableName)
if err != nil {
return err
}
modifiedTables[tableName] = modified
}
// Delete existing table data for specified fixtures before populating the data. This helps avoid
// DELETE CASCADE constraints when using the `UseAlterConstraint()` option.
l.Wipe()

// 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
}
err := l.helper.disableReferentialIntegrity(l.db, func(tx *sql.Tx) error {
modifiedTables, err := setModifiedTables(l, tx)
if err != nil {
return err
}

for _, file := range l.fixturesFiles {
Expand Down Expand Up @@ -400,6 +402,32 @@ func (l *Loader) Load() error {
return l.helper.afterLoad(l.db)
}

func (l *Loader) Wipe() error {
err := l.helper.disableReferentialIntegrity(l.db, func(tx *sql.Tx) error {
modifiedTables, err := setModifiedTables(l, tx)
if err != nil {
return err
}

for _, file := range l.fixturesFiles {
modified := modifiedTables[file.fileNameWithoutExtension()]
if !modified {
continue
}
if err := file.delete(tx, l.helper); err != nil {
return err
}
}

return nil
})
if err != nil {
return err
}

return nil
}

// InsertError will be returned if any error happens on database while
// inserting the record.
type InsertError struct {
Expand Down