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

Add an option to the PostgreSQL helper to not reset sequences #38

Merged
merged 3 commits into from
Nov 4, 2018

Conversation

dgsb
Copy link
Contributor

@dgsb dgsb commented Oct 26, 2018

We do not always want to reset the sequence especially then they are used as primary key in some table.

Copy link
Contributor

@andreynering andreynering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dgsb,

Can you explain a bit more about your use case?

Resetting the sequences will prevent errors on your tests, and shouldn't have any side effects.

@@ -16,6 +16,10 @@ type PostgreSQL struct {
// which requires SUPERUSER privileges.
UseAlterConstraint bool

// DontResetSequences prevents the reset of the databases
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// DontResetSequences prevents the reset of the databases
// SkipResetSequences prevents the reset of the databases

postgresql.go Outdated
@@ -16,6 +16,10 @@ type PostgreSQL struct {
// which requires SUPERUSER privileges.
UseAlterConstraint bool

// DontResetSequences prevents the reset of the databases
// sequences after load fixtures time
DontResetSequences bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DontResetSequences bool
SkipResetSequences bool

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I choose this negative logic so that the default value keep a compatible backward behaviour. But in the end its your call.

postgresql.go Outdated
@@ -217,6 +221,9 @@ func (h *PostgreSQL) makeConstraintsDeferrable(db *sql.DB, loadFn loadFunction)
func (h *PostgreSQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
// ensure sequences being reset after load
defer func() {
if h.DontResetSequences {
Copy link
Contributor

@andreynering andreynering Oct 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should actually avoid deferring this whole function if sequences resetting is being skipped:

if !h.SkipResetSequences {
	defer func() {
		// ...
	}()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my previous comment. I think its better to keep the behaviour backward compatible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dgsb Sorry, there was a typo in the snippet above. It should be:

if !h.SkipResetSequences {

Instead of:

if h.SkipResetSequences {

The value is still negated (and backwards compatible). I just suggested renaming it from Dont to Skip

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I read to fast your answer

@andreynering andreynering changed the title add an option to the postgresql helper to not reset sequences Add an option to the PostgreSQL helper to not reset sequences Oct 27, 2018
@andreynering
Copy link
Contributor

andreynering commented Oct 27, 2018

CI should be fixed if you update with master

@dgsb
Copy link
Contributor Author

dgsb commented Oct 27, 2018

@andreynering I will resync with master in order to have a working CI.
Concerning the reset of the sequence, I did not dig that much in the issue I stumble onto, but basically reseting sequences used as primary key for their default value end up in conflicting primary key upon database insertion.
Disabling the sequence reset has fixed everything.

@andreynering
Copy link
Contributor

@dgsb Sorry for taking long to respond.

Since it doesn't hurt to allow skipping reseting of sequences, I'll accept this patch.

But the truth is that this problem shouldn't happen. If you have more info about your issue, let me know.

@andreynering andreynering merged commit 2754314 into go-testfixtures:master Nov 4, 2018
@dgsb
Copy link
Contributor Author

dgsb commented Nov 5, 2018

@andreynering I will try to setup a minimalistic reproduction tests. I've opened #39 to not forget to identify the root cause of the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants