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

Online check constraint addition #76

Merged
merged 2 commits into from
Oct 25, 2023

Conversation

bplunkett-stripe
Copy link
Collaborator

Description

This was a pretty low-hanging fruit. We now add all check constraints as invalid and then validate them. This allows us to build check constraints "online".

Motivation

Closes #66

Testing

Tested with unit tests

@bplunkett-stripe bplunkett-stripe added the enhancement New feature or request label Oct 25, 2023
@@ -1461,20 +1476,20 @@ func (csg *checkConstraintSQLGenerator) Add(con schema.CheckConstraint) ([]State

if !con.IsValid {
sb.WriteString(" NOT VALID")
} else if !csg.isNewTable {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We strip the migration hazards for new tables within the TableSQLGenerator, so we don't actually need this conditional

alexaub-stripe
alexaub-stripe previously approved these changes Oct 25, 2023
Copy link
Collaborator

@alexaub-stripe alexaub-stripe left a comment

Choose a reason for hiding this comment

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

Looks great! Really cool improvement -- two thoughts

README.md Outdated
@@ -43,10 +43,13 @@ $ pg-schema-diff plan --dsn "postgres://postgres:postgres@localhost:5432/postgre
* The use of postgres native operations for zero-downtime migrations wherever possible:
* Concurrent index builds
* Online index replacement: If some index is changed, the new version will be built before the old version is dropped, preventing a window where no index is backing queries
* Online check constraint addition: Check constraints are added as `INVALID` before being validated, eliminating the need
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe use builds here? Addition is a bit of an awkward term in my head

Suggested change
* Online check constraint addition: Check constraints are added as `INVALID` before being validated, eliminating the need
* Online check constraint builds: Check constraints are added as `INVALID` before being validated, eliminating the need

@@ -838,6 +838,48 @@ var (
expectedStatements: nil,
expectedDiffErrIs: errDuplicateIdentifier,
},
{
name: "Online check constraint addition",
Copy link
Collaborator

Choose a reason for hiding this comment

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

thought: Do we already have a case for the validation of a previously invalid constraint?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a different test case:

  • one is testing altering a check constraint from invalid to valid
  • one is testing that add a constraint is online

// UDF's in check constraints are a bad idea. Check constraints are not re-validated
// if the UDF changes, so it's not really a safe practice. We won't support it for now
if len(con.DependsOnFunctions) > 0 {
return nil, fmt.Errorf("check constraints that depend on UDFs: %w", ErrNotImplemented)
}

var stmts []Statement
if !con.IsValid || csg.isNewTable {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If !con.IsValid shouldn't we have a validateConstraintStatement?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nope! Because the user is adding an invalid constraint! (They want it to be invalid)

@bplunkett-stripe
Copy link
Collaborator Author

r?

@bplunkett-stripe bplunkett-stripe merged commit 6bad92c into main Oct 25, 2023
5 checks passed
@bplunkett-stripe bplunkett-stripe deleted the bplunkett/online-check-constraint-addition branch October 25, 2023 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Online Check Constraint Adds
2 participants