Azure Pipelines - CI/CD:如何针对 DB 执行文件夹中的所有 .sql 文件 [英] Azure Pipelines - CI/CD: How to execute all .sql files in a folder against DB

查看:21
本文介绍了Azure Pipelines - CI/CD:如何针对 DB 执行文件夹中的所有 .sql 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将所有 SQL 文件签入 Azure DevOps 上的存储库.我有命名约定,可以让我知道其他 .sql 文件使用了哪些 .sql 文件(例如,文件创建了一个由存储过程使用的视图).我想强制使用 repo 来跟踪代码更改,并且不想使用 dacpac 文件.我希望每个函数/视图/存储过程都有自己的文件.

I have all the SQL files checked into a repo on Azure devops. I have naming convention that allows me to know which .sql files are used by other .sql files (ex. file creates a view that is used by a stored procedure). I am wanting to force the use of the repo for tracking code changes, and would prefer not to use dacpac file. I want each function / view / stored procedure to have it's own file.

我的问题是,我将如何针对 azure 管道中的数据库执行与..FunctionsBASE_*.sql"匹配的所有 .sql 文件?我尝试了以下方法,但不支持匹配多个文件.有没有更好的选择呢?我是否需要编写循环脚本并自己编写?

My question, how would I execute all .sql files matching '..FunctionsBASE_*.sql' against a database from an azure pipeline? I tried the below, but does not support multiple files being matched. Is there a better option that does? Do I need to script a loop and do it myself?

# pipeline

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:
- task: SqlDacpacDeploymentOnMachineGroup@0
  inputs:
    TaskType: 'sqlQuery'
    SqlFile: '$(System.DefaultWorkingDirectory)FunctionsBASE_*.sql'
    ServerName: '$(SQL_ServerName).database.windows.net'
    DatabaseName: '$(SQL_DatabaseName)'
    AuthScheme: 'sqlServerAuthentication'
    SqlUsername: '$(SQL_UserName)'
    SqlPassword: '$(SQL_Password)'

我得到的错误是:

Starting: SqlDacpacDeploymentOnMachineGroup
==============================================================================
Task         : SQL Server database deploy
Description  : Deploy a SQL Server database using DACPAC or SQL scripts
Version      : 0.3.23
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/sql-dacpac-deployment-on-machine-group
==============================================================================
##[error]Found more than one file to deploy with search pattern d:a1sFunctionsBASE_*.sql. There can be only one.
Finishing: SqlDacpacDeploymentOnMachineGroup

推荐答案

经过一天的研究和试验,我想出的最好办法是在 repo 中将文件分开,然后在 CI/中将多个文件组合在一起CD 管道,然后再针对 DB 运行它.

After a day's research and trial, the best I could come up with was to keep files separate in the repo, but then combine multiple files together in the CI/CD pipeline before running it against the DB.

我创建了一个模板,将匹配的文件合并到暂存目录中的单个文件中,发布它以调试管道,然后针对 SQL 服务器执行它.

I created a template to combine matching files into a single file in the staging directory, publish it for debugging the pipeline, then execute it against the SQL server.

模板为:

# Template for executing all SQL files matching a string search

parameters:
- name: path #$path = "$(System.DefaultWorkingDirectory)Functions"
  type: string
- name: match #$match = "BASE_*.sql"
  type: string
- name: outPath #$outPath = "$(System.DefaultWorkingDirectory)Functions"
  type: string
- name: outName #$outName = "BASE.sql"
  type: string

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      echo Source Files:
      Get-ChildItem ${{parameters.path}} -include ${{parameters.match}} -rec 
  displayName: 'Files to process: ${{parameters.match}}'
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      echo Creating: ${{parameters.outPath}}${{parameters.outName}}
      Get-ChildItem ${{parameters.path}} -include ${{parameters.match}} -rec | ForEach-Object {gc $_; ""} | out-file ${{parameters.outPath}}${{parameters.outName}}
  displayName: 'Combine: ${{parameters.outName}}'
- task: PublishPipelineArtifact@1
  inputs:
   targetPath: '${{parameters.outPath}}${{parameters.outName}}'
   artifact: '${{parameters.outName}}'
   publishLocation: 'pipeline'
  displayName: 'Publish: ${{parameters.outName}}'
- task: SqlDacpacDeploymentOnMachineGroup@0
  inputs:
    TaskType: 'sqlQuery'
    SqlFile: '${{parameters.outPath}}${{parameters.outName}}'
    ServerName: '$(SQL_ServerName).database.windows.net'
    DatabaseName: '$(SQL_DatabaseName)'
    AuthScheme: 'sqlServerAuthentication'
    SqlUsername: '$(SQL_UserName)'
    SqlPassword: '$(SQL_Password)'
  displayName: 'Create or Alter: ${{parameters.outName}}'
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: Remove-Item ${{parameters.path}}${{parameters.match}} -Recurse
  displayName: 'Delete Files: ${{parameters.match}}'

然后主管道使用不同的搜索字符串调用模板.

The main pipeline then calls the template with the different search strings.

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: MKDIR "$(System.DefaultWorkingDirectory)\Combined\Functions"
  displayName: 'Create Output Folder'
- template: azTemplate/CombineAndRunSQLFiles.yml # Functions: UTIL
  parameters:
    path: "$(System.DefaultWorkingDirectory)\Functions"
    match: "UTIL_*.sql"
    outPath: "$(System.DefaultWorkingDirectory)\Combined\Functions"
    outName: "UTIL.sql"
- template: azTemplate/CombineAndRunSQLFiles.yml # Functions: BASE
  parameters:
    path: "$(System.DefaultWorkingDirectory)\Functions"
    match: "BASE_*.sql"
    outPath: "$(System.DefaultWorkingDirectory)\Combined\Functions"
    outName: "BASE.sql"

结果:

Pool: Azure Pipelines
Image: windows-latest
Agent: Hosted Agent
Started: Today at 9:55 AM
Duration: 1m 6s

Job preparation parameters
5 artifacts produced
Job live console data:
Finishing: Job

这篇关于Azure Pipelines - CI/CD:如何针对 DB 执行文件夹中的所有 .sql 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆