@@ -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" , 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
+ o .Column .Check .Constraint ,
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
}
@@ -360,24 +378,6 @@ func upgradeNotNullConstraintToNotNullAttribute(ctx context.Context, conn db.DB,
360
378
return err
361
379
}
362
380
363
- func addNotNullConstraint (ctx context.Context , conn db.DB , table , column , physicalColumn string ) error {
364
- _ , err := conn .ExecContext (ctx , fmt .Sprintf ("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s IS NOT NULL) NOT VALID" ,
365
- pq .QuoteIdentifier (table ),
366
- pq .QuoteIdentifier (NotNullConstraintName (column )),
367
- pq .QuoteIdentifier (physicalColumn ),
368
- ))
369
- return err
370
- }
371
-
372
- func (o * OpAddColumn ) addCheckConstraint (ctx context.Context , tableName string , conn db.DB ) error {
373
- _ , err := conn .ExecContext (ctx , fmt .Sprintf ("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s) NOT VALID" ,
374
- pq .QuoteIdentifier (tableName ),
375
- pq .QuoteIdentifier (o .Column .Check .Name ),
376
- rewriteCheckExpression (o .Column .Check .Constraint , o .Column .Name ),
377
- ))
378
- return err
379
- }
380
-
381
381
// UniqueIndexName returns the name of the unique index for the given column
382
382
func UniqueIndexName (columnName string ) string {
383
383
return "_pgroll_uniq_" + columnName
0 commit comments