Skip to content

Export-DbaInstance - Script us_english custom errors before their translations - #10552

Merged
potatoqualitee merged 1 commit into
developmentfrom
export-dbainstance-custom-error-language-order
Aug 13, 2026
Merged

Export-DbaInstance - Script us_english custom errors before their translations#10552
potatoqualitee merged 1 commit into
developmentfrom
export-dbainstance-custom-error-language-order

Conversation

@andreasjordan

Copy link
Copy Markdown
Collaborator

Fixes #10550. Reported and correctly diagnosed by @tivivi63.

Problem

Export-DbaInstance writes customererrors.sql with the localized version of a message before its us_english original, so the generated script cannot be replayed:

You must add the us_english version of this message before you can add the 'Francais' version.

Mechanism

sp_addmessage only accepts a localized message once the us_english version of that message id exists. Get-DbaCustomError enumerates $server.UserDefinedMessages, which SMO returns sorted by message id and then by language name, so Francais comes out before us_english. Export-DbaInstance piped that straight into Export-DbaScript, preserving the wrong order.

Reproduced on the current development source against SQL Server 2019 (15.0.4430.1); the ordering comes from the SMO collection, not from the server version.

What changed

public/Export-DbaInstance.ps1 orders the messages before scripting them, the same way Copy-DbaCustomError already does:

$allCustomErrors = @(Get-DbaCustomError -SqlInstance $server -EnableException:$EnableException)
$orderedCustomErrors = @($allCustomErrors | Where-Object Language -eq "us_english")
$orderedCustomErrors += $allCustomErrors | Where-Object Language -ne "us_english"

What deliberately did not change

I swept every command that touches user defined messages:

Command Verdict
Copy-DbaCustomError Already puts us_english first, unchanged
Remove-DbaCustomError Already handles the mirror rule on drop, unchanged
Start-DbaMigration, Sync-DbaAvailabilityGroup Both go through Copy-DbaCustomError, unchanged
Get-DbaCustomError Still returns the plain SMO order. It is the only caller-visible ordering in the module and no other Get-Dba* command imposes one, so the fix belongs in the export

Tests

tests/Export-DbaInstance.Tests.ps1 now creates a French translation for one of the custom errors it already sets up, and the new test exports, drops every custom error on the instance and replays the exported script.

The AfterAll block drops whatever custom errors are still present instead of two fixed ids, so a failed replay does not leave the instance dirty.

Run against a SQL Server 2019 instance:

  • with the fix: 29 passed, 0 failed, 1 skipped (the policies test skips on pwsh)
  • with the fix reverted: the new test fails with exactly the error from the issue

🤖 Generated with Claude Code

…nslations

sp_addmessage refuses a localized message whose us_english version does not
exist yet, and SMO returns UserDefinedMessages sorted by language name. So a
French translation was scripted before its us_english original and the
generated customererrors.sql could not be replayed. The messages are now
ordered the same way Copy-DbaCustomError already orders them.

Copy-DbaCustomError and Remove-DbaCustomError already handle this rule, and
Start-DbaMigration and Sync-DbaAvailabilityGroup both go through
Copy-DbaCustomError, so they are unchanged. Get-DbaCustomError keeps returning
the plain SMO order.

The integration test now creates a second language for one of its custom
errors, drops all custom errors of the instance and replays the exported
script, which fails on the old code with the error from the issue.

Fixes #10550

(do Export-DbaInstance)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@potatoqualitee

Copy link
Copy Markdown
Member

awesome change, thank you 🙏🏼

@potatoqualitee
potatoqualitee merged commit 60832de into development Aug 13, 2026
21 checks passed
@potatoqualitee
potatoqualitee deleted the export-dbainstance-custom-error-language-order branch August 13, 2026 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export-DbaInstance - Custom messages export order can generate error in script

2 participants