@@ -106,7 +106,7 @@ public override void Execute()
106
106
107
107
// Scripting data is not avaialable in the scripter
108
108
options . ScriptData = false ;
109
- SetScriptingOptions ( options ) ;
109
+ SetScriptingOptions ( server , options ) ;
110
110
111
111
switch ( this . Parameters . Operation )
112
112
{
@@ -578,7 +578,39 @@ private static Dictionary<string, SqlServerVersion> LoadScriptCompatibilityMap()
578
578
579
579
}
580
580
581
- private void SetScriptingOptions ( ScriptingOptions scriptingOptions )
581
+ private static SqlServerVersion GetCompatibilityOptionFromVersion ( Version version )
582
+ {
583
+ switch ( version . Major )
584
+ {
585
+ case 8 :
586
+ return SqlServerVersion . Version80 ;
587
+ case 9 :
588
+ return SqlServerVersion . Version90 ;
589
+ case 10 :
590
+ if ( version . Minor == 50 )
591
+ {
592
+ return SqlServerVersion . Version105 ;
593
+ }
594
+ return SqlServerVersion . Version100 ;
595
+ case 11 :
596
+ return SqlServerVersion . Version110 ;
597
+ case 12 :
598
+ return SqlServerVersion . Version120 ;
599
+ case 13 :
600
+ return SqlServerVersion . Version130 ;
601
+ case 14 :
602
+ return SqlServerVersion . Version140 ;
603
+ case 15 :
604
+ return SqlServerVersion . Version150 ;
605
+ case 16 :
606
+ return SqlServerVersion . Version160 ;
607
+ case 17 :
608
+ default : //Please update the default value to latest version when a new version is released.
609
+ return SqlServerVersion . Version170 ;
610
+ }
611
+ }
612
+
613
+ private void SetScriptingOptions ( Server server , ScriptingOptions scriptingOptions )
582
614
{
583
615
scriptingOptions . AllowSystemObjects = true ;
584
616
@@ -588,21 +620,29 @@ private void SetScriptingOptions(ScriptingOptions scriptingOptions)
588
620
//We always want role memberships for users and database roles to be scripted
589
621
scriptingOptions . IncludeDatabaseRoleMemberships = true ;
590
622
SqlServerVersion targetServerVersion ;
591
- if ( scriptCompatibilityMap . TryGetValue ( this . Parameters . ScriptOptions . ScriptCompatibilityOption , out targetServerVersion ) )
623
+ if ( server . ServerVersion != null )
624
+ {
625
+ scriptingOptions . TargetServerVersion = GetCompatibilityOptionFromVersion ( server . Version ) ;
626
+ }
627
+ else if ( scriptCompatibilityMap . TryGetValue ( this . Parameters . ScriptOptions . ScriptCompatibilityOption , out targetServerVersion ) )
592
628
{
593
629
scriptingOptions . TargetServerVersion = targetServerVersion ;
594
630
}
595
631
else
596
632
{
597
633
//If you are getting this assertion fail it means you are working for higher
598
634
//version of SQL Server. You need to update this part of code.
599
- Logger . Warning ( "This part of the code is not updated corresponding to latest version change" ) ;
635
+ Logger . Warning ( "This part of the code is not updated corresponding to latest version change" ) ;
600
636
}
601
637
602
638
// for cloud scripting to work we also have to have Script Compat set to 105.
603
639
// the defaults from scripting options should take care of it
604
640
SqlScriptOptions . ScriptDatabaseEngineType targetDatabaseEngineType ;
605
- if ( Enum . TryParse < SqlScriptOptions . ScriptDatabaseEngineType > ( this . Parameters . ScriptOptions . TargetDatabaseEngineType , out targetDatabaseEngineType ) )
641
+ if ( server . DatabaseEngineType != null )
642
+ {
643
+ scriptingOptions . TargetDatabaseEngineType = server . DatabaseEngineType ;
644
+ }
645
+ else if ( Enum . TryParse < SqlScriptOptions . ScriptDatabaseEngineType > ( this . Parameters . ScriptOptions . TargetDatabaseEngineType , out targetDatabaseEngineType ) )
606
646
{
607
647
switch ( targetDatabaseEngineType )
608
648
{
@@ -616,7 +656,11 @@ private void SetScriptingOptions(ScriptingOptions scriptingOptions)
616
656
}
617
657
618
658
SqlScriptOptions . ScriptDatabaseEngineEdition targetDatabaseEngineEdition ;
619
- if ( Enum . TryParse < SqlScriptOptions . ScriptDatabaseEngineEdition > ( this . Parameters . ScriptOptions . TargetDatabaseEngineEdition , out targetDatabaseEngineEdition ) )
659
+ if ( server . DatabaseEngineEdition != null )
660
+ {
661
+ scriptingOptions . TargetDatabaseEngineEdition = server . DatabaseEngineEdition ;
662
+ }
663
+ else if ( Enum . TryParse < SqlScriptOptions . ScriptDatabaseEngineEdition > ( this . Parameters . ScriptOptions . TargetDatabaseEngineEdition , out targetDatabaseEngineEdition ) )
620
664
{
621
665
switch ( targetDatabaseEngineEdition )
622
666
{
0 commit comments