title: How to achieve zero downtime deployment to Azure web app with database updates?
body: <p>I'm trying to achieve <strong>zero downtime deployments</strong> of an Azure web app where <strong>database schema updates</strong> need to be applied as part of the deployment.</p>
  
  <p>I have the following setup:</p>
  
  <p><strong>Production</strong> web app with <strong>production-db</strong> connection string.</p>
  
  <p><strong>Staging</strong> deployment slot with <strong>staging-db</strong> connection string.</p>
  
  <p>My pseudo-deployment process is something like:</p>
  
  <ol>
  <li>Make a copy of the <strong>production-db</strong> database</li>
  <li>Configure the <strong>Staging</strong> slot to use the <strong>database copy</strong></li>
  <li>Deploy code to the <strong>Staging</strong> slot</li>
  <li>Apply <strong>schema updates</strong> to the database copy</li>
  <li>Test-run the app in the <strong>Staging</strong> slot (with the updated database) and pre-warm it</li>
  <li>If everything works, perform a <strong>hot swap</strong> so that the pre-warmed app in the <strong>Staging</strong> slot goes live, i.e. becomes the production slot, <strong>still using the updated database</strong></li>
  </ol>
  
  <p>In other words, after the hot swap, I want both the new code and the updated database to be live.</p>
  
  <p>If something goes wrong at this point, I can simply swap slots again, making the old production app (and its database) go live.</p>
  
  <p>As I understand it now, when I swap slots, the app will use the connection string of the production slot, restarting the app and forcing me to apply the database schema updates <em>after</em> the updated code goes live.</p>
  
  <p>I hope I'm describing this properly. :) This seems like it should be a fairly common scenario?</p>
  
  <p>Any help or pointers are greatly appreciated!</p>
  
  <p><strong>PS.</strong> I've looked at <a href="https://stackoverflow.com/questions/10597933/azure-seamless-upgrade-when-database-schema-changes">Azure seamless upgrade when database schema changes</a>, but that answer isn't valid, as I can't apply the schema updates without affecting the application.</p>
  
  <p><strong>Edit:</strong> Just a thought: perhaps I should skip putting the connection string as a portal setting, and instead just keep it in web.config. That way, when swapping, the app won't need to restart, and since web.config will be included in the swap, the new <strong>production</strong> slot will use the updated database.</p>
  
  <p><strong>Edit 2:</strong> I think I was mistaken about the app restart when app settings or connection strings differ between slots. It seems I can indeed set a different connection string (pointing to the database copy) for the deployment slot, and then swap without an app restart affecting website visitors.</p>