Skip to content

Commit dda4ec4

Browse files
Migrate DataMigration from generation to main (#24460)
* Move DataMigration to main * Update ChangeLog.md --------- Co-authored-by: Vincent Dai <[email protected]>
1 parent eac3393 commit dda4ec4

File tree

54 files changed

+1102
-169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1102
-169
lines changed

src/DataMigration/DataMigration.Autorest/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
This directory contains the PowerShell module for the DataMigration service.
44

55
---
6-
76
## Info
87
- Modifiable: yes
98
- Generated: all

src/DataMigration/DataMigration.Autorest/custom/Cmdlets/New-AzDataMigrationLoginsMigration.ps1

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -78,77 +78,66 @@ function New-AzDataMigrationLoginsMigration
7878
#Defining Default Output Path
7979
$DefaultOutputFolder = Get-DefaultLoginMigrationsOutputFolder
8080

81-
#Defining Base and Exe paths
82-
$BaseFolder = Join-Path -Path $DefaultOutputFolder -ChildPath Downloads;
83-
$ExePath = Join-Path -Path $BaseFolder -ChildPath Logins.Console.csproj\Logins.Console.exe;
81+
#Defining Downloads folder
82+
$DownloadsFolder = Join-Path -Path $DefaultOutputFolder -ChildPath Downloads;
8483

85-
#Checking if BaseFolder Path is valid or not
86-
if(-Not (Test-Path $BaseFolder))
84+
#Checking if DownloadsFolder Path is valid or not
85+
if(-Not (Test-Path $DownloadsFolder))
8786
{
88-
$null = New-Item -Path $BaseFolder -ItemType "directory"
87+
$null = New-Item -Path $DownloadsFolder -ItemType "directory"
88+
}
89+
else
90+
{
91+
#Delete old Login console app files
92+
Delete-OldLoginConsoleApp $DownloadsFolder;
8993
}
9094

91-
#Testing Whether Console App is downloaded or not
92-
$TestExePath = Test-Path -Path $ExePath;
93-
94-
#Console app download address
95-
$ZipSource = "https://sqlassess.blob.core.windows.net/app/LoginsMigration.zip";
96-
$ZipDestination = Join-Path -Path $BaseFolder -ChildPath "LoginsMigration.zip";
95+
#Determine latest version of Login console app
96+
$PackageId = "Microsoft.SqlServer.Migration.LoginsConsoleApp"
97+
$LatestNugetOrgDetails = Get-LatestConsoleAppVersionFromNugetOrg $PackageId
9798

98-
#Downloading and extracting LoginsMigration Zip file
99-
if(-Not $TestExePath)
99+
#Determine local version of Login console app
100+
$ConsoleAppFolders = Get-ChildItem -Path $DownloadsFolder -Filter "$PackageId.*"
101+
$LatestLocalNameAndVersion = ""
102+
if ($ConsoleAppFolders.Length -gt 0)
100103
{
101-
#Downloading and extracting LoginMigration Zip file
102-
Write-Host "Downloading and extracting latest LoginMigration Zip file..."
103-
Invoke-RestMethod -Uri $ZipSource -OutFile $ZipDestination;
104-
Expand-Archive -Path $ZipDestination -DestinationPath $BaseFolder -Force;
104+
$ConsoleAppFolders = $ConsoleAppFolders | Sort-Object -Property Name -Descending
105+
$LatestLocalNameAndVersion = $ConsoleAppFolders[0].Name
106+
Write-Host "Installed Login migration console app nupkg version: $LatestLocalNameAndVersion"
107+
108+
if ($AvailablePackagesOnNugetOrg -eq "")
109+
{
110+
$LatestNugetOrgDetails.NameAndVersion = $LatestLocalNameAndVersion
111+
}
105112
}
106113
else
107114
{
108-
# Get local exe version
109-
Write-Host "Checking installed Login.Console.exe version...";
110-
$installedVersion = (Get-Item $ExePath).VersionInfo.FileVersion;
111-
Write-Host "Installed version: $installedVersion";
112-
113-
# Get latest console app version
114-
Write-Host "Checking whether there is newer version...";
115-
$VersionFileSource = "https://sqlassess.blob.core.windows.net/app/loginconsoleappversion.json";
116-
$VersionFileDestination = Join-Path -Path $BaseFolder -ChildPath "loginconsoleappversion.json";
117-
Invoke-RestMethod -Uri $VersionFileSource -OutFile $VersionFileDestination;
118-
$jsonObj = Get-Content $VersionFileDestination | Out-String | ConvertFrom-Json;
119-
$latestVersion = $jsonObj.version;
120-
121-
# Compare the latest exe version with the local exe version
122-
if([System.Version]$installedVersion -lt [System.Version]$latestVersion)
115+
#No local console app
116+
if ($AvailablePackagesOnNugetOrg -eq "")
123117
{
124-
Write-Host "Found newer version of Logins.Console.exe '$latestVersion'";
125-
126-
Write-Host "Removing old Logins.Console.exe..."
127-
# Remove old zip file
128-
Remove-Item -Path $ZipDestination;
118+
#No version available to download
119+
Write-Host "Connection to NuGet.org required. Please check connection and try again."
120+
return;
121+
}
122+
}
129123

130-
# Remove existing folder and contents
131-
$ConsoleAppDestination = Join-Path -Path $BaseFolder -ChildPath "Logins.Console.csproj";
132-
Remove-Item -Path $ConsoleAppDestination -Recurse;
124+
Write-Host "Latest Login migration console app nupkg version on Nuget.org: $($LatestNugetOrgDetails.NameAndVersion)";
125+
$LatestNugetFolder = Join-Path -Path $DownloadsFolder -ChildPath $LatestNugetOrgDetails.NameAndVersion;
126+
$ExePath = "tools\Microsoft.SqlServer.Migration.Logins.ConsoleApp.exe";
133127

134-
# Remove version file
135-
Remove-Item -Path $VersionFileDestination;
128+
# Check for the latest console app version and download it if needed.
129+
CheckAndDownloadConsoleAppFromNugetOrg $LatestLocalNameAndVersion $LatestNugetOrgDetails $ExePath ([ref]$LatestNugetFolder)
136130

137-
#Downloading and extracting LoginMigration Zip file
138-
Write-Host "Downloading and extracting latest LoginMigration Zip file..."
139-
Invoke-RestMethod -Uri $ZipSource -OutFile $ZipDestination;
140-
Expand-Archive -Path $ZipDestination -DestinationPath $BaseFolder -Force;
141-
}
142-
else
143-
{
144-
Write-Host "Installed Logins.Console.exe is the latest one...";
145-
}
131+
if(-Not (Test-Path -Path "$LatestNugetFolder\$ExePath"))
132+
{
133+
Write-Host "Failed to locate executable."
134+
return
146135
}
147136

148137
#Collecting data
149138
if(('CommandLine') -contains $PSCmdlet.ParameterSetName)
150139
{
151-
# The array list $splat contains all the parameters that will be passed to '.\Logins.Console.exe LoginsMigration'
140+
# The array list $splat contains all the parameters that will be passed to '.\Microsoft.SqlServer.Migration.Logins.ConsoleApp.exe LoginsMigration'
152141

153142
$LoginsListArray = $($ListOfLogin -split " ")
154143
[System.Collections.ArrayList] $splat = @(
@@ -173,7 +162,10 @@ function New-AzDataMigrationLoginsMigration
173162

174163
}
175164
}
176-
# Running LoginsMigration
165+
166+
$ExePath = Join-Path -Path $LatestNugetFolder -ChildPath $ExePath;
167+
# Running LoginsMigration
168+
Write-Host "Starting Execution..."
177169
& $ExePath LoginsMigration @splat
178170
}
179171
else

src/DataMigration/DataMigration.Autorest/custom/Helpers/LoginMigrations.ps1

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,175 @@ function Get-DefaultLoginMigrationsOutputFolder {
5656
return $DefualtPath
5757

5858
}
59-
6059
}
60+
61+
function Delete-OldLoginConsoleApp {
62+
[Microsoft.Azure.PowerShell.Cmdlets.DataMigration.DoNotExportAttribute()]
63+
param(
64+
[Parameter(Mandatory=$true, Position=0)]
65+
[System.String]
66+
$DownloadsFolder
67+
)
68+
69+
process {
70+
#Remove old Login console app files
71+
#Old console app download address
72+
$ZipDestination = Join-Path -Path $DownloadsFolder -ChildPath "LoginsMigration.zip";
73+
74+
# Remove old zip file
75+
if(Test-Path $ZipDestination)
76+
{
77+
Remove-Item -Path $ZipDestination;
78+
}
79+
80+
# Remove existing folder and contents
81+
$ConsoleAppDestination = Join-Path -Path $DownloadsFolder -ChildPath "Logins.Console.csproj";
82+
if(Test-Path $ConsoleAppDestination)
83+
{
84+
Remove-Item -Path $ConsoleAppDestination -Recurse;
85+
}
86+
87+
# Remove version file
88+
$VersionFileDestination = Join-Path -Path $DownloadsFolder -ChildPath "loginconsoleappversion.json";
89+
if(Test-Path $VersionFileDestination)
90+
{
91+
Remove-Item -Path $VersionFileDestination;
92+
}
93+
}
94+
}
95+
96+
97+
function Get-LatestConsoleAppVersionFromNugetOrg {
98+
[Microsoft.Azure.PowerShell.Cmdlets.DataMigration.DoNotExportAttribute()]
99+
param(
100+
[Parameter(Mandatory=$true, Position=0)]
101+
[System.String]
102+
$PackageId
103+
)
104+
105+
process {
106+
$AvailablePackagesOnNugetOrg = ""
107+
108+
try {
109+
$AvailablePackagesOnNugetOrg = Find-Package -Source "https://api.nuget.org/v3/index.json" -Name $PackageId -AllowPrereleaseVersions -AllVersions
110+
$AvailablePackagesOnNugetOrg = $AvailablePackagesOnNugetOrg | Sort-Object -Property Version -Descending
111+
} catch {
112+
Write-Host "Unable to connect to NuGet.org to check for updates."
113+
}
114+
115+
$LatestNugetOrgName = $AvailablePackagesOnNugetOrg[0].Name
116+
$LatestNugetOrgVersion = $AvailablePackagesOnNugetOrg[0].Version
117+
$LatestNugetOrgNameAndVersion = "$LatestNugetOrgName.$LatestNugetOrgVersion";
118+
119+
return @{Name=$LatestNugetOrgName; Version=$LatestNugetOrgVersion; NameAndVersion=$LatestNugetOrgNameAndVersion}
120+
}
121+
}
122+
123+
function CheckAndDownloadConsoleAppFromNugetOrg {
124+
[Microsoft.Azure.PowerShell.Cmdlets.DataMigration.DoNotExportAttribute()]
125+
param(
126+
[Parameter(Mandatory=$true, Position=0)]
127+
[AllowEmptyString()]
128+
[System.String]
129+
$LatestLocalNameAndVersion,
130+
131+
[Parameter(Mandatory=$true, Position=1)]
132+
[HashTable]
133+
$LatestNugetOrgDetails,
134+
135+
[Parameter(Mandatory=$true, Position=2)]
136+
[System.String]
137+
$ExePath,
138+
139+
[Parameter(Mandatory=$true, Position=3)]
140+
[ref]
141+
[System.String]
142+
$LatestNugetFolder
143+
)
144+
145+
process {
146+
#User consent for Login migration console app update. By default it is set to yes.
147+
$userUpdateConsent = "yes";
148+
149+
# Prompt for user consent on Login migration console app update
150+
if($LatestLocalNameAndVersion -ne "" -and $LatestNugetOrgDetails.NameAndVersion -gt $LatestLocalNameAndVersion)
151+
{
152+
Write-Host "Newer Login migration console app nupkg version is available in Nuget.org...";
153+
while($true) {
154+
$userUpdateConsent = Read-Host -Prompt "Do you want to upgrade to the latest version? (yes/no)"
155+
156+
if ($userUpdateConsent -eq "yes")
157+
{
158+
Write-Host "You chose to upgrade. Proceeding..."
159+
break;
160+
}
161+
elseif ($userUpdateConsent -eq "no")
162+
{
163+
Write-Host "You chose not to upgrade."
164+
$LatestNugetFolder.Value = Join-Path -Path $DownloadsFolder -ChildPath $LatestLocalNameAndVersion;
165+
break;
166+
}
167+
else
168+
{
169+
Write-Host "Invalid input. Please enter 'yes' or 'no'."
170+
}
171+
}
172+
}
173+
174+
if ($LatestNugetOrgDetails.NameAndVersion -gt $LatestLocalNameAndVersion -and $userUpdateConsent -eq "yes")
175+
{
176+
#Update is available
177+
$DownloadUrl = "https://www.nuget.org/api/v2/package/$PackageId/$($LatestNugetOrgDetails.Version)"
178+
179+
#Checking if LatestNugetFolder Path is valid or not
180+
if(-Not (Test-Path $LatestNugetFolder.Value))
181+
{
182+
$null = New-Item -Path $LatestNugetFolder.Value -ItemType "directory";
183+
}
184+
185+
Write-Host "Downloading the latest Login migration console app nupkg: $($LatestNugetOrgDetails.NameAndVersion) ..."
186+
Invoke-WebRequest $DownloadUrl -OutFile "$($LatestNugetFolder.Value)\\$($LatestNugetOrgDetails.NameAndVersion).nupkg"
187+
188+
$ToolsPathExists = Test-Path -Path (Join-Path -Path $LatestNugetFolder.Value -ChildPath "tools");
189+
190+
if ($ToolsPathExists -eq $False)
191+
{
192+
$Nugets = Get-ChildItem -Path $LatestNugetFolder.Value -Filter "$PackageId.*.nupkg";
193+
194+
if ($Nugets.Length -gt 0)
195+
{
196+
Write-Host "Extracting the latest Login migration console app nupkg: $($LatestNugetOrgDetails.NameAndVersion) ..."
197+
$Nugets = $Nugets | Sort-Object -Property Name -Descending;
198+
$LatestNugetPath = $Nugets[0].FullName;
199+
Expand-Archive -Path $LatestNugetPath -DestinationPath $LatestNugetFolder.Value;
200+
}
201+
}
202+
203+
#Check if update was successful
204+
$TestPathResult = Test-Path -Path "$($LatestNugetFolder.Value)\$ExePath"
205+
206+
$NugetVersions = Get-ChildItem -Path $DownloadsFolder -Filter "$PackageId.*";
207+
$NugetVersions = $NugetVersions | Sort-Object -Property Name -Descending
208+
209+
if($TestPathResult)
210+
{
211+
Write-Host "Removing all older Login migration console apps..."
212+
#Remove all other NuGet versions except for the version just downloaded
213+
for ($NugetIndex = 0; $NugetIndex -lt $NugetVersions.Length; $NugetIndex = $NugetIndex + 1)
214+
{
215+
if($NugetVersions[$NugetIndex].Name -ne $LatestNugetOrgDetails.NameAndVersion)
216+
{
217+
Remove-Item -Path $NugetVersions[$NugetIndex].FullName -Recurse -Force
218+
}
219+
}
220+
}
221+
else
222+
{
223+
if($NugetVersions.Length -gt 0)
224+
{
225+
$LatestNugetFolder.Value = $NugetVersions[0].Name;
226+
}
227+
}
228+
}
229+
}
230+
}

src/DataMigration/DataMigration/Az.DataMigration.psd1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Microsoft Corporation
55
#
6-
# Generated on: 1/30/2024
6+
# Generated on: 3/23/2024
77
#
88

99
@{
@@ -53,17 +53,17 @@ DotNetFrameworkVersion = '4.7.2'
5353
# ProcessorArchitecture = ''
5454

5555
# Modules that must be imported into the global environment prior to importing this module
56-
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.15.1'; })
56+
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.16.0'; })
5757

5858
# Assemblies that must be loaded prior to importing this module
5959
RequiredAssemblies = 'DataMigration.Autorest/bin/Az.DataMigration.private.dll',
6060
'Microsoft.Azure.Management.DataMigration.dll'
6161

6262
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
63-
# ScriptsToProcess = @()
63+
ScriptsToProcess = @()
6464

6565
# Type files (.ps1xml) to be loaded when importing this module
66-
# TypesToProcess = @()
66+
TypesToProcess = @()
6767

6868
# Format files (.ps1xml) to be loaded when importing this module
6969
FormatsToProcess = 'DataMigration.Autorest/Az.DataMigration.format.ps1xml'
@@ -147,7 +147,8 @@ PrivateData = @{
147147
PSData = @{
148148

149149
# Tags applied to this module. These help with module discovery in online galleries.
150-
Tags = 'Azure','ResourceManager','ARM','Sql','Database','Data','Migration','Service'
150+
Tags = 'Azure', 'ResourceManager', 'ARM', 'Sql', 'Database', 'Data', 'Migration',
151+
'Service'
151152

152153
# A URL to the license for this module.
153154
LicenseUri = 'https://aka.ms/azps-license'
@@ -172,7 +173,7 @@ PrivateData = @{
172173

173174
} # End of PSData hashtable
174175

175-
} # End of PrivateData hashtable
176+
} # End of PrivateData hashtable
176177

177178
# HelpInfo URI of this module
178179
# HelpInfoURI = ''

src/DataMigration/DataMigration/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Changed the Login Migration Console App source to NuGet.org and added versioning support for updating the console app.
2122

2223
## Version 0.14.4
2324
* Added versioning to login migration console app.

0 commit comments

Comments
 (0)