Example #1
0
 public Stream loadPatch(PatchId patchId)
 {
     return configuration.loadPatch(patchId);
 }
Example #2
0
 public static Stream loadPatch(PatchId patchId)
 {
     return Resources.ResourcesManager.GetResource(String.Format("Patch_{0:D5}_{1}.xml", patchId.version, patchId.name));
 }
Example #3
0
        private static AbstractPatch _LoadById(PatchId id, Context context)
        {
            XDocument data;
            using(Stream xmlStream = context.loadPatch(id))
            {
                using(XmlReader reader = XmlReader.Create(xmlStream))
                {
                    data = XDocument.Load(reader);
                }
            }
            Checker.Check(data);
            XElement version = data.Root.Element("version");
            string number = version.Element("number").Value;
            string author = version.Element("author").Value;
            if((number != id.version.ToString()) || (author != id.name))
            {
                throw new ApplicationException(string.Format("Versions mismatch on patch #{0} from {1} (got #{2} from {3})", id.version, id.name, number, author));
            }

            HashSet<string> restrictToEnvironments;
            if(data.Root.Element("restrictToEnvironments") != null)
            {
                restrictToEnvironments = new HashSet<string>(from elem in data.Root.Element("restrictToEnvironments").Elements() select elem.Value);
            } else
            {
                restrictToEnvironments = new HashSet<string>();
            }

            XElement commandSet;
            bool isStrictCommandSet;
            {
                XElement strictCommandSet = data.Root.Element("strictCommandSet");
                XElement looseCommandSet = data.Root.Element("looseCommandSet");
                if (strictCommandSet != null && looseCommandSet == null)
                {
                    commandSet = strictCommandSet;
                    isStrictCommandSet = true;
                } else if (strictCommandSet == null && looseCommandSet != null)
                {
                    commandSet = looseCommandSet;
                    isStrictCommandSet = false;
                } else if (strictCommandSet != null && looseCommandSet != null)
                {
                    throw new ApplicationException("Malformed XML: both strictCommandSet and looseCommandSet found");
                } else
                {
                    throw new ApplicationException("Malformed XML: no CommandSet found");
                }
            }

            var commands = (from elem in commandSet.Elements() select AbstractCommand.Create(elem)).ToArray();

            if(isStrictCommandSet)
            {
                var isPersistent = commands.OfType<AbstractPersistentCommand>().Any();
                if (isPersistent)
                {
                    if (commands.Length != 1)
                    {
                        throw new ApplicationException("More than one persistent command");
                    }
                    if(context.DbDriver.IsDDLTransactional)
                    {
                        return new AtomicPatch(commands, true, restrictToEnvironments, context);
                    } else
                    {
                        return new PersistentPatch(commands[0], restrictToEnvironments, context);
                    }
                }
                else
                {
                    return new AtomicPatch(commands, true, restrictToEnvironments, context);
                }
            } else
            {
                return new AtomicPatch(commands, false, restrictToEnvironments, context);
            }
        }
Example #4
0
 public static AbstractPatch LoadById(PatchId id, Context context)
 {
     return Cache<AbstractPatch>.instance.get(new KeyValuePair<PatchId, Context>(id, context), () => _LoadById(id, context));
 }
Example #5
0
 public Stream loadPatch(PatchId patchId)
 {
     return this.updateParams.loadPatch(patchId);
 }
Example #6
0
        private void InstallPatch(PatchId patchId)
        {
            AbstractPatch patch = AbstractPatch.LoadById(patchId, this.context);

            if(!patch.DoesSupportEnvironment(this.context.EnvironmentName))
            {
                this.context.console.Report(" (skipping because of unsupported environment) ");
                return;
            }

            using(var transaction = this.context.CreateTransaction())
            {

                bool patchExistsInDB;
                XDocument rollbackData;

                var patchDBData = transaction.ExecuteReader(
                    string.Format(
                        "select {1} from {0} where {2} = {3} and {4} = {5}",
                        transaction.EscapeName(this.context.PatchesTable),
                        transaction.EscapeName("STATUS"),
                        transaction.EscapeName("VERSION"),
                        transaction.MarkParam("pversion"),
                        transaction.EscapeName("NAME"),
                        transaction.MarkParam("pname")
                    ),
                    new Dictionary<string, object>
                    {
                        { "pversion", patchId.version },
                        { "pname", patchId.name },
                    }
                ).ToList();

                if(patch is PersistentPatch)
                {
                    bool reinstall;

                    if(patchDBData.Any())
                    {
                        var patchInfo = patchDBData.Single();
                        switch(patchInfo["STATUS"])
                        {
                            case STATUS_INSTALLED:
                                throw new ApplicationException("Patch is already installed");
                            case STATUS_INSTALLING:
                                reinstall = true;
                                break;
                            default:
                                throw new ApplicationException(string.Format("Unknown status {0}", patchInfo["STATUS"]));
                        }
                    } else
                    {
                        reinstall = false;
                    }

                    if(reinstall)
                    {
                        patchExistsInDB = true;
                    }

                    throw new NotImplementedException("Persistent patch installation is not implemented yet");
                } else
                {
                    if(patchDBData.Any())
                    {
                        throw new ApplicationException("Patch is already installed");
                    }

                    patchExistsInDB = false;
                    rollbackData = patch.Apply(transaction);
                }

                //System.Threading.Thread.Sleep(1000);
                int affectedRows = transaction.ExecuteNonQuery(
                    String.Format(
                        patchExistsInDB
                            ? "update {0} set {5} = {6}, {7} = {8} where {1} = {2} and {3} = {4}"
                            : "insert into {0}({1}, {3}, {5}, {7}) values({2}, {4}, {6}, {8})"
                        ,
                        transaction.EscapeName(this.context.PatchesTable),
                        transaction.EscapeName("VERSION"),
                        transaction.MarkParam("pversion"),
                        transaction.EscapeName("NAME"),
                        transaction.MarkParam("pname"),
                        transaction.EscapeName("ROLLBACK_DATA"),
                        transaction.MarkParam("prollbackdata"),
                        transaction.EscapeName("STATUS"),
                        transaction.MarkParam("pstatus")
                    ),
                    new Dictionary<string, object>
                    {
                        { "pversion", patchId.version },
                        { "pname", patchId.name },
                        { "prollbackdata", rollbackData.ToString() },
                        { "pstatus", STATUS_INSTALLED },
                    }
                );
                if(affectedRows != 1)
                {
                    throw new ApplicationException("Unable to install patch; are you trying to run two patchers simultaneously?");
                }
                transaction.Commit();
            }
        }
Example #7
0
 private void UninstallPatch(PatchId patchId)
 {
     using(var transaction = this.context.CreateTransaction())
     {
         AbstractPatch patch = AbstractPatch.LoadById(patchId, context);
         var patchInstallInfo = transaction.ExecuteReader(
             String.Format(
                 "select {1} from {0} where {2} = {4} and {3} = {5} for update",
                 transaction.EscapeName(this.context.PatchesTable),
                 transaction.EscapeName("ROLLBACK_DATA"),
                 transaction.EscapeName("VERSION"),
                 transaction.EscapeName("NAME"),
                 transaction.MarkParam("pversion"),
                 transaction.MarkParam("pname")
             ),
             new Dictionary<string, object>
             {
                 { "pversion", patchId.version },
                 { "pname", patchId.name },
             }
         ).Single();
         patch.Rollback(transaction, XDocument.Parse(patchInstallInfo["ROLLBACK_DATA"]));
         int affectedRows = transaction.ExecuteNonQuery(
             String.Format(
                 "delete from {0} where {1} = {3} and {2} = {4}",
                 transaction.EscapeName(this.context.PatchesTable),
                 transaction.EscapeName("VERSION"),
                 transaction.EscapeName("NAME"),
                 transaction.MarkParam("pversion"),
                 transaction.MarkParam("pname")
             ),
             new Dictionary<string, object>
             {
                 { "pversion", patchId.version },
                 { "pname", patchId.name },
             }
         );
         if(affectedRows != 1)
         {
             throw new ApplicationException("Unable to install patch; are you trying to run two patchers simultaneously?");
         }
         transaction.Commit();
     }
 }