From 788ca7d69feef87b42280b983f5ad25cc5ef96e5 Mon Sep 17 00:00:00 2001 From: Andre Rodrigues Date: Thu, 30 Sep 2010 16:29:29 -0700 Subject: [PATCH] Created migration module and moved controller and view from experimental. --HG-- branch : dev --- .../Commands/CodeGenerationCommandsTests.cs | 63 ++++++++- .../SchemaCommandGeneratorTests.cs | 2 +- .../Commands/CodeGenerationCommands.cs | 15 ++- .../Orchard.Experimental.csproj | 1 - .../Views/Home/Index.aspx | 5 +- .../Controllers/DatabaseUpdateController.cs | 40 ++++++ .../Modules/Orchard.Migrations/Module.txt | 11 ++ .../Orchard.Migrations.csproj | 121 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 33 +++++ .../Views/DatabaseUpdate/Index.aspx | 3 +- .../Orchard.Migrations/Views/Web.config | 35 +++++ .../Modules/Orchard.Migrations/Web.config | 32 +++++ src/Orchard.sln | 13 ++ 13 files changed, 357 insertions(+), 17 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.Migrations/Controllers/DatabaseUpdateController.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Migrations/Module.txt create mode 100644 src/Orchard.Web/Modules/Orchard.Migrations/Orchard.Migrations.csproj create mode 100644 src/Orchard.Web/Modules/Orchard.Migrations/Properties/AssemblyInfo.cs rename src/Orchard.Web/Modules/{Orchard.Experimental => Orchard.Migrations}/Views/DatabaseUpdate/Index.aspx (86%) create mode 100644 src/Orchard.Web/Modules/Orchard.Migrations/Views/Web.config create mode 100644 src/Orchard.Web/Modules/Orchard.Migrations/Web.config diff --git a/src/Orchard.Tests.Modules/CodeGeneration/Commands/CodeGenerationCommandsTests.cs b/src/Orchard.Tests.Modules/CodeGeneration/Commands/CodeGenerationCommandsTests.cs index 9af5ea58f..5cbed1d4d 100644 --- a/src/Orchard.Tests.Modules/CodeGeneration/Commands/CodeGenerationCommandsTests.cs +++ b/src/Orchard.Tests.Modules/CodeGeneration/Commands/CodeGenerationCommandsTests.cs @@ -1,15 +1,68 @@ -using NUnit.Framework; +using System.Collections.Generic; +using System.IO; +using Autofac; +using Autofac.Features.Metadata; +using NUnit.Framework; using Orchard.CodeGeneration.Commands; +using Orchard.Commands; +using Orchard.Data; +using Orchard.Data.Migration.Generator; +using Orchard.Data.Providers; +using Orchard.Environment.Configuration; using Orchard.Environment.Extensions; +using Orchard.Environment.ShellBuilders; +using Orchard.Environment.ShellBuilders.Models; +using Orchard.FileSystems.AppData; +using Orchard.Localization; +using Orchard.Tests.FileSystems.AppData; namespace Orchard.Tests.Modules.CodeGeneration.Commands { [TestFixture] public class CodeGenerationCommandsTests { + private IContainer _container; + private IExtensionManager _extensionManager; + private ISchemaCommandGenerator _schemaCommandGenerator; + + [SetUp] + public void Init() { + string databaseFileName = Path.GetTempFileName(); + IDataServicesProviderFactory dataServicesProviderFactory = new DataServicesProviderFactory(new[] { + new Meta( + (dataFolder, connectionString) => new SqlCeDataServicesProvider(dataFolder, connectionString), + new Dictionary {{"ProviderName", "SqlCe"}}) + }); + + var builder = new ContainerBuilder(); + + builder.RegisterInstance(new ShellBlueprint()); + builder.RegisterInstance(new ShellSettings { Name = "Default", DataTablePrefix = "Test", DataProvider = "SqlCe" }); + builder.RegisterInstance(dataServicesProviderFactory).As(); + builder.RegisterInstance(AppDataFolderTests.CreateAppDataFolder(Path.GetDirectoryName(databaseFileName))).As(); + + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + + _container = builder.Build(); + _extensionManager = _container.Resolve(); + _schemaCommandGenerator = _container.Resolve(); + } + [Test] - public void CreateDataMigrationTest() { - //ExtensionManager extensionManager = new ExtensionManager(); - //CodeGenerationCommands codeGenerationCommands = new CodeGenerationCommands(); + public void CreateDataMigrationTestUnexistentFeature() { + CodeGenerationCommands codeGenerationCommands = new CodeGenerationCommands(_extensionManager, + _schemaCommandGenerator); + + TextWriter textWriterOutput = new StringWriter(); + codeGenerationCommands.Context = new CommandContext { Output = textWriterOutput }; + bool result = codeGenerationCommands.CreateDataMigration("feature"); + + Assert.That(result, Is.False); + Assert.That(textWriterOutput.ToString(), Is.EqualTo("Creating Data Migration for feature\r\nCreating data migration failed: target Feature feature could not be found.\r\n")); } } -} +} \ No newline at end of file diff --git a/src/Orchard.Tests/DataMigration/SchemaCommandGeneratorTests.cs b/src/Orchard.Tests/DataMigration/SchemaCommandGeneratorTests.cs index 15ac31daf..e4f3cd05c 100644 --- a/src/Orchard.Tests/DataMigration/SchemaCommandGeneratorTests.cs +++ b/src/Orchard.Tests/DataMigration/SchemaCommandGeneratorTests.cs @@ -1,5 +1,5 @@ #if REFACTORING -#error This must move to the Modules tests to accomodateOrchard.Experimental assembly reference +#error This must move to the Modules tests to accomodate Orchard.Experimental assembly reference using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; diff --git a/src/Orchard.Web/Modules/Orchard.CodeGeneration/Commands/CodeGenerationCommands.cs b/src/Orchard.Web/Modules/Orchard.CodeGeneration/Commands/CodeGenerationCommands.cs index 0e6b0594f..bc8e72d5e 100644 --- a/src/Orchard.Web/Modules/Orchard.CodeGeneration/Commands/CodeGenerationCommands.cs +++ b/src/Orchard.Web/Modules/Orchard.CodeGeneration/Commands/CodeGenerationCommands.cs @@ -17,6 +17,7 @@ namespace Orchard.CodeGeneration.Commands { [OrchardFeature("Generate")] public class CodeGenerationCommands : DefaultOrchardCommandHandler { + private readonly IExtensionManager _extensionManager; private readonly ISchemaCommandGenerator _schemaCommandGenerator; @@ -55,7 +56,7 @@ namespace Orchard.CodeGeneration.Commands { [CommandHelp("generate create datamigration \r\n\t" + "Create a new Data Migration class")] [CommandName("generate create datamigration")] - public void CreateDataMigration(string featureName) { + public bool CreateDataMigration(string featureName) { Context.Output.WriteLine(T("Creating Data Migration for {0}", featureName)); ExtensionDescriptor extensionDescriptor = _extensionManager.AvailableExtensions().FirstOrDefault(extension => extension.ExtensionType == "Module" && @@ -63,7 +64,7 @@ namespace Orchard.CodeGeneration.Commands { if (extensionDescriptor == null) { Context.Output.WriteLine(T("Creating data migration failed: target Feature {0} could not be found.", featureName)); - return; + return false; } string dataMigrationsPath = HostingEnvironment.MapPath("~/Modules/" + extensionDescriptor.Name + "/DataMigrations/"); @@ -77,7 +78,7 @@ namespace Orchard.CodeGeneration.Commands { if (File.Exists(dataMigrationPath)) { Context.Output.WriteLine(T("Data migration already exists in target Module {0}.", extensionDescriptor.Name)); - return; + return false; } List commands = _schemaCommandGenerator.GetCreateFeatureCommands(featureName, false).ToList(); @@ -111,21 +112,25 @@ namespace Orchard.CodeGeneration.Commands { File.WriteAllText(moduleCsProjPath, projectFileText); TouchSolution(Context.Output, T); Context.Output.WriteLine(T("Data migration created successfully in Module {0}", extensionDescriptor.Name)); + + return true; } [CommandHelp("generate create module [/IncludeInSolution:true|false]\r\n\t" + "Create a new Orchard module")] [CommandName("generate create module")] [OrchardSwitches("IncludeInSolution")] - public void CreateModule(string moduleName) { + public bool CreateModule(string moduleName) { Context.Output.WriteLine(T("Creating Module {0}", moduleName)); if ( _extensionManager.AvailableExtensions().Any(extension => String.Equals(moduleName, extension.DisplayName, StringComparison.OrdinalIgnoreCase)) ) { Context.Output.WriteLine(T("Creating Module {0} failed: a module of the same name already exists", moduleName)); - return; + return false; } IntegrateModule(moduleName); Context.Output.WriteLine(T("Module {0} created successfully", moduleName)); + + return true; } [CommandName("generate create theme")] diff --git a/src/Orchard.Web/Modules/Orchard.Experimental/Orchard.Experimental.csproj b/src/Orchard.Web/Modules/Orchard.Experimental/Orchard.Experimental.csproj index bad44aea0..432e27353 100644 --- a/src/Orchard.Web/Modules/Orchard.Experimental/Orchard.Experimental.csproj +++ b/src/Orchard.Web/Modules/Orchard.Experimental/Orchard.Experimental.csproj @@ -101,7 +101,6 @@ - diff --git a/src/Orchard.Web/Modules/Orchard.Experimental/Views/Home/Index.aspx b/src/Orchard.Web/Modules/Orchard.Experimental/Views/Home/Index.aspx index 7b95f214e..542cc385f 100644 --- a/src/Orchard.Web/Modules/Orchard.Experimental/Views/Home/Index.aspx +++ b/src/Orchard.Web/Modules/Orchard.Experimental/Views/Home/Index.aspx @@ -1,7 +1,6 @@ <%@ Page Language="C#" Inherits="Orchard.Mvc.ViewPage" %> <%@ Import Namespace="Orchard.Mvc.ViewModels"%> -

<%: Html.TitleForPage(T("Dev Tools").ToString()) %>

+

<%: Html.TitleForPage(T("Experimental").ToString()) %>

<%: Html.ActionLink(T("Contents").ToString(), "Index", "Content") %>

<%: Html.ActionLink(T("Metadata").ToString(), "Index", "Metadata") %>

-

<%: Html.ActionLink(T("Test Unauthorized Request").ToString(), "NotAuthorized", "Home")%>

-

<%: Html.ActionLink(T("Database Update").ToString(), "Index", "DatabaseUpdate")%>

+

<%: Html.ActionLink(T("Test Unauthorized Request").ToString(), "NotAuthorized", "Home")%>

\ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Migrations/Controllers/DatabaseUpdateController.cs b/src/Orchard.Web/Modules/Orchard.Migrations/Controllers/DatabaseUpdateController.cs new file mode 100644 index 000000000..e30530656 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Migrations/Controllers/DatabaseUpdateController.cs @@ -0,0 +1,40 @@ +using System; +using System.Web.Mvc; +using Orchard.Data.Migration.Generator; +using Orchard.Localization; +using Orchard.UI.Admin; +using Orchard.UI.Notify; + +namespace Orchard.Migrations.Controllers { + + [ValidateInput(false)] + [Admin] + public class DatabaseUpdateController : Controller { + private readonly ISchemaCommandGenerator _schemaCommandGenerator; + + public DatabaseUpdateController(ISchemaCommandGenerator schemaCommandGenerator, IOrchardServices orchardServices) { + _schemaCommandGenerator = schemaCommandGenerator; + Services = orchardServices; + } + + public IOrchardServices Services { get; set; } + public Localizer T { get; set; } + + public ActionResult Index() { + return View(); + } + + public ActionResult UpdateDatabase() { + try { + + _schemaCommandGenerator.UpdateDatabase(); + Services.Notifier.Information(T("Database updated successfuly")); + } + catch (Exception ex) { + Services.Notifier.Error(T("An error occured while updating the database: {0}", ex.Message)); + } + + return RedirectToAction("Index"); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Migrations/Module.txt b/src/Orchard.Web/Modules/Orchard.Migrations/Module.txt new file mode 100644 index 000000000..53ce1c4f0 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Migrations/Module.txt @@ -0,0 +1,11 @@ +Name: Migration module +antiforgery: enabled +author: The Orchard Team +website: http://orchardproject.net +version: 0.1.0 +orchardversion: 0.6.0 +description: +features: + Orchard.Migration: + Description: Database migration action. + Category: Developer \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Migrations/Orchard.Migrations.csproj b/src/Orchard.Web/Modules/Orchard.Migrations/Orchard.Migrations.csproj new file mode 100644 index 000000000..c393d796e --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Migrations/Orchard.Migrations.csproj @@ -0,0 +1,121 @@ + + + + Debug + AnyCPU + + + 2.0 + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1} + {F85E285D-A4E0-4152-9332-AB1D724D3325};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + Orchard.Migrations + Orchard.Migrations + v4.0 + false + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + + + + + + + + 3.5 + + + 3.5 + + + 3.5 + + + False + ..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll + + + 3.5 + + + + 3.5 + + + + + + + + + + + + + + + + + + + + + + + + {2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6} + Orchard.Framework + + + {9916839C-39FC-4CEB-A5AF-89CA7E87119F} + Orchard.Core + + + + + + + + + + + + + False + True + 57727 + / + + + False + False + + + False + + + + + \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Migrations/Properties/AssemblyInfo.cs b/src/Orchard.Web/Modules/Orchard.Migrations/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..5a163cda7 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Migrations/Properties/AssemblyInfo.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Orchard.Migrations")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyProduct("Orchard")] +[assembly: AssemblyCopyright("Copyright © CodePlex Foundation 2009")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("16ce5ce9-bfbd-4edc-8323-e7907f508e07")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("0.5.0")] +[assembly: AssemblyFileVersion("0.5.0")] \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Experimental/Views/DatabaseUpdate/Index.aspx b/src/Orchard.Web/Modules/Orchard.Migrations/Views/DatabaseUpdate/Index.aspx similarity index 86% rename from src/Orchard.Web/Modules/Orchard.Experimental/Views/DatabaseUpdate/Index.aspx rename to src/Orchard.Web/Modules/Orchard.Migrations/Views/DatabaseUpdate/Index.aspx index 19cbc5519..3f3a3fa8d 100644 --- a/src/Orchard.Web/Modules/Orchard.Experimental/Views/DatabaseUpdate/Index.aspx +++ b/src/Orchard.Web/Modules/Orchard.Migrations/Views/DatabaseUpdate/Index.aspx @@ -1,5 +1,4 @@ <%@ Page Language="C#" Inherits="Orchard.Mvc.ViewPage"%> <%@ Import Namespace="Orchard.Mvc.ViewModels"%>

<%: Html.TitleForPage(T("Data Migration").ToString()) %>

-

<%: Html.ActionLink(T("Update Database").ToString(), "UpdateDatabase", "DatabaseUpdate") %>

- +

<%: Html.ActionLink(T("Update Database").ToString(), "UpdateDatabase", "DatabaseUpdate") %>

\ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Migrations/Views/Web.config b/src/Orchard.Web/Modules/Orchard.Migrations/Views/Web.config new file mode 100644 index 000000000..c397ca482 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Migrations/Views/Web.config @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Orchard.Web/Modules/Orchard.Migrations/Web.config b/src/Orchard.Web/Modules/Orchard.Migrations/Web.config new file mode 100644 index 000000000..97c1c726a --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Migrations/Web.config @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Orchard.sln b/src/Orchard.sln index d015f6a09..9d386c948 100644 --- a/src/Orchard.sln +++ b/src/Orchard.sln @@ -84,6 +84,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.CodeGeneration", "O EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.Experimental", "Orchard.Web\Modules\Orchard.Experimental\Orchard.Experimental.csproj", "{AB3C207C-0126-4143-8D62-1119DF80D366}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.Migrations", "Orchard.Web\Modules\Orchard.Migrations\Orchard.Migrations.csproj", "{EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution CodeCoverage|Any CPU = CodeCoverage|Any CPU @@ -453,6 +455,16 @@ Global {AB3C207C-0126-4143-8D62-1119DF80D366}.FxCop|Any CPU.Build.0 = Release|Any CPU {AB3C207C-0126-4143-8D62-1119DF80D366}.Release|Any CPU.ActiveCfg = Release|Any CPU {AB3C207C-0126-4143-8D62-1119DF80D366}.Release|Any CPU.Build.0 = Release|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.CodeCoverage|Any CPU.ActiveCfg = Release|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.CodeCoverage|Any CPU.Build.0 = Release|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.Coverage|Any CPU.ActiveCfg = Release|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.Coverage|Any CPU.Build.0 = Release|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.FxCop|Any CPU.ActiveCfg = Release|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.FxCop|Any CPU.Build.0 = Release|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -480,6 +492,7 @@ Global {194D3CCC-1153-474D-8176-FDE8D7D0D0BD} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5} {C0C45321-B51D-4D8D-9B7B-AA4C2E0B2962} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5} {AB3C207C-0126-4143-8D62-1119DF80D366} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5} + {EA4F1DA7-F2AB-4384-9AA4-9B756E2026B1} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5} {ABC826D4-2FA1-4F2F-87DE-E6095F653810} = {74E681ED-FECC-4034-B9BD-01B0BB1BDECA} {F112851D-B023-4746-B6B1-8D2E5AD8F7AA} = {74E681ED-FECC-4034-B9BD-01B0BB1BDECA} {6CB3EB30-F725-45C0-9742-42599BA8E8D2} = {74E681ED-FECC-4034-B9BD-01B0BB1BDECA}