summaryrefslogtreecommitdiffstats
path: root/ansible_collections/lowlydba/sqlserver/plugins/modules/install_script.ps1
blob: ae552f486f9394dd0ecc62d1426fe6734c7226b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!powershell
# -*- coding: utf-8 -*-

# (c) 2022, John McCall (@lowlydba)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

#AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils
#Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" }
#Requires -Modules @{ ModuleName="dbops"; ModuleVersion="0.9.0" }

$spec = @{
    supports_check_mode = $true
    options = @{
        database = @{type = 'str'; required = $true }
        path = @{type = 'str'; required = $true }
        deployment_method = @{type = 'str'; required = $false; default = 'NoTransaction'
            choices = @('NoTransaction', 'SingleTransaction', 'TransactionPerScript', 'AlwaysRollback')
        }
        schema_version_table = @{type = 'str'; required = $false }
        no_log_version = @{type = 'bool'; required = $false; default = $false }
        connection_timeout = @{type = 'int'; required = $false; default = 30 }
        execution_timeout = @{type = 'int'; required = $false; default = 0 }
        output_file = @{type = 'str'; required = $false }
        create_database = @{type = 'bool'; required = $false; default = $false }
        no_recurse = @{type = 'bool'; required = $false; default = $false }
        match = @{type = 'str'; required = $false }
    }
}

$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
$sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
$database = $module.Params.database
$schemaVersionTable = $module.Params.schema_version_table
$path = $module.Params.path
$outputFile = $module.Params.output_file
$match = $module.Params.match
$connectionTimeout = $module.Params.connection_timeout
$executionTimeout = $module.Params.execution_timeout
$createDatabase = $module.Params.create_database
$noRecurse = $module.Params.no_recurse
$noLogVersion = $module.Params.no_log_version
$checkMode = $module.Checkmode
$PSDefaultParameterValues = @{ "*:Confirm" = $false; "*:WhatIf" = $checkMode }

try {
    $installSplat = @{
        SqlInstance = $sqlInstance
        Credential = $sqlCredential
        Database = $database
        Path = $path
        ConnectionTimeout = $connectionTimeout
        ExecutionTimeout = $executionTimeout
        CreateDatabase = $createDatabase
        NoRecurse = $noRecurse
        Silent = $true
        Type = "SqlServer"
    }
    if ($schemaVersionTable) {
        $installSplat.Add("SchemaVersionTable", $schemaVersionTable)
    }
    if ($outputFile) {
        $installSplat.Add("OutputFile", $outputFile)
    }
    if ($match) {
        $installSplat.Add("Match", $match)
    }
    if ($noLogVersion) {
        $installSplat.SchemaVersionTable = $null
    }

    $output = Install-DboScript @installSplat
    if ($output.DeploymentLog[-1] -ne "No new scripts need to be executed - completing.") {
        $module.Result.changed = $true

        if ($null -ne $output) {
            $resultData = ConvertTo-SerializableObject -InputObject $output
            $module.Result.data = $resultData
        }
    }

    $module.ExitJson()
}
catch {
    $module.FailJson("Failed migration script(s) execution: $($_.Exception.Message)", $_)
}