From b1173193cce2581b22f6c0f6c62cedb01d046194 Mon Sep 17 00:00:00 2001 From: Louis DeJardin Date: Mon, 15 Feb 2010 15:16:45 -0800 Subject: [PATCH] Finishing aggressive data scrubbing on setup Existing file destroyed for lightweight databases Tables dropped and recreated for client/server databases This is "setup" only scenario. Recreating an orchard site on a new database will need some alternate "attach" mechanism - orthogonal to "setup" mechanism --HG-- branch : dev --- .../Controllers/SetupController.cs | 5 +---- src/Orchard/Data/Builders/AbstractBuilder.cs | 10 ++++++--- .../Data/Builders/ISessionFactoryBuilder.cs | 8 ++++--- src/Orchard/Data/Builders/SQLiteBuilder.cs | 19 ++++++++++------ .../Data/Builders/SessionFactoryBuilder.cs | 10 ++++----- src/Orchard/Data/Builders/SqlServerBuilder.cs | 2 +- src/Orchard/Data/SessionFactoryHolder.cs | 22 +++++++++++++++---- .../Configuration/ShellSettingsLoader.cs | 7 ++++-- 8 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Setup/Controllers/SetupController.cs b/src/Orchard.Web/Modules/Orchard.Setup/Controllers/SetupController.cs index 200bafb14..2704119aa 100644 --- a/src/Orchard.Web/Modules/Orchard.Setup/Controllers/SetupController.cs +++ b/src/Orchard.Web/Modules/Orchard.Setup/Controllers/SetupController.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Web.Mvc; using Orchard.Comments.Models; using Orchard.ContentManagement; @@ -7,7 +6,6 @@ using Orchard.Core.Common.Models; using Orchard.Core.Navigation.Models; using Orchard.Core.Settings.Models; using Orchard.Data; -using Orchard.Data.Builders; using Orchard.Environment; using Orchard.Environment.Configuration; using Orchard.Security; @@ -15,7 +13,6 @@ using Orchard.Settings; using Orchard.Setup.ViewModels; using Orchard.Localization; using Orchard.UI.Notify; -using MenuItem=Orchard.Core.Navigation.Models.MenuItem; namespace Orchard.Setup.Controllers { public class SetupController : Controller { @@ -74,7 +71,7 @@ namespace Orchard.Setup.Controllers { try { // initialize database before the transaction is created var sessionFactoryHolder = finiteEnvironment.Resolve(); - sessionFactoryHolder.UpdateSchema(); + sessionFactoryHolder.CreateDatabase(); // create superuser diff --git a/src/Orchard/Data/Builders/AbstractBuilder.cs b/src/Orchard/Data/Builders/AbstractBuilder.cs index fb4dad706..b90768ed4 100644 --- a/src/Orchard/Data/Builders/AbstractBuilder.cs +++ b/src/Orchard/Data/Builders/AbstractBuilder.cs @@ -15,10 +15,10 @@ using Orchard.Environment; namespace Orchard.Data.Builders { public abstract class AbstractBuilder { - protected abstract IPersistenceConfigurer GetPersistenceConfigurer(); + protected abstract IPersistenceConfigurer GetPersistenceConfigurer(bool createDatabase); public ISessionFactory BuildSessionFactory(SessionFactoryParameters parameters) { - var database = GetPersistenceConfigurer(); + var database = GetPersistenceConfigurer(parameters.CreateDatabase); var persistenceModel = CreatePersistenceModel(parameters.RecordDescriptors); var sessionFactory = Fluently.Configure() @@ -31,7 +31,11 @@ namespace Orchard.Data.Builders { } private static void Initialization(SessionFactoryParameters parameters, Configuration configuration) { - if (parameters.UpdateSchema) { + if (parameters.CreateDatabase) { + var export = new SchemaExport(configuration); + export.Execute(false/*script*/, true/*export*/, false/*justDrop*/); + } + else if (parameters.UpdateSchema) { var update = new SchemaUpdate(configuration); update.Execute(false/*script*/, true /*doUpdate*/); } diff --git a/src/Orchard/Data/Builders/ISessionFactoryBuilder.cs b/src/Orchard/Data/Builders/ISessionFactoryBuilder.cs index 6ff177f62..feb9b1ebe 100644 --- a/src/Orchard/Data/Builders/ISessionFactoryBuilder.cs +++ b/src/Orchard/Data/Builders/ISessionFactoryBuilder.cs @@ -9,11 +9,13 @@ namespace Orchard.Data.Builders { } public class SessionFactoryParameters { - public IEnumerable RecordDescriptors { get; set; } - public bool UpdateSchema { get; set; } - public string Provider { get; set; } public string DataFolder { get; set; } public string ConnectionString { get; set; } + + public bool CreateDatabase { get; set; } + public bool UpdateSchema { get; set; } + + public IEnumerable RecordDescriptors { get; set; } } } diff --git a/src/Orchard/Data/Builders/SQLiteBuilder.cs b/src/Orchard/Data/Builders/SQLiteBuilder.cs index 126c843eb..f1f9bc7eb 100644 --- a/src/Orchard/Data/Builders/SQLiteBuilder.cs +++ b/src/Orchard/Data/Builders/SQLiteBuilder.cs @@ -11,20 +11,25 @@ namespace Orchard.Data.Builders { _connectionString = connectionString; } - protected override IPersistenceConfigurer GetPersistenceConfigurer() { + protected override IPersistenceConfigurer GetPersistenceConfigurer(bool createDatabase) { var persistence = SQLiteConfiguration.Standard; if (string.IsNullOrEmpty(_connectionString)) { - - if (!Directory.Exists(_dataFolder)) - Directory.CreateDirectory(_dataFolder); + var dataFile = Path.Combine(_dataFolder, "Orchard.db"); - persistence = persistence.UsingFile(Path.Combine(_dataFolder, "Orchard.db")); + if (!Directory.Exists(_dataFolder)) { + Directory.CreateDirectory(_dataFolder); + } + + if (createDatabase && File.Exists(dataFile)) { + File.Delete(dataFile); + } + + persistence = persistence.UsingFile(dataFile); } else { persistence = persistence.ConnectionString(_connectionString); } return persistence; } - } -} \ No newline at end of file +} diff --git a/src/Orchard/Data/Builders/SessionFactoryBuilder.cs b/src/Orchard/Data/Builders/SessionFactoryBuilder.cs index 2f2307eb2..b4b0cf7a1 100644 --- a/src/Orchard/Data/Builders/SessionFactoryBuilder.cs +++ b/src/Orchard/Data/Builders/SessionFactoryBuilder.cs @@ -3,15 +3,15 @@ using NHibernate; namespace Orchard.Data.Builders { public class SessionFactoryBuilder : ISessionFactoryBuilder { - public ISessionFactory BuildSessionFactory(SessionFactoryParameters sessionFactoryParameters) { + public ISessionFactory BuildSessionFactory(SessionFactoryParameters parameters) { AbstractBuilder builder; - if (string.Equals(sessionFactoryParameters.Provider, "SQLite", StringComparison.InvariantCultureIgnoreCase)) { - builder = new SQLiteBuilder(sessionFactoryParameters.DataFolder, sessionFactoryParameters.ConnectionString); + if (string.Equals(parameters.Provider, "SQLite", StringComparison.InvariantCultureIgnoreCase)) { + builder = new SQLiteBuilder(parameters.DataFolder, parameters.ConnectionString); } else { - builder = new SqlServerBuilder(sessionFactoryParameters.DataFolder, sessionFactoryParameters.ConnectionString); + builder = new SqlServerBuilder(parameters.DataFolder, parameters.ConnectionString); } - return builder.BuildSessionFactory(sessionFactoryParameters); + return builder.BuildSessionFactory(parameters); } } } diff --git a/src/Orchard/Data/Builders/SqlServerBuilder.cs b/src/Orchard/Data/Builders/SqlServerBuilder.cs index c4d15719a..7b51b24a0 100644 --- a/src/Orchard/Data/Builders/SqlServerBuilder.cs +++ b/src/Orchard/Data/Builders/SqlServerBuilder.cs @@ -12,7 +12,7 @@ namespace Orchard.Data.Builders { } - protected override IPersistenceConfigurer GetPersistenceConfigurer() { + protected override IPersistenceConfigurer GetPersistenceConfigurer(bool createDatabase) { var persistence = MsSqlConfiguration.MsSql2008; if (string.IsNullOrEmpty(_connectionString)) { throw new NotImplementedException(); diff --git a/src/Orchard/Data/SessionFactoryHolder.cs b/src/Orchard/Data/SessionFactoryHolder.cs index 14f51d0bd..6b64a600a 100644 --- a/src/Orchard/Data/SessionFactoryHolder.cs +++ b/src/Orchard/Data/SessionFactoryHolder.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using NHibernate; using Orchard.Data.Builders; using Orchard.Environment; @@ -8,6 +9,7 @@ using Orchard.Logging; namespace Orchard.Data { public interface ISessionFactoryHolder : ISingletonDependency { ISessionFactory GetSessionFactory(); + void CreateDatabase(); void UpdateSchema(); } @@ -33,6 +35,17 @@ namespace Orchard.Data { public ILogger Logger { get; set; } + public void CreateDatabase() { + lock (this) { + if (_sessionFactory != null) { + Logger.Error("CreateSchema can not be called after a session factory was created"); + throw new OrchardException("CreateSchema can not be called after a session factory was created"); + } + + _sessionFactory = BuildSessionFactory(true /*createDatabase*/, false /*updateSchema*/); + } + } + public void UpdateSchema() { lock (this) { if (_sessionFactory != null) { @@ -40,20 +53,20 @@ namespace Orchard.Data { throw new OrchardException("UpdateSchema can not be called after a session factory was created"); } - _sessionFactory = BuildSessionFactory(true); + _sessionFactory = BuildSessionFactory(false /*createDatabase*/, true /*updateSchema*/); } } public ISessionFactory GetSessionFactory() { lock (this) { if (_sessionFactory == null) { - _sessionFactory = BuildSessionFactory(false); + _sessionFactory = BuildSessionFactory(false /*createDatabase*/, false /*updateSchema*/); } } return _sessionFactory; } - private ISessionFactory BuildSessionFactory(bool updateSchema) { + private ISessionFactory BuildSessionFactory(bool createDatabase, bool updateSchema) { Logger.Debug("Building session factory"); var shellPath = _appDataFolder.CreateDirectory(Path.Combine("Sites", _shellSettings.Name)); @@ -62,6 +75,7 @@ namespace Orchard.Data { Provider = _shellSettings.DataProvider, DataFolder = shellPath, ConnectionString = _shellSettings.DataConnectionString, + CreateDatabase = createDatabase, UpdateSchema = updateSchema, RecordDescriptors = _compositionStrategy.GetRecordDescriptors(), }); diff --git a/src/Orchard/Environment/Configuration/ShellSettingsLoader.cs b/src/Orchard/Environment/Configuration/ShellSettingsLoader.cs index 7fabb3d71..81a7a8091 100644 --- a/src/Orchard/Environment/Configuration/ShellSettingsLoader.cs +++ b/src/Orchard/Environment/Configuration/ShellSettingsLoader.cs @@ -43,10 +43,13 @@ namespace Orchard.Environment.Configuration { } IEnumerable LoadFiles() { - var sitePaths = _appDataFolder.ListDirectories("Sites"); + var sitePaths = _appDataFolder + .ListDirectories("Sites") + .SelectMany(path => _appDataFolder.ListFiles(path)) + .Where(path => string.Equals(Path.GetFileName(path), "Settings.txt", StringComparison.OrdinalIgnoreCase)); foreach (var sitePath in sitePaths) { - var yamlStream = YamlParser.Load(_appDataFolder.MapPath(Path.Combine(sitePath, "Settings.txt"))); + var yamlStream = YamlParser.Load(_appDataFolder.MapPath(sitePath)); yield return yamlStream.Documents.Single(); } }