-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdbup-consolescripts.psm1
118 lines (95 loc) · 3.29 KB
/
dbup-consolescripts.psm1
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
<# Package Manager Console scripts to support DbUp #>
function New-Migration {
param (
[string] $Name,
[string] $Encoding = ""
)
_New-Migration-Private -isDynamic 0 -Name $Name -Encoding $Encoding
}
# Adds a migration file on disk but - doesn't add this in VS project
# This function is usable when we have a dynamic creation of Scripts in csproj - example below
# <!-- Include all scripts from Scripts Folder -->
# <Target Name="BeforeBuild">
# <ItemGroup>
# <EmbeddedResource Include="Scripts\*.sql"/>
# </ItemGroup>
# </Target>
function New-Migration-Dynamic {
param (
[string] $Name,
[string] $Encoding = ""
)
_New-Migration-Private -isDynamic 1 -Name $Name -Encoding $Encoding
}
function Start-Migrations {
param (
[switch] $WhatIf
)
$project = Get-Project
$outputPath = $project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value
$activeConfiguration = $dte.Solution.SolutionBuild.ActiveConfiguration.Name
Write-Host "Building..."
$dte.Solution.SolutionBuild.BuildProject($activeConfiguration, $project.FullName, $true)
$outputAssemblyName = $project.Properties.Item("AssemblyName").Value
$projectDirectory = Split-Path $project.FullName
$args = " --fromconsole"
if ($Whatif.IsPresent){
$args = $args + " --whatif"
}
If($outputPath.IndexOf("netcoreapp") -ge 0) {
$projectCmd = $projectDirectory + "\" + $outputPath + $outputAssemblyName + ".dll"
dotnet $projectCmd $args
}
Else {
$projectExe = $projectDirectory + "\" + $outputPath + $outputAssemblyName + ".exe"
& $projectExe $args
}
}
function _New-Migration-Private{
param (
[bool] $isDynamic,
[string] $Name,
[string] $Encoding = ""
)
$project = Get-Project
$projectDirectory = Split-Path $project.FullName
$scriptsDirectoryName = "Scripts"
$scriptDirectory = $projectDirectory + "\" + $scriptsDirectoryName
$fileNameBase = (Get-Date -UFormat "%y%m%d%H%M%S")
#Get reference to Scripts project item
$targetProjectItem = $null
if($isDynamic -eq $False){
try
{
$targetProjectItem = $project.ProjectItems.Item($scriptsDirectoryName)
}
catch
{
$project.ProjectItems.AddFolder($scriptsDirectoryName) | Out-Null
$targetProjectItem = $project.ProjectItems.Item($scriptsDirectoryName)
}
}
If ($name -ne ""){
$fileNameBase = $fileNameBase + "_" + $Name
}
$fileNameBase = $fileNameBase.Replace(" ","_")
$fileName = $fileNameBase + ".sql"
$filePath = $scriptDirectory + "\" + $fileName
New-Item -path $scriptDirectory -name $fileName -type "file" | Out-Null
try
{
"/* Migration Script ${fileName} */" | Out-File -Encoding $Encoding -FilePath $filePath
}
catch
{
"/* Migration Script ${fileName} */" | Out-File -Encoding ascii -FilePath $filePath
}
if($isDynamic -eq $False){
$targetProjectItem.ProjectItems.AddFromFile($filePath) | Out-Null
$item = $targetProjectItem.ProjectItems.Item($fileName)
$item.Properties.Item("BuildAction").Value = [int]3 #Embedded Resource
}
Write-Host "Created new migration: ${filePath}"
$dte.ExecuteCommand("File.OpenFile", $filePath)
}
Export-ModuleMember -Function New-Migration,New-Migration-Dynamic,Start-Migrations