@@ -55,14 +55,32 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, latestSch
5555 // the column as no DEFAULT or because the default value cannot be set using
5656 // the fast path optimization), add a NOT NULL constraint to the column which
5757 // will be validated on migration completion.
58+ skipInherit := false
59+ skipValidate := true
5860 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 {
6070 return nil , fmt .Errorf ("failed to add not null constraint: %w" , err )
6171 }
6272 }
6373
6474 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 {
6684 return nil , fmt .Errorf ("failed to add check constraint: %w" , err )
6785 }
6886 }
@@ -360,24 +378,6 @@ func upgradeNotNullConstraintToNotNullAttribute(ctx context.Context, conn db.DB,
360378 return err
361379}
362380
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-
381381// UniqueIndexName returns the name of the unique index for the given column
382382func UniqueIndexName (columnName string ) string {
383383 return "_pgroll_uniq_" + columnName
0 commit comments