Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Entity Framework migrations during Octopus Deploy CI to Azure

I need to set up a continuous integration process to deploy our application as an Azure cloud service, using Octopus Deploy. This process includes a step that executes Entity Framework 6.1 migrations against our Azure SQL database (by running migrate.exe from the local Octopus tentacle). However, port 1433 would need to be opened on the Octopus machine for this to work, and our admin won't do that.

Is there a different way you can suggest for having Entity Framework migrations executed during the automated deploy process?

like image 386
dopoto Avatar asked Apr 02 '15 06:04

dopoto


2 Answers

We ended up opening that port, as we couldn't find any other solution. For reference, here's the script we're running (our Deploy.ps1 script, executed by NuGet on each deployment).

# SOURCE: http://danpiessens.com/blog/2014/06/10/deploying-databases-with-octopus-deploy-part-2/

# Get the exe name based on the directory
$contentPath = (Join-Path $OctopusOriginalPackageDirectoryPath "content")
$fullPath = (Join-Path $OctopusOriginalPackageDirectoryPath "content\migrate.exe")

Write-Host "Content Path:" $contentPath
Write-Host "Migrate Path:" $fullPath

cd $contentPath
write-host "Working Dir: "$(get-location)

# Run the migration utility

& "$fullPath" MyApp.Data.dll /startUpConfigurationFile=MyApp.Web.dll.config /connectionString=$ApplicationConnectionString /connectionProviderName="System.Data.SqlClient" /verbose | Write-Host
like image 150
dopoto Avatar answered Nov 15 '22 11:11

dopoto


I run the migrations on application start using this code:

    class ApplicationDataContext : DbContext
    {
        internal static void UpdateDatabase()
        {
            Database.SetInitializer<ApplicationDataContext>(null);

            var settings = new Migrations.Configuration();
            var migrator = new DbMigrator(settings);
            migrator.Update();

        }
}
like image 45
Murariu Serban Avatar answered Nov 15 '22 12:11

Murariu Serban