英文:
How can I stop SQL CLR projects from generating DROP DATABASE in their deployment scripts?
问题
如何阻止 SQL CLR 项目在某些部署脚本中生成 DROP DATABASE 命令?
我已经使用 SQL Server SQL CLR 进行标量函数、聚合和用户定义类型等工作已经超过10年。在早期,生成的构建脚本执行了预期的操作,即部署了正在构建的程序集和对象。
但是在某个时刻,一些生成的 T-SQL 脚本开始生成 DROP DATABASE 命令!(我会尽量不重复提到这是多么疯狂的想法)。
今天我在 stackoverflow 上找到一个提出的解答,但实际上没有解决提问者的问题,因此我再次提问。
我正在使用 Visual Studio 2022,部署到 SQL Server 2016。有一个特定的文件引起了我的关注,文件名类似于 'projectname_Create.sql'。以下是有问题的部分:
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
另一个文件,文件名为 "projectname.sql",只是修改了程序集的相关内容:
USE [$(DatabaseName)];
GO
PRINT N'Altering Assembly [ConcatenateVS2022SS2016]...';
GO
ALTER ASSEMBLY [ConcatenateVS2022SS2016]
DROP FILE ALL;
GO
ALTER ASSEMBLY [ConcatenateVS2022SS2016]
FROM
0x<<<二进制数据用于程序集>>>;
很希望能阅读有关这两个文件实际何时使用的任何文档;有可能项目创建文件从未被 Visual Studio 执行,但是“我一直担心”有一天 Visual Studio 会在我部署或构建时删除数据库。
英文:
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.
答案1
得分: 1
以下是讨论这些选项的文档:
https://learn.microsoft.com/en-us/sql/ssdt/database-project-settings
“Create” 脚本是可选的,并且生成它由以下选项控制:
项目设置 选项卡
创建脚本(.sql 文件)
指定是否为项目中的所有对象生成完整的 .sql CREATE 脚本,并在构建项目时将其放置在 bin\debug 文件夹中。您可以使用 项目发布 命令或 SQL 比较工具创建增量更新脚本。
非“Create” 脚本是由 SSDT 用于发布/部署任何更改的增量脚本。可以通过以下选项强制增量脚本重新创建数据库(默认情况下禁用):
调试 选项卡
始终重新创建数据库
指定是否删除并重新创建数据库,而不是执行增量升级。例如,如果您想针对干净部署的数据库运行数据库单元测试,可能需要选择此复选框。如果取消选中此复选框,则将更新现有数据库,而不是删除并重新创建。
我不确定 SSDT 何时开始生成这两个脚本,但我相信它开始于 Visual Studio 2015 之前。因此,它已经存在一段时间了,但不应引起担忧。
英文:
Here is the documentation that discusses these options:
https://learn.microsoft.com/en-us/sql/ssdt/database-project-settings
The "Create" script is optional, and generating it is controlled via the following option:
Project Settings tab
> Create Script (.sql File)
>
> Specifies whether a full .sql CREATE script is generated for all the objects in the project, and placed in the bin\debug folder when the project is built. You can create an incremental update script using the Project Publish command or SQL Compare utility.
The non-"Create" script is the incremental script used by SSDT to publish/deploy any changes. It is possible to force the incremental script to also re-create the database via the following option (disabled by default):
Debug tab
> Always re-create database
>
> Specifies whether to drop and recreate the database instead of performing an incremental upgrade. You might want to select this check box if you want to run database unit tests against a clean deployment of the database, for example. If this check box is cleared, the existing database will be updated instead of dropped and re-created.
I don't recall exactly when SSDT started generating both scripts, but I believe it started prior to Visual Studio 2015. So, it has been around for awhile, but it should not be a cause for concern.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论