Exemple #1
0
        public static string[] Deploy(Arguments.IDeployArguments deployArguments, Logging.ILog log = null)
        {
            if (log == null)
            {
                log = new Logging.NoLogging();
            }

            if (System.IO.File.Exists(deployArguments.ArtifactFile))
            {
                string packageFile = deployArguments.ArtifactFile;//System.IO.Path.Combine(deployArguments.ArtifactFolder, PackageTask.PackageFile);
                if (System.IO.File.Exists(packageFile))
                {
                    string tempFolder = System.IO.Path.GetTempPath();
                    string newFolder  = System.Guid.NewGuid().ToString();
                    string ourFolder  = System.IO.Path.Combine(tempFolder, newFolder);
                    System.IO.Directory.CreateDirectory(ourFolder);
                    ICSharpCode.SharpZipLib.Zip.FastZip fz = new FastZip();

                    fz.ExtractZip(packageFile, ourFolder, null);

                    Arguments.BuildArguments ba = new BuildArguments();
                    ba.ConnectionString  = deployArguments.ConnectionString;
                    ba.DbProviderFactory = deployArguments.DbProviderFactory;
                    ba.ScriptProcessor   = deployArguments.ScriptProcessor;
                    ba.VersionParser     = deployArguments.VersionParser;
                    ba.DryRun            = false;
                    ba.BreakOnError      = true;
                    ba.MaximumVersion    = "99999.99999.99999";
                    ba.MinimumVersion    = "0.0.0";
                    ba.Recurse           = true;
                    ba.ScriptPath        = ourFolder;
                    ba.ScriptPattern     = "*.*";
                    ba.Transactional     = true;


                    return(BuildTask.Build(ba, log));
                }
            }

            return(null);
        }
        public static string[] Deploy(Arguments.IDeployArguments deployArguments, Logging.ILog log = null)
        {
            if (log == null) log = new Logging.NoLogging();

            if (System.IO.File.Exists(deployArguments.ArtifactFile))
            {
                string packageFile = deployArguments.ArtifactFile;//System.IO.Path.Combine(deployArguments.ArtifactFolder, PackageTask.PackageFile);
                if (System.IO.File.Exists(packageFile))
                {
                    string tempFolder = System.IO.Path.GetTempPath();
                    string newFolder = System.Guid.NewGuid().ToString();
                    string ourFolder = System.IO.Path.Combine(tempFolder, newFolder);
                    System.IO.Directory.CreateDirectory(ourFolder);
                    ICSharpCode.SharpZipLib.Zip.FastZip fz = new FastZip();

                    fz.ExtractZip(packageFile, ourFolder, null);

                    Arguments.BuildArguments ba = new BuildArguments();
                    ba.ConnectionString = deployArguments.ConnectionString;
                    ba.DbProviderFactory = deployArguments.DbProviderFactory;
                    ba.ScriptProcessor = deployArguments.ScriptProcessor;
                    ba.VersionParser = deployArguments.VersionParser;
                    ba.DryRun = false;
                    ba.BreakOnError = true;
                    ba.MaximumVersion = "99999.99999.99999";
                    ba.MinimumVersion = "0.0.0";
                    ba.Recurse = true;
                    ba.ScriptPath = ourFolder;
                    ba.ScriptPattern = "*.*";
                    ba.Transactional = true;

                    return BuildTask.Build(ba, log);

                }
            }

            return null;
        }
Exemple #3
0
        public static string[] Build(Arguments.IBuildArguments buildArguments, Logging.ILog log = null)
        {
            if (log == null)
            {
                log = new Logging.NoLogging();
            }
            if (string.IsNullOrEmpty(buildArguments.ScriptPath))
            {
                buildArguments.ScriptPath = System.Environment.CurrentDirectory;
            }

            if (string.IsNullOrEmpty(buildArguments.DbProviderFactory))
            {
                buildArguments.DbProviderFactory = "System.Data.SqlClient";
            }

            if (string.IsNullOrEmpty(buildArguments.ScriptProcessor))
            {
                buildArguments.ScriptProcessor = typeof(SqlScriptRunner.ScriptProcessing.SqlServerScriptProcessor).FullName;
            }

            if (string.IsNullOrEmpty(buildArguments.VersionParser))
            {
                buildArguments.VersionParser = typeof(SqlScriptRunner.Versioning.VersionDateParser).AssemblyQualifiedName;
            }


            Type t = Type.GetType(buildArguments.VersionParser);

            SqlScriptRunner.Versioning.IParseVersions versionParser =
                (Activator.CreateInstance(t) as
                 SqlScriptRunner.Versioning.IParseVersions);


            SqlScriptRunner.Versioning.Version minVersion = null;
            SqlScriptRunner.Versioning.Version maxVersion = null;

            if (string.IsNullOrEmpty(buildArguments.MinimumVersion))
            {
                minVersion = SqlScriptRunner.Versioning.Version.Min;
            }
            else
            {
                minVersion = versionParser.Parse(buildArguments.MinimumVersion);
            }

            if (string.IsNullOrEmpty(buildArguments.MaximumVersion))
            {
                maxVersion = SqlScriptRunner.Versioning.Version.Max;
            }
            else
            {
                maxVersion = versionParser.Parse(buildArguments.MaximumVersion);
            }
            log.Info("--------------------------------");
            log.Info(string.Format("Min:{0}, Max:{1}, ScriptPath:{2}, Transactional:{4}, DryRun:{5}\r\nConnectionString:{3}", minVersion, maxVersion, buildArguments.ScriptPath, buildArguments.ConnectionString, buildArguments.Transactional, buildArguments.DryRun));
            log.Info("--------------------------------");
            DbConnection connection = null;

            if (!buildArguments.DryRun)
            {
                //make sure we can connect to the database
                DbProviderFactory factory = DbProviderFactories.GetFactory(buildArguments.DbProviderFactory);
                connection = factory.CreateConnection();
                if (connection == null)
                {
                    throw new ArgumentException(
                              "Could not create a connection to the database, via the Provider Factory:" +
                              buildArguments.DbProviderFactory);
                }
                else
                {
                    connection.ConnectionString = buildArguments.ConnectionString;
                    connection.Open();
                }
            }

            SortedList <string, string> Files = SqlScriptRunner.ScriptRunner.ResolveScriptsFromPathAndVersion(buildArguments.ScriptPath, buildArguments.ScriptPattern, buildArguments.Recurse, System.Environment.CurrentDirectory, minVersion, maxVersion, versionParser);

            log.Info(string.Format("Resolved:{0} files.", Files.Count));

            foreach (var file in Files.Keys)
            {
                log.Info(file);
                if (!buildArguments.DryRun)
                {
                    try
                    {
                        log.Info("Executing");
                        string script = System.IO.File.ReadAllText(Files[file]);
                        SqlScriptRunner.ScriptRunner runner = new ScriptRunner(script, null);
                        if (buildArguments.Transactional)
                        {
                            if (connection.State == ConnectionState.Closed)
                            {
                                connection.Open();
                            }
                            System.Data.IDbTransaction transaction = null;
                            if (buildArguments.Transactional)
                            {
                                transaction = connection.BeginTransaction();
                            }
                            try
                            {
                                runner.Execute(connection, transaction);
                                if (buildArguments.Transactional)
                                {
                                    transaction.Commit();
                                }
                                log.Info("Success:" + file);
                            }
                            catch (Exception e)
                            {
                                log.Info("Fail [In Trx:" + buildArguments.Transactional + "]:" + file);
                                log.Fatal(e);
                                if (buildArguments.Transactional)
                                {
                                    transaction.Rollback();
                                }
                                throw;
                            }
                        }
                        else
                        {
                            runner.Execute(connection);
                        }
                    }
                    catch (Exception e)
                    {
                        if (buildArguments.BreakOnError)
                        {
                            throw;
                        }
                        else
                        {
                            log.Debug("There was an error with a script, since BreakOnError is false, we will continue.File:" + file, e);
                        }
                    }
                }
            }
            log.Info("Done Executing");
            return((from f in Files select f.Value).ToArray());
        }
Exemple #4
0
        public static string[] Package(Arguments.IPackageArguments packageArguments, Logging.ILog log = null)
        {
            if (log == null)
            {
                log = new Logging.NoLogging();
            }

            Arguments.BuildArguments ba = new BuildArguments();
            ba.ScriptPath     = packageArguments.ScriptPath;
            ba.ScriptPattern  = packageArguments.ScriptPattern;
            ba.Recurse        = packageArguments.Recurse;
            ba.MinimumVersion = packageArguments.MinimumVersion;
            ba.MaximumVersion = packageArguments.MaximumVersion;
            ba.DryRun         = true;
            ba.VersionParser  = packageArguments.VersionParser;

            string[] files = BuildTask.Build(ba, log);

            string tempFolder = System.IO.Path.GetTempPath();
            string newFolder  = System.Guid.NewGuid().ToString();
            string ourFolder  = System.IO.Path.Combine(tempFolder, newFolder);
            string ourZip     = System.IO.Path.Combine(tempFolder, newFolder + ".zip");

            System.IO.Directory.CreateDirectory(ourFolder);


            //create copies of our deployable files
            foreach (var file in files)
            {
                System.IO.FileInfo fi         = new FileInfo(file);
                string             dir        = fi.Directory.FullName.Replace(packageArguments.ScriptPath, "");
                string             newTempDir = System.IO.Path.Combine(ourFolder, dir);
                if (!System.IO.Directory.Exists(newTempDir))
                {
                    System.IO.Directory.CreateDirectory(newTempDir);
                }
                System.IO.File.Copy(file, System.IO.Path.Combine(newTempDir, fi.Name));
            }

            string DeploySample     = "SqlScriptRunner.Resources.Deploy.sample.msbuild";
            string deploySampleFile = System.IO.Path.Combine(ourFolder, "Deploy.sample.msbuild");

            using (System.IO.Stream stm = typeof(PackageTask).Assembly.GetManifestResourceStream(DeploySample))
            {
                using (System.IO.StreamReader rdr = new StreamReader(stm))
                {
                    string contents = rdr.ReadToEnd();
                    if (!string.IsNullOrEmpty(contents))
                    {
                        System.IO.FileInfo fi = new FileInfo(packageArguments.ArtifactFile);
                        contents = contents.Replace("[PACKAGEFILENAME]", fi.Name);
                        System.IO.File.WriteAllText(deploySampleFile, contents);
                    }
                }
            }

            //zip them up!
            ICSharpCode.SharpZipLib.Zip.FastZip fz = new FastZip();
            fz.CreateZip(ourZip, ourFolder, true, null);

            string path = ourZip;

            if (!packageArguments.DryRun)
            {
                //if (!System.IO.Directory.Exists(packageArguments.ArtifactFolder)) System.IO.Directory.CreateDirectory(packageArguments.ArtifactFolder);
                //path = System.IO.Path.Combine(packageArguments.ArtifactFolder, PackageFile);
                path = packageArguments.ArtifactFile;
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
                System.IO.File.Copy(ourZip, path);
            }

            return(new string[] { path });
        }
        public static string[] Package(Arguments.IPackageArguments packageArguments, Logging.ILog log = null)
        {
            if (log == null) log = new Logging.NoLogging();

            Arguments.BuildArguments ba = new BuildArguments();
            ba.ScriptPath = packageArguments.ScriptPath;
            ba.ScriptPattern = packageArguments.ScriptPattern;
            ba.Recurse = packageArguments.Recurse;
            ba.MinimumVersion = packageArguments.MinimumVersion;
            ba.MaximumVersion = packageArguments.MaximumVersion;
            ba.DryRun = true;
            ba.VersionParser = packageArguments.VersionParser;

            string[] files = BuildTask.Build(ba, log);

            string tempFolder = System.IO.Path.GetTempPath();
            string newFolder = System.Guid.NewGuid().ToString();
            string ourFolder = System.IO.Path.Combine(tempFolder, newFolder);
            string ourZip = System.IO.Path.Combine(tempFolder, newFolder + ".zip");
            System.IO.Directory.CreateDirectory(ourFolder);

            //create copies of our deployable files
            foreach (var file in files)
            {
                System.IO.FileInfo fi = new FileInfo(file);
                string dir = fi.Directory.FullName.Replace(packageArguments.ScriptPath, "");
                string newTempDir = System.IO.Path.Combine(ourFolder, dir);
                if (!System.IO.Directory.Exists(newTempDir)) System.IO.Directory.CreateDirectory(newTempDir);
                System.IO.File.Copy(file, System.IO.Path.Combine(newTempDir, fi.Name));
            }

            string DeploySample = "SqlScriptRunner.Resources.Deploy.sample.msbuild";
            string deploySampleFile = System.IO.Path.Combine(ourFolder, "Deploy.sample.msbuild");

            using (System.IO.Stream stm = typeof(PackageTask).Assembly.GetManifestResourceStream(DeploySample))
            {
                using (System.IO.StreamReader rdr = new StreamReader(stm))
                {
                    string contents = rdr.ReadToEnd();
                    if (!string.IsNullOrEmpty(contents))
                    {
                        System.IO.FileInfo fi = new FileInfo(packageArguments.ArtifactFile);
                        contents = contents.Replace("[PACKAGEFILENAME]", fi.Name);
                        System.IO.File.WriteAllText(deploySampleFile, contents);
                    }
                }
            }

            //zip them up!
            ICSharpCode.SharpZipLib.Zip.FastZip fz = new FastZip();
            fz.CreateZip(ourZip, ourFolder, true, null);

            string path = ourZip;
            if (!packageArguments.DryRun)
            {
                //if (!System.IO.Directory.Exists(packageArguments.ArtifactFolder)) System.IO.Directory.CreateDirectory(packageArguments.ArtifactFolder);
                //path = System.IO.Path.Combine(packageArguments.ArtifactFolder, PackageFile);
                path = packageArguments.ArtifactFile;
                if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
                System.IO.File.Copy(ourZip, path);
            }

            return new string[] { path };
        }
        public static string[] Build(Arguments.IBuildArguments buildArguments, Logging.ILog log = null)
        {
            if (log == null) log = new Logging.NoLogging();
            if (string.IsNullOrEmpty(buildArguments.ScriptPath))
                buildArguments.ScriptPath = System.Environment.CurrentDirectory;

            if (string.IsNullOrEmpty(buildArguments.DbProviderFactory))
                buildArguments.DbProviderFactory = "System.Data.SqlClient";

            if (string.IsNullOrEmpty(buildArguments.ScriptProcessor))
                buildArguments.ScriptProcessor = typeof(SqlScriptRunner.ScriptProcessing.SqlServerScriptProcessor).FullName;

            if (string.IsNullOrEmpty(buildArguments.VersionParser))
                buildArguments.VersionParser = typeof(SqlScriptRunner.Versioning.VersionDateParser).AssemblyQualifiedName;

            Type t = Type.GetType(buildArguments.VersionParser);

            SqlScriptRunner.Versioning.IParseVersions versionParser =
                (Activator.CreateInstance(t) as
                 SqlScriptRunner.Versioning.IParseVersions);

            SqlScriptRunner.Versioning.Version minVersion = null;
            SqlScriptRunner.Versioning.Version maxVersion = null;

            if (string.IsNullOrEmpty(buildArguments.MinimumVersion))
            {
                minVersion = SqlScriptRunner.Versioning.Version.Min;
            }
            else
            {
                minVersion = versionParser.Parse(buildArguments.MinimumVersion);
            }

            if (string.IsNullOrEmpty(buildArguments.MaximumVersion))
            {
                maxVersion = SqlScriptRunner.Versioning.Version.Max;
            }
            else
            {
                maxVersion = versionParser.Parse(buildArguments.MaximumVersion);
            }
            log.Info("--------------------------------");
            log.Info(string.Format("Min:{0}, Max:{1}, ScriptPath:{2}, Transactional:{4}, DryRun:{5}\r\nConnectionString:{3}", minVersion, maxVersion, buildArguments.ScriptPath, buildArguments.ConnectionString, buildArguments.Transactional, buildArguments.DryRun));
            log.Info("--------------------------------");
            DbConnection connection = null;

            if (!buildArguments.DryRun)
            {

                //make sure we can connect to the database
                DbProviderFactory factory = DbProviderFactories.GetFactory(buildArguments.DbProviderFactory);
                connection = factory.CreateConnection();
                if (connection == null)
                {
                    throw new ArgumentException(
                        "Could not create a connection to the database, via the Provider Factory:" +
                        buildArguments.DbProviderFactory);
                }
                else
                {
                    connection.ConnectionString = buildArguments.ConnectionString;
                    connection.Open();
                }
            }

            SortedList<string, string> Files = SqlScriptRunner.ScriptRunner.ResolveScriptsFromPathAndVersion(buildArguments.ScriptPath, buildArguments.ScriptPattern, buildArguments.Recurse, System.Environment.CurrentDirectory, minVersion, maxVersion, versionParser);
            log.Info(string.Format("Resolved:{0} files.", Files.Count));

            foreach (var file in Files.Keys)
            {
                log.Info(file);
                if (!buildArguments.DryRun)
                {
                    try
                    {

                        log.Info("Executing");
                        string script = System.IO.File.ReadAllText(Files[file]);
                        SqlScriptRunner.ScriptRunner runner = new ScriptRunner(script, null);
                        if (buildArguments.Transactional)
                        {
                            if (connection.State == ConnectionState.Closed) connection.Open();
                            System.Data.IDbTransaction transaction = null;
                            if (buildArguments.Transactional) transaction = connection.BeginTransaction();
                            try
                            {

                                runner.Execute(connection, transaction);
                                if (buildArguments.Transactional) transaction.Commit();
                                log.Info("Success:" + file);
                            }
                            catch (Exception e)
                            {
                                log.Info("Fail [In Trx:" + buildArguments.Transactional + "]:" + file);
                                log.Fatal(e);
                                if (buildArguments.Transactional) transaction.Rollback();
                                throw;
                            }
                        }
                        else
                        {
                            runner.Execute(connection);
                        }
                    }
                    catch (Exception e)
                    {
                        if (buildArguments.BreakOnError)
                        {
                            throw;
                        }
                        else
                        {
                            log.Debug("There was an error with a script, since BreakOnError is false, we will continue.File:" + file, e);
                        }
                    }
                }
            }
            log.Info("Done Executing");
            return (from f in Files select f.Value).ToArray();
        }