public void TestFixtureSetUp()
        {
            dbPlatform = new SQLitePlatform();
            migrationVersionInfoManager = new DbMigrationVersionInfoManager(dbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");
            connectionString = ConfigurationManager.AppSettings["connectionString"];

            dbPlatform.DeploymentManager.Deploy(connectionString, DbDeploymentMode.Redeploy);
        }
 public void ExecuteMigrationScripts(IDbPlatform dbPlatform, IMigrationVersionInfoManager migrationVersionInfoManager, 
     string connectionString, MigrationScriptCollection migrationScripts, 
     long currentVersion, long? targetVersion, MigrationMode migrationMode)
 {
     this.migrationScripts = migrationScripts;
     this.currentVersion = currentVersion;
     this.targetVersion = targetVersion;
     this.migrationMode = migrationMode;
 }
 public static long GetCurrentMigrationVersion(IMigrationVersionInfoManager migrationVersionInfoManager,
     IDbPlatform dbPlatform, string connectionString)
 {
     return DbUtil.ExecuteInTransaction<long>(dbPlatform, connectionString, 
         delegate(IDbTransaction transaction)
             {
                 return migrationVersionInfoManager.GetCurrentMigrationVersion(transaction);
             });
 }
 public static IList<long> GetRegisteredMigrationVersions(IMigrationVersionInfoManager migrationVersionInfoManager, 
     IDbPlatform dbPlatform, string connectionString)
 {
     return DbUtil.ExecuteInTransaction<IList<long>>(dbPlatform, connectionString, 
         delegate(IDbTransaction transaction)
             {
                 return migrationVersionInfoManager.GetRegisteredMigrationVersions(transaction);
             });
 }
        public void TestFixtureSetUp()
        {
            dbPlatform = new SQLitePlatform();
            migrationVersionInfoManager = new DbMigrationVersionInfoManager(dbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");
            sourceConnectionString = ConfigurationManager.AppSettings["connectionString"];
            targetConnectionString = ConfigurationManager.AppSettings["secondaryConnectionString"];

            migrationService = new MigrationService(dbPlatform, migrationVersionInfoManager,
                new DbMigrationScriptExecutive(new DbCommandExecutionStrategy()), null);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MigrationService"/> class.
        /// </summary>
        /// <param name="dbPlatform"></param>
        /// <param name="migrationVersionInfoManager"></param>
        /// <param name="migrationScriptExecutive"></param>
        /// <param name="nativeSqlResourceProvider"></param>
        public MigrationService(IDbPlatform dbPlatform, IMigrationVersionInfoManager migrationVersionInfoManager,
            IMigrationScriptExecutive migrationScriptExecutive, INativeSqlResourceProvider nativeSqlResourceProvider)
        {
            this.dbPlatform = dbPlatform;
            this.migrationVersionInfoManager = migrationVersionInfoManager;
            this.migrationScriptExecutive = migrationScriptExecutive;
            this.nativeSqlResourceProvider = nativeSqlResourceProvider;

            this.migrationScriptExecutive.Migrating +=
                delegate(object sender, MigrationScriptExecutionEventArgs args)
                    { InvokeMigrating(new MigrationEventArgs(args.Version, args.Mode)); };
            this.migrationScriptExecutive.Migrated +=
                delegate(object sender, MigrationScriptExecutionEventArgs args)
                    { InvokeMigrated(new MigrationEventArgs(args.Version, args.Mode)); };
        }
        public void ExecuteMigrationScripts(IDbPlatform dbPlatform, IMigrationVersionInfoManager migrationVersionInfoManager, 
            string connectionString, MigrationScriptCollection migrationScripts, 
            long currentVersion, long? targetVersion, MigrationMode migrationMode)
        {
            using(IDbConnection dbConnection = dbPlatform.ProviderFactory.CreateConnection())
            {
                dbConnection.ConnectionString = connectionString;
                dbConnection.Open();

                IList<MigrationScript> effectiveMigrationScripts = 
                    new List<MigrationScript>(GetMigrationScripts(migrationScripts, currentVersion, targetVersion, migrationMode));
                foreach(MigrationScript migrationScript in effectiveMigrationScripts)
                {
                    using(IDbTransaction ddlTransaction = BeginDdlTransaction(dbPlatform, dbConnection))
                    {
                        using(IDbCommand dbCommand = dbConnection.CreateCommand())
                        {
                            InvokeMigrating(new MigrationScriptExecutionEventArgs(migrationMode, migrationScript.MigrationVersion));

                            foreach(string ddlScript in migrationScript.DdlScripts)
                            {
                                dbCommand.CommandText = ddlScript;
                                dbCommand.CommandType = CommandType.Text;

                                Trace.WriteLine(ddlScript);

                                //
                                // Workaround for Jet
                                if(!(ddlTransaction is NullDbTransaction))
                                    dbCommand.Transaction = ddlTransaction;

                                try
                                {
                                    dbCommandExecutionStrategy.Execute(dbPlatform, dbCommand);
                                } // try

                                // TODO: Replace with DbPlatformException or something
                                catch(Exception e)
                                {
                                    throw new MigrationException(
                                        string.Format(migrationMode == MigrationMode.Upgrade ?
                                            Properties.Resources.ErrorWhileUpgradingToVersion:
                                            Properties.Resources.ErrorWhileDowngradingToVersion, migrationScript.MigrationVersion, e.ToString()), 
                                        ddlScript, e);
                                } // catch
                                
                            } // foreach
                        } // using

                        //
                        // If we're downgrading all way down, do not register it, since SchemaInfo
                        // will be deleted too.
                        if(!(migrationMode == MigrationMode.Downgrade && targetVersion == 0 && effectiveMigrationScripts.IndexOf(migrationScript) ==
                            effectiveMigrationScripts.Count - 1))
                            migrationVersionInfoManager.RegisterMigrationVersion(ddlTransaction, migrationMode,
                                migrationScript.MigrationVersion);

                        ddlTransaction.Commit();

                        InvokeMigrated(new MigrationScriptExecutionEventArgs(migrationMode, migrationScript.MigrationVersion));
                    } // using
                } // foreach
            } // foreach
        }