Exemplo n.º 1
0
        public void Initialize()
        {
            try
            {
                Catfish.Tests.Migrations.Configuration config = new Catfish.Tests.Migrations.Configuration();
                var migrator = new DbMigrator(config);

                foreach (string migName in migrator.GetPendingMigrations())
                {
                    Type        migration = config.MigrationsAssembly.GetType(string.Format("{0}.{1}", config.MigrationsNamespace, migName.Substring(16)));
                    DbMigration m         = (DbMigration)Activator.CreateInstance(migration);
                    m.Up();

                    var prop = m.GetType().GetProperty("Operations", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    if (prop != null)
                    {
                        IEnumerable <MigrationOperation> operations = prop.GetValue(m) as IEnumerable <MigrationOperation>;
                        var generator  = config.GetSqlGenerator("System.Data.SQLite");
                        var statements = generator.Generate(operations, "2008");
                        foreach (MigrationStatement item in statements)
                        {
                            Db.Database.ExecuteSqlCommand(item.Sql);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        public static void RemoveTriggers(this DbMigration migration)
        {
            var types = migration.GetType().Assembly.GetTypes().Where(t => typeof(InsertTriggerCustomAttribute).IsAssignableFrom(t));

            foreach (var triggerType in types)
            {
                string tt = "";
            }
        }
Exemplo n.º 3
0
        public static void RunMigration(this DbContext context, DbMigration migration)
        {
            var prop = migration.GetType().GetProperty("Operations", BindingFlags.NonPublic | BindingFlags.Instance);

            if (prop != null)
            {
                IEnumerable <MigrationOperation> operations = prop.GetValue(migration) as IEnumerable <MigrationOperation>;
                var generator  = new MySqlMigrationSqlGenerator();
                var statements = generator.Generate(operations, "2008");
                foreach (MigrationStatement item in statements)
                {
                    context.Database.ExecuteSqlCommand(item.Sql);
                }
            }
        }
Exemplo n.º 4
0
        public static void RunMigration(this DbContext context, DbMigration migration)
        {
            var prop = migration.GetType().GetProperty("Operations", BindingFlags.NonPublic | BindingFlags.Instance);

            if (prop != null)
            {
                IEnumerable <MigrationOperation> operations = prop.GetValue(migration) as IEnumerable <MigrationOperation>;
                MigrationSqlGenerator            generator  = (new DbMigrationsConfiguration()).GetSqlGenerator("JetEntityFrameworkProvider");
                var statements = generator.Generate(operations, "Jet");
                foreach (MigrationStatement item in statements)
                {
                    context.Database.ExecuteSqlCommand(item.Sql);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 当某个迁移操作升级完成后,为它添加相应的历史记录。
        /// </summary>
        /// <param name="database"></param>
        /// <param name="migration"></param>
        /// <returns></returns>
        internal Result AddAsExecuted(string database, DbMigration migration)
        {
            var history = new HistoryItem()
            {
                IsGenerated = false,
                TimeId = migration.TimeId,
                Description = migration.Description,
                MigrationClass = migration.GetType().AssemblyQualifiedName,
            };

            if (migration.MigrationType == MigrationType.AutoMigration)
            {
                history.IsGenerated = true;
                history.MigrationContent = SerializationHelper.XmlSerialize(migration);
            }

            return this.AddHistoryCore(database, history);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 当某个迁移操作升级完成后,为它添加相应的历史记录。
        /// </summary>
        /// <param name="database"></param>
        /// <param name="migration"></param>
        /// <returns></returns>
        internal Result AddAsExecuted(string database, DbMigration migration)
        {
            var history = new HistoryItem()
            {
                IsGenerated    = false,
                TimeId         = migration.TimeId,
                Description    = migration.Description,
                MigrationClass = migration.GetType().AssemblyQualifiedName,
            };

            if (migration.MigrationType == MigrationType.AutoMigration)
            {
                history.IsGenerated      = true;
                history.MigrationContent = SerializationHelper.XmlSerialize(migration);
            }

            return(this.AddHistoryCore(database, history));
        }
Exemplo n.º 7
0
        public void Initialize(MockConnection solrConnection)
        {
            //if (SolrService.IsInitialized)
            //{
            //    //TODO: Inject the new connection into this current solr thread.
            //}
            //else
            //{
            //    SolrService.ForceInit(solrConnection);
            //}

            SolrService.ForceInit(solrConnection);

            try
            {
                Catfish.Tests.Migrations.Configuration config = new Catfish.Tests.Migrations.Configuration();
                var migrator = new DbMigrator(config);

                foreach (string migName in migrator.GetLocalMigrations())
                {
                    Type        migration = config.MigrationsAssembly.GetType(string.Format("{0}.{1}", config.MigrationsNamespace, migName.Substring(16)));
                    DbMigration m         = (DbMigration)Activator.CreateInstance(migration);
                    m.Up();

                    var prop = m.GetType().GetProperty("Operations", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    if (prop != null)
                    {
                        IEnumerable <MigrationOperation> operations = prop.GetValue(m) as IEnumerable <MigrationOperation>;
                        var generator  = config.GetSqlGenerator("System.Data.SQLite");
                        var statements = generator.Generate(operations, "2008");
                        foreach (MigrationStatement item in statements)
                        {
                            Db.Database.ExecuteSqlCommand(item.Sql);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 当某个迁移操作升级完成后,为它添加相应的历史记录。
        /// </summary>
        /// <param name="database"></param>
        /// <param name="migration"></param>
        /// <returns></returns>
        internal Result AddAsExecuted(string database, DbMigration migration)
        {
            var migrationType = migration.GetType();

            var history = new HistoryItem()
            {
                IsGenerated = false,
                TimeId      = migration.TimeId,
                Description = migration.Description,
                //MigrationClass = migration.GetType().AssemblyQualifiedName,//Rafy.DbMigration.Operations.RemoveFKConstraint, Rafy, Version=3.51.3140.0, Culture=neutral, PublicKeyToken=f7937325279b37cf
                MigrationClass = migrationType.FullName + ", " + migrationType.Assembly.FullName,//忽略版本号,方便框架升级。Rafy.DbMigration.Operations.RemoveFKConstraint, Rafy
            };

            if (migration.MigrationType == MigrationType.AutoMigration)
            {
                history.IsGenerated      = true;
                history.MigrationContent = SerializationHelper.XmlSerialize(migration);
            }

            return(this.AddHistoryCore(database, history));
        }
Exemplo n.º 9
0
        internal static string GetMigrationSql(DbMigration dbMigration, SqlConnection sqlConnection)
        {
            StringBuilder sql  = new StringBuilder();
            var           prop = dbMigration.GetType().GetProperty("Operations", BindingFlags.NonPublic | BindingFlags.Instance);

            if (prop != null)
            {
                IEnumerable <MigrationOperation> operations = prop.GetValue(dbMigration) as IEnumerable <MigrationOperation>;
                foreach (var operation in operations)
                {
                    if (operation is AddForeignKeyOperation && (( AddForeignKeyOperation )operation).PrincipalColumns.Count == 0)
                    {
                        // In Rock, the principal column should always be the Id.  This isn't always the case . . . .
                        (( AddForeignKeyOperation )operation).PrincipalColumns.Add("Id");
                    }
                }
                var generator  = new SqlServerMigrationSqlGenerator();
                var statements = generator.Generate(operations, sqlConnection.ServerVersion.AsInteger() > 10 ? "2008" : "2005");
                foreach (MigrationStatement item in statements)
                {
                    if (item.Sql.StartsWith("CREATE TABLE"))
                    {
                        //So the way this works is ROCK makes our table for us.
                        //But table builder just tacks on the Primary Key constraint
                        //So remove most of the table text and change it to an alter table instead
                        var start      = item.Sql.IndexOf("CREATE TABLE") + 12;
                        var end        = item.Sql.IndexOf("(");
                        var tableName  = item.Sql.Substring(start, end - start);
                        var constraint = item.Sql.Substring(item.Sql.IndexOf("CONSTRAINT"));
                        constraint = constraint.Substring(0, constraint.Length - 1);
                        sql.Append(string.Format($"ALTER TABLE {tableName} ADD {constraint};"));
                    }
                    else
                    {
                        sql.Append(item.Sql + ";");
                    }
                }
            }
            return(sql.ToString());
        }
Exemplo n.º 10
0
        private static string GetDefaultSchema(DbMigration migration)
        {
            DebugCheck.NotNull(migration);

            try
            {
                var defaultSchema = new ResourceManager(migration.GetType()).GetString(DefaultSchemaResourceKey);

                return !string.IsNullOrWhiteSpace(defaultSchema) ? defaultSchema : EdmModelExtensions.DefaultSchema;
            }
            catch (MissingManifestResourceException)
            {
                // Upgrade scenario, no default schema resource found
                return EdmModelExtensions.DefaultSchema;
            }
        }