Incorrect schema change to be ignore in the output script
- Dominant language
- C#
- Stars
- 460
- Forks
- 29
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 7
Description
- SqlPackage or DacFx Version: 170.3.93
- .NET Framework (Windows-only) or .NET Core: .NET Core 10
- Environment (local platform and source/target platforms):
**Steps to Reproduce:**
1. Having a script inside a dacpac as
> CREATE TABLE [dbo].[ANR] (
> [ID] INT IDENTITY (1, 1) NOT NULL,
> [CANumberID] INT NOT NULL,
> [PANumberID] INT NOT NULL,
> CONSTRAINT [PK_ANR] PRIMARY KEY ([ID] ASC),
> CONSTRAINT [UQ_ANR_CANumberID_PANumberID] UNIQUE NONCLUSTERED ([CANumberID] ASC, [PANumberID] ASC),
> CONSTRAINT [FK_ANR_AN_CANumberID] FOREIGN KEY ([CANumberID]) REFERENCES [dbo].[AN] ([Id]),
> CONSTRAINT [FK_ANR_AN_PANumberID] FOREIGN KEY ([PANumberID]) REFERENCES [dbo].[AN] ([Id])
> )
2. Trying to run the compare process as
> var source = new SchemaCompareDatabaseEndpoint(sourceConnectionString);
> var target = new SchemaCompareDatabaseEndpoint(destConnectionString);
> var comparison = new SchemaComparison(source, target);
> var result = comparison.Compare();
3. The output script generated is
> /*
> Deployment script for XXXXXXXXXX
> This code was generated by a tool.
> Changes to this file may cause incorrect behavior and will be lost if
> the code is regenerated.
> */
> GO
> SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
> SET NUMERIC_ROUNDABORT OFF;
> GO
> :setvar DatabaseName "XXXXXXXXXX"
> :setvar DefaultFilePrefix "XXXXXXXXXX"
> :setvar DefaultDataPath "d:\MSSQL11.SQLSERVER\MSSQL\DATA\"
> :setvar DefaultLogPath "d:\MSSQL11.SQLSERVER\MSSQL\DATA\"
> GO
> :on error exit
> GO
> /*
> Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
> To re-enable the script after enabling SQLCMD mode, execute the following:
> SET NOEXEC OFF;
> */
> :setvar __IsSqlCmdEnabled "True"
> GO
> IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
> BEGIN
> PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
> SET NOEXEC ON;
> END
> GO
> USE [$(DatabaseName)];
> GO
> PRINT N'Dropping Foreign Key [dbo].[FK_ANR_AN_CANumberID]...';
> GO
> ALTER TABLE [dbo].[ANR] DROP CONSTRAINT [FK_ANR_AN_CANumberID];
> GO
> PRINT N'Dropping Foreign Key [dbo].[FK_ANR_AN_PANumberID]...';
> GO
> ALTER TABLE [dbo].[ANR] DROP CONSTRAINT [FK_ANR_AN_PANumberID];
> GO
> PRINT N'Starting rebuilding table [dbo].[ANR]...';
> GO
> BEGIN TRANSACTION;
> SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
> SET XACT_ABORT ON;
> CREATE TABLE [dbo].[tmp_ms_xx_ANR] (
> [ID] INT IDENTITY (1, 1) NOT NULL,
> [CANumberID] INT NOT NULL,
> [PANumberID] INT NOT NULL,
> CONSTRAINT [tmp_ms_xx_constraint_UQ_ANR_CANumberID_PANumberID1] UNIQUE NONCLUSTERED ([CANumberID] ASC, [PANumberID] ASC),
> CONSTRAINT [tmp_ms_xx_constraint_PK_ANR1] PRIMARY KEY CLUSTERED ([ID] ASC)
> );
> IF EXISTS (SELECT TOP 1 1
> FROM [dbo].[ANR])
> BEGIN
> SET IDENTITY_INSERT [dbo].[tmp_ms_xx_ANR] ON;
> INSERT INTO [dbo].[tmp_ms_xx_ANR] ([ID], [CANumberID], [PANumberID])
> SELECT [ID],
> [CANumberID],
> [PANumberID]
> FROM [dbo].[ANR]
> ORDER BY [ID] ASC;
> SET IDENTITY_INSERT [dbo].[tmp_ms_xx_ANR] OFF;
> END
> DROP TABLE [dbo].[ANR];
> EXECUTE sp_rename N'[dbo].[tmp_ms_xx_ANRelation]', N'ANR';
> EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_UQ_ANR_CANumberID_PANumberID1]', N'UQ_ANR_CANumberID_PANumberID', N'OBJECT';
> EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_ANR1]', N'PK_ANR', N'OBJECT';
> COMMIT TRANSACTION;
> SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
> GO
> PRINT N'Creating Foreign Key [dbo].[FK_ANR_AN_CANumberID]...';
> GO
> ALTER TABLE [dbo].[ANR] WITH NOCHECK
> ADD CONSTRAINT [FK_ANR_AN_CANumberID] FOREIGN KEY ([CANumberID]) REFERENCES [dbo].[AN] ([Id]);
> GO
> PRINT N'Creating Foreign Key [dbo].[FK_ANR_AN_PANumberID]...';
> GO
> ALTER TABLE [dbo].[ANR] WITH NOCHECK
> ADD CONSTRAINT [FK_ANR_AN_PANumberID] FOREIGN KEY ([PANumberID]) REFERENCES [dbo].[AN] ([Id]);
> GO
> USE [$(DatabaseName)];
> GO
> ALTER TABLE [dbo].[ANR] WITH CHECK CHECK CONSTRAINT [FK_ANR_AN_CANumberID];
> ALTER TABLE [dbo].[ANR] WITH CHECK CHECK CONSTRAINT [FK_ANR_AN_PANumberID];
> GO
> PRINT N'Update complete.';
> GO
**Did this occur in prior versions? If not - which version(s) did it work in?**
Why the DacFx tool is finding a schema change for this table ?
(DacFx/SqlPackage/SSMS/Azure Data Studio)
Contributor guide
Research direction
Start with the SchemaComparison entry point and reproduce the comparison using the ANR table definition and generated output shown in the issue. Determine why an unchanged schema causes a table rebuild and verify that the output script no longer includes the unnecessary schema change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100