【问题标题】:Generate SQL Server schema change script on Azure DevOps pipeline在 Azure DevOps 管道上生成 SQL Server 架构更改脚本
【发布时间】:2020-04-16 00:11:14
【问题描述】:

我正在尝试允许管道将架构更改发布到本地 SQL Server 2017 实例,但我想分两步完成:

  • 生成架构更改脚本操作
  • 审核通过后发布

我知道publishing to SQL Azure可以通过设置deploymentAction: 'Script'然后deploymentAction: 'Publish'来实现

有没有办法以类似的方式发布到本地 SQL Server?我已经尝试过SqlDacpacDeploymentOnMachineGroup 任务,但似乎无法通过此任务分两步完成

【问题讨论】:

    标签: sql-server azure azure-devops azure-pipelines


    【解决方案1】:

    我认为没有内置任务可以为您执行此操作。但是,SQLPackage.exe 有这个选项。看看这个帖子:http://diegogiacomelli.com.br/azure-pipelines-generating-db-script/。它描述了使用命令行任务生成 sql 脚本。

    一旦你有了脚本,你可以使用 SqlDacpacDeploymentOnMachineGroup 来发布脚本(虽然它不会重复使用已经生成的脚本),或者你可以编写一个 powershell 脚本来发布 sql 脚本到数据库。可以在此处找到此类脚本的示例:https://careers.centric.eu/nl/blog/custom-azure-devops-pipeline-task-execute-sql-script/

    【讨论】:

    • 第一篇文章确实为我指明了正确的方向,但是我认为可以使用 SqlDacpacDeploymentOnMachineGroup 发布增量 SQL 脚本。今晚我将发布我的解决方案。另外,第二篇文章中的代码链接失效了,这让像我这样对 Powershell 没有太多经验的人很难编写这样的脚本。
    【解决方案2】:

    我终于设法通过数据库更改实现 SQL 模式生成,然后发布这些更改(在批准后)。一些备注:

    • 如果更改会导致数据丢失,这将不起作用。
    • sqlpackage 的路径仅在安装 Visual Studio 2019 时正确,例如在 windows-2019 图像中。
    • 之前的package.dacpac 是通过构建.sqlproj 项目先前生成的。
    • 我通过组变量传递了以下变量(有关如何创建组变量的更多信息here):
      • targetDBConnectionString
      • servername
      • databasename
      • adminlogin
      • adminPassword
    • 我已将approval 添加到ApplyChanges 阶段 (在 Pipelines 菜单中,选择环境,然后选择 ApplyChanges 环境,然后从三个点approvals and checks 按钮,在右上角)。这样的变化不是 在手动批准之前应用到数据库。

    stage: VerifyScript
          displayName: 'Script database schema changes'
          dependsOn: 
            - Build
          jobs:
          - deployment: VerifyScript
            pool:
              vmImage: 'windows-2019'
            variables:
            - group: 'Timeline CV - Release'
            environment: 'scriptverification'
            strategy:
              runOnce:
                deploy:
                  steps:
                  - download: current
                    artifact: dropDacpac
                    patterns: '**/*'
    
              - task: CmdLine@2
                displayName: 'Generate schema changes script'
                inputs:
                  script: |
                    "c:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\140\sqlpackage.exe"    ^
                    /action:script ^
                    /diagnostics:true ^
                    /sourcefile:$(Pipeline.Workspace)\dropDacpac\path\to\the\dacpacFile\package.dacpac    ^
                    /targetConnectionString:$(targetDBConnectionString) ^
                    /outputpath:$(Build.StagingDirectory)\changesScript.sql
    
              - task: PublishPipelineArtifact@1
                inputs:
                  targetPath: '$(Build.StagingDirectory)'
                  artifactName: dropSqlSchemaChangesScript
                condition: succeededOrFailed()
    
              - task: PowerShell@2
                displayName: Show Auto Generated SQL Script
                inputs: 
                  targetType: 'inline'
                  script: | 
                    Write-Host "Auto Generated SQL Update Script:"
                    Get-Content $(Build.StagingDirectory)\changesScript.sql | foreach {Write-Output      $_}
    
    - stage: ApplyChanges
      displayName: 'Apply database schema changes'
      dependsOn: VerifyScript
      jobs:
      - deployment: ApplyChanges
        pool:
          vmImage: 'windows-2019'
        variables:
        - group: 'Timeline CV - Release'
        environment: 'applyChanges'
        strategy:
          runOnce:
            deploy:
              steps:
              - download: current
                artifact: dropSqlSchemaChangesScript
              - task: SqlDacpacDeploymentOnMachineGroup@0
                displayName: 'Deploy SQL schema changes script'
                inputs:
                  taskType: 'sqlQuery'
                  sqlFile: '$(Pipeline.Workspace)\dropSqlSchemaChangesScript\changesScript.sql'
                  targetMethod: 'server'
                  authScheme: 'sqlServerAuthentication'
                  serverName: '$(servername)'
                  databaseName: '$(databasename)'
                  sqlUsername: '$(adminlogin)'
                  sqlPassword: '$(adminPassword)'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 2021-01-21
      • 2017-12-16
      相关资源
      最近更新 更多