public static void EnsureDatabaseExists() { var builder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["CMS"].ConnectionString); var sqlScriptsPath = FindSqlScriptsPath(); builder.InitialCatalog = "master"; var masterConnectionString = builder.ConnectionString; builder.InitialCatalog = $"CMSi_{Host}"; var imageConnectionString = builder.ConnectionString; builder.InitialCatalog = "ELMAH"; var elmahConnectionString = builder.ConnectionString; builder.InitialCatalog = $"CMS_{Host}"; var standardConnectionString = builder.ConnectionString; if (!DbUtil.DatabaseExists(masterConnectionString, $"CMS_{Host}")) { var result = DbUtil.CreateDatabase(Host, sqlScriptsPath, masterConnectionString, imageConnectionString, elmahConnectionString, standardConnectionString); if (!string.IsNullOrEmpty(result)) { throw new Exception(result); } } }
public DatabaseFixture() { Items = new Dictionary <string, object>(); var c = FakeHttpContext(); HttpContextFactory.SetCurrentContext(c); if (BuildDb) { var csMaster = Util.GetConnectionString2("master"); var csElmah = Util.GetConnectionString2("elmah"); var scriptsDir = ScriptsDirectory(); if (DropDb) { var cn = new SqlConnection(csMaster); cn.Execute(@"drop database if exists CMS_test"); } DbUtil.CreateDatabase( Util.Host, scriptsDir, csMaster, Util.ConnectionStringImage, csElmah, Util.ConnectionString); } }
protected void Application_BeginRequest(object sender, EventArgs e) { if (ShouldBypassProcessing()) { return; } if (Util.AppOffline) { Response.Redirect("/Errors/AppOffline.htm"); return; } // MiniProfiler.Start(); var r = DbUtil.CheckDatabaseExists(Util.CmsHost); var redirect = ViewExtensions2.DatabaseErrorUrl(r); #if DEBUG if (r == DbUtil.CheckDatabaseResult.ServerNotFound) { Response.Redirect(redirect); return; } if (r == DbUtil.CheckDatabaseResult.DatabaseDoesNotExist && HttpContext.Current.Request.Url.LocalPath.EndsWith("/")) { var ret = DbUtil.CreateDatabase(); if (ret.HasValue()) { Response.Redirect($"/Errors/DatabaseCreationError.aspx?error={HttpUtility.UrlEncode(ret)}"); return; } } #else if (redirect != null) { Response.Redirect(redirect); return; } #endif try { Util.AdminMail = DbUtil.Db.Setting("AdminMail", ""); Util.DateSimulation = DbUtil.Db.Setting("UseDateSimulation"); } catch (SqlException) { throw; //Response.Redirect($"/Errors/DatabaseNotInitialized.aspx?dbname={Util.Host}"); } var cul = DbUtil.Db.Setting("Culture", "en-US"); Util.Culture = cul; Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul); }
protected void Application_BeginRequest(object sender, EventArgs e) { var url = Request.Url.OriginalString; if (url.Contains("/Errors/") || url.Contains("healthcheck.txt")) { return; } if (Util.AppOffline) { Response.Redirect("/Errors/AppOffline.htm"); return; } var r = DbUtil.CheckDatabaseExists(Util.CmsHost); var redirect = ViewExtensions2.DatabaseErrorUrl(r); #if DEBUG if (r == DbUtil.CheckDatabaseResult.ServerNotFound) { Response.Redirect(redirect); return; } if (r == DbUtil.CheckDatabaseResult.DatabaseDoesNotExist && HttpContext.Current.Request.Url.LocalPath.EndsWith("/")) { var ret = DbUtil.CreateDatabase(); if (ret.HasValue()) { Response.Redirect("/Errors/DatabaseCreationError.aspx?error=" + HttpUtility.UrlEncode(ret)); return; } } #else if (redirect != null) { Response.Redirect(redirect); return; } #endif try { Util.AdminMail = DbUtil.Db.Setting("AdminMail", ""); } catch (SqlException) { Response.Redirect("/Errors/DatabaseNotInitialized.aspx?dbname=" + Util.Host); } var cul = DbUtil.Db.Setting("Culture", "en-US"); Util.Culture = cul; Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul); }
protected void Application_BeginRequest(object sender, EventArgs e) { var url = Request.Url.OriginalString; if (url.Contains("/Errors/") || url.Contains("healthcheck.txt")) { return; } if (Util.AppOffline) { Response.Redirect("/Errors/AppOffline.htm"); return; } if (!DbUtil.CmsDatabaseExists()) { #if DEBUG var r = DbUtil.CheckDatabaseExists(Util.Host); if (r == DbUtil.CheckDatabaseResult.ServerNotFound) { Response.Redirect("/Errors/DatabaseServerNotFound.aspx?server=" + Util.DbServer); return; } var ret = DbUtil.CreateDatabase(); if (ret.HasValue()) { Response.Redirect("/Errors/DatabaseCreationError.aspx?error=" + HttpUtility.UrlEncode(ret)); return; } #else Response.Redirect("/Errors/DatabaseNotFound.aspx?dbname=" + Util.Host); return; #endif } try { Util.AdminMail = DbUtil.Db.Setting("AdminMail", ""); } catch (SqlException) { Response.Redirect("/Errors/DatabaseNotInitialized.aspx?dbname=" + Util.Host); } var cul = DbUtil.Db.Setting("Culture", "en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul); }
protected void Application_BeginRequest(object sender, EventArgs e) { if (IsRackspaceMonitoring()) { Response.StatusCode = (int)HttpStatusCode.OK; Response.ContentType = "text/plain"; Response.Write("OK"); CompleteRequest(); return; } if (HandleBvcmsDomain()) { return; } if (ShouldBypassProcessing()) { return; } Response.Headers?.Remove("Server"); if (Util.AppOffline) { Response.Redirect("/Errors/AppOffline.htm"); return; } var host = CMSDataContext.GetHost(new HttpContextWrapper(Context)); var r = DbUtil.CheckDatabaseExists($"CMS_{host}"); var redirect = ViewExtensions2.DatabaseErrorUrl(r); if (Util.IsDebug()) { if (r == DbUtil.CheckDatabaseResult.ServerNotFound) { Response.Redirect(redirect); return; } if (r == DbUtil.CheckDatabaseResult.DatabaseDoesNotExist && "localhost".Equal(Request.Url.Host)) { var ret = DbUtil.CreateDatabase(host); if (ret.HasValue()) { Response.Redirect($"/Errors/DatabaseCreationError.aspx?error={HttpUtility.UrlEncode(ret)}"); return; } } } else { if (redirect != null) { Response.Redirect(redirect); return; } } var db = CMSDataContext.Create(new HttpContextWrapper(Context)); Util.AdminMail = db.Setting("AdminMail", ""); Util.DateSimulation = db.Setting("UseDateSimulation"); var cul = db.Setting("Culture", "en-US"); Util.Culture = cul; Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul); var checkip = ConfigurationManager.AppSettings["CheckIp"]; if (Util.IsHosted && checkip.HasValue()) { if (1 == db.Connection.ExecuteScalar <int>(checkip, new { ip = Request.UserHostAddress })) { Response.Redirect("/Errors/AccessDenied.htm"); } } ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; }