示例#1
0
        public void RemoveMigrationTest()
        {
            var historyRow = new MigrationHistoryRow
            {
                Prefix            = "prefix",
                AppliedMigrations = new List <MigrationHistoryRowDetails>
                {
                    new MigrationHistoryRowDetails
                    {
                        Name = "foo",
                        Hash = "Hash1"
                    },
                    new MigrationHistoryRowDetails
                    {
                        Name = "bar",
                        Hash = "Hash2"
                    }
                }
            };

            historyRow.RemoveMigration("foo");

            Assert.AreEqual(1, historyRow.AppliedMigrations.Count);
            var applied = historyRow.AppliedMigrations.First();

            Assert.AreEqual("bar", applied.Name);
        }
示例#2
0
        public void GetMigrationTest()
        {
            var historyRow = new MigrationHistoryRow
            {
                Prefix            = "prefix",
                AppliedMigrations = new List <MigrationHistoryRowDetails>
                {
                    new MigrationHistoryRowDetails
                    {
                        Name = "Detail1",
                        Hash = "Hash1"
                    },
                    new MigrationHistoryRowDetails
                    {
                        Name = "Detail2",
                        Hash = "Hash2"
                    }
                }
            };

            var detail = historyRow.GetMigration("Detail1");

            Assert.IsNotNull(detail);
            Assert.AreEqual("Detail1", detail.Name);
        }
示例#3
0
        private void PushDummyMigrationHistoryMessage()
        {
            var migrationHistoryRow = new MigrationHistoryRow {
                Prefix = "test"
            };

            migrationHistoryRow.AppliedMigrations.Add(new MigrationHistoryRowDetails
            {
                Name           = "001_TestMigration",
                Hash           = "a0b87bef6d840b00ac344eb2a204442760794512bb8bc0873b63d8c7d5849e9f",
                DownOperations = new List <BaseOperation>
                {
                    new DeleteQueueOperation().SetName("bar"),
                    new DeleteExchangeOperation().SetName("foo")
                }
            });
            var migrationHistory = new MigrationHistory();

            migrationHistory.AllMigrations.Add(migrationHistoryRow);

            using (var connection = _connectionFactory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    var messageBody = JsonConvertHelper.SerializeObjectToByteArray(migrationHistory);
                    channel.BasicPublish("", Constants.HistoryQueue, false, null, messageBody);
                }
        }
        public void UpdateAppliedMigrations(MigrationHistoryRow appliedMigration)
        {
            Guard.ArgumentNotNull(nameof(appliedMigration), appliedMigration);

            var allAppliedMigrations = GetAllAppliedMigrations();

            if (allAppliedMigrations.AllMigrations.Any(x => x.Prefix == appliedMigration.Prefix))
            {
                var currentMigrations = allAppliedMigrations.AllMigrations.First(x => x.Prefix == appliedMigration.Prefix);

                currentMigrations.AppliedMigrations = appliedMigration.AppliedMigrations;
            }
            else
            {
                allAppliedMigrations.AllMigrations.Add(appliedMigration);
            }

            using var connection = _connectionFactory.CreateConnection();
            using var model      = connection.CreateModel();
            model.BasicGet(Constants.HistoryQueue, true);

            var messageText  = JsonConvertHelper.SerializeObjectToByteArray(allAppliedMigrations);
            var messageProps = model.CreateBasicProperties();

            messageProps.Persistent = true;
            model.BasicPublish(Constants.DefaultExchange, Constants.HistoryQueue, messageProps, messageText);
        }
示例#5
0
        public void TestUpdateAppliedMigrationsExistingPrefix()
        {
            _rabbitMqHistory.Init();
            SetupDefaultExchange();

            PushDummyMigrationHistoryMessage();
            var migrationHistoryRow = new MigrationHistoryRow {
                Prefix = "test"
            };

            migrationHistoryRow.AppliedMigrations.Add(new MigrationHistoryRowDetails {
                Name = "001_TestMigration"
            });
            migrationHistoryRow.AppliedMigrations.Add(new MigrationHistoryRowDetails {
                Name = "002_TestMigrationAddQueue"
            });

            _rabbitMqHistory.UpdateAppliedMigrations(migrationHistoryRow);

            using (var connection = _connectionFactory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    var message = channel.BasicGet(Constants.HistoryQueue, true);
                    Assert.IsNotNull(message);

                    var migrationHistory = JsonConvertHelper.DeserializeObject <MigrationHistory>(message.Body);
                    Assert.IsNotNull(migrationHistory);
                    Assert.AreEqual(1, migrationHistory.AllMigrations.Count);
                    var migration = migrationHistory.AllMigrations.First();
                    Assert.AreEqual("test", migration.Prefix);
                    Assert.AreEqual(2, migration.AppliedMigrations.Count);
                    CollectionAssert.AreEqual(migrationHistoryRow.AppliedMigrations.ToList(), migration.AppliedMigrations.ToList(), new AppliedMigrationsComparer());
                }
        }
示例#6
0
        public void TestMigrationAllReadyUp2DateNoHash()
        {
            var rabbitServer      = new RabbitServer();
            var connectionFactory = new FakeConnectionFactory(rabbitServer);

            var history = new Mock <IRabbitMqHistory>();

            history.Setup(x => x.GetAppliedMigrations(It.IsAny <string>())).Returns <string>(prefix =>
                                                                                             new MigrationHistoryRow {
                Prefix = prefix, AppliedMigrations = new List <MigrationHistoryRowDetails> {
                    new MigrationHistoryRowDetails {
                        Name = "001_TestMigration"
                    }
                }
            });
            MigrationHistoryRow result = null;

            history.Setup(x => x.UpdateAppliedMigrations(It.IsAny <MigrationHistoryRow>()))
            .Callback <MigrationHistoryRow>(x => result = x);

            var migrator = new RabbitMqMigrator(connectionFactory, history.Object);

            migrator.UpdateModel("UnitTest");

            Assert.AreEqual(0, rabbitServer.Exchanges.Count);
            Assert.AreEqual(0, rabbitServer.Queues.Count);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.AppliedMigrations.Count);
            Assert.AreEqual("001_TestMigration", result.AppliedMigrations.First().Name);
            Assert.IsNotNull(result.AppliedMigrations.First().Hash);
        }
        public static void AddMigration(this MigrationHistoryRow historyRow, KeyValuePair <string, RabbitMqMigration> migration)
        {
            Guard.ArgumentNotNull(nameof(historyRow), historyRow);

            historyRow.AppliedMigrations.Add(new MigrationHistoryRowDetails
            {
                Name           = migration.Key,
                Hash           = migration.Value?.CalculateHash(),
                DownOperations = migration.Value?.DownOperations
            });
        }
示例#8
0
        public void TestMigrationMigrationNotPresentAnymore()
        {
            var rabbitServer      = new RabbitServer();
            var connectionFactory = new FakeConnectionFactory(rabbitServer);

            using (var connection = connectionFactory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare("UnitTest.new_foo", ExchangeType.Direct, true);
                    channel.QueueDeclare("UnitTest.new_bar", true);
                }

            var history = new Mock <IRabbitMqHistory>();

            history.Setup(x => x.GetAppliedMigrations(It.IsAny <string>())).Returns <string>(prefix =>
                                                                                             new MigrationHistoryRow
            {
                Prefix            = prefix,
                AppliedMigrations = new List <MigrationHistoryRowDetails>
                {
                    new MigrationHistoryRowDetails {
                        Name = "001_TestMigration"
                    },
                    new MigrationHistoryRowDetails
                    {
                        Name           = "002_TestMigration",
                        Hash           = "a6964237ffad49ed9492dca9318f9d793c64aa6d6c7645b7c0db0db9f68d3659",
                        DownOperations = new List <BaseOperation>
                        {
                            new DeleteQueueOperation().SetName("new_bar"),
                            new DeleteExchangeOperation().SetName("new_foo")
                        }
                    }
                }
            });
            MigrationHistoryRow result = null;

            history.Setup(x => x.UpdateAppliedMigrations(It.IsAny <MigrationHistoryRow>()))
            .Callback <MigrationHistoryRow>(x => result = x);

            var migrator = new RabbitMqMigrator(connectionFactory, history.Object);

            migrator.UpdateModel("UnitTest");

            Assert.AreEqual(0, rabbitServer.Exchanges.Count);
            Assert.AreEqual(0, rabbitServer.Queues.Count);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.AppliedMigrations.Count);
            Assert.AreEqual("001_TestMigration", result.AppliedMigrations.First().Name);
            Assert.IsNotNull(result.AppliedMigrations.First().Hash);
        }
示例#9
0
        public void AddMigrationTest()
        {
            var historyRow = new MigrationHistoryRow
            {
                Prefix            = "prefix",
                AppliedMigrations = new List <MigrationHistoryRowDetails>()
            };

            historyRow.AddMigration(new KeyValuePair <string, RabbitMqMigration>("foo", new TestMigration()));

            Assert.AreEqual(1, historyRow.AppliedMigrations.Count);
            var applied = historyRow.AppliedMigrations.First();

            Assert.AreEqual("foo", applied.Name);
            Assert.IsNotNull(applied.Hash);
            Assert.IsNotNull(applied.DownOperations);
        }
        private void PushDummyMigrationHistoryMessage()
        {
            var migrationHistoryRow = new MigrationHistoryRow {
                Prefix = "test"
            };

            migrationHistoryRow.AppliedMigrations.Add("001_TestMigration");
            var migrationHistory = new MigrationHistory();

            migrationHistory.AllMigrations.Add(migrationHistoryRow);

            using (var connection = _connectionFactory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    var messageBody = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(migrationHistory));
                    channel.BasicPublish("", Constants.HistoryQueue, false, null, messageBody);
                }
        }
示例#11
0
        private void PushDummyMigrationHistoryMessage()
        {
            var migrationHistoryRow = new MigrationHistoryRow {
                Prefix = "test"
            };
            var migrationHistory = new MigrationHistory {
                Version = 3
            };

            migrationHistory.AllMigrations.Add(migrationHistoryRow);

            using (var connection = _connectionFactory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    var messageBody = JsonConvertHelper.SerializeObjectToByteArray(migrationHistory);
                    channel.BasicPublish("", Constants.HistoryQueue, false, null, messageBody);
                }
        }
示例#12
0
        public void TestRevertMigrations()
        {
            var rabbitServer      = new RabbitServer();
            var connectionFactory = new FakeConnectionFactory(rabbitServer);

            using (var connection = connectionFactory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare("UnitTest.foo", ExchangeType.Direct, true);
                    channel.QueueDeclare("UnitTest.bar", true);
                }

            var history = new Mock <IRabbitMqHistory>();

            history.Setup(x => x.GetAppliedMigrations(It.IsAny <string>())).Returns <string>(prefix =>
                                                                                             new MigrationHistoryRow
            {
                Prefix            = prefix,
                AppliedMigrations = new List <MigrationHistoryRowDetails> {
                    new MigrationHistoryRowDetails {
                        Name = "001_TestMigration"
                    }
                }
            });
            MigrationHistoryRow result = null;

            history.Setup(x => x.UpdateAppliedMigrations(It.IsAny <MigrationHistoryRow>()))
            .Callback <MigrationHistoryRow>(x => result = x);

            var migrator = new RabbitMqMigrator(connectionFactory, history.Object);

            migrator.RevertAll("UnitTest");

            Assert.AreEqual(0, rabbitServer.Exchanges.Count);
            Assert.AreEqual(0, rabbitServer.Queues.Count);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.AppliedMigrations.Count);
        }
        public static MigrationHistoryRowDetails GetMigration(this MigrationHistoryRow historyRow, string migrationName)
        {
            Guard.ArgumentNotNull(nameof(historyRow), historyRow);

            return(historyRow.AppliedMigrations.FirstOrDefault(x => x.Name == migrationName));
        }
        public static void RemoveMigration(this MigrationHistoryRow historyRow, string migrationName)
        {
            Guard.ArgumentNotNull(nameof(historyRow), historyRow);

            historyRow.AppliedMigrations.Remove(historyRow.AppliedMigrations.First(x => x.Name == migrationName));
        }