Goal
Clone a SQL database to a different remote SQL Server using a PowerShell script
What Works
Using SSMS to import the BACPAC file into different servers (remotely & local) works without (reported) warnings or errors.
What Doesn't
Importing the BACPAC into a remote SQL Server results in the following error using sqlpackage.exe & PowerShell dbatools:
Warning SQL72038: The object [XXX] already exists in database with a different definition and will not be altered.
Error SQL72014: .Net SqlClient Data Provider: Msg 15023, Level 16, State 1, Line 1 User, group, or role 'XXX' already exists in the current database.
Error SQL72045: Script execution error. The executed script:
CREATE USER [XXX] FOR LOGIN [XXX];
I also tried using PS dbatools DACPAC approach: https://dbatools.io/clone/
The error message with different settings changed to:
Initializing deployment (Start)
The object [XXX] already exists in database with a different definition and will not be altered.
Initializing deployment (Complete)
Analyzing deployment plan (Start)
Analyzing deployment plan (Complete)
Reporting and scripting deployment plan (Start)
Reporting and scripting deployment plan (Complete)
Updating database (Start)
Creating NEW_DATABASE...
The database settings cannot be modified. You must be a SysAdmin to apply these settings.
Creating [XXX]...
.Net SqlClient Data Provider: Msg 15023, Level 16, State 1, Line 1 User, group, or role 'XXX' already
exists in the current database.
Script execution error. The executed script:
CREATE USER [XXX] WITHOUT LOGIN;
An error occurred while the batch was being executed.
Updating database (Failed)
The next step was then to disable users and add back the required users & roles via a script. Using the following link as a reference, resulted in a database that I was unable to drop with our existing administrator login & password.
DacPac exclude users and logins on export or import
To fix this, we had to change our administrator password in RDS
AWS RDS SQL Server unable to drop database
Notes
- I can't remove user XXX because it's mapped to different databases
- SQL Server Management Studio v17.9.1
- PowerShell dbatools v1.0.30
Questions
- Is there a way to find out what SSMS is executing so that I can replicate it via a script?
- What are the options to work around this issue?