Beispiel #1
0
        private IEnumerable <SqlEntity> GetDependantsByEntity(SqlEntity entity, bool reversed)
        {
            var dependants = DependentEntities.TryGet(entity.Name).EmptyIfNull();

            foreach (var dependant in dependants)
            {
                if (!reversed)
                {
                    foreach (var grandDependant in GetDependantsByEntity(dependant, reversed))
                    {
                        yield return(grandDependant);
                    }
                    yield return(dependant);
                }
                else
                {
                    yield return(dependant);

                    foreach (var grandDependant in GetDependantsByEntity(dependant, reversed))
                    {
                        yield return(grandDependant);
                    }
                }
            }
        }
Beispiel #2
0
 private static Dictionary <string, SqlEntity> GetFileToEntityMapping(Settings settings)
 {
     return(Directory
            .EnumerateFiles(settings.Path, "*.sql", SearchOption.AllDirectories)
            .AsParallel().WithDegreeOfParallelism(4).WithExecutionMode(ParallelExecutionMode.ForceParallelism)
            .Select(filePath => new { path = filePath, content = GetContent(filePath) })
            .Select(e => SqlEntity.Create(e.path, e.content, settings))
            .Where(e => e.Type != SqlEntityType.Unknown)
            .AsSequential()
            .ToDictionary(e => e.Path, e => e));
 }
Beispiel #3
0
        public static SqlEntity Create(string path, string content, Settings settings)
        {
            var type   = content.Maybe(GetEntityType, SqlEntityType.Unknown);
            var entity = new SqlEntity
            {
                Path          = path,
                Type          = type,
                Name          = content.Maybe(GetEntityName),
                IsSchemaBound = content.Maybe(c => c.ContainsSql("schemabinding"), defaultValue: false) ||
                                type == SqlEntityType.Type,
                Content = ReplaceVariables(content, settings)
            };

            return(entity);
        }
Beispiel #4
0
 public DropCommand(SqlEntity entity)
 {
     this.Entity = entity;
 }
Beispiel #5
0
 public CreateCommand(SqlEntity entity)
 {
     Entity = entity;
 }
Beispiel #6
0
        public void HandleInternal(string oldPath, string path)
        {
            var watch = Stopwatch.StartNew();

            Logger.Log("started handling update of file {0}", path.Substring(settings.Path.Length));

            var actions = new List <ICommand>();

            var dependants         = project.GetDependantsByPath(oldPath ?? path);
            var reversedDependants = project.GetDependantsByPath(oldPath ?? path, reversed: true);

            actions.AddRange(dependants.Select(e => new DropCommand(e)));

            var oldEntity = project.FileToEntityMapping.TryGet(oldPath ?? path);

            if (oldEntity != null)
            {
                actions.Add(new DropCommand(oldEntity));
            }


            if (File.Exists(path))
            {
                var content = SqlProjectInfo.GetContent(path);
                if (content == null)
                {
                    Logger.Log("failed to get content for file {0}", path);
                }
                else
                {
                    if (oldEntity.Maybe(_ => _.Content) == content)
                    {
                        Logger.Log("fake update of {0}", path);
                        return;                         // short circuit return to prevent fake updates
                    }

                    var entity = SqlEntity.Create(path, content, settings);
                    if (oldEntity.Maybe(e => e.Name) != entity.Name)
                    {
                        actions.Add(new DropCommand(entity));
                    }

                    actions.Add(new CreateCommand(entity));
                }
            }
            else
            {
                if (dependants.Any())
                {
                    Logger.Log("you have removed {0}, but some entities depends on it: {1}",
                               project.FileToEntityMapping.TryGet(path).Maybe(e => e.Name) ?? path,
                               string.Join(";", dependants));

                    return;
                }
            }

            actions.AddRange(reversedDependants.Select(e => new CreateCommand(e)));

            while (transaction.Connection != null && actions.Any())
            {
                foreach (var action in actions.ToList())
                {
                    if (transaction.Connection == null)
                    {
                        break;
                    }

                    action.Perform(transaction, project, actions.Add);
                    actions.Remove(action);
                }
            }

            Logger.Log("finished handling update of file {0} for {1}", path.Substring(settings.Path.Length), watch.Elapsed);
        }