How can I stop SQL CLR projects from generating DROP DATABASE in some deployment scripts?
I have been using SQL Server SQL CLR for 10+ years for scalar functions, aggregates and User-Defined Types. Early on, the T-SQL scripts generated by a build did what was expected; they deployed the assemblies and objects being built.
But at some point, some of the generated T-SQL scripts began generating DROP DATABASE commands! (I'll try not to repeat too many times how lunatic an idea this is).
Today I found a 10-year-old proposed answer on stackoverflow that didn't actually solve the poster's problem, so I'm asking again.
I'm using Visual Studio 2022, deploying to SQL Server 2016. There is one file of concern in particular, with a name like 'projectname_Create.sql'. Here's the offending section:
IF (DB_ID(N'$(DatabaseName)') IS NOT NULL)
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [$(DatabaseName)];
END
GO
PRINT N'Creating database $(DatabaseName)...'
GO
CREATE DATABASE [$(DatabaseName)] COLLATE SQL_Latin1_General_CP1_CI_AS
GO
Another file, with the name "projectname.sql", just goes about the business of altering the assembly:
USE [$(DatabaseName)];
GO
PRINT N'Altering Assembly [ConcatenateVS2022SS2016]...';
GO
ALTER ASSEMBLY [ConcatenateVS2022SS2016]
DROP FILE ALL;
GO
ALTER ASSEMBLY [ConcatenateVS2022SS2016]
FROM
0x<<<binary for assembly>>>
It would be great to read any documentation on when these two files are actually used; it could be that the project_create file is never executed by Visual Studio, but "I live in fear" that someday Visual Studio will drop a database when I deploy or build.
Createscript but it's not selected by default. It's intended, so far as I'm aware, to be used instead of a dacpac to deploy the whole database in pristine form, not to support any incremental deployment features.USE [$(DatabaseName)]is a normal script for dacpac deployments so something to take up with Microsoft not the questioner heremaster]; SELECT 'SQL Injection possibility'--and the script generated did escape the closing square bracket:setvar DatabaseName "master]]; SELECT 'SQL Injection possibility'--"(so defeating my SQL injection attempt)