dataplat / dbops

⚙ dbops - Powershell module that provides continuous database deployments on any scale
MIT License
164 stars 39 forks source link

Variable names with underscore characters ('_') are not getting substituted #129

Closed jarnoj closed 2 years ago

jarnoj commented 2 years ago

Here is example that uses variable substitution (same SQL script also attached below). It inserts a new row where the First_Name variable does not get substituted (but LastName variable works ok):

DECLARE @FirstName VARCHAR(255)='#{First_Name}' -- underscore causes some issue
DECLARE @LastName VARCHAR(255)='#{LastName}'  -- this works (no underscore)

IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Employee]') AND type in (N'U'))
    CREATE TABLE [dbo].[Employee]( FirstName varchar(20), LastName varchar(20));

INSERT INTO dbo.Employee(FirstName, LastName) VALUES(@FirstName, @LastName)

SQLQuery1.zip

Related PowerShell commands (tested with v0.7.0 dbops module):

# create a package
$scriptPath = ''
$pkgPath = 'C:\tmp\dbops_var_test\pkg.zip'
$includePattern = '.*\.sql'
New-DBOPackage -Path $pkgPath -ScriptPath $scriptPath -Match $includePattern -Force

# deploy the package
$server = '.\SQL2017'
$db = 'Foo'
Install-DBOPackage -Path $pkgPath -Variables @{ First_Name = 'John'; LastName = 'Doe' } -SqlInstance $server -Database $db -SchemaVersionTable $null -CreateDatabase
nvarscar commented 2 years ago

This is controlled by the following regex, which only accounts for . and -: https://github.com/dataplat/dbops/blob/development/internal/functions/Resolve-VariableToken.ps1#L27

Should be a simple fix.

nvarscar commented 2 years ago

Fixed in 0.7.1