@@ -2553,9 +2553,7 @@ private IReadOnlyList<MigrationOperation> RewriteOperations(
2553
2553
}
2554
2554
2555
2555
var isTemporalTable = renameTableOperation [ SqlServerAnnotationNames . IsTemporal ] as bool ? == true ;
2556
- if ( isTemporalTable &&
2557
- ! temporalInformation . DisabledVersioning &&
2558
- ! temporalInformation . ShouldEnableVersioning )
2556
+ if ( isTemporalTable )
2559
2557
{
2560
2558
DisableVersioning (
2561
2559
tableName ,
@@ -2636,16 +2634,12 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema
2636
2634
var oldPeriodEndColumnName =
2637
2635
alterTableOperation . OldTable [ SqlServerAnnotationNames . TemporalPeriodEndColumnName ] as string ;
2638
2636
2639
- if ( ! temporalInformation . DisabledVersioning
2640
- && ! temporalInformation . ShouldEnableVersioning )
2641
- {
2642
- DisableVersioning (
2643
- tableName ,
2644
- schema ,
2645
- temporalInformation ,
2646
- suppressTransaction ,
2647
- shouldEnableVersioning : null ) ;
2648
- }
2637
+ DisableVersioning (
2638
+ tableName ,
2639
+ schema ,
2640
+ temporalInformation ,
2641
+ suppressTransaction ,
2642
+ shouldEnableVersioning : null ) ;
2649
2643
2650
2644
if ( ! temporalInformation . DisabledPeriod )
2651
2645
{
@@ -2692,27 +2686,39 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema
2692
2686
addColumnOperation . DefaultValue = DateTime . MaxValue ;
2693
2687
}
2694
2688
2689
+ var isSparse = addColumnOperation [ SqlServerAnnotationNames . Sparse ] as bool ? == true ;
2690
+ var isComputed = addColumnOperation . ComputedColumnSql != null ;
2691
+
2692
+ if ( isSparse || isComputed )
2693
+ {
2694
+ DisableVersioning (
2695
+ tableName ,
2696
+ schema ,
2697
+ temporalInformation ,
2698
+ suppressTransaction ,
2699
+ shouldEnableVersioning : true ) ;
2700
+ }
2701
+
2695
2702
// when adding sparse column to temporal table, we need to disable versioning.
2696
2703
// This is because it may be the case that HistoryTable is using compression (by default)
2697
2704
// and the add column operation fails in that situation
2698
2705
// in order to make it work we need to disable versioning (if we haven't done it already)
2699
2706
// and de-compress the HistoryTable
2700
- if ( addColumnOperation [ SqlServerAnnotationNames . Sparse ] as bool ? == true )
2707
+ if ( isSparse )
2701
2708
{
2702
- if ( ! temporalInformation . DisabledVersioning
2703
- && ! temporalInformation . ShouldEnableVersioning )
2704
- {
2705
- DisableVersioning (
2706
- tableName ,
2707
- schema ,
2708
- temporalInformation ,
2709
- suppressTransaction ,
2710
- shouldEnableVersioning : true ) ;
2711
- }
2712
-
2713
2709
DecompressTable ( temporalInformation . HistoryTableName ! , temporalInformation . HistoryTableSchema , suppressTransaction ) ;
2714
2710
}
2715
2711
2712
+ if ( addColumnOperation . ComputedColumnSql != null )
2713
+ {
2714
+ DisableVersioning (
2715
+ tableName ,
2716
+ schema ,
2717
+ temporalInformation ,
2718
+ suppressTransaction ,
2719
+ shouldEnableVersioning : true ) ;
2720
+ }
2721
+
2716
2722
operations . Add ( addColumnOperation ) ;
2717
2723
2718
2724
// when adding (non-period) column to an existing temporal table we need to check if we have disabled versioning
@@ -2725,6 +2731,16 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema
2725
2731
var addHistoryTableColumnOperation = CopyColumnOperation < AddColumnOperation > ( addColumnOperation ) ;
2726
2732
addHistoryTableColumnOperation . Table = temporalInformation . HistoryTableName ! ;
2727
2733
addHistoryTableColumnOperation . Schema = temporalInformation . HistoryTableSchema ;
2734
+
2735
+ if ( addHistoryTableColumnOperation . ComputedColumnSql != null )
2736
+ {
2737
+ // computed columns are not allowed inside HistoryTables
2738
+ // but the historical computed value will be copied over to the non-computed counterpart,
2739
+ // as long as their names and types (including nullability) match
2740
+ // so we remove ComputedColumnSql info, so that the column in history table "appears normal"
2741
+ addHistoryTableColumnOperation . ComputedColumnSql = null ;
2742
+ }
2743
+
2728
2744
operations . Add ( addHistoryTableColumnOperation ) ;
2729
2745
}
2730
2746
}
@@ -2743,18 +2759,14 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema
2743
2759
var droppingPeriodColumn = dropColumnOperation . Name == temporalInformation . PeriodStartColumnName
2744
2760
|| dropColumnOperation . Name == temporalInformation . PeriodEndColumnName ;
2745
2761
2746
- if ( ! temporalInformation . DisabledVersioning
2747
- && ! temporalInformation . ShouldEnableVersioning )
2748
- {
2749
- // if we are dropping non-period column, we should enable versioning at the end.
2750
- // When dropping period column there is no need - we are removing the versioning for this table altogether
2751
- DisableVersioning (
2752
- tableName ,
2753
- schema ,
2754
- temporalInformation ,
2755
- suppressTransaction ,
2756
- shouldEnableVersioning : droppingPeriodColumn ? null : true ) ;
2757
- }
2762
+ // if we are dropping non-period column, we should enable versioning at the end.
2763
+ // When dropping period column there is no need - we are removing the versioning for this table altogether
2764
+ DisableVersioning (
2765
+ tableName ,
2766
+ schema ,
2767
+ temporalInformation ,
2768
+ suppressTransaction ,
2769
+ shouldEnableVersioning : droppingPeriodColumn ? null : true ) ;
2758
2770
2759
2771
if ( droppingPeriodColumn && ! temporalInformation . DisabledPeriod )
2760
2772
{
@@ -2820,6 +2832,14 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema
2820
2832
2821
2833
if ( temporalInformation . IsTemporalTable )
2822
2834
{
2835
+ if ( alterColumnOperation . OldColumn . ComputedColumnSql != alterColumnOperation . ComputedColumnSql )
2836
+ {
2837
+ throw new NotSupportedException (
2838
+ SqlServerStrings . TemporalMigrationModifyingComputedColumnNotSupported (
2839
+ alterColumnOperation . Name ,
2840
+ alterColumnOperation . Table ) ) ;
2841
+ }
2842
+
2823
2843
// for alter column operation converting column from nullable to non-nullable in the temporal table
2824
2844
// we must disable versioning in order to properly handle it
2825
2845
// specifically, switching values in history table from null to the default value
@@ -2831,9 +2851,7 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema
2831
2851
var changeToSparse = alterColumnOperation . OldColumn [ SqlServerAnnotationNames . Sparse ] as bool ? != true
2832
2852
&& alterColumnOperation [ SqlServerAnnotationNames . Sparse ] as bool ? == true ;
2833
2853
2834
- if ( ( changeToNonNullable || changeToSparse )
2835
- && ! temporalInformation . DisabledVersioning
2836
- && ! temporalInformation . ShouldEnableVersioning )
2854
+ if ( changeToNonNullable || changeToSparse )
2837
2855
{
2838
2856
DisableVersioning (
2839
2857
tableName ! ,
@@ -2878,9 +2896,7 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema
2878
2896
2879
2897
case DropPrimaryKeyOperation :
2880
2898
case AddPrimaryKeyOperation :
2881
- if ( temporalInformation . IsTemporalTable
2882
- && ! temporalInformation . DisabledVersioning
2883
- && ! temporalInformation . ShouldEnableVersioning )
2899
+ if ( temporalInformation . IsTemporalTable )
2884
2900
{
2885
2901
DisableVersioning (
2886
2902
tableName ! ,
@@ -2948,16 +2964,20 @@ void DisableVersioning(
2948
2964
bool suppressTransaction ,
2949
2965
bool ? shouldEnableVersioning )
2950
2966
{
2951
- temporalInformation . DisabledVersioning = true ;
2967
+ if ( ! temporalInformation . DisabledVersioning
2968
+ && ! temporalInformation . ShouldEnableVersioning )
2969
+ {
2970
+ temporalInformation . DisabledVersioning = true ;
2952
2971
2953
- AddDisableVersioningOperation ( tableName , schema , suppressTransaction ) ;
2972
+ AddDisableVersioningOperation ( tableName , schema , suppressTransaction ) ;
2954
2973
2955
- if ( shouldEnableVersioning != null )
2956
- {
2957
- temporalInformation . ShouldEnableVersioning = shouldEnableVersioning . Value ;
2958
- if ( shouldEnableVersioning . Value )
2974
+ if ( shouldEnableVersioning != null )
2959
2975
{
2960
- temporalInformation . SuppressTransaction = suppressTransaction ;
2976
+ temporalInformation . ShouldEnableVersioning = shouldEnableVersioning . Value ;
2977
+ if ( shouldEnableVersioning . Value )
2978
+ {
2979
+ temporalInformation . SuppressTransaction = suppressTransaction ;
2980
+ }
2961
2981
}
2962
2982
}
2963
2983
}
0 commit comments