1

我一直在使用 SQL Server Data Tools、ms build 和 Jenkins 自动化数据库部署。我正在使用虚拟发布配置文件

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <IncludeCompositeObjects>True</IncludeCompositeObjects>
    <TargetDatabaseName>AdventureWorksDW</TargetDatabaseName>
    <DeployScriptFileName>AdventureWorksDW.sql</DeployScriptFileName>
    <TargetConnectionString>Data Source=.;Integrated     Security=True;Pooling=False</TargetConnectionString>
    <BlockWhenDriftDetected>False</BlockWhenDriftDetected>
    <RegisterDataTierApplication>False</RegisterDataTierApplication>
    <BlockOnPossibleDataLoss>True</BlockOnPossibleDataLoss>
    <IgnorePermissions>True</IgnorePermissions>
    <CreateNewDatabase>True</CreateNewDatabase>
    <ProfileVersionNumber>1</ProfileVersionNumber>
  </PropertyGroup>
</Project>

然后,我使用带有 ms build 的属性开关覆盖其中的某些值。

尽管没有在项目中包含权限并将 IgnorePermisssions 设置为 true,但我从 ms build 中获得了以下输出。

Microsoft (R) Build Engine version 4.0.30319.17929
[Microsoft .NET Framework, version 4.0.30319.17929]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 05/09/2012 17:35:28.
Project "E:\Jenkins\workspace\Publish SSDT with parameters\Database\ETL POC\AdventureWorksDW\AdventureWorksDW.sqlproj" on node 1 (Publish target(s)).
SqlPublish:
  Deployment script generated to:
  E:\Jenkins\workspace\Publish SSDT with parameters\Database\ETL POC\AdventureWorksDW\bin\Debug\AdventureWorksDW.publish.sql

  Creating AdventureWorksDW...
  Creating [My-Domain\jenkins]...
E:\Jenkins\workspace\Publish SSDT with parameters\Database\ETL POC\AdventureWorksDW\bin\Debug\AdventureWorksDW.publish.sql(173,1,173,1): Deploy error SQL72014: .Net SqlClient Data Provider: Msg 15063, Level 16, State 1, Line 1 The login already has an account under a different user name. [E:\Jenkins\workspace\Publish SSDT with parameters\Database\ETL POC\AdventureWorksDW\AdventureWorksDW.sqlproj]
  An error occurred while the batch was being executed.
Done Building Project "E:\Jenkins\workspace\Publish SSDT with parameters\Database\ETL POC\AdventureWorksDW\AdventureWorksDW.sqlproj" (Publish target(s)) -- FAILED.

Build FAILED.

编辑:

我现在已经从 msbuild 归档了发布脚本。下面的部分已被编辑以排除正在设置的不相关的数据库选项,并以引发错误的命令结束。

/*
Deployment script for AdventureWorksDW

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 "AdventureWorksDW"
:setvar DefaultFilePrefix "AdventureWorksDW"
:setvar DefaultDataPath "E:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\"
:setvar DefaultLogPath "E:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\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 [master];
GO

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 $(DatabaseName)...'
GO
CREATE DATABASE [$(DatabaseName)]
    ON 
    PRIMARY(NAME = [$(DatabaseName)], FILENAME = N'$(DefaultDataPath)$(DefaultFilePrefix)_Primary.mdf')
    LOG ON (NAME = [$(DatabaseName)_log], FILENAME = N'$(DefaultLogPath)$(DefaultFilePrefix)_Primary.ldf') COLLATE Latin1_General_CI_AS
GO
GO
PRINT N'Creating [MY-DOMAIN\jenkins]...';

GO
CREATE USER [MY-DOMAIN\jenkins] FOR LOGIN [DR-FOSTER\jenkins];

我已经在 J​​enkins 之外运行了这个脚本并且它成功了,但是那是在另一个使用的帐户下。

似乎尽管 MY-DOMAIN\jenkins 创建了数据库,因此作为所有者,发布脚本想要创建一个用户并将其设置为 db_owner。

有没有人有类似的经验?这开始感觉像是一个错误。

谢谢

4

1 回答 1

2

在尝试将 Windows 用户添加到数据库之前,当用户已经处于 dbowner 角色中时,我已经看到了 SQL Server 错误。将 dbowner 更改为类似 sa 允许您将用户添加到数据库中。

这将有助于共享正在处理的实际 SQL 语句。

于 2012-09-06T02:35:46.067 回答