@@ -406,108 +406,24 @@ func convertAlterTableAddColumn(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd
406
406
return nil , nil
407
407
}
408
408
409
- columnDef := cmd .GetDef ().GetColumnDef ()
410
- if ! canConvertColumnDef (columnDef ) {
411
- return nil , nil
412
- }
413
-
414
- columnType , err := pgq .DeparseTypeName (columnDef .GetTypeName ())
409
+ qualifiedName := getQualifiedRelationName (stmt .GetRelation ())
410
+ column , err := convertColumnDef (qualifiedName , cmd .GetDef ().GetColumnDef ())
415
411
if err != nil {
416
- return nil , fmt .Errorf ("failed to deparse type name : %w" , err )
412
+ return nil , fmt .Errorf ("error converting column definition : %w" , err )
417
413
}
418
-
419
- operation := & migrations.OpAddColumn {
420
- Column : migrations.Column {
421
- Name : columnDef .GetColname (),
422
- Type : columnType ,
423
- Nullable : true ,
424
- },
425
- Table : getQualifiedRelationName (stmt .GetRelation ()),
426
- Up : PlaceHolderSQL ,
427
- }
428
-
429
- if len (columnDef .GetConstraints ()) > 0 {
430
- for _ , constraint := range columnDef .GetConstraints () {
431
- switch constraint .GetConstraint ().GetContype () {
432
- case pgq .ConstrType_CONSTR_NULL :
433
- operation .Column .Nullable = true
434
- case pgq .ConstrType_CONSTR_NOTNULL :
435
- operation .Column .Nullable = false
436
- case pgq .ConstrType_CONSTR_PRIMARY :
437
- operation .Column .Pk = true
438
- operation .Column .Nullable = false
439
- case pgq .ConstrType_CONSTR_UNIQUE :
440
- operation .Column .Unique = true
441
- case pgq .ConstrType_CONSTR_CHECK :
442
- raw , err := pgq .DeparseExpr (constraint .GetConstraint ().GetRawExpr ())
443
- if err != nil {
444
- return nil , fmt .Errorf ("failed to deparse raw expression: %w" , err )
445
- }
446
- operation .Column .Check = & migrations.CheckConstraint {
447
- Constraint : raw ,
448
- Name : constraint .GetConstraint ().GetConname (),
449
- }
450
- case pgq .ConstrType_CONSTR_DEFAULT :
451
- defaultExpr := constraint .GetConstraint ().GetRawExpr ()
452
- def , err := extractDefault (defaultExpr )
453
- if err != nil {
454
- return nil , err
455
- }
456
- if ! def .IsNull () {
457
- v := def .MustGet ()
458
- operation .Column .Default = & v
459
- }
460
- case pgq .ConstrType_CONSTR_FOREIGN :
461
- onDelete , err := parseOnDeleteAction (constraint .GetConstraint ().GetFkDelAction ())
462
- if err != nil {
463
- return nil , err
464
- }
465
- fk := & migrations.ForeignKeyReference {
466
- Name : constraint .GetConstraint ().GetConname (),
467
- OnDelete : onDelete ,
468
- Column : constraint .GetConstraint ().GetPkAttrs ()[0 ].GetString_ ().GetSval (),
469
- Table : getQualifiedRelationName (constraint .GetConstraint ().GetPktable ()),
470
- }
471
- operation .Column .References = fk
472
- }
473
- }
414
+ if column == nil {
415
+ return nil , nil
474
416
}
475
417
476
- return operation , nil
418
+ return & migrations.OpAddColumn {
419
+ Column : * column ,
420
+ Table : qualifiedName ,
421
+ Up : PlaceHolderSQL ,
422
+ }, nil
477
423
}
478
424
479
425
func canConvertAddColumn (cmd * pgq.AlterTableCmd ) bool {
480
- if cmd .GetMissingOk () {
481
- return false
482
- }
483
- for _ , constraint := range cmd .GetDef ().GetColumnDef ().GetConstraints () {
484
- switch constraint .GetConstraint ().GetContype () {
485
- case pgq .ConstrType_CONSTR_DEFAULT ,
486
- pgq .ConstrType_CONSTR_NULL ,
487
- pgq .ConstrType_CONSTR_NOTNULL ,
488
- pgq .ConstrType_CONSTR_PRIMARY ,
489
- pgq .ConstrType_CONSTR_UNIQUE ,
490
- pgq .ConstrType_CONSTR_FOREIGN ,
491
- pgq .ConstrType_CONSTR_CHECK :
492
- switch constraint .GetConstraint ().GetFkUpdAction () {
493
- case "r" , "c" , "n" , "d" :
494
- // RESTRICT, CASCADE, SET NULL, SET DEFAULT
495
- return false
496
- case "a" :
497
- // NO ACTION, the default
498
- break
499
- }
500
- case pgq .ConstrType_CONSTR_ATTR_DEFERRABLE ,
501
- pgq .ConstrType_CONSTR_ATTR_DEFERRED ,
502
- pgq .ConstrType_CONSTR_IDENTITY ,
503
- pgq .ConstrType_CONSTR_GENERATED :
504
- return false
505
- case pgq .ConstrType_CONSTR_ATTR_NOT_DEFERRABLE , pgq .ConstrType_CONSTR_ATTR_IMMEDIATE :
506
- break
507
- }
508
- }
509
-
510
- return true
426
+ return ! cmd .GetMissingOk ()
511
427
}
512
428
513
429
func convertAlterTableDropColumn (stmt * pgq.AlterTableStmt , cmd * pgq.AlterTableCmd ) (migrations.Operation , error ) {
0 commit comments