RavenDB schema migrations - ravendb

I want to change my RavenDB "schema" and perform a Migration at startup of ASP.Net MVC web app.
Patching mechanism seems to fit the purpose, and I try to use it like:
store.DatabaseCommands.UpdateByIndex(
"Raven/DocumentsByEntityName",
new IndexQuery
{
Query = "Tag:LogEntries",
},
new ScriptedPatchRequest()
{
Script = #"
this.IsDeleted = false;
"
}
).WaitForCompletion();
The problem is, that Patch will simply throw exception if the index is stale. But I really need to be sure that Migration was performed before continue to run the app. Are there better alternatives than wrapping it in something like:
while (true)
{
try
{
RunPatch();
break;
}
catch (Exception)
{
}
}
With Entity Framework or NHibernate (FluentMigrations) it's easy to write a Migration class, assign a Version to it, and then needed Migrations will be executed automatically in proper order.
Is there a similar built-in mechanism for Raven? Or any known best-practices for that?

Here's an answer, albeit 6 years after you asked! There is the RavenMigrations project on GitHub here:
https://github.com/migrating-ravens/RavenMigrations
This is a migration framework for RavenDB, similar to those that you will find for relational databases.

Related

Can I determine `IsDevelopment` from `IWebJobsBuilder`

Very much an XY problem, but I'm interested in the underlying answer too.
See bottom for XY context.
I'm in a .NET Core 3 AzureFunctions (v3) App project.
This code makes my question fairly clear, I think:
namespace MyProj.Functions
{
internal class CustomStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var isDevelopment = true; //Can I correctly populate this, such that it's true only for local Dev?
if(isDevelopment)
{
// Do stuff I wouldn't want to do in Prod, or on CI...
}
}
}
}
XY Context:
I have set up Swagger/Swashbuckle for my Function, and ideally I want it to auto-open the swagger page when I start the Function, locally.
On an API project this is trivial to do in Project Properties, but a Functions csproj doesn't have the option to start a web page "onDebug"; that whole page of project Properties is greyed out.
The above is the context in which I'm calling builder.AddSwashBuckle(Assembly.GetExecutingAssembly()); and I've added a call to Diagnostics.Process to start a webpage during Startup. This works just fine for me.
I've currently got that behind a [Conditional("DEBUG")] flag, but I'd like it to be more constrained if possible. Definitely open to other solutions, but I haven't been able to find any so ...
While I am not completely sure that it is possible in azure functions I think that setting the ASPNETCORE_ENVIRONMENT application setting as described in https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings should allow you to get whether the environment is set as production or development by injecting a IHostEnvironment dependency and checking
.IsDevelopment()
on the injected dependency.

Possible to Have Multiple Try/Catch Blocks Within Each Other in a SSIS Script?

I have a project that I'm working on and I wanted to double check before going through the work of coding and testing if this was even possible. What I'm attempting to do is something along the lines of:
try {
// Do stuff
try {
// Do other stuff
}
catch {
// Fail silently
}
// Do more stuff
}
catch (...) {
// Process error
}
Is it possible to have try/catch's within try/catch's? Most languages allow this but when I attempted to search the web I could not, for the life of me, find any answers.
Thanks
An SSIS script task is C# (or VB.NET). C# allows this, ergo SSIS allows this.
Having written a few SSIS Script Tasks in my time, I'd encourage you to take advantage of Raising Events in the Script Task.
Depending on your version + deployment model + invocation method, you might want to also turn on the native SSIS Logging. Project Deployment Model (in 2012+) provides native logging. Otherwise, you will need to specify the events you'd like to log as well as the logging provider(s) you'd like to use with them. This would need to be done as part of package development. Otherwise, I like a DTEXEC call with /REP EWI will ensure Errors, Warnings and Information events are logged to the console/SQL Agent.

Wix install with multiple services, and I don't how many

I need to install my windows service, and have at least one instance of it. The user decides how many services they will have. But which approach will I take?
Try to make my ServiceInstall foreach the number of windows services or install them with an customization via installutil?
and then I have to solve so it does not install duplicates. But I can make an xml file that contains the service names to be installed, and read from there.
But I have read that people don't like using foreach in wix because it complicates stuff much more, and some people say installutil isn't good either. But since I don't know how many services it will be I have to solve it something like this.
And with installutil i won't get rollback either?
Does anyone know another approach?
You can run custom action which patches your MSI including service elements to install. This approach allows you to read service names from wherever you want and install as many services as you need.
Though it would be quite non-trivial for those who read your installer source code later. And to be honest I've never used this approach. But it should work...
I ended up doing this
public static void Main(string[] args)
{
if (System.Environment.UserInteractive)
{
string parameter = string.Concat(args);
switch (parameter)
{
case "install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case "uninstall":
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
else
{
ServiceBase.Run(new WindowsService());
}
}
}
making my service installable and uninstallable on its own and then made customactions to install and uninstall them

Triggering EF migration at application startup by code

Using Entity Framework Migrations (Beta1), using Update-Database command is all good during development.
But when the application is running on some customer's server somewhere, I really want my application to automatically update it's database schema to the latest version when it's started.
Is this possible? Documentation is scarce.
They aren't providing a way to do this until RTM, at which point they have promised a command line app and a msdeploy provider.
Source: http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx
Of course not being satisfied with that, the powershell command is stored in the packages directory and is plain text, it appears to just load up an assembly called EntityFramework.Migrations.Commands stored in the same directory.
Tracing through that assembly I came up with the following
public class MyContext : DbContext
{
static MyContext()
{
DbMigrationsConfiguration configuration = new DbMigrationsConfiguration() {
MigrationsAssembly = typeof(MyContext).Assembly,
ContextType = typeof(MyContext),
AutomaticMigrationsEnabled = true,
};
DbMigrator dbMigrator = new DbMigrator(configuration);
dbMigrator.Update(null);
}
}
UPDATE: after a bit of experimentation I figured out a few more things
Performing an update in the static constructor for your context is bad as it breaks the powershell commands, much better off adding the code to application startup another way (Global.asax, WebActivator or Main method)
The above code only works when using AutomaticMigrations, you need to set the MigrationsNamespace for it to pickup on manually created migrations
The configuration class I was creating should already exist in your project (added when you install the migration nuget package), so just instantiate that instead.
Which means the code is simplified to
DbMigrator dbMigrator = new DbMigrator(new NAMESPACE.TO.MIGRATIONS.Configuration());
dbMigrator.Update(null);
Another options for this issue is to add
Database.SetInitializer<MyContext>(new MigrateDatabaseToLatestVersion<MyContext, NAMESPACE.TO.MIGRATIONS.Configuration>());
line to your Global.asax Application_Start method.

NHibernate Optimistic Concurrency

I'm investigating optimistic concurrency in NHibernate. I have a scenario that is very similar to what is being described here:
http://weblogs.asp.net/stefansedich/archive/2008/10/01/set-the-value-of-a-version-column-in-nhibernate-manually.aspx
Would you recommend going with the proposed solution in this blog post?
Thanks
The blog suggests using an interceptor to re-load the current version number from the database in order to perform a manual version check with the version passed in through the entity from a DTO object. This would certainly work, but as described in the article, it adds an extra DB hit to load the current version number.
The better solution, which seems pretty obvious since it's actually what's described in the documentation for "Application version checking" as described and quoted in that blog entry. That is, perform the version check on the initially loaded entity using the DTO's version. More specifically, using the code from the article (changes to the article's code are bold):
public void Update(MyDTO dto) {
// Select the item.
var item = this.repository.SelectById(dto.Id);
// Verify there hasn't been a concurrent change
if(item.Version != dto.Version)
{
throw new StaleObjectStateException();
}
// Map values from DTO to model.
item.Name = dto.Name;
item.Version = dto.Version;
// Call update
this.repository.Update(item);
}