@@ -55,14 +55,32 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, latestSch
55
55
// the column as no DEFAULT or because the default value cannot be set using
56
56
// the fast path optimization), add a NOT NULL constraint to the column which
57
57
// will be validated on migration completion.
58
+ skipInherit := false
59
+ skipValidate := true
58
60
if ! o .Column .IsNullable () && (o .Column .Default == nil || ! fastPathDefault ) {
59
- if err := addNotNullConstraint (ctx , conn , table .Name , o .Column .Name , TemporaryName (o .Column .Name )); err != nil {
61
+ if err := NewCreateCheckConstraintAction (
62
+ conn ,
63
+ table .Name ,
64
+ NotNullConstraintName (o .Column .Name ),
65
+ fmt .Sprintf ("%s IS NOT NULL" , TemporaryName (o .Column .Name )),
66
+ []string {o .Column .Name },
67
+ skipInherit ,
68
+ skipValidate ,
69
+ ).Execute (ctx ); err != nil {
60
70
return nil , fmt .Errorf ("failed to add not null constraint: %w" , err )
61
71
}
62
72
}
63
73
64
74
if o .Column .Check != nil {
65
- if err := o .addCheckConstraint (ctx , table .Name , conn ); err != nil {
75
+ if err := NewCreateCheckConstraintAction (
76
+ conn ,
77
+ table .Name ,
78
+ o .Column .Check .Name ,
79
+ rewriteCheckExpression (o .Column .Check .Constraint , o .Column .Name ),
80
+ []string {o .Column .Name },
81
+ skipInherit ,
82
+ skipValidate ,
83
+ ).Execute (ctx ); err != nil {
66
84
return nil , fmt .Errorf ("failed to add check constraint: %w" , err )
67
85
}
68
86
}
@@ -363,24 +381,6 @@ func upgradeNotNullConstraintToNotNullAttribute(ctx context.Context, conn db.DB,
363
381
return err
364
382
}
365
383
366
- func addNotNullConstraint (ctx context.Context , conn db.DB , table , column , physicalColumn string ) error {
367
- _ , err := conn .ExecContext (ctx , fmt .Sprintf ("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s IS NOT NULL) NOT VALID" ,
368
- pq .QuoteIdentifier (table ),
369
- pq .QuoteIdentifier (NotNullConstraintName (column )),
370
- pq .QuoteIdentifier (physicalColumn ),
371
- ))
372
- return err
373
- }
374
-
375
- func (o * OpAddColumn ) addCheckConstraint (ctx context.Context , tableName string , conn db.DB ) error {
376
- _ , err := conn .ExecContext (ctx , fmt .Sprintf ("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s) NOT VALID" ,
377
- pq .QuoteIdentifier (tableName ),
378
- pq .QuoteIdentifier (o .Column .Check .Name ),
379
- rewriteCheckExpression (o .Column .Check .Constraint , o .Column .Name ),
380
- ))
381
- return err
382
- }
383
-
384
384
// UniqueIndexName returns the name of the unique index for the given column
385
385
func UniqueIndexName (columnName string ) string {
386
386
return "_pgroll_uniq_" + columnName
0 commit comments