private void CleanUpDB(DocumentStore documentStore) { // This can fail the very first time the application runs as it will not yet have created the necessary schema objects, // so we can just quietly move on try { var docSession = documentStore.OpenSession(); var configInfo = docSession.Load<Config>("DinnerParty/Config"); if(configInfo == null) { configInfo = new Config(); configInfo.Id = "DinnerParty/Config"; configInfo.LastTruncateDate = DateTime.Now.AddHours(-48); //No need to delete data if config doesnt exist but setup ready for next time docSession.Store(configInfo); docSession.SaveChanges(); return; } if((DateTime.Now - configInfo.LastTruncateDate).TotalHours < 24) return; long docCount = 0, dbSize = 0; using(var cmd = docSession.Connection.CreateCommand()) { var dinnerTableName = documentStore.Schema.MappingFor(typeof(Dinner)).Table.QualifiedName; cmd.CommandText = $"SELECT COUNT(*) FROM {dinnerTableName}"; docCount = (long)cmd.ExecuteScalar(); } using(var cmd = docSession.Connection.CreateCommand()) { var dinnerPartyDbName = docSession.Connection.Database; cmd.CommandText = $"SELECT pg_database_size('{dinnerPartyDbName}')"; dbSize = (long)cmd.ExecuteScalar(); configInfo.LastTruncateDate = DateTime.Now; docSession.SaveChanges(); } //If database size >15mb or 1000 documents delete documents older than a week if(docCount > 1000 || dbSize > 15000000) //its actually 14.3mb but goood enough { docSession.DeleteWhere<Dinner>(dp => dp.LastModified < DateTime.Now.AddDays(-7)); } } catch(Exception ex) { LogManager.GetCurrentClassLogger().Warn(ex, "Failed to clean up database"); } }
private void CleanUpDB(DocumentStore documentStore) { var docSession = documentStore.OpenSession(); var configInfo = docSession.Load<Config>("DinnerParty/Config"); if (configInfo == null) { configInfo = new Config(); configInfo.Id = "DinnerParty/Config"; configInfo.LastTruncateDate = DateTime.Now.AddHours(-48); //No need to delete data if config doesnt exist but setup ready for next time docSession.Store(configInfo); docSession.SaveChanges(); return; } else { if ((DateTime.Now - configInfo.LastTruncateDate).TotalHours < 24) return; configInfo.LastTruncateDate = DateTime.Now; docSession.SaveChanges(); //If database size >15mb or 1000 documents delete documents older than a week #if DEBUG var jsonData = documentStore.JsonRequestFactory.CreateHttpJsonRequest(null, "http://localhost:8080/database/size", "GET", documentStore.Credentials, documentStore.Conventions).ReadResponseJson(); #else var jsonData = documentStore.JsonRequestFactory.CreateHttpJsonRequest(null, "https://aeo.ravenhq.com/databases/DinnerParty-DinnerPartyDB/database/size", "GET", documentStore.Credentials, documentStore.Conventions).ReadResponseJson(); #endif int dbSize = int.Parse(jsonData.SelectToken("DatabaseSize").ToString()); long docCount = documentStore.DatabaseCommands.GetStatistics().CountOfDocuments; if (docCount > 1000 || dbSize > 15000000) //its actually 14.3mb but goood enough { documentStore.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName", new IndexQuery { Query = docSession.Advanced.LuceneQuery<object>() .WhereEquals("Tag", "Dinners") .AndAlso() .WhereLessThan("LastModified", DateTime.Now.AddDays(-7)).ToString() }, false); } } }