public Script[] GetScripts(ScriptKind kind)
        {
            Func<DirectoryInfo, bool> selector;
            switch (kind)
            {
                case ScriptKind.PreMigration:
                    selector = IsPreMigrationFolder;
                    break;
                case ScriptKind.Migration:
                    selector = IsMigrationFolder;
                    break;
                case ScriptKind.PostMigration:
                    selector = IsPostMigrationFolder;
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(kind), kind, null);
            }

            var q = from d in _directory.EnumerateDirectories("*") 
                    where selector(d) 
                    from s in d.GetFiles("*.sql", SearchOption.AllDirectories) 
                    let collectionName = Path.GetFileName(d.FullName.Substring(_directory.FullName.Length + 1).Split(Path.PathSeparator).First()) 
                    let scriptName = s.FullName.Substring(d.FullName.Length + 1) 
                    let content = ReadFile(s.FullName) 
                    let checksum = content.Checksum() 
                    orderby d.FullName ascending, scriptName ascending 
                    select new Script(collectionName, scriptName, content, checksum);

            var scripts = q.ToArray();

            return scripts;
        }
Esempio n. 2
0
        public bool ExecuteScripts(bool whatif, ScriptKind kind)
        {
            var scripts = _scriptFileRepository.GetScripts(kind);

            var maxFolderNameLength = scripts.Select(s => s.Collection.Length).DefaultIfEmpty().Max();
            var maxFileNameLength = scripts.Select(s => s.ScriptName.Length).DefaultIfEmpty().Max();

            foreach (var script in scripts)
            {
                Logger.Info($"[{script.Collection}".PadRight(maxFolderNameLength + 1) + "] ");
                Logger.Info((script.ScriptName + "... ").PadRight(maxFileNameLength + 4));
                try
                {
                    if (!whatif)
                    {
                        _database.RunInTransaction(script.Content);
                        Logger.Ok();
                    }
                }
                catch (Exception e)
                {
                    Logger.ErrorLine("ERROR: " + e.Message);
                    return false;
                }
                Console.WriteLine();
            }
            return true;
        }
Esempio n. 3
0
 public bool HasScripts(ScriptKind kind)
 {
     var scripts = _scriptFileRepository.GetScripts(kind);
     return scripts.Any();
 }