Example #1
0
        public static DelegatedMigrator CreateFromMigrationConfig(DbMigrationsConfiguration c, DbConnection cnn)
        {
            var impl          = new DbMigrator(c);
            var scriptingImpl = new MigratorScriptingDecorator(new DbMigrator(c));
            var migrator      = new DelegatedMigrator(impl.GetPendingMigrations, impl.GetDatabaseMigrations, impl.Update,
                                                      scriptingImpl.ScriptUpdate, cnn)
            {
                IsAutoMigrationsEnabled = c.AutomaticMigrationsEnabled,
                ConfigurationTypeName   = c.GetType().FullName
            };

            return(migrator);
        }
Example #2
0
        /// <summary>
        /// Run Automatic migrations
        /// </summary>
        internal static void UpdateAutoMigrations(string name, DbMigrationsConfiguration <DataContext> migrationConfig)
        {
            DbMigrationsConfiguration config = migrationConfig ?? new DbMigrationsConfiguration <DataContext>
            {
                AutomaticMigrationsEnabled        = true,
                AutomaticMigrationDataLossAllowed = false,
                //TargetDatabase = new DbConnectionInfo(name),
            };

            Console.WriteLine("Migrator: " + config.GetType().ToString());
            config.ContextType    = typeof(DataContext);
            config.TargetDatabase = new DbConnectionInfo(name);
            var migrator = new DbMigrator(config);

            migrator.Update();
        }
        private SortedDictionary <string, IMigrationsConfiguration> GetPendingMigrations()
        {
            var result = new SortedDictionary <string, IMigrationsConfiguration>();

            foreach (IMigrationsConfiguration migrationConfig in migrationConfigs)
            {
                DbMigrationsConfiguration configuration = migrationConfig.GetConfiguration();
                Console.WriteLine("Processing migration config: " + configuration.GetType().FullName);
                var migrator = new DbMigrator(configuration);
                IEnumerable <string> pendingMigrations = migrator.GetPendingMigrations().ToArray();

                migrationConfig.IsUpToDate = !pendingMigrations.Any();

                foreach (string migration in pendingMigrations)
                {
                    result[migration] = migrationConfig;
                }
            }

            return(result);
        }
Example #4
0
        /// <summary>
        /// Run Automatic migrations
        /// </summary>
        public void UpdateAutoMigrations(string connectionName, DbMigrationsConfiguration migrationConfig)   // < DataContext >
        //
        {
            if (_ty.TypeInitializer != null)
            {
                var _tempType = Activator.CreateInstance <T>();
                //_ty.TypeInitializer.Invoke(null, null);
            }
            DbMigrationsConfiguration config = migrationConfig ?? new DbMigrationsConfiguration //<DataContext>
            {
                AutomaticMigrationsEnabled        = true,
                AutomaticMigrationDataLossAllowed = false,
            };

            Trace.WriteLine("Migrating " + _ty.Name + " with migrator config: " + config.GetType().ToString());
            config.MigrationsAssembly  = _ty.Assembly;
            config.MigrationsNamespace = _ty.Namespace;
            config.ContextKey          = _ty.Name;
            config.ContextType         = this.GetType();
            config.TargetDatabase      = new DbConnectionInfo(connectionName);
            var migrator = new DbMigrator(config);

            migrator.Update();
        }
        public static IEnumerable <SqlResourceInfo> ExecuteSqlScripts <TContext>(this DbMigrationsConfiguration <TContext> config, TContext context, ScriptExecuteRule rule = ScriptExecuteRule.Modified, string packName = null) where TContext : DbContext
        {
            packName = (packName ?? String.Empty).Trim();
            var configType  = config.GetType();
            var startString = configType.Namespace + SqlResourceInfo.scriptFolderName + (string.IsNullOrWhiteSpace(packName) ? "" : packName + ".");

            var migrateCultures = new[] { "Default", GetResourceCultureName() };

            var resources = configType.Assembly.GetManifestResourceNames().Where(s => s.StartsWith(startString, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                 s.EndsWith(".sql", StringComparison.InvariantCultureIgnoreCase)
                                                                                 ).Select(s => new SqlResourceInfo(configType, packName, s))
                            .Where(r => migrateCultures.Contains(r.CultureName, StringComparer.InvariantCultureIgnoreCase));

            using (var historyContext = new SqlScriptsHistoryContext(context.Database.Connection, false)) {
                InitializeDatabase(historyContext);
                historyContext.Database.CommandTimeout = (int)Math.Max(context.Database.CommandTimeout ?? 0, 120);
                var histories = historyContext.SqlScriptsHistory.Where(h => h.ScriptName.StartsWith(startString)).ToDictionary(h => h.ScriptName.ToLower(), h => h.Hash);

                DbContextTransaction packTran = null;
                switch (rule)
                {
                case ScriptExecuteRule.Modified:
                    resources = resources.Where(r => {
                        string hashVal;
                        return(!histories.TryGetValue(r.Path.ToLower(), out hashVal) || !hashVal.Equals(r.Hash, StringComparison.InvariantCultureIgnoreCase));
                    });
                    break;

                case ScriptExecuteRule.ModifiedPack:
                    if (!resources.Any(r => {
                        string hashVal;
                        return(!histories.TryGetValue(r.Path.ToLower(), out hashVal) || !hashVal.Equals(r.Hash, StringComparison.InvariantCultureIgnoreCase));
                    }))
                    {
                        resources = new SqlResourceInfo[] { };
                    }
                    else
                    {
                        packTran = historyContext.Database.BeginTransaction();
                    }
                    break;

                case ScriptExecuteRule.Once:
                    resources = resources.Where(r => !histories.ContainsKey(r.Path.ToLower()));
                    break;
                    //default: // do nothing on ScriptExecuteRule.Always
                }

                resources = resources
                            .OrderBy(r => r.CultureName == "Default" ? 0 : 1)
                            .ThenBy(r => r.FolderName + "z")
                            .ThenBy(r => r.Path).ToArray();
                try {
                    foreach (var resourceInfo in resources.ToArray())
                    {
                        resourceInfo.ExecuteSqlScript(historyContext, packTran);
                    }
                    if (packTran != null)
                    {
                        packTran.Commit();
                    }
                } finally {
                    if (packTran != null)
                    {
                        packTran.Dispose();
                    }
                }
                return(resources);
            }
        }