What are the challenges you face when working across database platforms? Take the survey
Options

How can i access the current semantic version during build?

PeterDanielsPeterDaniels Posts: 89 Bronze 3
edited February 11, 2019 2:13PM in SQL Change Automation
I am using semantic versioning  for migration folder ordering. Ilike how it populates the version column in the __migrationLog table with the current/ latest migration folder name.   I'd like to access this value on my build server.  Is there a way to get that value before building the project? Perhaps in a PoSh script in the build/ci server?

I'm hoping to avoid writing a script to look for the latest migration folder in the project. 

TIA,

- Peter
Tagged:

Best Answers

  • Options
    DiogoDiogo Posts: 67 Silver 5
    Hi @PeterDaniels,

    I'm afraid we don't expose that at the moment. 
  • Options
    PeterDanielsPeterDaniels Posts: 89 Bronze 3
    I did, @Monday. Here's the function:
    $VersionInfo = Get-LatestVersionAndMigrationNumber -ProjectFilePath $ProjectFilePath
    $PackageVersion = $VersionInfo.Version + '-migration' + $VersionInfo.MigrationNumber
    $ReleaseVersion = $PackageVersion
    </code># Get latest version and migration from project file path
    # This is cool.  Version like 1.1.0 and Migration number like 5 - to build ReleaseVersion and/or PackageVersion
    function Get-LatestVersionAndMigrationNumber {
        [CmdletBinding()]
    
        param (
            [parameter(Mandatory=$true)]
            [string]
            $ProjectFilePath
        )
    
        process {
            # First, strip the project file name and find the Migrations folder
            $ProjectRootFolder = Split-Path -Path $ProjectFilePath    
            $MigrationsFolder = Join-Path -Path $ProjectRootFolder -ChildPath "Migrations"
            if (-not (Test-Path -Path $MigrationsFolder)) {
                Write-Error -Message "Invalid path: $MigrationsFolder"
                exit
            }
    
            # Get the latest migration folder sem ver
            [string]$Latest = Get-ChildItem -Path $MigrationsFolder -Directory | ForEach-Object {[Version]($_.Name -split "-")[0]} | Sort-Object -Descending | Select -First 1
    
            # Get the base folder back now that I found the latest version:
            $LatestFolder = Get-ChildItem -Path $MigrationsFolder -Directory | Where-Object {$_.Name -like "$Latest*"}
    
            # Now get the migrations in that folder
            $LatestMigrationsFolder = Join-Path -Path $MigrationsFolder -ChildPath $LatestFolder
    
            $LatestMigration = (Get-ChildItem -Path $LatestMigrationsFolder -File).Name | Sort-Object -Descending | select -First 1
    
            [int]$LatestMigrationSequenceNumber = ($LatestMigration -split "_")[0]
    
            # Return a custom object with the latest sem version and migration sequence num
            #$Latest + "-migration" + $LatestMigrationSequenceNumber
            $obj = New-Object -TypeName PSCustomObject -Property @{Version = $Latest;MigrationNumber = $LatestMigrationSequenceNumber}
            $obj
        }
    }
    </pre><div>I then use it:<br><pre class="CodeBlock"><code>
    To create a version that looks like (for example) 1.1.0-migration14


Answers

  • Options
    MondayMonday Posts: 77 Silver 3
    This is a good question and one I am hoping to understand as well. I could write a script but seems it would be cleaner to use a built in API if possible.
  • Options
    Bummer.  I'd like to create coherent concept of "version" from SCA to Bamboo to Octo as well as in the __MigrationLog.  Frankly, I want it to integrate with DLM Dashboard, too.  But...I digress.

    I think I'll have to add a custom build step to recurse through the migrations folder to look for the latest sem ver folder name....
  • Options
    MondayMonday Posts: 77 Silver 3
    PeterDaniels Did you ever write a script to recurse through the migrations folder to look for the latest version? If so would you mind sharing? If not I have a need as well and will be looking into this myself.
  • Options
    MondayMonday Posts: 77 Silver 3
    @PeterDaniels Pretty much what I did as well. I was going to read the .sqlproj file and get how it is configured instead of assuming how I think it is. Maybe I will do that later.

        <DeployOnceSubFolder>Migrations</DeployOnceSubFolder>
        <MigrationOrdering>FilePath</MigrationOrdering>
  • Options
    PeterDanielsPeterDaniels Posts: 89 Bronze 3
    @Monday - I love how your'e thinking here.  I was considering adding that functionality to make it more robust, but decided on a quick dev first.
Sign In or Register to comment.