Skip to content

Instantly share code, notes, and snippets.

@mika76
Forked from mitchelldavis/Liquibase.Common.Tasks
Created November 1, 2022 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mika76/06bba1bd9021cd8a3c3533562aa4831d to your computer and use it in GitHub Desktop.
Save mika76/06bba1bd9021cd8a3c3533562aa4831d to your computer and use it in GitHub Desktop.
Some MSBuild Scripts for Liquibase and MS Sql.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Liquibase_Common_Targets>true</Liquibase_Common_Targets>
<sqljdbcDriver Condition=" '$(sqljdbcDriver)' == '' ">..\Dependencies\sqljdbc_4.0\enu\sqljdbc4.jar</sqljdbcDriver>
<Server Condition=" '$(Server)' == '' ">localhost</Server>
<Database Condition=" '$(Database)' == '' "></Database>
<Username Condition=" '$(Username)' == '' "></Username>
<Password Condition=" '$(Password)' == '' "></Password>
<ChangeLogFile Condition=" '$(ChangeLogFile)' == '' "></ChangeLogFile>
<UseIS Condition=" '$(UseIS)' == '' ">true</UseIS>
<x86Java Condition=" '$(x86Java)' == '' ">true</x86Java>
<Development Condition=" '$(Development)' == '' ">true</Development>
<Tag Condition=" '$(Tag)' == '' ">$([System.DateTime]::Now.ToString(&quot;yyyy-MM-dd hh:mm:ss.fff&quot;))</Tag>
<RollbackToDate Condition=" '$(RollbackToDate)' == '' ">$(Tag)</RollbackToDate>
<RollbackCount Condition=" '$(RollbackCount)' == '' ">0</RollbackCount>
</PropertyGroup>
<ItemGroup>
<LiquibaseJar Include="..\Dependencies\liquibase\liquibase.jar" />
<sqljdbcAuth Include="..\Dependencies\sqljdbc_4.0\enu\auth\x86" Condition=" '$(x86Java)' == 'true' " />
<sqljdbcAuth Include="..\Dependencies\sqljdbc_4.0\enu\auth\x64" Condition=" '$(x86Java)' == 'false' " />
<PesterBat Include="..\packages\Pester\tools\bin\Pester.bat"/>
<ChangeLogFile Include="$(ChangeLogFile)" Condition=" '$(ChangeLogFile)' != '' "/>
</ItemGroup>
<Target Name="Validate">
<Error Text="You must supply the path to the sqljdbc driver: /P:sqljdbcDriver&lt;Path to Driver&gt;"
Condition=" '$(sqljdbcDriver)' == '' " />
<Error Text="You must supply the url of the Server to deploy to: /P:Server&lt;Server url&gt;"
Condition=" '$(Server)' == '' " />
<Error Text="You must supply the name of the database to deploy to: /P:Database&lt;Database to deploy to&gt;"
Condition=" '$(Database)' == '' " />
<Error Text="You must supply the username to use to log into the database: /P:Username&lt;Username to use&gt;"
Condition=" '$(Username)' == '' AND '$(UseIS)' == 'false' " />
<Error Text="You must supply the password to use to log into the database: /P:Password&lt;Password to use&gt;"
Condition=" '$(Password)' == '' AND '$(UseIS)' == 'false' " />
<Error Text="You must supply an ChangeLogFile item."
Condition=" '@(ChangeLogFile -> '%(Identity)', ' ')' == '' " />
<Error Text="The Change Log File you supplied (%(FullPath)) was not found. Please make sure you supply a change log file that exists."
Condition="!Exists(%(ChangeLogFile.FullPath)) AND '$(ChangeLogFile)' == '' " />
<Error Text="The Liquibase jar file was not found. It is expected to be at (%(LiquibaseJar.Identity))."
Condition="!Exists(%(LiquibaseJar.Identity))" />
<Error Text="The location of the sqljdbcAuth dll was not found. It is expected to be at (%(sqljdbcAuth.Identity))."
Condition=" !Exists(%(sqljdbcAuth.Identity)) " />
<Message Text="Build Validation successful. Good to go..." />
</Target>
<Target Name="CreateDatabase" Condition=" '$(Development)' == 'true' " DependsOnTargets="_ResolveDependencyPackages;RunPesterTests">
<CreateDatabaseTask ScriptsDirectory="$(MSBuildThisFileDirectory)\Scripts" Server="$(Server)" Database="$(Database)"/>
</Target>
<Target Name="DropDatabase" Condition=" '$(Development)' == 'true' " DependsOnTargets="_ResolveDependencyPackages;RunPesterTests">
<DropDatabaseTask ScriptsDirectory="$(MSBuildThisFileDirectory)\Scripts" Server="$(Server)" Database="$(Database)"/>
</Target>
<Target Name="RunPesterTests" DependsOnTargets="_ResolveDependencyPackages;">
<ItemGroup>
<execute Include="powershell -NonInteractive -NoProfile -ExecutionPolicy unrestricted -command &quot;"/>
<execute Include="Import-Module '$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)..\packages\Pester\tools\Pester.psm1))'%3B"/>
<execute Include="Invoke-Pester -relative_path '$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)Scripts))' -EnableExit;" />
<execute Include="&quot;"/>
</ItemGroup>
<Exec Command="@(execute -> '%(Identity)', '')" CustomErrorRegularExpression="\[-\]"/>
</Target>
<Target Name="UpdateTestingRollback" DependsOnTargets="Validate;CreateDatabase">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=INFO" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="updateTestingRollback"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="Update" DependsOnTargets="Validate;CreateDatabase">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=INFO" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="update"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="Status" DependsOnTargets="Validate;CreateDatabase">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=INFO" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="status"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="ClearCheckSums" DependsOnTargets="Validate;CreateDatabase">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=INFO" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="clearCheckSums"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="ChangeLogSync" DependsOnTargets="Validate;CreateDatabase">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=INFO" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="changeLogSync"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="TagDatabase" DependsOnTargets="Validate;CreateDatabase">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=INFO" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="tag &quot;$(Tag)&quot;"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="Rollback" DependsOnTargets="Validate;CreateDatabase">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=INFO" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="rollback &quot;$(Tag)&quot;"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="RollbackToDate" DependsOnTargets="Validate;CreateDatabase">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=INFO" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="rollbackToDate &quot;$(RollbackToDate)&quot;"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="RollbackCount" DependsOnTargets="Validate;CreateDatabase">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=INFO" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="rollbackCount $(RollbackCount)"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="ExtractFromDatabase" DependsOnTargets="Validate">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=WARNING" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="generateChangeLog"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -Xmx1000m -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="ExportData" DependsOnTargets="Validate">
<ItemGroup>
<executable Include="liquibase.integration.commandline.Main" />
<executable Include="--changeLogFile=%(ChangeLogFile.Identity)"/>
<executable Include="--classpath=$(sqljdbcDriver)"/>
<executable Include="--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<executable Include="--logLevel=WARNING" />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BUsername=$(Username)%3BPassword=$(Password);&quot;"
Condition=" '$(UseIS)' == 'false' " />
<executable Include="&quot;--url=jdbc:sqlserver://$(Server)%3BdatabaseName=$(Database)%3BintegratedSecurity=true;&quot;"
Condition=" '$(UseIS)' == 'true' " />
<executable Include="--diffTypes=&quot;data&quot;"/>
<executable Include="generateChangeLog"/>
</ItemGroup>
<ItemGroup>
<executableProperties Include="java.library.path=%(sqljdbcAuth.FullPath)" />
<executableProperties Include="$(ChangeLogProperties)"
Condition=" '$(ChangeLogProperties)' != '' "/>
</ItemGroup>
<Exec Command="java -Xmx1000m -cp %(LiquibaseJar.Identity) @(executableProperties -> '&quot;-D%(Identity)&quot;', ' ') @(executable,' ')"
CustomErrorRegularExpression="SEVERE" />
</Target>
<Target Name="CleanDatabase" DependsOnTargets="Validate;DropDatabase" />
<Target Name="Build" DependsOnTargets="UpdateTestingRollback"/>
<Target Name="Clean" DependsOnTargets="CleanDatabase"/>
<Target Name="Rebuild" DependsOnTargets="Clean;Build"/>
<Import Project="$(MSBuildThisFileDirectory)\NuGet.targets" Condition=" '$(NuGet_Targets)' == '' " />
<Import Project="$(MSBuildThisFileDirectory)\Liquibase.Common.tasks"/>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project InitialTargets="" DefaultTargets="All" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<AssemblyFile>$(MSBuildThisFileDirectory)\..\packages\MSBuild.Extension.Pack\tools\net40\MSBuild.ExtensionPack.TaskFactory.PowerShell.dll</AssemblyFile>
</PropertyGroup>
<UsingTask TaskFactory="PowershellTaskFactory" TaskName="CreateDatabaseTask" AssemblyFile="$(AssemblyFile)">
<ParameterGroup>
<ScriptsDirectory Required="true" ParameterType="System.String" />
<Server Required="true" ParameterType="System.String" />
<Database Required="true" ParameterType="System.String" />
</ParameterGroup>
<Task>
<![CDATA[
$log.LogMessage([Microsoft.Build.Framework.MessageImportance]"normal", "Loading required Scripts...")
. (join-path $ScriptsDirectory "DoesDatabaseExist.ps1")
. (join-path $ScriptsDirectory "CreateDatabase.ps1")
if(-Not (DoesDatabaseExist $Server $Database))
{
$log.LogMessage([Microsoft.Build.Framework.MessageImportance]"normal", "Creating {0} on {1}", $Database, $Server)
CreateDatabase $Server $Database
}
else
{
$log.LogMessage([Microsoft.Build.Framework.MessageImportance]"normal", "{0} already exists on {1}. Skipping...", $Database, $Server)
}
]]>
</Task>
</UsingTask>
<UsingTask TaskFactory="PowershellTaskFactory" TaskName="DropDatabaseTask" AssemblyFile="$(AssemblyFile)">
<ParameterGroup>
<ScriptsDirectory Required="true" ParameterType="System.String" />
<Server Required="true" ParameterType="System.String" />
<Database Required="true" ParameterType="System.String" />
</ParameterGroup>
<Task>
<![CDATA[
$log.LogMessage([Microsoft.Build.Framework.MessageImportance]"normal", "Loading required Scripts...")
. (join-path $ScriptsDirectory "DoesDatabaseExist.ps1")
. (join-path $ScriptsDirectory "DropDatabase.ps1")
if(DoesDatabaseExist $Server $Database)
{
$log.LogMessage([Microsoft.Build.Framework.MessageImportance]"normal", "{0} does exist, dropping...", $Database)
DropDatabase $Server $Database
}
]]>
</Task>
</UsingTask>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment