从Azure SQL数据库提取表CREATE TABLE脚本至Azure Repo遇问题求助
Azure SQL表脚本提取问题:DACPAC方式缺失字段/约束,Git提交无变更
需求
- 替换现有AzureCLI@2脚本为PowerShell脚本,实现从Azure SQL数据库提取每个表的
CREATE TABLE脚本并同步到Azure Repo - 当前通过生成DACPAC文件的方式实现,但存在脚本不完整和Git提交无变更的问题
当前使用的AzureCLI@2脚本
- task: AzureCLI@2 inputs: azureSubscription: '$(azureSubscription)' scriptType: 'bash' scriptLocation: 'inlineScript' inlineScript: | set -e echo "Starting DACPAC extraction..." # Obtain an access token echo "Obtaining access token..." accessToken=$(az account get-access-token --resource https://database.windows.net/ --query accessToken --output tsv) echo "Access token obtained." # Check if the access token was retrieved if [ -z "$accessToken" ]; then echo "Failed to obtain access token. Exiting." exit 1 fi # Create directories if they don't exist mkdir -p $(Build.SourcesDirectory)/temp/DACPAC mkdir -p $(Build.SourcesDirectory)/some/ran/dir/ # Extract DACPAC file to a temporary directory echo "Extracting DACPAC file..." ./sqlpackage/sqlpackage /Action:Extract /SourceServerName:$(sqlServerName) /SourceDatabaseName:$(sqlDatabaseName) /TargetFile:$(Build.SourcesDirectory)/temp/DACPAC/$(sqlDatabaseName).dacpac /AccessToken:$accessToken # Unzip the extracted DACPAC file echo "Unzipping DACPAC file..." unzip -o $(Build.SourcesDirectory)/temp/DACPAC/$(sqlDatabaseName).dacpac -d $(Build.SourcesDirectory)/temp/DACPAC # Move extracted schema files to the target directory, replacing existing files echo "Replacing existing .sql files with updated schema..." find $(Build.SourcesDirectory)/temp/DACPAC -name '*.sql' -exec cp -v {} $(Build.SourcesDirectory)/some/ran/dir/ \; 2>error.log echo "Schema extraction and replacement completed." echo "Listing files in target directory..." ls -l $(Build.SourcesDirectory)/some/ran/dir/ ls -l $(Build.SourcesDirectory)/temp/DACPAC # Commit and push the changes echo "Committing and pushing changes..." cd $(Build.SourcesDirectory) git config --global user.email "$(sqlAdminLogin)" git config --global user.name "Name" git add --all git status git commit -m "Update schema files from DACPAC extraction" || echo "No changes to commit" git push https://$(azureDevOpsPat)@dev.azure.com/my/git/repo/here HEAD:$(Build.SourceBranch) --force ls -l $(Build.SourcesDirectory)/some/ran/dir/ echo "Changes committed and pushed."
执行后遇到的问题
1. Git提交无变更
管道执行日志显示:
HEAD detached at fc510bf nothing to commit, working tree clean HEAD detached at fc510bf nothing to commit, working tree clean No changes to commit Everything up-to-date
但目标目录下确实存在.sql文件。
2. 提取的脚本不完整
提取出的[Contact Report]表脚本缺少[Staff ID]字段及外键约束,与Azure Data Factory中使用的完整脚本不一致。已尝试更新目标路径为$(Build.SourcesDirectory)/Database/dbo/Tables/,问题仍未解决。
求助方向
- 是否可以改用PowerShell脚本实现完整的表脚本提取?
- 如何解决当前DACPAC方式下脚本不完整和Git提交无变更的问题?
内容的提问来源于stack exchange,提问作者WorkingProgrammer
相关产品推荐
相关产品推荐

