@@ -5320,6 +5320,117 @@ fn parse_create_type_as_enum() {
5320
5320
}
5321
5321
}
5322
5322
5323
+ #[ test]
5324
+ fn parse_alter_type ( ) {
5325
+ struct TestCase {
5326
+ sql : & ' static str ,
5327
+ canonical : & ' static str ,
5328
+ name : & ' static str ,
5329
+ operation : AlterTypeOperation ,
5330
+ }
5331
+ vec ! [
5332
+ TestCase {
5333
+ sql: r#"ALTER TYPE public.my_type
5334
+ RENAME TO my_new_type"# ,
5335
+ canonical: "ALTER TYPE public.my_type RENAME TO my_new_type" ,
5336
+ name: "public.my_type" ,
5337
+ operation: AlterTypeOperation :: Rename {
5338
+ new_name: Ident :: new( "my_new_type" ) ,
5339
+ } ,
5340
+ } ,
5341
+ TestCase {
5342
+ sql: r#"ALTER TYPE public.my_type
5343
+ ADD VALUE IF NOT EXISTS 'label3.5'
5344
+ BEFORE 'label4'"# ,
5345
+ canonical:
5346
+ "ALTER TYPE public.my_type ADD VALUE IF NOT EXISTS 'label3.5' BEFORE 'label4'" ,
5347
+ name: "public.my_type" ,
5348
+ operation: AlterTypeOperation :: AddValue {
5349
+ if_not_exists: true ,
5350
+ value: Ident :: with_quote( '\'' , "label3.5" ) ,
5351
+ position: AlterTypeOperationAddValuePosition :: Before ( Ident :: with_quote(
5352
+ '\'' , "label4" ,
5353
+ ) ) ,
5354
+ } ,
5355
+ } ,
5356
+ TestCase {
5357
+ sql: r#"ALTER TYPE public.my_type
5358
+ ADD VALUE 'label3.5' BEFORE 'label4'"# ,
5359
+ canonical: "ALTER TYPE public.my_type ADD VALUE 'label3.5' BEFORE 'label4'" ,
5360
+ name: "public.my_type" ,
5361
+ operation: AlterTypeOperation :: AddValue {
5362
+ if_not_exists: false ,
5363
+ value: Ident :: with_quote( '\'' , "label3.5" ) ,
5364
+ position: AlterTypeOperationAddValuePosition :: Before ( Ident :: with_quote(
5365
+ '\'' , "label4" ,
5366
+ ) ) ,
5367
+ } ,
5368
+ } ,
5369
+ TestCase {
5370
+ sql: r#"ALTER TYPE public.my_type
5371
+ ADD VALUE IF NOT EXISTS 'label3.5'
5372
+ AFTER 'label3'"# ,
5373
+ canonical:
5374
+ "ALTER TYPE public.my_type ADD VALUE IF NOT EXISTS 'label3.5' AFTER 'label3'" ,
5375
+ name: "public.my_type" ,
5376
+ operation: AlterTypeOperation :: AddValue {
5377
+ if_not_exists: true ,
5378
+ value: Ident :: with_quote( '\'' , "label3.5" ) ,
5379
+ position: AlterTypeOperationAddValuePosition :: After ( Ident :: with_quote(
5380
+ '\'' , "label3" ,
5381
+ ) ) ,
5382
+ } ,
5383
+ } ,
5384
+ TestCase {
5385
+ sql: r#"ALTER TYPE public.my_type
5386
+ ADD VALUE 'label3.5'
5387
+ AFTER 'label3'"# ,
5388
+ canonical: "ALTER TYPE public.my_type ADD VALUE 'label3.5' AFTER 'label3'" ,
5389
+ name: "public.my_type" ,
5390
+ operation: AlterTypeOperation :: AddValue {
5391
+ if_not_exists: false ,
5392
+ value: Ident :: with_quote( '\'' , "label3.5" ) ,
5393
+ position: AlterTypeOperationAddValuePosition :: After ( Ident :: with_quote(
5394
+ '\'' , "label3" ,
5395
+ ) ) ,
5396
+ } ,
5397
+ } ,
5398
+ TestCase {
5399
+ sql: r#"ALTER TYPE public.my_type
5400
+ ADD VALUE IF NOT EXISTS 'label5'"# ,
5401
+ canonical: "ALTER TYPE public.my_type ADD VALUE IF NOT EXISTS 'label5'" ,
5402
+ name: "public.my_type" ,
5403
+ operation: AlterTypeOperation :: AddValue {
5404
+ if_not_exists: true ,
5405
+ value: Ident :: with_quote( '\'' , "label5" ) ,
5406
+ position: AlterTypeOperationAddValuePosition :: Default ,
5407
+ } ,
5408
+ } ,
5409
+ TestCase {
5410
+ sql: r#"ALTER TYPE public.my_type
5411
+ ADD VALUE 'label5'"# ,
5412
+ canonical: "ALTER TYPE public.my_type ADD VALUE 'label5'" ,
5413
+ name: "public.my_type" ,
5414
+ operation: AlterTypeOperation :: AddValue {
5415
+ if_not_exists: false ,
5416
+ value: Ident :: with_quote( '\'' , "label5" ) ,
5417
+ position: AlterTypeOperationAddValuePosition :: Default ,
5418
+ } ,
5419
+ } ,
5420
+ ]
5421
+ . into_iter ( )
5422
+ . enumerate ( )
5423
+ . for_each ( |( index, tc) | {
5424
+ let statement = pg_and_generic ( ) . one_statement_parses_to ( tc. sql , tc. canonical ) ;
5425
+ if let Statement :: AlterType { name, operation } = statement {
5426
+ assert_eq ! ( tc. name, name. to_string( ) , "TestCase[{index}].name" ) ;
5427
+ assert_eq ! ( tc. operation, operation, "TestCase[{index}].operation" ) ;
5428
+ } else {
5429
+ unreachable ! ( "{:?} should parse to Statement::AlterType" , tc. canonical) ;
5430
+ }
5431
+ } ) ;
5432
+ }
5433
+
5323
5434
#[ test]
5324
5435
fn parse_bitstring_literal ( ) {
5325
5436
let select = pg_and_generic ( ) . verified_only_select ( "SELECT B'111'" ) ;
0 commit comments