/
Setup and run DB Migration

Setup and run DB Migration

This page provides detailed instructions on how to execute the migration scripts for the database schema updates and data transformations. The migration project is designed to ensure that all required database changes are applied seamlessly, including updates to tables, foreign key constraints, and data corrections.

As part of this process, you will be executing SQL migration scripts that will modify the structure and content of your database. These migrations are critical to ensuring that the database remains aligned with the evolving application requirements, and that data integrity and relationships are maintained.

This documentation will guide you step-by-step through the process of executing the migration scripts in Visual Studio. Additionally.

By following this guide, you will ensure the database is properly migrated to the latest schema and that all business logic requirements are met.

 


Clone Repository.

1. Open Visual Studio

  • Launch Visual Studio and ensure you’re signed into your account if necessary.

2. Clone the Repository

  • Go to: File > Clone Repository...

  • Choose your Repository:

    • In the Clone a repository window, you have two main options:

      • GitHub/GitLab/Bitbucket Repository: If the repository is hosted on one of these platforms, sign in to your account and select the repository from your list.

      • Direct URL: Paste the URL Coral Backend Repository in the Repository location field.

  • Set Local Path:

    • Choose a local folder where you want to clone the repository.

  • Click Clone:

    • Visual Studio will pull down all files and the current branches from the repository to your local environment.

3. Open the Solution

  • Once the repository is cloned, navigate to the solution file (.sln) within the cloned folder in Visual Studio, and open it.


Setup Migration Project.

In the Solution Explorer window, find the coral.DBMigrations project.

Right-click on coral.DBMigrations and select Set as StartUp Project. This will ensure that coral.DBMigrations is the default project that runs when you start debugging or executing the solution.

image-20241111-180642.png

To specify the environment and tenant for your database migration in the Coral.DBMigrations project, you need to set the Environment and Tenant enums located in the Program.cs file. These enums determine which database environment and project the migration will be applied to.

Here are the instructions:

Locate the Program.cs File

In Visual Studio, open the Coral.DBMigrations project.

Navigate to the Program.cs file, which is typically located under the main directory of the Coral.DBMigrations project.

Set the Environment Enum (Line 55)

The Environment enum defines the database environment to apply the migration to.

On line 55, update the enum to specify the environment you want:

LOCALHOST
DEV
QA
STAGE

Example: To apply the migration to the QA environment:

Environment environment = Environment.QA;

Set the Tenant Enum (Line 56)

The Tenant enum specifies the database project (or tenant) for the migration.

On line 56, update the enum to specify the tenant for your migration:

DEFAULT
MontgomeryPA
PalmBeach

Example: To apply the migration to the PalmBeach tenant:

Tenant tenant = Tenant.PalmBeach;

Save Your Changes

After setting the desired Environment and Tenant values, save the changes in Program.cs.


Open Package Manager Console

  • In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Console.

  • This will open the Package Manager Console at the bottom of Visual Studio, where you can execute commands.

2. Select the Project Containing Your Migrations

  • In the Default Project dropdown in the Package Manager Console, select the project where the migrations are defined (e.g., coral.DBMigrations).

3. Run the Update-Database Command

  • In the Package Manager Console, type the following command and press Enter:

    Update-Database -Context Main
  • The -Context Main parameter specifies the particular DbContext you want to use. This context should be configured to handle the specific database setup associated with your migrations.

  • Use -Context Central to apply migrations to the Central database.

Add label

Related content