Setting up local database to simulate Azure production database

My Context

General Context

To accelerate the development of new SaaS ideas, I created a versatile code base that includes essential features such as a landing page, login system, and payment integration. For this project, I selected Django for the backend, Bootstrap for the frontend, and Stripe for payment processing. The deployment is planned on Azure.

I cloned my existing code base and began modifying it to develop my first SaaS application

Technical Context

I am currently developing in a Windows machine using WSL2 with Ubuntu 22.04 to run Docker (followed the steps of this awesome Gist to set it up). I code using VS Code from the Ubuntu system.

  • VS Code version 1.92
  • SQL Database Projects version 1.4.3

The Problem

I have an existing SQL database in Azure, created from a scraping project using Azure Functions. My new SaaS needs to access this data. However, I prefer not to modify the existing Azure database during development.

The Solution

 I considered two options:

  • Create a development instance in Azure by copying the existing database..
  • Set up a local database that mirrors the existing one..

I chose the second option to avoid additional Azure costs, as this is a side project.

Install SQL Database Projects VS Code Extension

The SQL Database Projects extension in VS Code allows you to create database projects from existing Azure databases. It can build a .dacpac file and publish it to a local container.

Create a database project from existing database

Open SQL Database Projects extension. Click the three dots at the top and select “Create Project From Database.”

Choose an existing connection profile or create a new one by providing the server name and database.

Select the database.

Enter a project name.

Select a location to save the project.

The project will be show in the Database Project sidebar extension.

Build and Publish the project

Before proceeding, ensure that the Docker service is running by executing “sudo service docker start”. The build process generates a “.dacpac” file, which is a package that contains the database schema. To build your project, right-click over the root node of your database project than click in “Build”.

At this stage, I encountered an error related to the .NET SDK. To resolve this, I installed the .NET SDK on my Ubuntu system by following the instructions in the “Install .NET SDK or .NET Runtime on Ubuntu“.

Once the build is complete, you can publish the project. Right-click the root node of your database project again and choose “Publish.” You will be prompted to select a publishing destination. The first option creates a local container running SQL Server, while the second option allows you to publish to an existing container if you already have one set up.

I opted for the first option to create a new container. The subsequent prompts will ask for connection details and a password for the database admin login (the default username is “sa”). You can proceed with the default settings and set a strong password for the “sa” user.

After publishing, you will see the SQL Server container running in the VS Code Docker extension.

You can connect to the database using Azure Data Studio. Create a new connection with “localhost, 1433” as the server, “sa” as the username, and your defined password.

By setting up a local database that mirrors my Azure database, I can develop and test efficiently. I can track modifications using a Git repository and eventually update my Azure database with the changes.

Sources

Leave a Reply

Your email address will not be published. Required fields are marked *