Database Managment project
Build
Build the project would genenerate a .dacpac file which would be used for deployment.
Deploy
We can use SqlPackage.exe command tool for deployment.
It can be found from path like “C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\SqlPackage.exe”. When install sql server please include the feature SQL Server Development Tools (SSDT). Currently seems there is an issue for visual studio, the package file cannot be executed by previous version of Sqlpackage.exe, we can use the nuget package https://www.nuget.org/packages/Microsoft.Data.Tools.Msbuild for lastest Sqlpackage. For easy access, we have created a project SqlPackageNugetReference which will download that package.
Samples to call the cmd:
First perform a check of what will be updated in the target database.
SqlPackage.exe /Action:DeployReport /SourceFile:./db.dacpac /Profile:./db.publish.xml /OutputPath:./ChangesToDeploy.xml /TargetPassword:xxxx
If everything is fine, deploy your changes with the following command. May need to enable other options.
SqlPackage.exe /Action:Publish /SourceFile:./db.dacpac /Profile:./db.publish.xml /TargetPassword:xxxx
Specify SQLCMD parameter if needed like below:
/Variables:Mode=LOCAL
Sample deployment cmd lines
Specify correct password. And you may need to set 'BlockOnPossibleDataLoss' to False in case make some table schema changes like alter column.
Deploy to central SQL Server
.\SqlPackage\sqlpackage.exe /Action:Publish /SourceFile:./DataServiceDB.dacpac /TargetServerName:xxx /TargetDatabaseName:XXX /TargetUser:sa /TargetPassword:xxx /Variables:Mode=REMOTE /p:BlockOnPossibleDataLoss=True
Deploy to SQL Express 2012 with LOCAL mode
.\SqlPackage\sqlpackage.exe /Action:Publish /SourceFile:./DataServiceDB.dacpac /TargetServerName:xxx\SQL2012,1433 /TargetDatabaseName:XXX /TargetUser:sa /TargetPassword:XXX /Variables:Mode=LOCAL /p:BlockOnPossibleDataLoss=False
Notes
By default it only controls DB schema changes. Currently we are using own data version table to manage data version in post deployment script.
Can create the project by importing from existings DB or script files.
Should only have one pre or post deployment script. But it can include more than one scripts for non build item. Also you need to enable SQLCMD mode by click menu "SQL" -> "Execution Settings" -> "SQLCMD Mode".
Sample reference script file, it has version control to make sure only run the file once:
/*Check the version to avoid duplicate update*/
declare @versionId nvarchar(50)
SELECT @versionId = 'Version1'
if EXISTS (select VersionId from __DataVersion where VersionId = @versionId )
BEGIN
PRINT @versionId + ' has already been applied. Ingore it.'
RETURN
END
/*Only allow to run script under specified mode, the mode is defined as a global SQLCMD variable.
--Only allow to deploy it under LOCAL mode
IF ('$(Mode)' <> 'LOCAL')
BEGIN
PRINT 'No need to deploy ' + @versionId + ' for $(Mode) Mode.'
RETURN
END
*/
BEGIN TRY
BEGIN TRANSACTION
/*Insert version at first*/
insert into __DataVersion (VersionId, [State], Note) VALUES (@versionId, 'processing','Version1 Desc')
/*
* Handle data changes here
*/
--***********Replace code here************--
print @versionId + ' Complete'
/*Update version state at last*/
UPDATE __DataVersion SET [State] = 'finished' WHERE VersionId = @versionId
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
print @versionId + ' failed'
DELETE FROM __DataVersion WHERE VersionId = @versionId
DECLARE
@ErrorMessage NVARCHAR(4000),
@ErrorSeverity INT,
@ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (
@ErrorMessage,
@ErrorSeverity,
@ErrorState
);
END CATCH
Important rules for DB update:
Known Issues
When deploy to SQL 2005, it may get below exception. Just ignore it and rerun it againg.
Error SQL72014: .Net SqlClient Data Provider: Msg 2812, Level 16, State 62, Line 1 找不到存储过程 'sp_refreshsqlmodule'。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型