-
Notifications
You must be signed in to change notification settings - Fork 27
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -838,6 +838,48 @@ var ( | |
expectedStatements: nil, | ||
expectedDiffErrIs: errDuplicateIdentifier, | ||
}, | ||
{ | ||
name: "Online check constraint addition", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a different test case:
|
||
oldSchema: schema.Schema{ | ||
Tables: []schema.Table{ | ||
{ | ||
Name: "foobar", | ||
Columns: []schema.Column{ | ||
{Name: "id", Type: "integer"}, | ||
}, | ||
ReplicaIdentity: schema.ReplicaIdentityDefault, | ||
}, | ||
}, | ||
}, | ||
newSchema: schema.Schema{ | ||
Tables: []schema.Table{ | ||
{ | ||
Name: "foobar", | ||
Columns: []schema.Column{ | ||
{Name: "id", Type: "integer"}, | ||
}, | ||
CheckConstraints: []schema.CheckConstraint{ | ||
{Name: "id_check", Expression: "(id > 0)", IsInheritable: true, IsValid: true}, | ||
}, | ||
ReplicaIdentity: schema.ReplicaIdentityDefault, | ||
}, | ||
}, | ||
}, | ||
expectedStatements: []Statement{ | ||
{ | ||
DDL: "ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"id_check\" CHECK((id > 0)) NOT VALID", | ||
Timeout: statementTimeoutDefault, | ||
LockTimeout: lockTimeoutDefault, | ||
Hazards: nil, | ||
}, | ||
{ | ||
DDL: "ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"id_check\"", | ||
Timeout: statementTimeoutDefault, | ||
LockTimeout: lockTimeoutDefault, | ||
Hazards: nil, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Invalid check constraint made valid", | ||
oldSchema: schema.Schema{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1444,14 +1444,29 @@ type checkConstraintSQLGenerator struct { | |
} | ||
|
||
func (csg *checkConstraintSQLGenerator) Add(con schema.CheckConstraint) ([]Statement, error) { | ||
var hazards []MigrationHazard | ||
|
||
// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
stmts = append(stmts, csg.createCheckConstraintStatement(con)) | ||
} else { | ||
// If the check constraint is not on a new table and is marked as valid, we should: | ||
// 1. Build the constraint as invalid | ||
// 2. Validate the constraint | ||
con.IsValid = false | ||
stmts = append(stmts, csg.createCheckConstraintStatement(con)) | ||
stmts = append(stmts, validateConstraintStatement(csg.tableName, schema.EscapeIdentifier(con.Name))) | ||
} | ||
|
||
return stmts, nil | ||
} | ||
|
||
func (csg *checkConstraintSQLGenerator) createCheckConstraintStatement(con schema.CheckConstraint) Statement { | ||
var hazards []MigrationHazard | ||
sb := strings.Builder{} | ||
sb.WriteString(fmt.Sprintf("%s CHECK(%s)", | ||
addConstraintPrefix(csg.tableName, schema.EscapeIdentifier(con.Name)), con.Expression)) | ||
|
@@ -1461,20 +1476,20 @@ func (csg *checkConstraintSQLGenerator) Add(con schema.CheckConstraint) ([]State | |
|
||
if !con.IsValid { | ||
sb.WriteString(" NOT VALID") | ||
} else if !csg.isNewTable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} else { | ||
hazards = append(hazards, MigrationHazard{ | ||
Type: MigrationHazardTypeAcquiresAccessExclusiveLock, | ||
Message: "This will lock reads and writes to the owning table while the constraint is being added. " + | ||
"Instead, consider adding the constraint as NOT VALID and validating it later.", | ||
}) | ||
} | ||
|
||
return []Statement{{ | ||
return Statement{ | ||
DDL: sb.String(), | ||
Timeout: statementTimeoutDefault, | ||
LockTimeout: lockTimeoutDefault, | ||
Hazards: hazards, | ||
}}, nil | ||
} | ||
} | ||
|
||
func (csg *checkConstraintSQLGenerator) Delete(con schema.CheckConstraint) ([]Statement, error) { | ||
|
There was a problem hiding this comment.
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