使用 sqlpackage.exe 自定义 SQL 服务器数据工具 (SSDT) 包的部署以因环境而异

Customizing deployments of a SQL Server Data Tools (SSDT) package using sqlpackage.exe to vary per environment

我有一个 SQL 服务器数据工具 (SSDT) .sqlproj 文件,其中包含一个 Table 类型,如下所示:

CREATE TYPE [dbo].[tableType] AS TABLE
(
    [id] uniqueidentifier INDEX [idx],
)
 WITH  
        (MEMORY_OPTIMIZED = ON); 

我正在将此项目部署到 Azure SQL。问题是我的测试数据库是 Azure SQL 标准(而不是高级或关键业务)。因此,它们不支持内存优化表。所以我想弄清楚的是一种在测试中不进行内存优化而在生产中进行内存优化的部署类型的方法。命令行开关、env 变量,任何有效的方法。我正在使用 sqlpackage.exe 进行部署,但我愿意接受其他方法来实现这一点。

我会尝试 SSDT SQLCMD 变量:

SQLCMD Variables

In SQL Server Database Projects you can utilize SQLCMD variables to provide dynamic substitution to be used for debugging or publishing. You enter the variable name and values and during build, the values will be substituted. If there are no local values, the default value will be used. By entering these variables in project properties, they will automatically be offered in publishing and are stored in publishing profiles. You can pull in the project values of the variables into publish via the Load Values button.

Make sure the right variables are entered in project properties, because these variables are not validated against a script in the project, nor are the variables used in script automatically populated.

图片来源:https://social.msdn.microsoft.com/Forums/sqlserver/en-US/30895c7f-9a49-4fab-b3d1-b690fff82792/sqlcmd-variables-not-recognized-by-ssdt-build-fails-when-these-are-used-in-a-script

CREATE TYPE [dbo].[tableType] AS TABLE
(
    [id] uniqueidentifier INDEX [idx],
) $(param_name);

并且在发布期间可以定义为:

TEST - empty
PROD -  WITH (MEMORY_OPTIMIZED = ON); 

虽然我强烈建议升级测试环境。

由于这是特定于环境的,我建议您选择以下方法之一:

您可以采用三种方法:

  1. 正如@Lukasz Szozda 所建议的,您可以拥有 SQLCMD 变量。定义 SQLCMD 变量并提及环境。根据环境,您可以决定在 post 部署脚本中执行哪个脚本。
IF '$(Environment)' = 'Prod'
        BEGIN
            :r .\ProdSpecificScript\Prod_TableTable.sql
        END
        ELSE
        BEGIN
           :r .\NonProdSpecificScript\NonProd_TableTable.sql
        END 
  1. 您可以使用以下信息更新项目文件,以根据环境使用不同的文件覆盖部署后脚本。您可以有两个不同的 post 部署脚本和不同的 TableType 脚本。
<Target Name="BeforeBuild">
      <Message Text="Copy files task running for configuration: $(Configuration)" Importance="high" />
      <Copy Condition=" '$(Environment)' == 'Prod' " SourceFiles="Scripts\Post-Deployment\Default.Script.PostDeployment.sql" DestinationFiles="Scripts\Post-Deployment\Script.PostDeployment.sql" OverwriteReadOnlyFiles="true" />
      <Copy Condition=" '$(Environment)' == 'NonProd' " SourceFiles="Scripts\Post-Deployment\Default.Script.PostDeployment.sql" DestinationFiles="Scripts\Post-Deployment\Script.PostDeployment.sql" OverwriteReadOnlyFiles="true" />      
  </Target>
  1. 您可以检查 Azure 服务层并据此决定脚本。
DECLARE @ServiceTier VARCHAR(30)
SET @ServiceTier = (SELECT COALESCE(DATABASEPROPERTYEX(DB_NAME(), 'ServiceObjective'), 'N/A in v11') AS AzureTier FROM sys.database_service_objectives

IF @ServiceTier = 'P1'
BEGIN
    :r .\ProdSpecificScript\Prod_TableTable.sql
END
ELSE
BEGIN
   :r .\NonProdSpecificScript\NonProd_TableTable.sql
END