コード例 #1
0
 private void Execute(DatabaseVersionSetup.Profile profile, StringBuilder stringBuilder, ITransaction transaction)
 {
     var trim = stringBuilder.ToString().Trim();
     if (trim.Length > 0){
         profile.RepositoryProfile.GetDatabase().ExecuteSql(profile.VariableReplace(trim), transaction);
     }
 }
コード例 #2
0
        public DatabaseVersionSetup Load(TextReader reader)
        {
            var versionSetup = new DatabaseVersionSetup();

            var shemas = new XmlSchemaSet();
            Assembly assem = typeof(DatabaseVersionSetup).Assembly;
            using (Stream stream = assem.GetManifestResourceStream("DatabaseVersionControl.Core.Xsd.DVCSchema.xsd"))
            {
                if (stream != null){
                    shemas.Add("", XmlReader.Create(stream));
                }
                else{
                    throw new Exception("Could not load embedded resource Xsd.DVCSchema.xsd");
                }
            }
            XDocument element = XDocument.Load(reader);
            element.Validate(shemas, (sender, e) =>
                                         { if (_exception != null) _exception(e); }, true);
            var profiles = element.XPathSelectElements("//profiles/profile");
            versionSetup.DefaultProfile = element.Root.Attribute("defaultProfile").Value;
            LoadProfiles(versionSetup, profiles);
            var databases = element.XPathSelectElements("//repositories/repository");
            LoadRepositories(versionSetup, databases);
            return versionSetup;
        }
コード例 #3
0
 public void Load(TextReader reader)
 {
     var versionSetup = new DatabaseVersionSetup();
         var profiles = XElement.Load(reader).XPathSelectElements("/Profiles/Profile");
         LoadProfiles(versionSetup, profiles);
         var databases = XElement.Load(reader).XPathSelectElements("/Databases/Database");
         LoadDatabases(versionSetup, databases);
 }
コード例 #4
0
        private void LoadDatabases(DatabaseVersionSetup versionSetup, IEnumerable<XElement> enumerable)
        {
            foreach (var element in enumerable)
            {
                var db = new DatabaseVersionSetup.Database();
                //reader

                // versionSetup.Databases.Add(db);
            }
        }
コード例 #5
0
 /// <summary>
 /// Public constructor with read only database version
 /// </summary>
 /// <param name="setup"></param>
 /// <param name="profileId"></param>
 /// <param name="repositoryId"></param>
 /// <param name="runTestData"></param>
 public DvcController(DatabaseVersionSetup setup, string profileId, bool runTestData)
 {
     _setup = setup;
     _runTestData = runTestData;
     Log.Info(string.Format("Loading profile {0}", profileId));
     _profile = _setup.GetProfile(profileId);
     if (_profile == null){
         throw new Exception(string.Format("Could not find profile {0} in list of profiles", profileId));
     }
     //add variable from repository to profile
     _profile.AddRepositoryProperties(GetDefaultRepository().Properties);
 }
コード例 #6
0
        private void LoadProfiles(DatabaseVersionSetup versionSetup, IEnumerable<XElement> enumerable)
        {
            foreach (var element in enumerable)
            {
                var profile = new DatabaseVersionSetup.Profile()
                                  {
                                      Id = (string) element.Attribute("Id"),
                                      ConnectionString = element.Element("ConnectionString").Value,
                                      Tracking = LoadTracking(element.Element("Tracking"))
                                  };

                versionSetup.Profiles.Add(profile);
            }
        }
コード例 #7
0
 /// <summary>
 /// Execute the sql command by slitting file by lines that contain 'GO'
 /// </summary>
 /// <param name="profile"></param>
 /// <param name="transaction"></param>
 public void ExecuteSql(DatabaseVersionSetup.Profile profile, ITransaction transaction)
 {
     var stringBuilder = new StringBuilder();
     foreach (var file in _files){
         using (var stream = File.OpenRead(profile.VariableReplace(file)))
         {
             using (var streamReader = new StreamReader(stream)){
                 while (!streamReader.EndOfStream){
                     var readLine = streamReader.ReadLine();
                     if (readLine.Trim().Equals("GO")){
                         Execute(profile, stringBuilder, transaction);
                         stringBuilder = new StringBuilder();
                     }
                     else{
                         stringBuilder.AppendLine(readLine);
                     }
                 }
                 //make sure we execute everything
                 Execute(profile, stringBuilder, transaction);
             }
         }
     }
 }
コード例 #8
0
        private void LoadProfiles(DatabaseVersionSetup versionSetup, IEnumerable<XElement> enumerable)
        {
            foreach (var element in enumerable)
            {
                string value = element.Attribute("id").Value;
                IRepositoryProfile profile = null;
                XElement databaseProfile = element.Element("repositoryProfile");

                KeyValuePair<string,string>[] props;
                if (databaseProfile != null)
                {
                    XElement trackingByTable = databaseProfile.Element("trackingByTable");

                    ITracker databaseTrackingTable = null;
                    if (trackingByTable != null)
                    {
                        databaseTrackingTable = new AgnosticDatabaseTrackingTable(trackingByTable.Attribute("tableName").Value, Convert.ToBoolean(trackingByTable.Attribute("autoCreate").Value));
                    }

                    var databaseProfileType = databaseProfile.Attribute("type").Value;
                    if (databaseProfileType == "sqlServer")
                    {
                       int commandTimeoutValue = 30;
                       var commandTimeoutString = databaseProfile.Attribute("commandTimeout");
                       if(commandTimeoutString != null)
                          commandTimeoutValue = int.Parse(commandTimeoutString.Value);

                        profile = new SqlServerDatabaseProfile(
                                databaseProfile.Attribute("connectionString").Value,
                                databaseProfile.Attribute("databaseName").Value,
                                databaseProfile.Attribute("repositoryID").Value,
                                commandTimeoutValue,
                                databaseTrackingTable);
                    }
                    else if (databaseProfileType == "mySql")
                    {
                        int commandTimeoutValue = 30;
                        var commandTimeoutString = databaseProfile.Attribute("commandTimeout");
                        if (commandTimeoutString != null)
                            commandTimeoutValue = int.Parse(commandTimeoutString.Value);

                        profile = new MySqlRepositoryProfile(
                                databaseProfile.Attribute("connectionString").Value,
                                databaseProfile.Attribute("databaseName").Value,
                                databaseProfile.Attribute("repositoryID").Value,
                                commandTimeoutValue,
                                databaseTrackingTable);
                    }
                    else
                    {
                        throw new Exception(string.Format("Unknown databaseProfile type {0}", databaseProfileType));
                    }
                    XElement properties = databaseProfile.Element("properties");
                    props = LoadProperties(properties);
                }
                else{
                    throw new Exception("repositoryProfile missing from profile");
                }
                var newProfile = new DatabaseVersionSetup.Profile(value,profile);
                if (props != null) newProfile.AddProperties(props);

                versionSetup.Profiles.Add(newProfile);
            }
        }
コード例 #9
0
 public void ExecuteSql(DatabaseVersionSetup.Profile profile, ITransaction transaction)
 {
     IConnection connection = profile.RepositoryProfile.GetDatabase();
     connection.ExecuteSql(profile.VariableReplace(_sql), transaction);
 }
コード例 #10
0
 /// <summary>
 /// Execute the test data 
 /// </summary>
 public void ExecuteTestData(DatabaseVersionSetup.Profile profile, ITransaction transaction)
 {
     _testsStatements.ExecuteSql(profile, transaction);
 }
 /// <summary>
 /// Sets the version number
 /// </summary>
 /// <param name="profile"></param>
 /// <param name="version"></param>
 /// <param name="transaction"></param>
 public void SetVersion(DatabaseVersionSetup.Profile profile, double version, ITransaction transaction)
 {
     IConnection connection = profile.RepositoryProfile.GetDatabase();
     var access = new DoTrackDataAccess(() => connection, TableName);
     var track = new DoTrack { DatabaseName = profile.RepositoryProfile.DatabaseRepository, Version = version };
     try{
         access.Insert(track, transaction);
     }
     catch (Exception){
         access.Update(track, transaction);
     }
 }
        /// <summary>
        /// Used to get the current profile version
        /// </summary>
        /// <param name="profile"></param>
        /// <param name="transaction"></param>
        public double GetVersion(DatabaseVersionSetup.Profile profile, ITransaction transaction)
        {
            IConnection connection = profile.RepositoryProfile.GetDatabase();

            var access = new DoTrackDataAccess(() => connection, TableName);
            IDoTrack doTrack = access.Select(profile.RepositoryProfile.DatabaseRepository, transaction);
            if (doTrack != null) return doTrack.Version;
            //
            // todo: Rolf remove this as some stage
            //
            Log.Info("Tracker not found on repository name. Older version used database name. Trying on database name");
            doTrack = access.Select(profile.RepositoryProfile.DatabaseName, transaction);
            if (doTrack != null){
                Log.Info("Tracker found. Renaming to DatabaseRepository name");
                access.Delete(profile.RepositoryProfile.DatabaseName, transaction);
                doTrack.DatabaseName = profile.RepositoryProfile.DatabaseRepository;
                access.Insert(doTrack, transaction);
                return doTrack.Version;
            }

            return -1;
        }
コード例 #13
0
 /// <summary>
 /// Execute rollback
 /// </summary>
 public void ExecuteRollback(DatabaseVersionSetup.Profile profile, ITransaction transaction)
 {
     _rollbackStatement.ExecuteSql(profile, transaction);
 }
コード例 #14
0
 private void LoadRepositories(DatabaseVersionSetup versionSetup, IEnumerable<XElement> enumerable)
 {
     foreach (var element in enumerable){
         string value = element.Attribute("id").Value;
         var initializeRunners = ReadUpdates(element.Element("initialRunner"));
         XElement properties = element.Element("properties");
         KeyValuePair<string, string>[] props = LoadProperties(properties);
         var databaseRepository = new DatabaseRepository(value, initializeRunners[0]);
         databaseRepository.AddProperties(props);
         databaseRepository.Updates.AddRange(ReadUpdates(element.Element("updates")));
         versionSetup.Repository.Add(databaseRepository);
     }
     //check for duplicate repository ID
     if (versionSetup.Repository.Select(x => x.Id).Distinct().Count() != versionSetup.Repository.Count)
     {
         throw new Exception(string.Format("Duplicate repository ID names ({0})", string.Join(", ", versionSetup.Repository.Select(x => x.Id).ToArray())));
     }
 }
コード例 #15
0
 public void ExecuteSql(DatabaseVersionSetup.Profile profile, ITransaction transaction)
 {
     var sqlServerBulkExporterImporter = new SqlServerBulkExporterImporter(profile.RepositoryProfile.GetDatabase(), transaction);
     sqlServerBulkExporterImporter.ImportTableFromFile(_table,_fileName);
 }
コード例 #16
0
 public void Setup()
 {
     _databaseVersionSetup = new DatabaseVersionSetup();
     _mockIRepositoryProfile = new Mock<IRepositoryProfile>(MockBehavior.Strict);
     _databaseVersionSetup.DefaultProfile = DefaultProfile;
     var profile = new DatabaseVersionSetup.Profile(DefaultProfile, _mockIRepositoryProfile.Object);
     _mockITracker = new Mock<ITracker>(MockBehavior.Strict);
     _mockIDatabase = new Mock<IConnection>(MockBehavior.Strict);
     _mockITransaction = new Mock<ITransaction>(MockBehavior.Strict);
     _databaseVersionSetup.Profiles.Add(profile);
     _mockIRunner = new Mock<IRunner>(MockBehavior.Strict);
     var runner = new UpdatesMetadata(1, DateTime.MinValue.ToShortDateString(), "FirstUpdate", "Rolf", _mockIRunner.Object);
     _databaseVersionSetup.Repository.Add(new DatabaseRepository("Repo",runner));
     _mockIRepositoryProfile.Setup(mc => mc.DatabaseRepository)
         .Returns("Repo");
     _dvcController = new DvcController(_databaseVersionSetup, DefaultProfile, true);
 }
コード例 #17
0
        public DatabaseVersionSetup GetVersionSetup()
        {
            var versionSetup = new DatabaseVersionSetup();
            mockSqlServerDatabaseProfile = new Mock<SqlServerDatabaseProfile>(MockBehavior.Strict, "Data Source=GMV-RW-LT;Initial Catalog=Intercontinental;Integrated Security=True"
                , "Intercontinental4"
                , "Intercontinental"
                , _mockITracker.Object);

            var profile = new DatabaseVersionSetup.Profile("Default",
                                                        mockSqlServerDatabaseProfile.Object);
            profile.AddProperties("test1", "test1Prof|withvar|${dp.DatabaseName}|");
            profile.AddProperties("test2", "test2");
            profile.AddProperties("test2", "test2|over");
            profile.AddProperties("insert", "insert.sql");
            profile.AddProperties("update", "update.sql");
            profile.AddProperties("delete", "delete.sql");
            versionSetup.Profiles.Add(profile);

            var runner = new SqlRunner("CREATE DATABASE ${dp.DatabaseName};", "USE Master; DROP DATABASE ${dp.DatabaseName};");
            var repository = new DatabaseRepository("Intercontinental", new UpdatesMetadata(1, "Todau", "Initialize", "rolf", runner));
            repository.AddProperties("test0", "test0");
            repository.AddProperties("test1", "test1");
            var versions = new UpdatesMetadata(2, "2010-04-16", "First update", "Rolf Wessels",
                new SqlRunner(
                    @"Create2 ${test0} ${test1} ${test2} ${test3}",
                    @"RoleBack2 ${test0} ${test1} ${test2} ${test3}",
                    @"TestData2 ${test0} ${test1} ${test2} ${test3}"
                    )
                );
            repository.Updates.Add(versions);

            versions = new UpdatesMetadata(3, "2010-04-16", "First update", "Rolf Wessels",
                new SqlRunner(
                    @"Create3 ${test0} ${test1} ${test2} ${test3}",
                    @"RoleBack3 ${test0} ${test1} ${test2} ${test3}",
                    @"TestData3 ${test0} ${test1} ${test2} ${test3}"
                    )
                );
            repository.Updates.Add(versions);
            File.WriteAllText(@"resources\insert.sql", @"Create4 ${test0} ${test1} ${test2} ${test3}");
            File.WriteAllText(@"resources\update.sql", @"RoleBack4 ${test0} ${test1} ${test2} ${test3}");
            File.WriteAllText(@"resources\delete.sql", @"TestData4 ${test0} ${test1} ${test2} ${test3}");
            repository.Updates.Add(new UpdatesMetadata(4, "2010-04-16", "First update", "Rolf Wessels",
                new SqlRunner(new SqlFilesExecuter(new[] { @"resources\${insert}" }),
                    new SqlFilesExecuter(new[] { @"resources\${update}" }),
                    new SqlFilesExecuter(new[] { @"resources\${delete}" }))));

            versionSetup.Repository.Add(repository);

            return versionSetup;
        }
 /// <summary>
 /// Will initialize on initial setup
 /// </summary>
 /// <param name="profile"></param>
 /// <param name="transaction"></param>
 public void InitializeNewTracker(DatabaseVersionSetup.Profile profile, ITransaction transaction)
 {
     string replace = profile.VariableReplace(profile.RepositoryProfile is SqlServerDatabaseProfile? SqlServerCreateTracker : MysqSqlCreateTracker);
     replace = replace.Replace(TablePlaceHolder, TableName);
     profile.RepositoryProfile.GetDatabase().ExecuteSql(replace, transaction);
 }
コード例 #19
0
 /// <summary>
 /// Execute create statement
 /// </summary>
 public void ExecuteCommand(DatabaseVersionSetup.Profile profile, ITransaction transaction)
 {
     _commandStatement.ExecuteSql(profile, transaction);
 }