gpt4 book ai didi

azure-devops - Azure Pipelines - 针对 DB 的文件夹中的 CI/CD : How to execute all . sql 文件

转载 作者:行者123 更新时间:2023-12-04 09:42:35 24 4
gpt4 key购买 nike

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

我的问题是,我将如何执行所有匹配“”的 .sql 文件..\Functions\BASE_*.sql ' 针对来自蔚蓝管道的数据库?我尝试了以下方法,但不支持匹配多个文件。有没有更好的选择呢?我是否需要编写循环脚本并自己完成?

# pipeline

trigger:
- master

pool:
vmImage: 'windows-latest'

steps:
- task: SqlDacpacDeploymentOnMachineGroup@0
inputs:
TaskType: 'sqlQuery'
SqlFile: '$(System.DefaultWorkingDirectory)\Functions\BASE_*.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:\a\1\s\Functions\BASE_*.sql. There can be only one.
Finishing: SqlDacpacDeploymentOnMachineGroup

最佳答案

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

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

模板是:

# 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}}'

然后主管道使用不同的搜索字符串调用模板。
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-devops - Azure Pipelines - 针对 DB 的文件夹中的 CI/CD : How to execute all . sql 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62262172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com