Esempio n. 1
0
        public String GenerateScript(List <ComparePair> items)
        {
            _syncScript = new SyncScript(_options);

            foreach (var item in items)
            {
                switch (item.Result)
                {
                case CompareResult.Removed:
                    DropObject(item.DestinationObject);
                    break;

                case CompareResult.New:
                    CreateObject(item.SourceObject);
                    break;

                case CompareResult.Different:
                    UpdateObject(item.SourceObject, item.DestinationObject);
                    break;

                case CompareResult.Equals:
                    break;
                }
            }

            return(_syncScript.ToString());
        }
Esempio n. 2
0
 public static void CreateTable(SqlTable table, SyncScript syncScript)
 {
     syncScript.Add(new SyncAction
     {
         Name = table.QualifiedName,
         Text = table.HeaderCreateScript(),
         Type = SyncActionType.CreateTable
     });
     CreateTableSubObjects(table, syncScript);
 }
Esempio n. 3
0
 public void FillScript(SyncScript script)
 {
     if (IsNeedRecreate())
     {
         ReCreateScript(script);
     }
     else
     {
         AlterScript(script);
     }
 }
Esempio n. 4
0
        private void ReCreateScript(SyncScript syncScript)
        {
            String newName     = $"{_dest.Name}_temp{( Guid.NewGuid().ToString("N") )}";
            String fullNewName = $"[{_dest.SchemaName}].[{newName}]";

            foreach (var key in _dest.Dependencies)
            {
                syncScript.Add(new SyncAction
                {
                    Name = key.Name,
                    Text = key.DropScript(),
                    Type = SyncActionType.DropFK
                });
            }

            DropTableSubObjects(_dest, syncScript);


            StringBuilder script = new StringBuilder();

            script.AppendLine($"EXEC sp_rename N'{_dest.QualifiedName}', N'{newName}' ");
            script.AppendLine(SqlStatement.GO);
            script.Append(_source.HeaderCreateScript());

            var columns = _source.Columns.Intersect(_dest.Columns, _columnNameComparer)
                          .Where(c => !c.IsComputed).Select(c => c.Name);

            String columnsText = String.Join(",", columns);

            if (_source.HasIdentity)
            {
                script.AppendLine($"SET IDENTITY_INSERT {_source.QualifiedName} ON");
            }
            script.AppendLine($"INSERT INTO {_dest.QualifiedName} ({columnsText}) ");
            script.AppendLine($"SELECT {columnsText} FROM {fullNewName}");
            if (_source.HasIdentity)
            {
                script.AppendLine($"SET IDENTITY_INSERT {_source.QualifiedName} OFF");
            }
            script.AppendLine(SqlStatement.GO);
            script.AppendLine($"DROP TABLE {fullNewName}");
            script.AppendLine(SqlStatement.GO);

            syncScript.Add(new SyncAction
            {
                Name = _source.QualifiedName,
                Text = script.ToString(),
                Type = SyncActionType.CreateTable
            });

            CreateTableSubObjects(_source, syncScript);

            foreach (var key in _dest.Dependencies.Intersect(_source.Dependencies, _depNameComparer))
            {
                syncScript.Add(new SyncAction
                {
                    Name = key.Name,
                    Text = key.CreateScript(true),
                    Type = SyncActionType.CreateFK
                });
            }
        }
Esempio n. 5
0
 private SyncScript AlterScript(SyncScript script)
 {
     throw new NotImplementedException();
 }
Esempio n. 6
0
        public static void DropTableSubObjects(SqlTable table, SyncScript syncScript)
        {
            if (table.PrimarKey != null)
            {
                syncScript.Add(new SyncAction
                {
                    Name = table.PrimarKey.Name,
                    Text = table.PrimarKey.DropScript(),
                    Type = SyncActionType.DropPK
                });
            }

            foreach (var subItem in table.UniqueConstraints)
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.DropScript(),
                    Type = SyncActionType.DropUC
                });
            }

            foreach (var subItem in table.DefaultConstraints)
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.DropScript(),
                    Type = SyncActionType.DropDC
                });
            }

            foreach (var subItem in table.CheckConstraints)
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.DropScript(),
                    Type = SyncActionType.DropCC
                });
            }

            foreach (var subItem in table.Indexes.Reverse <SqlIndex>())
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.DropScript(),
                    Type = SyncActionType.DropIndex
                });
            }

            foreach (var subItem in table.ForeignKeys)
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.DropScript(),
                    Type = SyncActionType.DropFK
                });
            }
        }
Esempio n. 7
0
        public static void CreateTableSubObjects(SqlTable table, SyncScript syncScript)
        {
            if (table.PrimarKey != null)
            {
                syncScript.Add(new SyncAction
                {
                    Name = table.PrimarKey.Name,
                    Text = table.PrimarKey.CreateScript(),
                    Type = SyncActionType.CreatePK
                });
            }

            foreach (var subItem in table.UniqueConstraints)
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.CreateScript(),
                    Type = SyncActionType.CreateUC
                });
            }

            foreach (var subItem in table.DefaultConstraints)
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.CreateScript(),
                    Type = SyncActionType.CreateDC
                });
            }

            foreach (var subItem in table.CheckConstraints)
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.CreateScript(),
                    Type = SyncActionType.CreateCC
                });
            }

            foreach (var subItem in table.Indexes)
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.CreateScript(),
                    Type = SyncActionType.CreateIndex
                });
            }

            foreach (var subItem in table.ForeignKeys)
            {
                syncScript.Add(new SyncAction
                {
                    Name = subItem.Name,
                    Text = subItem.CreateScript(true),
                    Type = SyncActionType.CreateFK
                });
            }
        }