Azure Data Studio - Database Project - schema compare changes failed - dacpac

I can successfully build my DB project in the Azure data studio with the SQL DB Projects extension. (not with SSDT but similar to this: https://www.sqlshack.com/two-ways-to-build-sql-database-projects-in-azure-data-studio/
When I right-click on the DB project and select "Update Project from database", it lists the delta between the online Azure SQL DB and the local db project.
The problem now is that when I either click on "Generate script" or on "apply", I receive an error.
Generate script: Performing script generation is not possible for this comparison result.
Performing script generation is not possible for this comparison result.
Apply schema compare changes: Object reference not set to an instance of an object.
Object reference not set to an instance of an object.
I already reinstalled the Azure Data Studio but with no success.
Any hint what I could do to fix this?

In our case these errors occur because it is added a Role Member to the Standard MSSQL Database Roles called db_datareader and db_datawriter. These roles (or objects as ADS calls them) do not exist in the local dacpac or ADS Database Project. It seems to map even if the roles are checked for update or not, so the Schema compare can't find the objects locally.
If the same applies to your project I will suggest checking if that user really needs to be a part of those two roles as they are not being mapped locally.
If you need the member in the role, a workaround is to exclude the Database Roles objects in the Schema Compare:
In Schema Compare, press Options.
In modal on the right, press the Include Object Types-tab
Find Database Roles and untick.
Press OK and re-compare.
I've set up an issue to the ADS-team, so hopefully the Schema Compare- and Database Project-responsibles can cook up a good solution on this problem.

Related

SSDT - specify authorization user on schema create

I have a sqlproj XML for Azure SQL Database. I want to deploy database with several tables into specific schema which is also created using this build. How to specify what user should be used for CREATE SCHEMA [schema-name] AUTHORIZATION [user] ? By default it uses user dbo but I need to change it. How to specify it in the sqlproj?
Sounds like you are looking in a wrong place. All database schema definitions in SSDT, by default, are placed into the Security folder. In it, you will have your schema-name.sql file, and there you can adjust the authorization clause.
For the project to build, the owner of that schema should also be present in the project. Most likely, you will find it in the same folder.
It appeared the user (service principal) trying to deploy the solution to SQL Database was not set as an AAD admin on the SQL server. It seems a little bit strange to me, but it worked.

DACPAC SQL Server dropping schema permissions

I am trying to use a DACPAC database project in Azure Data Studio.
So far, it works fine except for the "publishing" of the project to the actual DB.
When I generate the delta script, I see that all permissions on all schemas are being dropped.
I know that I could exclude certain objects:
SQL Server DACPAC Deployment Dropping Users/Roles/Permissions
However, I would prefer to have also the permissions of DB roles on a schema in the DB project.
Here, of course, the order is important. (Create schema, create user, create DB role, add user to DB role, grant permission for DB role on schema)
How (and to which file) can I add the schema permissions to the project and how can it be ensured that the TSQL statements that are generated are executed in the correct oder?
Thx.
At the linked QA have a look at the XML: there are separate parameters
DropPermissionsNotInSource
DropRoleMembersNotInSource
represented in sqlproj/publish.xml in a reversed way
<DoNotDropRoleMembership>True</DoNotDropRoleMembership>
<DoNotDropPermissions>True</DoNotDropPermissions>
By switching them to "do not drop" state you can avoid excluding permissions from deployment. Thus new permissions (defined in project but missing on target server) would be created but old ones (existing on target server but missing in the project) will not be dropped. Same goes for role membership.
Valid command order in publish script is guaranteed by the SSDT engine.
Still, permissions on target server can be lost if publishing requires object recreation. For example if you alter table-type then referencing procs will be dropped and recreated after type recreation.

Who created a database in my SQL Server instance?

I'm trying to determine who created a database in my SQL Server instance. The .trc logs seem to have been purged and I can't locate a backup of them. I know when the database was created and have found the .bak file that was used to create the database, but I can't determine WHO created it.
Any other ideas how I can figure this out? (SSMS schema history report also doesn't go back far enough)
Based on the following article:
There is no dbo concept for server scope securables. They are always owned by the login that created them, no matter of any server roles that the login might be a member of.
So by default, the database owner is the one who created the database, but you have to make sure that no one changed this property:
To check the database owner, in SQL Server management studio, Right click on the database and in the Properties window >> General Tab >> check the owner property:

SQL Server - data is not written in to the correct schema

In my database I have two schema - default .dbo and another called .ptx
Almost all tables in the two schema are identical. We need two schema to handle data separately for two different products. How I switch between these two schema is by changing the SQL server login user from application level. i.e., we have two SQL users ANF_DEFAULT and ANF_PTX. If the product is default the user is ANF_DEFAULT and if the product is PTX, I switch the user to ANF_PTX and create the SqlConnection accordingly.
Everything has been working good so far, the two schema were populated correctly. But suddenly after we move in to a new production environment, the PTX product data goes in to the .dbo schema instead of the .ptx schema. This is still working fine in my local environment but the issue is visible in production. The only visible change is that I use SQL Server 2008, where as the production runs SQL Server 2012, but I think this is hardly the issue. I also checked the properties of the ANF_PTX user:
USE [ANX_GLF]
GO
/****** Object: User [ANF_PTX] Script Date: 8/27/2014 5:29:04 AM ******/
CREATE USER [ANF_PTX] FOR LOGIN [ANF_PTX] WITH DEFAULT_SCHEMA=[PTX]
GO
Please help me figure out the issue here.
I figured out the reason for this issue. Apparently, in the new deployment, the _CMS user looked fine, with its default schema being pointed to .cms. However, the problem was with the login FMSI_APP_CMS in the SQL Server. In this login, the FMSI_APP_CMS user was assigned to the super admin role. When the user is a super admin, its default schema is ignored and it always points to the .dbo schema. When the superadmin role was removed from the login, the schema switch was working fine again. More information about this can be found in the following:
http://msdn.microsoft.com/en-us/library/ms176060.aspx
Here it says:
"he value of DEFAULT_SCHEMA is ignored if the user is a member of the sysadmin fixed server role. All members of the sysadmin fixed server role have a default schema of dbo."

Syncing permissions between a Database project and a database using Visual studio

I have a database project in visual studio (2008) that I am using to keep my database structure in a version control system (Git). I use the data schema compare tool in VS to maintain this database project. The issue that I'm encountering is that I keep losing my permissions for new objects.
So how do I:
Capture these permissions using the schema compare tool.
Apply these permissions to a server using the schema compare tool.
The easiest method to capture and apply the permissions that I have found is to go into tools -> options -> database tools -> schema compare and uncheck the ignore permissions checkbox. Then run the schema comparision and apply updates.
Note: That this will update every object with permissions in your schema. I have been capturing the permission after I sync my schema changes and then resetting the ignore permissions checkbox

Resources