示例#1
0
        public ActionResult ExecuteUpdate()
        {
            if (User.Identity.IsAuthenticated && User.HasAccess("ADMIN"))
            {
                // Execute all incremental updates in a transaction.
                using (IDbTransaction tx = Database.OpenTransaction()) {
                    for (int n = Data.Database.InstalledVersion + 1; n <= Data.Database.CurrentVersion; n++)
                    {
                        // Read embedded create script
                        Stream str = Assembly.GetExecutingAssembly().GetManifestResourceStream(Database.ScriptRoot + ".Updates." +
                                                                                               n.ToString() + ".sql");
                        String sql = new StreamReader(str).ReadToEnd();
                        str.Close();

                        // Split statements and execute
                        string[] stmts = sql.Split(new char[] { ';' });
                        foreach (string stmt in stmts)
                        {
                            if (!String.IsNullOrEmpty(stmt.Trim()))
                            {
                                SysUser.Execute(stmt.Trim(), tx);
                            }
                        }

                        // Check for update class
                        var utype = Type.GetType("Piranha.Data.Updates.Update" + n.ToString());
                        if (utype != null)
                        {
                            IUpdate update = (IUpdate)Activator.CreateInstance(utype);
                            update.Execute(tx);
                        }
                    }
                    // Now lets update the database version.
                    SysUser.Execute("UPDATE sysparam SET sysparam_value = @0 WHERE sysparam_name = 'SITE_VERSION'",
                                    tx, Data.Database.CurrentVersion);
                    SysParam.InvalidateParam("SITE_VERSION");
                    tx.Commit();
                }
                return(RedirectToAction("index", "account"));
            }
            else
            {
                return(RedirectToAction("update"));
            }
        }
示例#2
0
        public ActionResult Create(InstallModel m)
        {
            if (m.InstallType == "SCHEMA" || ModelState.IsValid)
            {
                // Read embedded create script
                Stream str = Assembly.GetExecutingAssembly().GetManifestResourceStream(Database.ScriptRoot + ".Create.sql");
                String sql = new StreamReader(str).ReadToEnd();
                str.Close();

                // Read embedded data script
                str = Assembly.GetExecutingAssembly().GetManifestResourceStream(Database.ScriptRoot + ".Data.sql");
                String data = new StreamReader(str).ReadToEnd();
                str.Close();

                // Split statements and execute
                string[] stmts = sql.Split(new char[] { ';' });
                using (IDbTransaction tx = Database.OpenTransaction()) {
                    // Create database from script
                    foreach (string stmt in stmts)
                    {
                        if (!String.IsNullOrEmpty(stmt.Trim()))
                        {
                            SysUser.Execute(stmt, tx);
                        }
                    }
                    tx.Commit();
                }

                if (m.InstallType.ToUpper() == "FULL")
                {
                    // Split statements and execute
                    stmts = data.Split(new char[] { ';' });
                    using (IDbTransaction tx = Database.OpenTransaction()) {
                        // Create user
                        SysUser usr = new SysUser()
                        {
                            Login     = m.UserLogin,
                            Email     = m.UserEmail,
                            GroupId   = new Guid("7c536b66-d292-4369-8f37-948b32229b83"),
                            CreatedBy = new Guid("ca19d4e7-92f0-42f6-926a-68413bbdafbc"),
                            UpdatedBy = new Guid("ca19d4e7-92f0-42f6-926a-68413bbdafbc"),
                            Created   = DateTime.Now,
                            Updated   = DateTime.Now
                        };
                        usr.Save(tx);

                        // Create user password
                        SysUserPassword pwd = new SysUserPassword()
                        {
                            Id       = usr.Id,
                            Password = m.Password,
                            IsNew    = false
                        };
                        pwd.Save(tx);

                        // Create default data
                        foreach (string stmt in stmts)
                        {
                            if (!String.IsNullOrEmpty(stmt.Trim()))
                            {
                                SysUser.Execute(stmt, tx);
                            }
                        }
                        tx.Commit();
                    }
                }
                return(RedirectToAction("index", "account"));
            }
            return(Index());
        }