Example #1
0
        public void Apply(IEnumerable <ChangeScript> changeScripts, bool createChangeLogTable)
        {
            string filename = syntax.GetTemplateFileNameFor(GetTemplateQualifier());

            var model = new Hashtable();

            model.Add("scripts", changeScripts);
            model.Add("changeLogTableName", changeLogTableName);
            model.Add("delimiter", delimiter);
            model.Add("separator", delimiterType is RowDelimiter ? Environment.NewLine : string.Empty);

            try
            {
                var props = new ExtendedProperties();

                var assemblyName = GetType().Assembly.GetName().Name;

                ReplaceManagersWithDbDeployVersions(props, assemblyName);

                if (templateDirectory == null)
                {
                    props.AddProperty("resource.loader", "assembly");
                    props.AddProperty("assembly.resource.loader.class",
                                      // See the ; there? It will be replaced by , in the resource loader factory
                                      // this is because if we add a property with a comma in the value, it will add *two* values to the property.
                                      // oh joy.
                                      typeof(DbDeployAssemblyResourceLoader).FullName + "; " + assemblyName);
                    props.AddProperty("assembly.resource.loader.assembly", assemblyName);
                    filename = "Net.Sf.Dbdeploy.Resources." + filename;
                }
                else
                {
                    props.SetProperty("file.resource.loader.path", templateDirectory.FullName);
                }

                if (createChangeLogTable)
                {
                    writer.Write(syntax.CreateChangeLogTableSqlScript(changeLogTableName));
                }

                var templateEngine = new VelocityEngine(props);

                var context = new VelocityContext(model);

                var template = templateEngine.GetTemplate(filename);

                template.Merge(context, writer);
            }
            catch (ResourceNotFoundException ex)
            {
                string locationMessage;
                if (templateDirectory == null)
                {
                    locationMessage = "";
                }
                else
                {
                    locationMessage = " at " + templateDirectory.FullName;
                }
                throw new UsageException(
                          "Could not find template named " + filename + locationMessage + Environment.NewLine
                          + "Check that you have got the name of the database syntax correct.",
                          ex);
            }
        }