@@ -4,7 +4,9 @@ package sql2pgroll
4
4
5
5
import (
6
6
"fmt"
7
+ "strconv"
7
8
9
+ "github.com/oapi-codegen/nullable"
8
10
pgq "github.com/pganalyze/pg_query_go/v6"
9
11
10
12
"github.com/xataio/pgroll/pkg/migrations"
@@ -38,6 +40,8 @@ func convertAlterTableStmt(stmt *pgq.AlterTableStmt) (migrations.Operations, err
38
40
op , err = convertAlterTableAddConstraint (stmt , alterTableCmd )
39
41
case pgq .AlterTableType_AT_DropColumn :
40
42
op , err = convertAlterTableDropColumn (stmt , alterTableCmd )
43
+ case pgq .AlterTableType_AT_ColumnDefault :
44
+ op , err = convertAlterTableSetColumnDefault (stmt , alterTableCmd )
41
45
}
42
46
43
47
if err != nil {
@@ -158,11 +162,66 @@ func convertAlterTableAddUniqueConstraint(stmt *pgq.AlterTableStmt, constraint *
158
162
}, nil
159
163
}
160
164
161
- // convertAlterTableDropColumn converts SQL statements like:
165
+ // convertAlterTableSetColumnDefault converts SQL statements like:
162
166
//
163
- // `ALTER TABLE foo DROP COLUMN bar
167
+ // `ALTER TABLE foo COLUMN bar SET DEFAULT 'foo'`
168
+ // `ALTER TABLE foo COLUMN bar SET DEFAULT 123`
169
+ // `ALTER TABLE foo COLUMN bar SET DEFAULT 123.456`
170
+ // `ALTER TABLE foo COLUMN bar SET DEFAULT true`
171
+ // `ALTER TABLE foo COLUMN bar SET DEFAULT B'0101'`
172
+ // `ALTER TABLE foo COLUMN bar SET DEFAULT null`
173
+ // `ALTER TABLE foo COLUMN bar DROP DEFAULT`
164
174
//
165
- // to an OpDropColumn operation.
175
+ // to an OpAlterColumn operation.
176
+ func convertAlterTableSetColumnDefault (stmt * pgq.AlterTableStmt , cmd * pgq.AlterTableCmd ) (migrations.Operation , error ) {
177
+ operation := & migrations.OpAlterColumn {
178
+ Table : stmt .GetRelation ().GetRelname (),
179
+ Column : cmd .GetName (),
180
+ Down : PlaceHolderSQL ,
181
+ Up : PlaceHolderSQL ,
182
+ }
183
+
184
+ if c := cmd .GetDef ().GetAConst (); c != nil {
185
+ if c .GetIsnull () {
186
+ // The default can be set to null
187
+ operation .Default = nullable .NewNullNullable [string ]()
188
+ return operation , nil
189
+ }
190
+
191
+ // We have a constant
192
+ switch v := c .GetVal ().(type ) {
193
+ case * pgq.A_Const_Sval :
194
+ operation .Default = nullable .NewNullableWithValue (v .Sval .GetSval ())
195
+ case * pgq.A_Const_Ival :
196
+ operation .Default = nullable .NewNullableWithValue (strconv .FormatInt (int64 (v .Ival .Ival ), 10 ))
197
+ case * pgq.A_Const_Fval :
198
+ operation .Default = nullable .NewNullableWithValue (v .Fval .Fval )
199
+ case * pgq.A_Const_Boolval :
200
+ operation .Default = nullable .NewNullableWithValue (strconv .FormatBool (v .Boolval .Boolval ))
201
+ case * pgq.A_Const_Bsval :
202
+ operation .Default = nullable .NewNullableWithValue (v .Bsval .Bsval )
203
+ default :
204
+ return nil , fmt .Errorf ("unknown constant type: %T" , c .GetVal ())
205
+ }
206
+
207
+ return operation , nil
208
+ }
209
+
210
+ if cmd .GetDef () != nil {
211
+ // We're setting it to something other than a constant
212
+ return nil , nil
213
+ }
214
+
215
+ // We're not setting it to anything, which is the case when we are dropping it
216
+ if cmd .GetBehavior () == pgq .DropBehavior_DROP_RESTRICT {
217
+ operation .Default = nullable .NewNullNullable [string ]()
218
+ return operation , nil
219
+ }
220
+
221
+ // Unknown case, fall back to raw SQL
222
+ return nil , nil
223
+ }
224
+
166
225
func convertAlterTableDropColumn (stmt * pgq.AlterTableStmt , cmd * pgq.AlterTableCmd ) (migrations.Operation , error ) {
167
226
if ! canConvertDropColumn (cmd ) {
168
227
return nil , nil
0 commit comments