@@ -19,12 +19,12 @@ var (
19
19
_ Createable = (* OpAddColumn )(nil )
20
20
)
21
21
22
- func (o * OpAddColumn ) Start (ctx context.Context , l Logger , conn db.DB , s * schema.Schema ) (* backfill.Task , error ) {
22
+ func (o * OpAddColumn ) Start (ctx context.Context , l Logger , conn db.DB , s * schema.Schema ) ([] DBAction , * backfill.Task , error ) {
23
23
l .LogOperationStart (o )
24
24
25
25
table := s .GetTable (o .Table )
26
26
if table == nil {
27
- return nil , TableDoesNotExistError {Name : o .Table }
27
+ return nil , nil , TableDoesNotExistError {Name : o .Table }
28
28
}
29
29
30
30
// If the column has a DEFAULT, check if it can be added using the fast path
@@ -33,20 +33,19 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema
33
33
if o .Column .HasDefault () {
34
34
v , err := defaults .UsesFastPath (ctx , conn , table .Name , o .Column .Type , * o .Column .Default )
35
35
if err != nil {
36
- return nil , fmt .Errorf ("failed to check for fast path default optimization: %w" , err )
36
+ return nil , nil , fmt .Errorf ("failed to check for fast path default optimization: %w" , err )
37
37
}
38
38
fastPathDefault = v
39
39
}
40
40
41
- if err := addColumn (ctx , conn , * o , table , fastPathDefault ); err != nil {
42
- return nil , fmt .Errorf ("failed to start add column operation: %w" , err )
41
+ action , err := addColumn (conn , * o , table , fastPathDefault )
42
+ if err != nil {
43
+ return nil , nil , err
43
44
}
45
+ dbActions := []DBAction {action }
44
46
45
47
if o .Column .Comment != nil {
46
- err := NewCommentColumnAction (conn , table .Name , TemporaryName (o .Column .Name ), o .Column .Comment ).Execute (ctx )
47
- if err != nil {
48
- return nil , fmt .Errorf ("failed to add comment to column: %w" , err )
49
- }
48
+ dbActions = append (dbActions , NewCommentColumnAction (conn , table .Name , TemporaryName (o .Column .Name ), o .Column .Comment ))
50
49
}
51
50
52
51
// If the column is `NOT NULL` and there is no default value (either because
@@ -56,47 +55,48 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema
56
55
skipInherit := false
57
56
skipValidate := true
58
57
if ! o .Column .IsNullable () && (o .Column .Default == nil || ! fastPathDefault ) {
59
- if err := NewCreateCheckConstraintAction (
60
- conn ,
61
- table .Name ,
62
- NotNullConstraintName (o .Column .Name ),
63
- fmt .Sprintf ("%s IS NOT NULL" , o .Column .Name ),
64
- []string {o .Column .Name },
65
- skipInherit ,
66
- skipValidate ,
67
- ).Execute (ctx ); err != nil {
68
- return nil , fmt .Errorf ("failed to add not null constraint: %w" , err )
69
- }
58
+ dbActions = append (dbActions ,
59
+ NewCreateCheckConstraintAction (
60
+ conn ,
61
+ table .Name ,
62
+ NotNullConstraintName (o .Column .Name ),
63
+ fmt .Sprintf ("%s IS NOT NULL" , o .Column .Name ),
64
+ []string {o .Column .Name },
65
+ skipInherit ,
66
+ skipValidate ,
67
+ ))
70
68
}
71
69
72
70
if o .Column .Check != nil {
73
- if err := NewCreateCheckConstraintAction (
74
- conn ,
75
- table .Name ,
76
- o .Column .Check .Name ,
77
- o .Column .Check .Constraint ,
78
- []string {o .Column .Name },
79
- skipInherit ,
80
- skipValidate ,
81
- ).Execute (ctx ); err != nil {
82
- return nil , fmt .Errorf ("failed to add check constraint: %w" , err )
83
- }
71
+ dbActions = append (dbActions ,
72
+ NewCreateCheckConstraintAction (
73
+ conn ,
74
+ table .Name ,
75
+ o .Column .Check .Name ,
76
+ o .Column .Check .Constraint ,
77
+ []string {o .Column .Name },
78
+ skipInherit ,
79
+ skipValidate ,
80
+ ))
84
81
}
85
82
86
83
if o .Column .Unique {
87
- createIndex := NewCreateUniqueIndexConcurrentlyAction (conn , s .Name , UniqueIndexName (o .Column .Name ), table .Name , TemporaryName (o .Column .Name ))
88
- err := createIndex .Execute (ctx )
89
- if err != nil {
90
- return nil , fmt .Errorf ("failed to add unique index: %w" , err )
91
- }
84
+ dbActions = append (dbActions ,
85
+ NewCreateUniqueIndexConcurrentlyAction (
86
+ conn ,
87
+ s .Name ,
88
+ UniqueIndexName (o .Column .Name ),
89
+ table .Name ,
90
+ TemporaryName (o .Column .Name ),
91
+ ))
92
92
}
93
93
94
94
// If the column has a DEFAULT that cannot be set using the fast path
95
95
// optimization, the `up` SQL expression must be used to set the DEFAULT
96
96
// value for the column.
97
97
if o .Column .HasDefault () && ! fastPathDefault {
98
98
if o .Up != * o .Column .Default {
99
- return nil , UpSQLMustBeColumnDefaultError {Column : o .Column .Name }
99
+ return nil , nil , UpSQLMustBeColumnDefaultError {Column : o .Column .Name }
100
100
}
101
101
}
102
102
@@ -118,7 +118,7 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema
118
118
tmpColumn .Name = TemporaryName (o .Column .Name )
119
119
table .AddColumn (o .Column .Name , tmpColumn )
120
120
121
- return task , nil
121
+ return dbActions , task , nil
122
122
}
123
123
124
124
func toSchemaColumn (c Column ) * schema.Column {
@@ -253,7 +253,7 @@ func (o *OpAddColumn) Validate(ctx context.Context, s *schema.Schema) error {
253
253
return nil
254
254
}
255
255
256
- func addColumn (ctx context. Context , conn db.DB , o OpAddColumn , t * schema.Table , fastPathDefault bool ) error {
256
+ func addColumn (conn db.DB , o OpAddColumn , t * schema.Table , fastPathDefault bool ) ( DBAction , error ) {
257
257
// don't add non-nullable columns with no default directly
258
258
// they are handled by:
259
259
// - adding the column as nullable
@@ -266,7 +266,7 @@ func addColumn(ctx context.Context, conn db.DB, o OpAddColumn, t *schema.Table,
266
266
}
267
267
268
268
if o .Column .Generated != nil {
269
- return fmt .Errorf ("adding generated columns to existing tables is not supported" )
269
+ return nil , fmt .Errorf ("adding generated columns to existing tables is not supported" )
270
270
}
271
271
272
272
// Don't add a column with a CHECK constraint directly.
@@ -299,7 +299,7 @@ func addColumn(ctx context.Context, conn db.DB, o OpAddColumn, t *schema.Table,
299
299
o .Column .Name = TemporaryName (o .Column .Name )
300
300
301
301
withPK := true
302
- return NewAddColumnAction (conn , t .Name , o .Column , withPK ). Execute ( ctx )
302
+ return NewAddColumnAction (conn , t .Name , o .Column , withPK ), nil
303
303
}
304
304
305
305
// upgradeNotNullConstraintToNotNullAttribute validates and upgrades a NOT NULL
0 commit comments