public void TestCollectsAllVersionsFromAssembly()
 {
     DBVersionRepository repo = new DBVersionRepository()
         .AddVersions(typeof(TestDBVersionSystem).Assembly)
         ;
     Assert.AreEqual(1, repo.VersionsTable.Count);
 }
 public DBVersionRepository AddVersions(DBVersionRepository repo)
 {
     foreach (DBVersionInformation info in repo.VersionsTable.Values)
     {
         this.AddVersions(info);
     }
     return(this);
 }
 public DBVersionRepository AddVersions(DBVersionRepository repo)
 {
     foreach (DBVersionInformation info in repo.VersionsTable.Values)
     {
         this.AddVersions(info);
     }
     return this;
 }
 private SortedDictionary<int, IDBVersionInformation> Sort(DBVersionRepository repo)
 {
     SortedDictionary<int, IDBVersionInformation> list = new SortedDictionary<int, IDBVersionInformation>();
     foreach (IDBVersionInformation info in repo.VersionsTable.Values)
     {
         list.Add(info.VersionAttribute.Version, info);
     }
     return list;
 }
 /// <summary>
 /// Creates all instances of DBVersion from provided DBVersionSystem
 /// </summary>
 public DBVersionSystemRunner CreateAllVersionsInDB(DBVersionRepository system)
 {
     DBVersionSystemRunner runner = new DBVersionSystemRunner();
     foreach(IDBVersionInformation info in system.VersionsTable.Values)
     {
         runner.CreateVersionInDB(info);
     }
     return this;
 }
        private SortedDictionary <int, IDBVersionInformation> Sort(DBVersionRepository repo)
        {
            SortedDictionary <int, IDBVersionInformation> list = new SortedDictionary <int, IDBVersionInformation>();

            foreach (IDBVersionInformation info in repo.VersionsTable.Values)
            {
                list.Add(info.VersionAttribute.Version, info);
            }
            return(list);
        }
        /// <summary>
        /// Creates all instances of DBVersion from provided DBVersionSystem
        /// </summary>
        public DBVersionSystemRunner CreateAllVersionsInDB(DBVersionRepository system)
        {
            DBVersionSystemRunner runner = new DBVersionSystemRunner();

            foreach (IDBVersionInformation info in system.VersionsTable.Values)
            {
                runner.CreateVersionInDB(info);
            }
            return(this);
        }
//		public DBVersionRepository ForVersionType(Type type)
//		{
//			DBVersionRepository repo = new DBVersionRepository();
//
//			foreach(IDBVersionInformation versionInfo in VersionsTable.Values)
//			{
//				if (versionInfo.VersionAttribute.GetType().Equals(type))
//					repo.AddVersions(versionInfo);
//			}
//			return repo;
//		}

        public DBVersionRepository FromVersion(int currentVersion)
        {
            DBVersionRepository repo = new DBVersionRepository();

            foreach (IDBVersionInformation versionInfo in VersionsTable.Values)
            {
                int ver = versionInfo.VersionAttribute.Version;
                if (ver > currentVersion)
                {
                    repo.AddVersions(versionInfo);
                }
            }
            return(repo);
        }
        public void UpdateToNewest()
        {
            m_repository.CheckVersions();

            int dbVersion = GetCurrentVersion();

            //			m_log.Info("Injecting DBStructure versions into database");
            DBVersionRepository repo = new DBVersionRepository()
                                   .AddVersions(m_repository
                             .FromVersion(dbVersion))
                                                ;
            new DBVersionSystemRunner()
                .ExecuteUpdateToDB(repo)
            //				.CreateAllVersionsInDB(repo)
                ;
            m_log.Info("DB update finished successfully");
        }
        public void UpdateToNewest()
        {
            m_repository.CheckVersions();

            int dbVersion = GetCurrentVersion();


//			m_log.Info("Injecting DBStructure versions into database");
            DBVersionRepository repo = new DBVersionRepository()
                                       .AddVersions(m_repository
                                                    .FromVersion(dbVersion))
            ;

            new DBVersionSystemRunner()
            .ExecuteUpdateToDB(repo)
//				.CreateAllVersionsInDB(repo)
            ;
            m_log.Info("DB update finished successfully");
        }
        public void TestExecutesDBVersions()
        {
            DBVersionRepository repo = new DBVersionRepository()
                .AddVersions(typeof(TestDBVersionSystem).Assembly)
                ;
            new DBVersionSystem(new DBVersionRepository().AddVersions(typeof(TestDBVersionsFile).Assembly)).UpdateToNewest();
            using (IConnection con = new ConnectionCollector().GetConnection())
            {
                // DB Version was executed?
                IDataReader reader = con.ExecuteReader("SELECT COUNT(*) FROM TROL_TEST");
                Assert.IsTrue(reader.Read());
                Assert.AreEqual(1, reader.GetInt32(0));

                // Version number was written?
                Assert.AreEqual(1, DBVersionSystem.GetCurrentVersion());

                // DB version information is written corretly (was bug, which duplicated lines)
                reader = con.ExecuteReader("SELECT COUNT(*) FROM DB_VERSION");
                Assert.IsTrue(reader.Read());
                Assert.AreEqual(1, reader.GetInt32(0));
            }
        }
        public DBVersionSystemRunner ExecuteUpdateToDB(DBVersionRepository repo)
        {
            m_log.Debug(string.Format("Preparing to inject {0} new versions into database",
                                      repo.VersionsTable.Count));

            foreach (IDBVersionInformation info in Sort(repo).Values)
            {
                string versionInfo = string.Format(
                    "[{0} - {1}]",
                    info.VersionAttribute.Version,
                    info.VersionAttribute.Date);
                try
                {
                    using (IConnection con = new ConnectionCollector().GetConnection())
                    {
//							m_log.InfoFormat("Injecting version {0} into database", versionInfo);
                        info.VersionBody.Execute();
                        CreateVersionInDB(info);
                        con.Commit();
                    }
                }
                catch (Exception e)
                {
                    m_log.Error(string.Format("Could not commit version {0}", versionInfo), e);
                    m_log.Error(string.Format("Rolling back"));
                    throw new DBUpdateException(string.Format("Error injecting DB version {1}, date '{0}'.",
                                                              info.VersionAttribute.Date,
                                                              info.VersionAttribute.Version)
                                                , e);
                }
            }

            m_log.Debug(string.Format("Success - Injected {0} new versions into database",
                                      repo.VersionsTable.Count));
            return(this);
        }
        public DBVersionSystemRunner ExecuteUpdateToDB(DBVersionRepository repo)
        {
            m_log.Debug(string.Format("Preparing to inject {0} new versions into database",
                                      repo.VersionsTable.Count));

            foreach (IDBVersionInformation info in Sort(repo).Values)
            {
                string versionInfo = string.Format(
                    "[{0} - {1}]",
                    info.VersionAttribute.Version,
                    info.VersionAttribute.Date);
                try
                {
                    using (IConnection con = new ConnectionCollector().GetConnection())
                    {
            //							m_log.InfoFormat("Injecting version {0} into database", versionInfo);
                        info.VersionBody.Execute();
                        CreateVersionInDB(info);
                        con.Commit();
                    }
                }
                catch (Exception e)
                {
                    m_log.Error(string.Format("Could not commit version {0}", versionInfo), e);
                    m_log.Error(string.Format("Rolling back"));
                    throw new DBUpdateException(string.Format("Error injecting DB version {1}, date '{0}'.",
                                                              info.VersionAttribute.Date,
                                                              info.VersionAttribute.Version)
                                                , e);
                }
            }

            m_log.Debug(string.Format("Success - Injected {0} new versions into database",
                                     repo.VersionsTable.Count));
            return this;
        }
 //        private DBVersionRepository m_repoCore = null;
 public DBVersionSystem(DBVersionRepository repoClient)
 {
     this.m_repository = repoClient;
 }
//		private DBVersionRepository m_repoCore = null;

        public DBVersionSystem(DBVersionRepository repoClient)
        {
            this.m_repository = repoClient;
        }
        //        public DBVersionRepository ForVersionType(Type type)
        //        {
        //            DBVersionRepository repo = new DBVersionRepository();
        //            
        //            foreach(IDBVersionInformation versionInfo in VersionsTable.Values)
        //            {
        //                if (versionInfo.VersionAttribute.GetType().Equals(type))
        //                    repo.AddVersions(versionInfo);
        //            }
        //            return repo;
        //        }
        public DBVersionRepository FromVersion(int currentVersion)
        {
            DBVersionRepository repo = new DBVersionRepository();

            foreach(IDBVersionInformation versionInfo in VersionsTable.Values)
            {
                int ver = versionInfo.VersionAttribute.Version;
                if (ver > currentVersion)
                {
                    repo.AddVersions(versionInfo);
                }
            }
            return repo;
        }