microsoft / azuredatastudio

Azure Data Studio is a data management and development tool with connectivity to popular cloud and on-premises databases. Azure Data Studio supports Windows, macOS, and Linux, with immediate capability to connect to Azure SQL and SQL Server. Browse the extension library for more database support options including MySQL, PostgreSQL, and MongoDB.
https://learn.microsoft.com/sql/azure-data-studio
MIT License
7.53k stars 893 forks source link

Schema Compare issues when there are multiple objects with same name under a bucket #25337

Open zijchen opened 6 months ago

zijchen commented 6 months ago

Version: 1.48.0-insider (user setup) Commit: 8a715ed3b1e87153e46651b920367170564eb376 Date: 2024-02-15T12:58:00.069Z VS Code: 1.82.0 Electron: 25.9.8 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Windows_NT x64 10.0.22631

Scenario 1

  1. Create a database with this:
    
    CREATE SCHEMA [Apps]
    GO

CREATE TABLE [Apps].[Apps] ([Col1] INT NULL) GO

2. Create a new blank database project
3. Update project from database
4. Select "Flat" for folder structure"
![image](https://github.com/microsoft/azuredatastudio/assets/13544267/5f599db7-f90b-4c5f-8f20-891e7eb186fe)
5. In the Schema Compare window that opens, both the schema and table show up correctly. But if you click "Apply", on the schema is added to the sqlproj as Apps.sql
![image](https://github.com/microsoft/azuredatastudio/assets/13544267/540f6d8d-cb38-46cb-b6de-dbe1f6fd7f92)
6. If we run schema compare again, the table now shows as a difference because it wasn't added the first time
7. If you hit "Apply" again, the Apps.sql with the schema is **overwritten** with the table script and build fails because now it can't find the schema. `SQL71501: SqlTable: [Apps].[Apps] has an unresolved reference to SqlSchema [Apps].`

![image](https://github.com/microsoft/azuredatastudio/assets/13544267/1997beab-c800-4166-9d7e-68e36798c3d0)

## Scenario 2
1. Create a database with this:
```tsql
CREATE LOGIN [Apps] WITH PASSWORD = '***'
GO

CREATE USER [Apps] FOR LOGIN [Apps] WITH DEFAULT_SCHEMA = [Apps]
GO

CREATE SCHEMA [Apps] AUTHORIZATION [Apps]
GO
  1. Create a new database project, add only the login to it:
    CREATE LOGIN [Apps] WITH PASSWORD = '***'
  2. Update project from database. This time it doesn't matter which folder structure you choose, both the user and the schema ends up in the same folder. If you apply changes, only the schema is created, and you get a build error SQL71501: SqlSchema: [Apps] has an unresolved reference to object [Apps]. image
  3. If you schema compare again, the User shows up as a change because it wasn't added the first time. Applying this change results in the schema Apps being overwritten by the user, and we get a new build error SQL71502: SqlUser: [Apps] has an unresolved reference to SqlSchema [Apps]. image
zijchen commented 6 months ago

If I create a project directly from database, or extract using sqlpackage, the objects are created properly. Distinct files are created for each object image