Integration Testing | Use DbUp for easy script execution while deploying DB

Spread the love

Pre-requisite:

  1. DbUp Setup for Easy Management and DB Upgrade (.Net) – FaltuTech.Com
  2. Configure Integration Test (.Net Core) – FaltuTech.Com

Now this is for DB first approach. So, we can’ use the migration scripts to automatically create a DB in docker container and run the scripts. What we could do then?

We will be executing the DbUp project’s main method (since it’s a console application) by passing some arguments.

E.g. Code to call at the place of dbContext.Database.EnsureCreated()

var args = new[] { "-x", "-c", connectionString };
DbUp.Program.Main(args);

And then in the main method of DbUp we will parse thse arguments:

 var argsList = args.ToList();
 var connectionStringIndex = argsList.IndexOf("-c");
 string? connectionStringInArg = connectionStringIndex > -1 
                                     ? argsList.ElementAtOrDefault(connectionStringIndex + 1) 
                                     : null;
 bool xArgument = args.Any(a => string.Compare(a, "-x") == 0);

Now your scripts will be deployed to the container. It’s that simple.

Cheers and Peace Out!!!