/// <summary>
        /// Gets a collections of module updates that have already been applied
        /// to the system.
        /// </summary>
        private async Task <ICollection <ModuleVersion> > GetUpdateVersionHistoryAsync()
        {
            var query = @"
                if (exists (select * 
                                 from information_schema.tables 
                                 where table_schema = 'Cofoundry' 
                                 and  table_name = 'ModuleUpdate'))
                begin
                    select Module, MAX([Version]) as Version
	                from  Cofoundry.ModuleUpdate
	                group by Module
	                order by Module
                end";

            var moduleVersions = await _db.ReadAsync(query, r =>
            {
                var moduleVersion     = new ModuleVersion();
                moduleVersion.Module  = (string)r["Module"];
                moduleVersion.Version = (int)r["Version"];

                return(moduleVersion);
            });

            return(moduleVersions);
        }
Пример #2
0
        /// <summary>
        /// Creates DbUpateCommands from sql scripts embedded in an assembly. Only new schema updates
        /// are included and functions/sps/triggers etc are only returned if there are any relevant schema
        /// updates, so you need to create a schema update file if you want to force these to update.
        /// </summary>
        /// <param name="assembly">The assembly to scan for sql scripts.</param>
        /// <param name="currentVersion">The current version of the module</param>
        /// <param name="scriptPath">The folder path of the script files which defaults to 'Install.Db.' (which equates to 'Install/Db/')</param>
        /// <returns>Collecton of IUpdateCommands that represents all the required db updates</returns>
        public IEnumerable <IVersionedUpdateCommand> Create(Assembly assembly, ModuleVersion currentVersion, string scriptPath = "Install.Db.")
        {
            Condition.Requires(assembly).IsNotNull();
            var scriptFiles      = GetScripts(assembly, scriptPath);
            var commands         = new List <UpdateDbCommand>();
            int maxVersionNumber = 0;

            // Get schema scripts we need so we know the version number.
            foreach (var scriptFile in scriptFiles.Where(s => s.Contains(".Schema.")))
            {
                var fileName = GetScriptFileName(scriptFile);
                var version  = IntParser.ParseOrNull(fileName);
                if (!version.HasValue)
                {
                    throw new InvalidCastException("Unable to parse version number from schema update file: " + scriptFile);
                }

                if (currentVersion != null && version.Value <= currentVersion.Version)
                {
                    continue;
                }

                var command = new UpdateDbCommand();
                command.Version     = version.Value;
                command.ScriptType  = DbScriptType.Schema;
                command.Sql         = GetResource(assembly, scriptFile);
                command.Description = scriptFile;
                command.FileName    = fileName;
                commands.Add(command);

                if (maxVersionNumber < version.Value)
                {
                    maxVersionNumber = version.Value;
                }
            }

            if (!commands.Any())
            {
                return(Enumerable.Empty <IVersionedUpdateCommand>());
            }

            foreach (var scriptFile in scriptFiles.Where(s => !s.Contains(".Schema.")))
            {
                var command = new UpdateDbCommand();
                command.Version     = maxVersionNumber;
                command.Sql         = GetResource(assembly, scriptFile);
                command.ScriptType  = MapScriptType(scriptFile);
                command.Description = scriptFile;
                command.FileName    = GetScriptFileName(scriptFile);
                commands.Add(command);
            }

            return(commands);
        }