Скрипт миграции автоматически создает скрипт изменения схемы в Redgate Sql - PullRequest
0 голосов
/ 07 ноября 2019

Несколько дней назад я написал скрипт миграции в Redgate SQL Source Control, который успешно работал на разных серверах развертывания.

Вчера я написал еще один скрипт, но когда я фиксирую его с помощью управления исходным кодом SQL, Redgate создает файл с именем AutomaticSchemaChange.patch , который показывает соответствующие изменения, но также показывает некоторые таблицы, которые создаются заново. которые уже присутствуют в базе данных. Вызывает разочарование, что Redgate предполагает, что эти таблицы отсутствуют в базе данных, где на самом деле они присутствуют.

Ниже приведен мой сценарий миграции:

IF OBJECT_ID('dbo.Fonts', 'U') IS NOT NULL
BEGIN

DECLARE @ListOfFonts TABLE (
    FontLabel varchar(max) NOT NULL,
    FontSize varchar(max) NOT NULL
)

INSERT INTO @ListOfFonts
Values  ('AdelleBasic_Bold.otf','97280')
       ,('ALoveofThunder_0.ttf', '201728')


DECLARE @FontName VARCHAR(MAX);
DECLARE @FontSize VARCHAR(MAX);

DECLARE FONT_CURSOR CURSOR FOR  
Select * FROM @ListOfFonts
OPEN FONT_CURSOR   
FETCH NEXT FROM FONT_CURSOR INTO @FontName, @FontSize

WHILE @@FETCH_STATUS = 0   
BEGIN   

IF EXISTS(SELECT 1 FROM dbo.Fonts WHERE [Name] = @FontName COLLATE Latin1_General_CI_AS)
    BEGIN

        UPDATE dbo.Fonts
        SET Size = @FontSize
        WHERE [Name] = @FontName COLLATE Latin1_General_CI_AS

    END

FETCH NEXT FROM FONT_CURSOR INTO @FontName, @FontSize

END   

CLOSE FONT_CURSOR   
DEALLOCATE FONT_CURSOR
END;

И ниже приведен файл изменений, который генерирует redgate.

diff --git a/RedGateDatabaseInfo.xml b/RedGateDatabaseInfo.xml
index be7face..56db8ba 100644
--- a/RedGateDatabaseInfo.xml
+++ b/RedGateDatabaseInfo.xml
@@ -1,64 +1,63 @@
-<?xml version="1.0" encoding="utf-8"?>
-<DatabaseInformation Version="2">
-  <ScriptFileEncoding>UTF8</ScriptFileEncoding>
-  <DefaultCollation>SQL_Latin1_General_CP1_CI_AS</DefaultCollation>
-  <DefaultSchema>dbo</DefaultSchema>
-  <DefaultUser>dbo</DefaultUser>
-  <DefaultFilegroup>PRIMARY</DefaultFilegroup>
-  <DatabaseVersion>14</DatabaseVersion>
-  <IsAzure>False</IsAzure>
-  <ScriptFolderType>Default</ScriptFolderType>
-  <MaxDataFileSize>10485760</MaxDataFileSize>
-  <ProjectGuid>b33245cc-f66d-4f95-a185-b22b582d07ac</ProjectGuid>
-  <WriteToFileOptions>
-    <Prefixes>
-      <None>
-      </None>
-      <Table>Tables</Table>
-      <StoredProcedure>Stored Procedures</StoredProcedure>
-      <View>Views</View>
-      <Default>Defaults</Default>
-      <FullTextCatalog>Storage\Full Text Catalogs</FullTextCatalog>
-      <Function>Functions</Function>
-      <Role>Security\Roles</Role>
-      <Rule>Rules</Rule>
-      <User>Security\Users</User>
-      <UserDefinedType>Types\User-defined Data Types</UserDefinedType>
-      <Trigger>
-      </Trigger>
-      <DdlTrigger>Database Triggers</DdlTrigger>
-      <Assembly>Assemblies</Assembly>
-      <Synonym>Synonyms</Synonym>
-      <XmlSchemaCollection>Types\XML Schema Collections</XmlSchemaCollection>
-      <MessageType>Service Broker\Message Types</MessageType>
-      <Contract>Service Broker\Contracts</Contract>
-      <Queue>Service Broker\Queues</Queue>
-      <Service>Service Broker\Services</Service>
-      <Route>Service Broker\Routes</Route>
-      <EventNotification>Service Broker\Event Notifications</EventNotification>
-      <PartitionScheme>Storage\Partition Schemes</PartitionScheme>
-      <PartitionFunction>Storage\Partition Functions</PartitionFunction>
-      <Field>
-      </Field>
-      <Index>
-      </Index>
-      <Schema>Security\Schemas</Schema>
-      <ServiceBinding>Service Broker\Remote Service Bindings</ServiceBinding>
-      <Certificate>Security\Certificates</Certificate>
-      <SymmetricKey>Security\Symmetric Keys</SymmetricKey>
-      <AsymmetricKey>Security\Asymmetric Keys</AsymmetricKey>
-      <CheckConstraint>
-      </CheckConstraint>
-      <FullTextStoplist>Storage\Full Text Stoplists</FullTextStoplist>
-      <ExtendedProperty>Extended Properties</ExtendedProperty>
-      <Data>Data</Data>
-      <Sequence>Sequences</Sequence>
-      <SearchPropertyList>Search Property Lists</SearchPropertyList>
-      <SecurityPolicy>Security Policies</SecurityPolicy>
-    </Prefixes>
-    <DataWriteAllFilesInOneDirectory>True</DataWriteAllFilesInOneDirectory>
-  </WriteToFileOptions>
-  <DataFileSet>
-    <Count>0</Count>
-  </DataFileSet>
+<?xml version="1.0" encoding="utf-8"?>
+<DatabaseInformation Version="2">
+  <ScriptFileEncoding>UTF8</ScriptFileEncoding>
+  <DefaultCollation>SQL_Latin1_General_CP1_CI_AS</DefaultCollation>
+  <DefaultSchema>dbo</DefaultSchema>
+  <DefaultUser>dbo</DefaultUser>
+  <DefaultFilegroup>PRIMARY</DefaultFilegroup>
+  <DatabaseVersion>14</DatabaseVersion>
+  <IsAzure>False</IsAzure>
+  <ScriptFolderType>Default</ScriptFolderType>
+  <MaxDataFileSize>10485760</MaxDataFileSize>
+  <WriteToFileOptions>
+    <Prefixes>
+      <None>
+      </None>
+      <Table>Tables</Table>
+      <StoredProcedure>Stored Procedures</StoredProcedure>
+      <View>Views</View>
+      <Default>Defaults</Default>
+      <FullTextCatalog>Storage\Full Text Catalogs</FullTextCatalog>
+      <Function>Functions</Function>
+      <Role>Security\Roles</Role>
+      <Rule>Rules</Rule>
+      <User>Security\Users</User>
+      <UserDefinedType>Types\User-defined Data Types</UserDefinedType>
+      <Trigger>
+      </Trigger>
+      <DdlTrigger>Database Triggers</DdlTrigger>
+      <Assembly>Assemblies</Assembly>
+      <Synonym>Synonyms</Synonym>
+      <XmlSchemaCollection>Types\XML Schema Collections</XmlSchemaCollection>
+      <MessageType>Service Broker\Message Types</MessageType>
+      <Contract>Service Broker\Contracts</Contract>
+      <Queue>Service Broker\Queues</Queue>
+      <Service>Service Broker\Services</Service>
+      <Route>Service Broker\Routes</Route>
+      <EventNotification>Service Broker\Event Notifications</EventNotification>
+      <PartitionScheme>Storage\Partition Schemes</PartitionScheme>
+      <PartitionFunction>Storage\Partition Functions</PartitionFunction>
+      <Field>
+      </Field>
+      <Index>
+      </Index>
+      <Schema>Security\Schemas</Schema>
+      <ServiceBinding>Service Broker\Remote Service Bindings</ServiceBinding>
+      <Certificate>Security\Certificates</Certificate>
+      <SymmetricKey>Security\Symmetric Keys</SymmetricKey>
+      <AsymmetricKey>Security\Asymmetric Keys</AsymmetricKey>
+      <CheckConstraint>
+      </CheckConstraint>
+      <FullTextStoplist>Storage\Full Text Stoplists</FullTextStoplist>
+      <ExtendedProperty>Extended Properties</ExtendedProperty>
+      <Data>Data</Data>
+      <Sequence>Sequences</Sequence>
+      <SearchPropertyList>Search Property Lists</SearchPropertyList>
+      <SecurityPolicy>Security Policies</SecurityPolicy>
+    </Prefixes>
+    <DataWriteAllFilesInOneDirectory>True</DataWriteAllFilesInOneDirectory>
+  </WriteToFileOptions>
+  <DataFileSet>
+    <Count>0</Count>
+  </DataFileSet>
</DatabaseInformation>
\ No newline at end of file
**==========Following are the tables which it is creating again==========**
diff --git a/Tables/dbo.Customer.sql b/Tables/dbo.Customer.sql
index a98d893..8320c94 100644
--- a/Tables/dbo.Customer.sql
+++ b/Tables/dbo.Customer.sql
@@ -9,10 +9,18 @@
 [Active] [bit] NULL,
 [Comment] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 [CustomerDefinedUrl] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
-[ClusterId] [int] NOT NULL IDENTITY(1, 1)
+[ClusterId] [int] NOT NULL IDENTITY(1, 1),
+[CreatedAt] [datetime2] NULL,
+[CreatedBy] [int] NULL,
+[UpdatedAt] [datetime2] NULL,
+[UpdatedBy] [int] NULL
 ) ON [PRIMARY]
 GO
 ALTER TABLE [dbo].[Customer] ADD CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED  ([ClusterId]) ON [PRIMARY]
 GO
 ALTER TABLE [dbo].[Customer] ADD CONSTRAINT [uc_CustomerName] UNIQUE NONCLUSTERED  ([CustomerName]) ON [PRIMARY]
 GO
+ALTER TABLE [dbo].[Customer] ADD CONSTRAINT [FK__Customer__Create__2275EAC3] FOREIGN KEY ([CreatedBy]) REFERENCES [dbo].[Users] ([ClusterId])
+GO
+ALTER TABLE [dbo].[Customer] ADD CONSTRAINT [FK__Customer__Update__236A0EFC] FOREIGN KEY ([UpdatedBy]) REFERENCES [dbo].[Users] ([ClusterId])
+GO
diff --git a/Tables/dbo.Fonts.sql b/Tables/dbo.Fonts.sql
new file mode 100644
index 0000000..45b78eb
--- /dev/null
+++ b/Tables/dbo.Fonts.sql
@@ -0,0 +1,24 @@
+CREATE TABLE [dbo].[Fonts]
+(
+[Id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_Fonts_Id] DEFAULT (newsequentialid()),
+[Name] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
+[Description] [nvarchar] (max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
+[Size] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
+[IsActive] [bit] NOT NULL CONSTRAINT [DF_Fonts_IsActive] DEFAULT ((1)),
+[CustomerId] [uniqueidentifier] NULL,
+[CreatedAt] [datetime] NULL,
+[UpdatedAt] [datetime] NULL,
+[ClusterId] [int] NOT NULL IDENTITY(1, 1),
+[CustomerClusterId] [int] NULL,
+[CreatedByClusterId] [int] NULL,
+[UpdatedByClusterId] [int] NULL
+) ON [PRIMARY]
+GO
+ALTER TABLE [dbo].[Fonts] ADD CONSTRAINT [PK_Fonts] PRIMARY KEY CLUSTERED  ([ClusterId]) ON [PRIMARY]
+GO
+ALTER TABLE [dbo].[Fonts] ADD CONSTRAINT [FK_Fonts_Customer] FOREIGN KEY ([CustomerClusterId]) REFERENCES [dbo].[Customer] ([ClusterId])
+GO
+ALTER TABLE [dbo].[Fonts] ADD CONSTRAINT [FK_Fonts_UserCreatedBy] FOREIGN KEY ([CreatedByClusterId]) REFERENCES [dbo].[Users] ([ClusterId])
+GO
+ALTER TABLE [dbo].[Fonts] ADD CONSTRAINT [FK_Fonts_UserUpdatedBy] FOREIGN KEY ([UpdatedByClusterId]) REFERENCES [dbo].[Users] ([ClusterId])
+GO
diff --git a/Tables/dbo.Site.sql b/Tables/dbo.Site.sql
index 3d5d3c5..cea2650 100644
--- a/Tables/dbo.Site.sql
+++ b/Tables/dbo.Site.sql
@@ -27,7 +27,8 @@
 [DmaClusterId] [int] NULL,
 [FranchiseClusterId] [int] NULL,
 [SalientSiteClusterId] [int] NULL,
-[UserClusterId] [int] NULL
+[UserClusterId] [int] NULL,
+[IsDeleted] [bit] NOT NULL CONSTRAINT [DF_Site_IsDeleted] DEFAULT ((0))
 ) ON [PRIMARY]
 GO
 ALTER TABLE [dbo].[Site] ADD CONSTRAINT [Pk_Site] PRIMARY KEY CLUSTERED  ([ClusterId]) ON [PRIMARY]
diff --git a/Tables/dbo.Users.sql b/Tables/dbo.Users.sql
index 976040d..e1a6ce9 100644
--- a/Tables/dbo.Users.sql
+++ b/Tables/dbo.Users.sql
@@ -17,15 +17,24 @@
 [ClusterId] [int] NOT NULL IDENTITY(1, 1),
 [CustomerClusterId] [int] NULL,
 [RoleClusterId] [int] NULL,
-[SiteClusterId] [int] NULL
+[SiteClusterId] [int] NULL,
+[CreatedAt] [datetime2] NULL,
+[CreatedBy] [int] NULL,
+[UpdatedAt] [datetime2] NULL,
+[UpdatedBy] [int] NULL,
+[PwdChangeCount] [int] NOT NULL CONSTRAINT [DF_Users_PwdChangeCount] DEFAULT ((0))
 ) ON [PRIMARY]
 GO
 ALTER TABLE [dbo].[Users] ADD CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED  ([ClusterId]) ON [PRIMARY]
 GO
 ALTER TABLE [dbo].[Users] ADD CONSTRAINT [AK_UserName] UNIQUE NONCLUSTERED  ([UserName]) ON [PRIMARY]
 GO
+ALTER TABLE [dbo].[Users] ADD CONSTRAINT [FK__Users__CreatedBy__3D29E0FF] FOREIGN KEY ([CreatedBy]) REFERENCES [dbo].[Users] ([ClusterId])
+GO
 ALTER TABLE [dbo].[Users] WITH NOCHECK ADD CONSTRAINT [FK__Users__LanguageI__1940BAED] FOREIGN KEY ([LanguageID]) REFERENCES [dbo].[Language] ([LanguageID])
 GO
+ALTER TABLE [dbo].[Users] ADD CONSTRAINT [FK__Users__UpdatedBy__3E1E0538] FOREIGN KEY ([UpdatedBy]) REFERENCES [dbo].[Users] ([ClusterId])
+GO
 ALTER TABLE [dbo].[Users] WITH NOCHECK ADD CONSTRAINT [FK_CustomerID] FOREIGN KEY ([CustomerClusterId]) REFERENCES [dbo].[Customer] ([ClusterId])
 GO
 ALTER TABLE [dbo].[Users] WITH NOCHECK ADD CONSTRAINT [FK_Users_Roles] FOREIGN KEY ([RoleClusterId]) REFERENCES [dbo].[Roles] ([ClusterId])

Мы используем процесс автоматического развертывания, который сравниваетSQL присутствует на сервере развертывания, а затем развертывает новую схему, но поскольку эти таблицы уже присутствуют, сравнение схем не выполняется, и, следовательно, наша сборка также не выполняется.

Пожалуйста, дайте мне знать причину и решение этой проблемы.

...