Sources/Data/Export-MySqlDump.ps1
|
using namespace System.Collections.Generic using namespace System.Web using module ./MySqlSchema.psm1 <# .SYNOPSIS Exports the specified schema to a SQL dump in the specified directory. #> function Export-MySqlDump { [CmdletBinding()] [OutputType([void])] param ( # The database schema. [Parameter(Mandatory, Position = 1)] [MySqlSchema] $Schema, # The path to the output directory. [Parameter(Mandatory, Position = 2)] [ValidateScript({ Test-Path $_ -IsValid }, ErrorMessage = "The specified output path is invalid.")] [string] $Path, # The connection URI. [Parameter(Mandatory)] [uri] $Uri, # The table name. [string[]] $Table = @() ) $file = "$($Table.Count -eq 1 ? "$($Schema.Name).$($Table[0])" : $Schema.Name).sql" $userName, $password = ($Uri.UserInfo -split ":").ForEach{ [uri]::UnescapeDataString($_) } $arguments = [List[string]] @( "--default-character-set=$([HttpUtility]::ParseQueryString($Uri.Query)["charset"] ?? "utf8mb4")" "--host=$($Uri.Host)" "--password=$password" "--port=$($Uri.IsDefaultPort ? 3306 : $Uri.Port)" "--result-file=$(Join-Path $Path $file)" "--user=$userName" ) if ($Uri.Host -notin "::1", "127.0.0.1", "localhost") { $arguments.Add("--compress") } $arguments.Add($Schema.Name) $arguments.AddRange($Table) mysqldump @arguments } |