-
Notifications
You must be signed in to change notification settings - Fork 9
/
04_ImportSampleDataFromStackexchange.ps1
154 lines (144 loc) · 7.26 KB
/
04_ImportSampleDataFromStackexchange.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
[CmdletBinding()]
param (
[String]$DatabaseDefinitionFile = '/tmp/tmp_DatabaseDefinition.json',
[String]$SampleDataPath = '/tmp/stackexchange',
[String]$StackexchangeSite = 'dba.meta',
[switch]$UseExistingData
)
$ErrorActionPreference = 'Stop'
Import-Module -Name PSFramework
$DatabaseDefinition = Get-Content -Path $DatabaseDefinitionFile | ConvertFrom-Json
# Load sample data
if (-not $UseExistingData) {
try {
$start = Get-Date
if (Test-Path -Path $SampleDataPath) {
Remove-Item -Path $SampleDataPath -Recurse -Force
}
$null = New-Item -Path $SampleDataPath -ItemType Directory
Push-Location -Path $SampleDataPath
Invoke-WebRequest -Uri https://archive.org/download/stackexchange/$StackexchangeSite.stackexchange.com.7z -OutFile tmp.7z -UseBasicParsing
$null = 7za e tmp.7z
Remove-Item -Path tmp.7z
Pop-Location
$duration = (Get-Date) - $start
Write-PSFMessage -Level Host -Message "Dowload sample data finished in $($duration.TotalSeconds) seconds"
} catch {
Write-PSFMessage -Level Warning -Message "Dowload sample data failed: $_"
exit 1
}
}
$tableNames = 'Badges', 'Comments', 'PostLinks', 'Posts', 'Users', 'Votes'
$dbDef = $DatabaseDefinition | Where-Object ContainerName -eq 'SQLServer'
if ($dbDef) {
try {
$start = Get-Date
. $PSScriptRoot\..\SQLServer\Connect-SqlInstance.ps1
. $PSScriptRoot\..\SQLServer\Import-SqlTable.ps1
$credential = [PSCredential]::new('StackOverflow', (ConvertTo-SecureString -String $dbDef.AdminPassword -AsPlainText -Force))
$connection = Connect-SqlInstance -Instance $dbDef.Instance -Credential $credential -Database 'StackOverflow' -EnableException
foreach ($tableName in $tableNames) {
$columnMap = $null
if ($tableName -eq 'Badges') {
$columnMap = @{ CreationDate = 'Date' }
}
Import-SqlTable -Path $SampleDataPath/$tableName.xml -Connection $connection -Table $tableName -TruncateTable -ColumnMap $columnMap -EnableException
}
$duration = (Get-Date) - $start
Write-PSFMessage -Level Host -Message "Importing sample data to SQL Server finished in $($duration.TotalSeconds) seconds"
} catch {
Write-PSFMessage -Level Warning -Message "Importing sample data to SQL Server failed: $_"
}
}
$dbDef = $DatabaseDefinition | Where-Object ContainerName -eq 'Oracle'
if ($dbDef) {
try {
$start = Get-Date
. $PSScriptRoot\..\Oracle\Import-OraLibrary.ps1
. $PSScriptRoot\..\Oracle\Connect-OraInstance.ps1
. $PSScriptRoot\..\Oracle\Import-OraTable.ps1
Import-OraLibrary -EnableException
$credential = [PSCredential]::new('stackoverflow', (ConvertTo-SecureString -String $dbDef.AdminPassword -AsPlainText -Force))
$connection = Connect-OraInstance -Instance $dbDef.Instance -Credential $credential -EnableException
foreach ($tableName in $tableNames) {
$columnMap = $null
if ($tableName -eq 'Badges') {
$columnMap = @{ CreationDate = 'Date' }
}
Import-OraTable -Path $SampleDataPath/$tableName.xml -Connection $connection -Table $tableName -TruncateTable -ColumnMap $columnMap -EnableException
}
$duration = (Get-Date) - $start
Write-PSFMessage -Level Host -Message "Importing sample data to Oracle finished in $($duration.TotalSeconds) seconds"
} catch {
Write-PSFMessage -Level Warning -Message "Importing sample data to Oracle failed: $_"
}
}
$dbDef = $DatabaseDefinition | Where-Object ContainerName -eq 'MySQL'
if ($dbDef) {
try {
$start = Get-Date
. $PSScriptRoot\..\MySQL\Import-MyLibrary.ps1
. $PSScriptRoot\..\MySQL\Connect-MyInstance.ps1
. $PSScriptRoot\..\MySQL\Import-MyTable.ps1
Import-MyLibrary -EnableException
$credential = [PSCredential]::new('stackoverflow', (ConvertTo-SecureString -String $dbDef.AdminPassword -AsPlainText -Force))
$connection = Connect-MyInstance -Instance $dbDef.Instance -Credential $credential -Database 'stackoverflow' -AllowLoadLocalInfile -EnableException
foreach ($tableName in $tableNames) {
$columnMap = $null
if ($tableName -eq 'Badges') {
$columnMap = @{ CreationDate = 'Date' }
}
Import-MyTable -Path $SampleDataPath/$tableName.xml -Connection $connection -Table $tableName -TruncateTable -ColumnMap $columnMap -EnableException
}
$duration = (Get-Date) - $start
Write-PSFMessage -Level Host -Message "Importing sample data to MySQL finished in $($duration.TotalSeconds) seconds"
} catch {
Write-PSFMessage -Level Warning -Message "Importing sample data to MySQL failed: $_"
}
}
$dbDef = $DatabaseDefinition | Where-Object ContainerName -eq 'MariaDB'
if ($dbDef) {
try {
$start = Get-Date
. $PSScriptRoot\..\MySQL\Import-MyLibrary.ps1
. $PSScriptRoot\..\MySQL\Connect-MyInstance.ps1
. $PSScriptRoot\..\MySQL\Import-MyTable.ps1
Import-MyLibrary -EnableException
$credential = [PSCredential]::new('stackoverflow', (ConvertTo-SecureString -String $dbDef.AdminPassword -AsPlainText -Force))
$connection = Connect-MyInstance -Instance $dbDef.Instance -Credential $credential -Database 'stackoverflow' -AllowLoadLocalInfile -EnableException
foreach ($tableName in $tableNames) {
$columnMap = $null
if ($tableName -eq 'Badges') {
$columnMap = @{ CreationDate = 'Date' }
}
Import-MyTable -Path $SampleDataPath/$tableName.xml -Connection $connection -Table $tableName -TruncateTable -ColumnMap $columnMap -EnableException
}
$duration = (Get-Date) - $start
Write-PSFMessage -Level Host -Message "Importing sample data to MariaDB finished in $($duration.TotalSeconds) seconds"
} catch {
Write-PSFMessage -Level Warning -Message "Importing sample data to MariaDB failed: $_"
}
}
$dbDef = $DatabaseDefinition | Where-Object ContainerName -eq 'PostgreSQL'
if ($dbDef) {
try {
$start = Get-Date
. $PSScriptRoot\..\PostgreSQL\Import-PgLibrary.ps1
. $PSScriptRoot\..\PostgreSQL\Connect-PgInstance.ps1
. $PSScriptRoot\..\PostgreSQL\Import-PgTable.ps1
Import-PgLibrary -EnableException
$credential = [PSCredential]::new('stackoverflow', (ConvertTo-SecureString -String $dbDef.AdminPassword -AsPlainText -Force))
$connection = Connect-PgInstance -Instance $dbDef.Instance -Credential $credential -Database 'stackoverflow' -EnableException
foreach ($tableName in $tableNames) {
$columnMap = $null
if ($tableName -eq 'Badges') {
$columnMap = @{ CreationDate = 'Date' }
}
Import-PgTable -Path $SampleDataPath/$tableName.xml -Connection $connection -Table $tableName -TruncateTable -ColumnMap $columnMap -EnableException
}
$duration = (Get-Date) - $start
Write-PSFMessage -Level Host -Message "Importing sample data to PostgreSQL finished in $($duration.TotalSeconds) seconds"
} catch {
Write-PSFMessage -Level Warning -Message "Importing sample data to PostgreSQL failed: $_"
}
}