Esempio n. 1
0
		public void Will_limit_replication_history_size_on_items_marked_with_not_for_replication()
		{
			var store1 = CreateStore();

			using (var session = store1.OpenSession())
			{
				var entity = new Company {Name = "Hibernating Rhinos"};
				session.Store(entity);
				session.Advanced.GetMetadataFor(entity)["Raven-Not-For-Replication"] = "true";
				session.SaveChanges();
			}

			for (int i = 0; i < 100; i++)
			{
				using (var session = store1.OpenSession())
				{
					var company = session.Load<Company>(1);
					company.Name = i%2 == 0 ? "a" : "b";
					session.SaveChanges();
				}
			}

			using (var session = store1.OpenSession())
			{
				var company = session.Load<Company>(1);
				var ravenJArray = session.Advanced.GetMetadataFor(company).Value<RavenJArray>(Constants.RavenReplicationHistory);
				Assert.Equal(50, ravenJArray.Length);
			}

		}
Esempio n. 2
0
		public void Can_add_entity_with_expiry_then_read_it_before_it_expires()
		{
			var company = new Company {Name = "Company Name"};
			var expiry = SystemTime.UtcNow.AddMinutes(5);
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.Advanced.GetMetadataFor(company)["Raven-Expiration-Date"] = new RavenJValue(expiry);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var company2 = session.Load<Company>(company.Id);
				Assert.NotNull(company2);
				var metadata = session.Advanced.GetMetadataFor(company2);
				var expirationDate = metadata["Raven-Expiration-Date"];
				Assert.NotNull(expirationDate);
				DateTime dateTime;
				try
				{
					dateTime = expirationDate.Value<DateTime>();
				}
				catch (Exception e)
				{
					throw new IOException("Could not convert " + expirationDate + " " + expirationDate.Type + " value: " + expirationDate.Value<object>() + " " +
										  " type: " + expirationDate.Value<object>().GetType().AssemblyQualifiedName, e);
				}
				Assert.Equal(DateTimeKind.Utc, dateTime.Kind);
				Assert.Equal(expiry, expirationDate);
			}
		}
Esempio n. 3
0
        public void StoreAndLoad()
        {
            const string CompanyName = "Company Name";
            var company = new Company { Name = CompanyName };
            using (var session = documentStore.OpenSession())
            {
                session.Store(company);
                session.SaveChanges();
            }

            using (var session = documentStore.OpenSession())
            {
                Assert.Equal(company.Name, session.Load<Company>(1).Name);
            }

            AssertPlainTextIsNotSavedInDatabase(CompanyName);
        }
Esempio n. 4
0
		public void Will_automatically_set_metadata()
		{
			var company = new Company { Name = "Company Name" };
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var company2 = session.Load<Company>(company.Id);
				var metadata = session.Advanced.GetMetadataFor(company2);
				Assert.Equal("Current", metadata.Value<string>("Raven-Document-Revision-Status"));
				Assert.Equal(1, metadata.Value<int>("Raven-Document-Revision"));
			}
		}
Esempio n. 5
0
		public void Will_automatically_create_duplicate_on_first_insert()
		{
			var company = new Company {Name = "Company Name"};
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var company2 = session.Load<Company>(company.Id + "/revisions/1");
				var metadata = session.Advanced.GetMetadataFor(company2);
				Assert.Equal(company.Name, company2.Name);
				Assert.Equal("Historical", metadata.Value<string>("Raven-Document-Revision-Status"));
			}
		}
Esempio n. 6
0
 public void Can_add_entity_with_expiry_but_will_not_be_able_to_read_it_after_expiry()
 {
     var company = new Company { Name = "Company Name" };
     var expiry = DateTime.UtcNow.AddMinutes(5);
     using (var session = documentStore.OpenSession())
     {
         session.Store(company);
         session.Advanced.GetMetadataFor(company)["Raven-Expiration-Date"] = new RavenJValue(expiry);
         session.SaveChanges();
     }
     SystemTime.UtcDateTime = () => DateTime.UtcNow.AddMinutes(10);
    
     using (var session = documentStore.OpenSession())
     {
         var company2 = session.Load<Company>(company.Id);
         Assert.Null(company2);
     }
 }
Esempio n. 7
0
		public void Can_add_entity_with_expiry_then_read_it_before_it_expires()
		{
			var company = new Company {Name = "Company Name"};
			var expiry = DateTime.UtcNow.AddMinutes(5);
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.Advanced.GetMetadataFor(company)["Raven-Expiration-Date"] = new RavenJValue(expiry);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var company2 = session.Load<Company>(company.Id);
				Assert.NotNull(company2);
				var metadata = session.Advanced.GetMetadataFor(company2);
				var expirationDate = metadata.Value<DateTime>("Raven-Expiration-Date");
				Assert.Equal(DateTimeKind.Utc, expirationDate.Kind);
				Assert.Equal(expiry, expirationDate);
			}
		}
Esempio n. 8
0
		public async Task Can_get_all_revisions_async()
		{
			var company = new Company { Name = "Company Name" };
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
				Assert.Equal(1, session.Advanced.GetMetadataFor(company).Value<int>("Raven-Document-Revision"));
			}
			using (var session = documentStore.OpenSession())
			{
				var company3 = session.Load<Company>(company.Id);
				company3.Name = "Hibernating Rhinos";
				session.SaveChanges();
				Assert.Equal(2, session.Advanced.GetMetadataFor(company3).Value<int>("Raven-Document-Revision"));
			}
			using (var session = documentStore.OpenAsyncSession())
			{
				var companiesRevisions = await session.Advanced.GetRevisionsForAsync<Company>(company.Id, 0, 25);
				Assert.Equal("Company Name", companiesRevisions[0].Name);
				Assert.Equal("Hibernating Rhinos", companiesRevisions[1].Name);
			}
		}
Esempio n. 9
0
		public void Previously_deleted_docs_will_survive_export_import_cycle_if_purge_is_false()
		{
			using (var session = documentStore.OpenSession())
			{
				session.Store(new VersioningConfiguration
				{
					Exclude = false,
					PurgeOnDelete = false,
					Id = "Raven/Versioning/Companies",
					MaxRevisions = 5
				});
				session.SaveChanges();
			}

			var company = new Company { Id = "companies/1", Name = "Company Name" };

			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
				company.Name = "Company Name 2";
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var doc = session.Load<Company>("companies/1");
				Assert.Equal(2, session.Advanced.GetMetadataFor(doc).Value<int>("Raven-Document-Revision"));

				session.Delete(doc);
				session.SaveChanges();
			}

            var file = Path.GetTempFileName();
		    try
			{
				new SmugglerApi().ExportData(new SmugglerExportOptions { ToFile = file, From = new RavenConnectionStringOptions { Url = documentStore.Url, DefaultDatabase = documentStore.DefaultDatabase } }).Wait();

				using (var documentStore2 = CreateDocumentStore(port: 8078))
				{
					var importSmuggler = new SmugglerApi();
					importSmuggler.ImportData(
						new SmugglerImportOptions
						{
							FromFile = file,
							To = new RavenConnectionStringOptions
							{
								Url = documentStore2.Url,
								Credentials = documentStore2.Credentials,
								DefaultDatabase = documentStore2.DefaultDatabase
							}
						}).Wait();

					using (var session = documentStore2.OpenSession())
					{
						session.Store(company);
						session.SaveChanges();
						Assert.Equal(3, session.Advanced.GetMetadataFor(company).Value<int>("Raven-Document-Revision"));
					}

					using (var session = documentStore2.OpenSession())
					{
						var doc = session.Load<Company>("companies/1");
						doc.Name = "Company Name 3";
						session.SaveChanges();
						Assert.Equal(4, session.Advanced.GetMetadataFor(doc).Value<int>("Raven-Document-Revision"));
					}
				}
			}
			finally
			{
                if (File.Exists(file))
				{
                    File.Delete(file);
				}
			}
		}
Esempio n. 10
0
		public void After_a_put_delete_put_sequence_Will_continue_revision_numbers_from_last_value_if_purge_is_false()
		{
			using (var session = documentStore.OpenSession())
			{
				session.Store(new VersioningConfiguration
				{
					Exclude = false,
					PurgeOnDelete = false,
					Id = "Raven/Versioning/Companies",
					MaxRevisions = 5
				});
				session.SaveChanges();
			}

			var company = new Company { Id = "companies/1", Name = "Company Name" };

			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
				company.Name = "Company Name 2";
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var doc = session.Load<Company>("companies/1");
				var metadata = session.Advanced.GetMetadataFor(doc);
				Assert.Equal(2, metadata.Value<int>("Raven-Document-Revision"));

				session.Delete(doc);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();

				var metadata = session.Advanced.GetMetadataFor(company);
				Assert.Equal(3, metadata.Value<int>("Raven-Document-Revision"));
			}
		}
Esempio n. 11
0
		public void Will_not_delete_child_revisions_if_purge_is_false()
		{
			using (var session = documentStore.OpenSession())
			{
				session.Store(new VersioningConfiguration
				{
					Exclude = false,
					PurgeOnDelete = false,
					Id = "Raven/Versioning/Companies"
				});

				session.SaveChanges();
			}

			var company = new Company { Name = "Company Name" };
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var doc = session.Load<object>("companies/1");
				Assert.NotNull(doc);

				session.Delete(doc);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var doc = session.Load<object>("companies/1/revisions/1");
				Assert.NotNull(doc);
			}
		}
Esempio n. 12
0
		public void Will_delete_revisions_if_version_is_deleted()
		{
			var company = new Company { Name = "Company Name" };
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var doc = session.Load<object>("companies/1/revisions/1");
				var comp = session.Load<object>("companies/1");
				Assert.NotNull(doc);

				session.Delete(comp);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var doc = session.Load<object>("companies/1/revisions/1");
				Assert.NotNull(doc);

				session.Advanced.Defer(new DeleteCommandData
				{
					Key = "companies/1/revisions/1",
					TransactionInformation = new TransactionInformation()
				});
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var doc = session.Load<object>("companies/1/revisions/1");
				Assert.Null(doc);
			}
		}
Esempio n. 13
0
		public void Will_not_delete_revisions_if_parent_exists()
		{
			var company = new Company { Name = "Company Name" };
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var doc = session.Load<object>("companies/1/revisions/1");
				Assert.NotNull(doc);

				session.Advanced.Defer(new DeleteCommandData
				{
					Key = "companies/1/revisions/1",
					TransactionInformation = new TransactionInformation()
				});

				Assert.Throws<ErrorResponseException>(() => session.SaveChanges());
			}
		}
Esempio n. 14
0
		public void Will_delete_old_revisions()
		{
			var company = new Company { Name = "Company #1" };
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
				for (int i = 0; i < 10; i++)
				{
					company.Name = "Company #" + i + 2;
					session.SaveChanges();
				}
			}

			using (var session = documentStore.OpenSession())
			{
				for (int i = 1; i < 7; i++)
				{
					Assert.Null(session.Load<Company>(company.Id + "/revisions/" + i));
				}

				for (int i = 7; i < 12; i++)
				{
					Assert.NotNull(session.Load<Company>(company.Id + "/revisions/" + i));
				}

				for (int i = 12; i < 21; i++)
				{
					Assert.Null(session.Load<Company>(company.Id + "/revisions/" + i));
				}
			}
		}
Esempio n. 15
0
		public void Will_automatically_create_duplicate_on_next_insert()
		{
			var company = new Company { Name = "Company Name" };
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
				Assert.Equal(1, session.Advanced.GetMetadataFor(company).Value<int>("Raven-Document-Revision"));
			}
			using (var session = documentStore.OpenSession())
			{
				var company3 = session.Load<Company>(company.Id);
				company3.Name = "Hibernating Rhinos";
				session.SaveChanges();
				Assert.Equal(2, session.Advanced.GetMetadataFor(company3).Value<int>("Raven-Document-Revision"));
			}
			using (var session = documentStore.OpenSession())
			{
				var company2 = session.Load<Company>(company.Id + "/revisions/1");
				var metadata = session.Advanced.GetMetadataFor(company2);
				Assert.Equal("Company Name", company2.Name);
				Assert.Equal("Historical", metadata.Value<string>("Raven-Document-Revision-Status"));
				Assert.Null(metadata.Value<string>("Raven-Document-Parent-Revision"));

				company2 = session.Load<Company>(company.Id + "/revisions/2");
				metadata = session.Advanced.GetMetadataFor(company2);
				Assert.Equal("Hibernating Rhinos", company2.Name);
				Assert.Equal("Historical", metadata.Value<string>("Raven-Document-Revision-Status"));
				Assert.Equal("companies/1/revisions/1", metadata.Value<string>("Raven-Document-Parent-Revision"));
			}
		}
Esempio n. 16
0
		public void After_expiry_passed_document_will_be_physically_deleted()
		{
			var company = new Company
			{
				Id = "companies/1",
				Name = "Company Name"
			};
			var expiry = DateTime.UtcNow.AddMinutes(5);
			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.Advanced.GetMetadataFor(company)["Raven-Expiration-Date"] = new RavenJValue(expiry);
				session.SaveChanges();

				session.Advanced.LuceneQuery<Company>("Raven/DocumentsByExpirationDate")
					.WaitForNonStaleResults()
					.ToList();
			}
			SystemTime.UtcDateTime = () => DateTime.UtcNow.AddMinutes(10);

			using (var session = documentStore.OpenSession())
			{
				session.Store(new Company
				{
					Id = "companies/2",
					Name = "Company Name"
				});
				session.SaveChanges(); // this forces the background task to run
			}

			JsonDocument documentByKey = null;
			for (int i = 0; i < 100; i++)
			{
				ravenDbServer.Database.TransactionalStorage.Batch(accessor =>
				{
					documentByKey = accessor.Documents.DocumentByKey("companies/1", null);
				});
				if (documentByKey == null)
					return;
				Thread.Sleep(100);
			}
			Assert.False(true, "Document was not deleted");
		}
Esempio n. 17
0
		public void Will_remove_tombstones_when_deleting_and_creating_new_item_with_same_id()
		{
			var store1 = CreateStore();
			var store2 = CreateStore();

			TellFirstInstanceToReplicateToSecondInstance();
			var item = new Company {Name = "Hibernating Rhinos"};
			using (var session = store1.OpenSession())
			{
				session.Store(item);
				session.SaveChanges();
			}

			Company company = null;
			for (int i = 0; i < RetriesCount; i++)
			{
				using (var session = store2.OpenSession())
				{
					company = session.Load<Company>("companies/1");
					if (company != null)
						break;
					Thread.Sleep(100);
				}
			}
			Assert.NotNull(company);
			Assert.Equal("Hibernating Rhinos", company.Name);

			using (var session = store1.OpenSession())
			{
				session.Delete(session.Load<Company>("companies/1"));
				session.SaveChanges();
			}


			Company deletedCompany = null;
			for (int i = 0; i < RetriesCount; i++)
			{
				using (var session = store2.OpenSession())
					deletedCompany = session.Load<Company>("companies/1");
				if (deletedCompany == null)
					break;
				Thread.Sleep(100);
			}
			Assert.Null(deletedCompany);

			using (var session = store1.OpenSession())
			{
				session.Store(item);
				session.SaveChanges();
			}

			for (int i = 0; i < RetriesCount; i++)
			{
				using (var session = store2.OpenSession())
				{
					company = session.Load<Company>("companies/1");
					if (company != null)
						break;
					Thread.Sleep(100);
				}
			}
			Assert.NotNull(company);
			Assert.Equal("Hibernating Rhinos", company.Name);

			foreach (var ravenDbServer in servers)
			{
				ravenDbServer.Database.TransactionalStorage.Batch(
					accessor => Assert.Null(accessor.Lists.Read("Raven/Replication/Docs/Tombstones", "companies/1")));
			}
		}
Esempio n. 18
0
		public void Will_not_replicate_replicated_documents()
		{
			var store1 = CreateStore();
			var store2 = CreateStore();

			TellFirstInstanceToReplicateToSecondInstance();

			TellSecondInstanceToReplicateToFirstInstance();
			Company company = null;

			string etag;
			string id;
			using (var session = store1.OpenSession())
			{
				 company = new Company { Name = "Hibernating Rhinos" };
				session.Store(company);
				session.SaveChanges();
				id = company.Id;
				session.Advanced.Clear();
				company = session.Load<Company>(id);
				etag = session.Advanced.GetMetadataFor(company).Value<string>("@etag");
			}



			for (int i = 0; i < RetriesCount; i++)
			{
				using (var session = store2.OpenSession()) // waiting for document to show up.
				{
					company = session.Load<Company>(id);
					if (company != null)
						break;
					Thread.Sleep(100);

				}
			}
			Assert.NotNull(company);
			Assert.Equal("Hibernating Rhinos", company.Name);

			// assert that the etag haven't changed (we haven't replicated)
			for (int i = 0; i < 15; i++)
			{
				using (var session = store1.OpenSession())
				{
					company = session.Load<Company>(id);
					Assert.Equal(etag, session.Advanced.GetMetadataFor(company).Value<string>("@etag"));
				}
				Thread.Sleep(100);
			}
		}
Esempio n. 19
0
		public void Previously_deleted_docs_will_survive_export_import_cycle_if_purge_is_false()
		{
			using (var session = documentStore.OpenSession())
			{
				session.Store(new VersioningConfiguration
				{
					Exclude = false,
					PurgeOnDelete = false,
					Id = "Raven/Versioning/Companies",
					MaxRevisions = 5
				});
				session.SaveChanges();
			}

			var company = new Company { Id = "companies/1", Name = "Company Name" };

			using (var session = documentStore.OpenSession())
			{
				session.Store(company);
				session.SaveChanges();
				company.Name = "Company Name 2";
				session.SaveChanges();
			}

			using (var session = documentStore.OpenSession())
			{
				var doc = session.Load<Company>("companies/1");
				Assert.Equal(2, session.Advanced.GetMetadataFor(doc).Value<int>("Raven-Document-Revision"));

				session.Delete(doc);
				session.SaveChanges();
			}

			var options = new SmugglerOptions { BackupPath = Path.GetTempFileName() };
			try
			{
				var exportSmuggler = new SmugglerApi(options, new RavenConnectionStringOptions { Url = documentStore.Url });
				exportSmuggler.ExportData(options);

				using (CreateRavenDbServer(port: 8078))
				using (var documentStore2 = CreateDocumentStore(port: 8078))
				{
					var importSmuggler = new SmugglerApi(options, new RavenConnectionStringOptions { Url = documentStore2.Url });
					importSmuggler.ImportData(options);

					using (var session = documentStore2.OpenSession())
					{
						session.Store(company);
						session.SaveChanges();
						Assert.Equal(3, session.Advanced.GetMetadataFor(company).Value<int>("Raven-Document-Revision"));
					}

					using (var session = documentStore2.OpenSession())
					{
						var doc = session.Load<Company>("companies/1");
						doc.Name = "Company Name 3";
						session.SaveChanges();
						Assert.Equal(4, session.Advanced.GetMetadataFor(doc).Value<int>("Raven-Document-Revision"));
					}
				}
			}
			finally
			{
				if (File.Exists(options.BackupPath))
				{
					File.Delete(options.BackupPath);
				}
			}
		}