@@ -789,20 +789,19 @@ mod tests {
789
789
use v9:: { RawModuleDefV9Builder , TableAccess } ;
790
790
use validate:: tests:: expect_identifier;
791
791
792
- #[ test]
793
- fn successful_auto_migration ( ) {
794
- let mut old_builder = RawModuleDefV9Builder :: new ( ) ;
795
- let old_schedule_at = old_builder. add_type :: < ScheduleAt > ( ) ;
796
- let old_sum_ty = AlgebraicType :: sum ( [ ( "v1" , AlgebraicType :: U64 ) ] ) ;
797
- let old_sum_refty = old_builder. add_algebraic_type ( [ ] , "sum" , old_sum_ty, true ) ;
798
- old_builder
792
+ fn initial_module_def ( ) -> ModuleDef {
793
+ let mut builder = RawModuleDefV9Builder :: new ( ) ;
794
+ let schedule_at = builder. add_type :: < ScheduleAt > ( ) ;
795
+ let sum_ty = AlgebraicType :: sum ( [ ( "v1" , AlgebraicType :: U64 ) ] ) ;
796
+ let sum_refty = builder. add_algebraic_type ( [ ] , "sum" , sum_ty, true ) ;
797
+ builder
799
798
. build_table_with_new_type (
800
799
"Apples" ,
801
800
ProductType :: from ( [
802
801
( "id" , AlgebraicType :: U64 ) ,
803
802
( "name" , AlgebraicType :: String ) ,
804
803
( "count" , AlgebraicType :: U16 ) ,
805
- ( "sum" , old_sum_refty . into ( ) ) ,
804
+ ( "sum" , sum_refty . into ( ) ) ,
806
805
] ) ,
807
806
true ,
808
807
)
@@ -812,7 +811,7 @@ mod tests {
812
811
. with_index ( btree ( [ 0 , 1 ] ) , "id_name_index" )
813
812
. finish ( ) ;
814
813
815
- old_builder
814
+ builder
816
815
. build_table_with_new_type (
817
816
"Bananas" ,
818
817
ProductType :: from ( [
@@ -825,59 +824,61 @@ mod tests {
825
824
. with_access ( TableAccess :: Public )
826
825
. finish ( ) ;
827
826
828
- let old_deliveries_type = old_builder
827
+ let deliveries_type = builder
829
828
. build_table_with_new_type (
830
829
"Deliveries" ,
831
830
ProductType :: from ( [
832
831
( "scheduled_id" , AlgebraicType :: U64 ) ,
833
- ( "scheduled_at" , old_schedule_at . clone ( ) ) ,
834
- ( "sum" , AlgebraicType :: array ( old_sum_refty . into ( ) ) ) ,
832
+ ( "scheduled_at" , schedule_at . clone ( ) ) ,
833
+ ( "sum" , AlgebraicType :: array ( sum_refty . into ( ) ) ) ,
835
834
] ) ,
836
835
true ,
837
836
)
838
837
. with_auto_inc_primary_key ( 0 )
839
838
. with_index_no_accessor_name ( btree ( 0 ) )
840
839
. with_schedule ( "check_deliveries" , 1 )
841
840
. finish ( ) ;
842
- old_builder . add_reducer (
841
+ builder . add_reducer (
843
842
"check_deliveries" ,
844
- ProductType :: from ( [ ( "a" , AlgebraicType :: Ref ( old_deliveries_type ) ) ] ) ,
843
+ ProductType :: from ( [ ( "a" , AlgebraicType :: Ref ( deliveries_type ) ) ] ) ,
845
844
None ,
846
845
) ;
847
846
848
- old_builder
847
+ builder
849
848
. build_table_with_new_type (
850
849
"Inspections" ,
851
850
ProductType :: from ( [
852
851
( "scheduled_id" , AlgebraicType :: U64 ) ,
853
- ( "scheduled_at" , old_schedule_at . clone ( ) ) ,
852
+ ( "scheduled_at" , schedule_at . clone ( ) ) ,
854
853
] ) ,
855
854
true ,
856
855
)
857
856
. with_auto_inc_primary_key ( 0 )
858
857
. with_index_no_accessor_name ( btree ( 0 ) )
859
858
. finish ( ) ;
860
859
861
- old_builder . add_row_level_security ( "SELECT * FROM Apples" ) ;
860
+ builder . add_row_level_security ( "SELECT * FROM Apples" ) ;
862
861
863
- let old_def : ModuleDef = old_builder
862
+ builder
864
863
. finish ( )
865
864
. try_into ( )
866
- . expect ( "old_def should be a valid database definition" ) ;
865
+ . expect ( "old_def should be a valid database definition" )
866
+ }
867
867
868
- let mut new_builder = RawModuleDefV9Builder :: new ( ) ;
869
- let _ = new_builder. add_type :: < u32 > ( ) ; // reposition ScheduleAt in the typespace, should have no effect.
870
- let new_schedule_at = new_builder. add_type :: < ScheduleAt > ( ) ;
871
- let new_sum_ty = AlgebraicType :: sum ( [ ( "v1" , AlgebraicType :: U64 ) , ( "v2" , AlgebraicType :: Bool ) ] ) ;
872
- let new_sum_refty = new_builder. add_algebraic_type ( [ ] , "sum" , new_sum_ty, true ) ;
873
- new_builder
868
+ fn updated_module_def ( ) -> ModuleDef {
869
+ let mut builder = RawModuleDefV9Builder :: new ( ) ;
870
+ let _ = builder. add_type :: < u32 > ( ) ; // reposition ScheduleAt in the typespace, should have no effect.
871
+ let schedule_at = builder. add_type :: < ScheduleAt > ( ) ;
872
+ let sum_ty = AlgebraicType :: sum ( [ ( "v1" , AlgebraicType :: U64 ) , ( "v2" , AlgebraicType :: Bool ) ] ) ;
873
+ let sum_refty = builder. add_algebraic_type ( [ ] , "sum" , sum_ty, true ) ;
874
+ builder
874
875
. build_table_with_new_type (
875
876
"Apples" ,
876
877
ProductType :: from ( [
877
878
( "id" , AlgebraicType :: U64 ) ,
878
879
( "name" , AlgebraicType :: String ) ,
879
880
( "count" , AlgebraicType :: U16 ) ,
880
- ( "sum" , new_sum_refty . into ( ) ) ,
881
+ ( "sum" , sum_refty . into ( ) ) ,
881
882
] ) ,
882
883
true ,
883
884
)
@@ -889,7 +890,7 @@ mod tests {
889
890
. with_index ( btree ( [ 0 , 2 ] ) , "id_count_index" )
890
891
. finish ( ) ;
891
892
892
- new_builder
893
+ builder
893
894
. build_table_with_new_type (
894
895
"Bananas" ,
895
896
ProductType :: from ( [
@@ -907,13 +908,13 @@ mod tests {
907
908
. with_access ( TableAccess :: Private )
908
909
. finish ( ) ;
909
910
910
- let new_deliveries_type = new_builder
911
+ let deliveries_type = builder
911
912
. build_table_with_new_type (
912
913
"Deliveries" ,
913
914
ProductType :: from ( [
914
915
( "scheduled_id" , AlgebraicType :: U64 ) ,
915
- ( "scheduled_at" , new_schedule_at . clone ( ) ) ,
916
- ( "sum" , AlgebraicType :: array ( new_sum_refty . into ( ) ) ) ,
916
+ ( "scheduled_at" , schedule_at . clone ( ) ) ,
917
+ ( "sum" , AlgebraicType :: array ( sum_refty . into ( ) ) ) ,
917
918
] ) ,
918
919
true ,
919
920
)
@@ -922,18 +923,18 @@ mod tests {
922
923
// remove schedule def
923
924
. finish ( ) ;
924
925
925
- new_builder . add_reducer (
926
+ builder . add_reducer (
926
927
"check_deliveries" ,
927
- ProductType :: from ( [ ( "a" , AlgebraicType :: Ref ( new_deliveries_type ) ) ] ) ,
928
+ ProductType :: from ( [ ( "a" , AlgebraicType :: Ref ( deliveries_type ) ) ] ) ,
928
929
None ,
929
930
) ;
930
931
931
- let new_inspections_type = new_builder
932
+ let new_inspections_type = builder
932
933
. build_table_with_new_type (
933
934
"Inspections" ,
934
935
ProductType :: from ( [
935
936
( "scheduled_id" , AlgebraicType :: U64 ) ,
936
- ( "scheduled_at" , new_schedule_at . clone ( ) ) ,
937
+ ( "scheduled_at" , schedule_at . clone ( ) ) ,
937
938
] ) ,
938
939
true ,
939
940
)
@@ -944,28 +945,33 @@ mod tests {
944
945
. finish ( ) ;
945
946
946
947
// add reducer.
947
- new_builder . add_reducer (
948
+ builder . add_reducer (
948
949
"perform_inspection" ,
949
950
ProductType :: from ( [ ( "a" , AlgebraicType :: Ref ( new_inspections_type) ) ] ) ,
950
951
None ,
951
952
) ;
952
953
953
954
// Add new table
954
- new_builder
955
+ builder
955
956
. build_table_with_new_type ( "Oranges" , ProductType :: from ( [ ( "id" , AlgebraicType :: U32 ) ] ) , true )
956
957
. with_index ( btree ( 0 ) , "id_index" )
957
958
. with_column_sequence ( 0 )
958
959
. with_unique_constraint ( 0 )
959
960
. with_primary_key ( 0 )
960
961
. finish ( ) ;
961
962
962
- new_builder . add_row_level_security ( "SELECT * FROM Bananas" ) ;
963
+ builder . add_row_level_security ( "SELECT * FROM Bananas" ) ;
963
964
964
- let new_def : ModuleDef = new_builder
965
+ builder
965
966
. finish ( )
966
967
. try_into ( )
967
- . expect ( "new_def should be a valid database definition" ) ;
968
+ . expect ( "new_def should be a valid database definition" )
969
+ }
968
970
971
+ #[ test]
972
+ fn successful_auto_migration ( ) {
973
+ let old_def = initial_module_def ( ) ;
974
+ let new_def = updated_module_def ( ) ;
969
975
let plan = ponder_auto_migrate ( & old_def, & new_def) . expect ( "auto migration should succeed" ) ;
970
976
971
977
let apples = expect_identifier ( "Apples" ) ;
@@ -1413,4 +1419,45 @@ mod tests {
1413
1419
// but different columns from an old one.
1414
1420
// We've left the check in, just in case this changes in the future.
1415
1421
}
1422
+ #[ test]
1423
+ fn print_empty_to_populated_schema_migration ( ) {
1424
+ // Start with completely empty schema
1425
+ let old_builder = RawModuleDefV9Builder :: new ( ) ;
1426
+ let old_def: ModuleDef = old_builder
1427
+ . finish ( )
1428
+ . try_into ( )
1429
+ . expect ( "old_def should be a valid database definition" ) ;
1430
+
1431
+ let new_def = initial_module_def ( ) ;
1432
+ let plan = ponder_migrate ( & old_def, & new_def) . expect ( "auto migration should succeed" ) ;
1433
+
1434
+ insta:: assert_snapshot!(
1435
+ "empty_to_populated_migration" ,
1436
+ plan. pretty_print( false ) . expect( "should pretty print" )
1437
+ ) ;
1438
+ }
1439
+
1440
+ #[ test]
1441
+ fn print_supervised_migration ( ) {
1442
+ let old_def = initial_module_def ( ) ;
1443
+ let new_def = updated_module_def ( ) ;
1444
+ let plan = ponder_migrate ( & old_def, & new_def) . expect ( "auto migration should succeed" ) ;
1445
+
1446
+ insta:: assert_snapshot!(
1447
+ "updated pretty print" ,
1448
+ plan. pretty_print( false ) . expect( "should pretty print" )
1449
+ ) ;
1450
+ }
1451
+
1452
+ #[ test]
1453
+ fn no_color_print_supervised_migration ( ) {
1454
+ let old_def = initial_module_def ( ) ;
1455
+ let new_def = updated_module_def ( ) ;
1456
+ let plan = ponder_migrate ( & old_def, & new_def) . expect ( "auto migration should succeed" ) ;
1457
+
1458
+ insta:: assert_snapshot!(
1459
+ "updated pretty print no color" ,
1460
+ plan. pretty_print( true ) . expect( "should pretty print" )
1461
+ ) ;
1462
+ }
1416
1463
}
0 commit comments