Skip to content

feat: reset sequences in one exec #208

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

Merged
Merged
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testfixtures
import (
"database/sql"
"fmt"
"strings"
)

type mySQL struct {
Expand Down Expand Up @@ -103,17 +104,21 @@ func (h *mySQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (er
}

func (h *mySQL) resetSequences(db *sql.DB) error {
if len(h.tables) == 0 {
return nil
}

resetSequencesTo := h.resetSequencesTo
if resetSequencesTo == 0 {
resetSequencesTo = 10000
}

b := strings.Builder{}
for _, t := range h.tables {
if _, err := db.Exec(fmt.Sprintf("ALTER TABLE %s AUTO_INCREMENT = %d", h.quoteKeyword(t), resetSequencesTo)); err != nil {
return err
}
b.WriteString(fmt.Sprintf("ALTER TABLE %s AUTO_INCREMENT = %d;", h.quoteKeyword(t), resetSequencesTo))

This comment was marked as resolved.

}
return nil
_, err := db.Exec(b.String())
return err
}

func (h *mySQL) isTableModified(q queryable, tableName string) (bool, error) {
Expand Down
14 changes: 9 additions & 5 deletions postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,22 @@ func (h *postgreSQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction
}

func (h *postgreSQL) resetSequences(db *sql.DB) error {
if len(h.sequences) == 0 {
return nil
}

resetSequencesTo := h.resetSequencesTo
if resetSequencesTo == 0 {
resetSequencesTo = 10000
}

b := strings.Builder{}
for _, sequence := range h.sequences {
_, err := db.Exec(fmt.Sprintf("SELECT SETVAL('%s', %d)", sequence, resetSequencesTo))
if err != nil {
return err
}
b.WriteString(fmt.Sprintf("SELECT SETVAL('%s', %d);", sequence, resetSequencesTo))
}
return nil

_, err := db.Exec(b.String())
return err
}

func (h *postgreSQL) isTableModified(q queryable, tableName string) (bool, error) {
Expand Down
Loading