public void ShouldCreateCreateTableCommands()
        {
            var commands = _generator.GetCreateFeatureCommands("Feature1", false).ToList();

            Assert.That(commands, Is.Not.Null);
            Assert.That(commands.Count(), Is.EqualTo(3));

            var blogRecord = commands.Where(c => c.Name == "TEST_Feature1_BlogRecord").First();

            Assert.That(blogRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "Id" && !c.IsIdentity && c.IsPrimaryKey && c.DbType == DbType.Int32));
            Assert.That(blogRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "Description" && c.DbType == DbType.String));
            Assert.That(blogRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "PostCount" && c.DbType == DbType.Int32));

            var blogArchiveRecord = commands.Where(c => c.Name == "TEST_Feature1_BlogArchiveRecord").First();

            Assert.That(blogArchiveRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "Id" && c.IsPrimaryKey && c.DbType == DbType.Int32));
            Assert.That(blogArchiveRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "Year" && c.DbType == DbType.Int32));
            Assert.That(blogArchiveRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "Month" && c.DbType == DbType.Int32));
            Assert.That(blogArchiveRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "PostCount" && c.DbType == DbType.Int32));
            Assert.That(blogArchiveRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "Blog_id" && c.DbType == DbType.Int32));

            var bodyRecord = commands.Where(c => c.Name == "TEST_Feature1_BodyRecord").First();

            Assert.That(bodyRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "Id" && c.IsPrimaryKey && c.DbType == DbType.Int32));
            Assert.That(bodyRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "Text" && c.DbType == DbType.String && c.Length == 10000));
            Assert.That(bodyRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "Format" && c.DbType == DbType.String && c.Length == 42));
            Assert.That(bodyRecord.TableCommands.OfType <CreateColumnCommand>().Any(c => c.ColumnName == "ContentItemRecord_id" && c.DbType == DbType.Int32));
        }
예제 #2
0
        public void CreateTables(string featureName)
        {
            var stringInterpreter = new StringCommandInterpreter(Context.Output);

            try
            {
                var commands = _schemaCommandGenerator.GetCreateFeatureCommands(featureName, Drop).ToList();
                if (commands.Any())
                {
                    foreach (var command in commands)
                    {
                        stringInterpreter.Visit(command);
                        _dataMigrationInterpreter.Visit(command);
                    }
                }
                else
                {
                    Context.Output.WriteLine(T("There are no tables to create for feature {0}.", featureName));
                    return;
                }
            }
            catch (Exception ex)
            {
                throw new BoyingException(T("An error occured while creating the tables."), ex);
            }

            Context.Output.WriteLine(T("Tables created"));
        }
        public void CreateDataMigration(string featureName)
        {
            Context.Output.WriteLine(T("Creating Data Migration for {0}", featureName));
            ExtensionDescriptor extensionDescriptor = _extensionManager.AvailableExtensions().FirstOrDefault(extension => DefaultExtensionTypes.IsModule(extension.ExtensionType) &&
                                                                                                             extension.Features.Any(feature => String.Equals(feature.Id, featureName, StringComparison.OrdinalIgnoreCase)));

            if (extensionDescriptor == null)
            {
                Context.Output.WriteLine(T("Creating data migration failed: target Feature {0} could not be found.", featureName));
                return;
            }

            string dataMigrationFolderPath = HostingEnvironment.MapPath("~/Modules/" + extensionDescriptor.Id + "/");
            string dataMigrationFilePath   = dataMigrationFolderPath + "Migrations.cs";
            string templatesPath           = HostingEnvironment.MapPath("~/Modules/Orchard." + ModuleName + "/CodeGenerationTemplates/");
            string moduleCsProjPath        = HostingEnvironment.MapPath(string.Format("~/Modules/{0}/{0}.csproj", extensionDescriptor.Id));

            if (!Directory.Exists(dataMigrationFolderPath))
            {
                Directory.CreateDirectory(dataMigrationFolderPath);
            }

            if (File.Exists(dataMigrationFilePath))
            {
                Context.Output.WriteLine(T("Data migration already exists in target Module {0}.", extensionDescriptor.Id));
                return;
            }

            List <SchemaCommand> commands = _schemaCommandGenerator.GetCreateFeatureCommands(featureName, false).ToList();
            string dataMigrationText;

            using (var stringWriter = new StringWriter()) {
                var interpreter = new CodeGenerationCommandInterpreter(stringWriter);

                foreach (var command in commands)
                {
                    interpreter.Visit(command);
                    stringWriter.WriteLine();
                }

                dataMigrationText = File.ReadAllText(templatesPath + "DataMigration.txt");
                dataMigrationText = dataMigrationText.Replace("$$FeatureName$$", featureName);
                dataMigrationText = dataMigrationText.Replace("$$Commands$$", stringWriter.ToString());
            }
            File.WriteAllText(dataMigrationFilePath, dataMigrationText);

            string projectFileText = File.ReadAllText(moduleCsProjPath);

            // The string searches in solution/project files can be made aware of comment lines.
            if (projectFileText.Contains("<Compile Include"))
            {
                string compileReference = string.Format("<Compile Include=\"{0}\" />\r\n    ", "Migrations.cs");
                projectFileText = projectFileText.Insert(projectFileText.LastIndexOf("<Compile Include"), compileReference);
            }
            else
            {
                string itemGroupReference = string.Format("</ItemGroup>\r\n  <ItemGroup>\r\n    <Compile Include=\"{0}\" />\r\n  ", "Migrations.cs");
                projectFileText = projectFileText.Insert(projectFileText.LastIndexOf("</ItemGroup>"), itemGroupReference);
            }

            File.WriteAllText(moduleCsProjPath, projectFileText);
            TouchSolution(Context.Output);
            Context.Output.WriteLine(T("Data migration created successfully in Module {0}", extensionDescriptor.Id));
        }