コード例 #1
0
        private static void RunUpdate(IEnumerable <FileInfo> files)
        {
            var scripts = new List <SqlScript>();

            foreach (var file in files)
            {
                try
                {
                    scripts.Add(SqlScript.FromFile(file.Name, file.FullName, true, true));
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Failed to parse file {file.FullName}: {e.Message}");
                    Console.WriteLine($"Canceling update.");
                    return;
                }
            }

            Console.WriteLine($"Failed parsed successfully");

            var context = GetContext();

            scripts = scripts
                      .Where(s => s.Version.CompareTo(context.GetInstalledVersion().GetVersion()) == 1)
                      .OrderBy(s => s.Version.Major)
                      .ThenBy(s => s.Version.Minor)
                      .ThenBy(s => s.Version.Revision).ToList();

            Console.WriteLine("Filtered and ordered scripts. Order is: ");
            foreach (var script in scripts)
            {
                Console.WriteLine($"- {script.Version.ToString()} ({script.Name})");
            }

            if (RequestContinueConfirmation())
            {
                foreach (var script in scripts)
                {
                    try
                    {
                        Console.Write($"Executing {script.Version.ToString()} {script.Name} ... ");

                        context.ExecuteScript(script);

                        Console.WriteLine("Success");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Failed to execute script {script.Name}: {e.ToString()}");
                        Console.WriteLine("Canceling update");
                        return;
                    }
                }

                Console.WriteLine($"Update completed successfully. Current version is: {context.GetInstalledVersion()}");
            }
        }
コード例 #2
0
        public IEnumerable <SqlScript> GetScripts()
        {
            var files = Directory.GetFiles(_directoryPath, "*.sql").AsEnumerable();

            if (_filter != null)
            {
                files = files.Where(_filter);
            }
            return(files.Select(x => SqlScript.FromFile(x, _encoding)).ToList());
        }
コード例 #3
0
        /// <summary>
        /// Gets all scripts that should be executed.
        /// </summary>
        public IEnumerable <SqlScript> GetScripts(IConnectionManager connectionManager)
        {
            var files = Directory.GetFiles(directoryPath, "*.sql", ShouldSearchSubDirectories()).AsEnumerable();

            if (this.filter != null)
            {
                files = files.Where(filter);
            }
            return(files.Select(x => SqlScript.FromFile(x, encoding)).ToList());
        }
コード例 #4
0
        protected override IEnumerable <SqlScript> GetAllScripts()
        {
            var files = GetAllFiles(_directoryPath, _fileSystemScriptOptions)
                        .Select(file => file.FullName)
                        .Where(_filter)
                        .Select(file => SqlScript.FromFile(file))
                        .OrderBy(script => script.Name)
                        .ToList();

            return(files);
        }
コード例 #5
0
        public void Can_Parse_Version()
        {
            var path    = "test_parse_version.sql";
            var content = @"-- 3.2.34
                            SELECT * FROM test;";

            File.WriteAllText(path, content);

            var script = SqlScript.FromFile("test_script", path, useSpecifiedDatabase: true, useTransaction: true);

            Assert.That(script.Version.CompareTo(new SqlVersion(3, 2, 34)) == 0);
        }
コード例 #6
0
        protected IEnumerable <SqlScript> GetAllProviderScripts(string path)
        {
            var encoding         = System.Text.Encoding.UTF8;
            var sqlScriptOptions = new SqlScriptOptions();

            var files = GetFiles(path, true);

            return(files.Select(x =>
            {
                var s = SqlScript.FromFile(path, x.FullName, encoding, sqlScriptOptions);
                return new SqlScript(x.Name, s.Contents, s.SqlScriptOptions);
            }));
        }
コード例 #7
0
        public void Can_Init_From_File()
        {
            var path    = "test_init_from_file.sql";
            var command = "SELECT 42";
            var version = new SqlVersion(1, 0, 0);

            File.WriteAllText(path, command);
            SqlScript script = null;

            script = SqlScript.FromFile("test", path, version, useSpecifiedDatabase: true, useTransaction: false);

            Assert.AreEqual(command, script.Command);
            Assert.AreEqual(version, script.Version);
        }
コード例 #8
0
        /// <summary>
        /// Gets all scripts that should be executed.
        /// </summary>
        public IEnumerable <SqlScript> GetScripts(IConnectionManager connectionManager)
        {
            var files = new List <string>();

            foreach (var scriptExtension in options.Extensions)
            {
                files.AddRange(Directory.GetFiles(directoryPath, scriptExtension, ShouldSearchSubDirectories()));
            }
            if (filter != null)
            {
                files = files.Where(filter).ToList();
            }
            return(files.Select(x => SqlScript.FromFile(directoryPath, x, encoding, sqlScriptOptions))
                   .OrderBy(x => x.Name)
                   .ToList());
        }
コード例 #9
0
        /// <summary>
        /// Gets all scripts that should be executed.
        /// </summary>
        public IEnumerable <SqlScript> GetScripts(IConnectionManager connectionManager)
        {
            var files = Directory.GetFiles(directoryPath, "*.sql", ShouldSearchSubDirectories()).AsEnumerable();

            if (filter != null)
            {
                files = files.Where(filter);
            }

            return(files.Select(x => {
                // if subdirectory name is needed, remove root folder from file path to obtain the subdirectory name and file name
                var scriptName = ScriptOptions.IncludeSubDirectoryInName ? new Regex(Regex.Escape(directoryPath)).Replace(x, string.Empty, 1).TrimStart('\\') : null;

                return SqlScript.FromFile(x, encoding, scriptName);
            }).OrderBy(x => x.Name).ToList());
        }
コード例 #10
0
        public IEnumerable <SqlScript> GetScripts(IConnectionManager connectionManager)
        {
            var deserializer = new DeserializerBuilder()
                               .WithTypeConverter(new DateTimeTypeConverter())
                               .WithNamingConvention(new CamelCaseNamingConvention())
                               .Build();

            var input = new StreamReader(_planFile);
            var plan  = deserializer.Deserialize <Plan>(input);

            input.Dispose();

            var scripts = plan.Scripts.Select(script => SqlScript.FromFile(script.Path));

            return(scripts);
        }
コード例 #11
0
 /// <summary>
 /// Gets all scripts that should be executed.
 /// </summary>
 public IEnumerable <SqlScript> GetScripts(IConnectionManager connectionManager)
 {
     if (!options.UseOnlyFilenameForScriptName)
     {
         var files = new List <string>();
         foreach (var scriptExtension in options.Extensions)
         {
             files.AddRange(Directory.GetFiles(directoryPath, scriptExtension, ShouldSearchSubDirectories()));
         }
         if (filter != null)
         {
             files = files.Where(filter).ToList();
         }
         return(files.Select(x => SqlScript.FromFile(directoryPath, x, encoding, sqlScriptOptions))
                .OrderBy(x => x.Name)
                .ToList());
     }
     else
     {
         var files = new List <FileInfo>();
         foreach (var scriptExtension in options.Extensions)
         {
             files.AddRange(
                 new DirectoryInfo(directoryPath).GetFiles(scriptExtension, ShouldSearchSubDirectories())
                 );
         }
         var dupeFiles = files.GroupBy(f => f.Name).Where(grp => grp.Count() > 1).SelectMany(f => f.Select(fi => fi.FullName)).ToList();
         if (dupeFiles.Count > 0)
         {
             var sbError = new StringBuilder();
             sbError.AppendLine("Duplicate filenames:");
             foreach (var file in dupeFiles)
             {
                 sbError.AppendLine($"- {file}");
             }
             throw new Exception(sbError.ToString());
         }
         if (filter != null)
         {
             files = files.Where(x => filter(x.Name)).ToList();
         }
         return(files.Select(x => SqlScript.FromStream(x.Name, new FileStream(x.FullName, FileMode.Open, FileAccess.Read), encoding, sqlScriptOptions))
                .OrderBy(x => x.Name)
                .ToList());
     }
 }
コード例 #12
0
        public static UpgradeEngineBuilder WithScriptManifest(this UpgradeEngineBuilder builder, string manifestFilePath)
        {
            var scripts = new List <SqlScript>(500);

            var paths = File.ReadAllLines(manifestFilePath);

            int groupOrder = DbUpDefaults.DefaultRunGroupOrder; // 100

            foreach (var scriptPath in paths)
            {
                var script = SqlScript.FromFile(
                    Path.GetDirectoryName(scriptPath),
                    scriptPath,
                    DbUpDefaults.DefaultEncoding, // UTF-8
                    new SqlScriptOptions {
                    RunGroupOrder = groupOrder, ScriptType = ScriptType.RunOnce
                });
                scripts.Add(script);
                groupOrder += 100;
            }

            builder.WithScripts(scripts);
            return(builder);
        }