internal/functions/Get-DbUpJournal.ps1

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
function Get-DbUpJournal {
    # Returns a DbUp builder with a proper connection object
    Param (
        [Parameter(Mandatory)]
        [scriptblock]$Connection,
        [scriptblock]$Log,
        [string]$Schema,
        [string]$SchemaVersionTable,
        [Parameter(Mandatory)]
        [DBOps.ConnectionType]$Type
    )
    if ($SchemaVersionTable) {
        # retrieve schema and table names
        $table = $SchemaVersionTable.Split('.')
        if ($table.Count -gt 2) {
            Stop-PSFFunction -EnableException $true -Message 'Incorrect table name - use the following syntax: schema.table'
            return
        }
        elseif ($table.Count -eq 2) {
            $tableName = $table[1]
            $schemaName = $table[0]
        }
        else {
            $tableName = $table[0]
            if ($Schema) {
                $schemaName = $Schema
            }
        }
        # define journal type based on target connection type
        if ($Type -eq [DBOps.ConnectionType]::SQLServer) {
            if (!$schemaName) {
                $schemaName = 'dbo'
            }
            $dbUpJournalType = [DbUp.SqlServer.SqlTableJournal]
        }
        elseif ($Type -eq [DBOps.ConnectionType]::Oracle) {
            $dbUpJournalType = [DbUp.Oracle.OracleTableJournal]
        }
        elseif ($Type -eq [DBOps.ConnectionType]::MySQL) {
            $dbUpJournalType = [DBOpsMySqlTableJournal]
        }
        elseif ($Type -eq [DBOps.ConnectionType]::PostgreSQL) {
            $dbUpJournalType = [DbUp.Postgresql.PostgresqlTableJournal]
        }
        else {
            Stop-PSFFunction -Message "Unknown type $Type" -EnableException $true
            return
        }
        # return a journal object
        Write-PSFMessage -Level Verbose -Message "Creating journal object for $Type in $schemaName.$tableName"
        return $dbUpJournalType::new($Connection, $Log, $schemaName, $tableName)
    }
    else {
        # return a null journal to disable journalling
        return [DbUp.Helpers.NullJournal]::new()
    }
}