Ejemplo n.º 1
0
        /// <summary>
        /// Runs migrations from the specified assemblies.
        /// </summary>
        public void MigrateStructure(IList <ModuleDescriptor> moduleDescriptors)
        {
            IList <long> versions = new List <long>();
            IDictionary <ModuleDescriptor, IList <Type> > moduleWithMigrations = new Dictionary <ModuleDescriptor, IList <Type> >();

            foreach (var moduleDescriptor in moduleDescriptors)
            {
                var migrationTypes = assemblyLoader.GetLoadableTypes(moduleDescriptor.GetType().Assembly, typeof(Migration));
                if (migrationTypes != null)
                {
                    var types = migrationTypes as IList <Type> ?? migrationTypes.ToList();
                    moduleWithMigrations.Add(moduleDescriptor, types);

                    foreach (var migrationType in types)
                    {
                        var migrationAttributes = migrationType.GetCustomAttributes(typeof(MigrationAttribute), true);
                        if (migrationAttributes.Length > 0)
                        {
                            var attribute = migrationAttributes[0] as MigrationAttribute;
                            if (attribute != null)
                            {
                                versions.Add(attribute.Version);
                            }
                        }
                    }
                }
            }

            versions = versions.OrderBy(f => f).ToList();

            foreach (var version in versions)
            {
                foreach (var moduleWithMigration in moduleWithMigrations)
                {
                    if (!versionChecker.VersionExists(moduleWithMigration.Key.Name, version))
                    {
                        Migrate(moduleWithMigration.Key, moduleWithMigration.Value, version);
                        versionChecker.AddVersion(moduleWithMigration.Key.Name, version);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs migrations from the specified assemblies.
        /// </summary>
        public void MigrateStructure(IList <ModuleDescriptor> moduleDescriptors)
        {
            var versions             = new Dictionary <long, IList <ModuleDescriptor> >();
            var moduleWithMigrations = new Dictionary <ModuleDescriptor, IList <Type> >();

            foreach (var moduleDescriptor in moduleDescriptors)
            {
                var migrationTypes = assemblyLoader.GetLoadableTypes(moduleDescriptor.GetType().Assembly, typeof(Migration));
                if (migrationTypes != null)
                {
                    var types = migrationTypes as IList <Type> ?? migrationTypes.ToList();
                    moduleWithMigrations.Add(moduleDescriptor, types);

                    foreach (var migrationType in types)
                    {
                        var migrationAttributes = migrationType.GetCustomAttributes(typeof(MigrationAttribute), true);
                        if (migrationAttributes.Length > 0)
                        {
                            var attribute = migrationAttributes[0] as MigrationAttribute;
                            if (attribute != null)
                            {
                                if (!versions.ContainsKey(attribute.Version))
                                {
                                    versions[attribute.Version] = new List <ModuleDescriptor>();
                                }
                                versions[attribute.Version].Add(moduleDescriptor);
                            }
                        }
                    }
                }
            }

            foreach (var version in versions.OrderBy(f => f.Key))
            {
                var versionNumber = version.Key;

                foreach (var moduleDescriptor in version.Value)
                {
                    if (!versionChecker.VersionExists(moduleDescriptor.Name, versionNumber))
                    {
                        var migrationTypes = moduleWithMigrations[moduleDescriptor];
                        Migrate(moduleDescriptor, migrationTypes, versionNumber);

                        versionChecker.AddVersion(moduleDescriptor.Name, versionNumber);
                    }
                }
            }
        }