You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -49,12 +49,17 @@ To run this sample in your subscription, make sure to fork the repository into y
49
49
50
50
## Repo branches
51
51
52
-
This repo has different branches that shows the development at different stages. This is the branch 4.0.
52
+
This repo has different branches that shows the development at different stages. This is the branch 5.0.
53
53
54
54
- 1.0: First version, no database support
55
55
- 2.0: Database support added
56
-
- 3.0: Authentication and Authorization
57
-
- 4.0: [This Branch] Resilient connections using Polly
56
+
- 3.0: Authentication and Authorization
57
+
- 4.0: Resilient connections using Polly
58
+
- 5.0: [This Branch] Database imperative or declarative CI/CD
59
+
60
+
### V5.0 Notes
61
+
62
+
Continuing from V4.0, this branch focuses on including the database in the deployment pipeline and gives you the option to choose between an imperative or a declarative database deployment style.
58
63
59
64
### V4.0 Notes
60
65
@@ -66,7 +71,7 @@ In this branch the backend REST API service and the database are modified so tha
66
71
67
72
### V2.0 Notes
68
73
69
-
In this branch the backend REST API service is modified so that the to-do list can be saved an manged using an Azure SQL database. Communication with the database is done using JSON too, as Azure SQL support [JSON natively](https://docs.microsoft.com/sql/relational-databases/json/json-data-sql-server?view=sql-server-ver15).
74
+
In this branch the backend REST API service is modified so that the to-do list can be saved an manged using an Azure SQL database. Communication with the database is done using JSON too, as Azure SQL support [JSON natively](https://docs.microsoft.com/sql/relational-databases/json/json-data-sql-server).
70
75
71
76
### V1.0 Notes
72
77
@@ -76,7 +81,7 @@ In this branch the solution will have a full working front-end, sending REST req
76
81
77
82
-`/api`: the NodeJs Azure Function code used to provide the backend API, called by the Vue.Js client.
78
83
-`/client`: the Vue.Js client. Original source code has been taken from official Vue.js sample and adapted to call a REST client instead of using local storage to save and retrieve todos
79
-
-`/database`: the database scripts and the database deployment tool
84
+
-`/database`: the database scripts and the database deployment tools
80
85
81
86
## Install the dependencies
82
87
@@ -98,38 +103,45 @@ npm i -g @azure/static-web-apps-cli
98
103
99
104
If you don't have a Azure SQL server already, you can create one (no additional costs for a server) running the following [AZ CLI](https://docs.microsoft.com/cli/azure/) command (via [WSL](https://docs.microsoft.com/windows/wsl/), or Linux or [Azure Cloud Shell](https://azure.microsoft.com/features/cloud-shell/)):
100
105
101
-
102
106
```sh
103
107
az sql server create -n <server-name> -l <location> --admin-user <admin-user> --admin-password <admin-password> -g <resource-group>
104
108
```
105
109
106
110
Create a new Azure SQL database:
107
111
108
112
```sh
109
-
az sql db create -g <resource-group> -s <server-name> -n todo_v4 --service-objective GP_Gen5_2
113
+
az sql db create -g <resource-group> -s <server-name> -n todo_v5 --service-objective GP_Gen5_2
110
114
```
111
115
112
-
Another option is to run the `azure-create-sql-db.sh` script in the `./databases` folder. The script uses the ARM template available in the same folder to create a server and a `todo_v4` database.
116
+
Another option is to run the `azure-create-sql-db.sh` script in the `./databases` folder. The script uses the ARM template available in the same folder to create a server and a `todo_v5` database.
113
117
114
118
Make sure you have the firewall configured to allow your machine to access Azure SQL:
115
119
116
-
```
120
+
```sh
117
121
az sql server firewall-rule create --resource-group <resource-group> --server <server-name> --name AllowMyClientIP_1 --start-ip-address <your_public_ip> --end-ip-address <your_public_ip>
118
122
```
119
123
120
124
you can get your public IP from here, for example: https://ifconfig.me/
121
125
122
126
## Deploy the database
123
127
124
-
Database is deployed using [DbUp](http://dbup.github.io/). Switch to the `./database/deploy` folder and create new `.env` file containing the connection string to the created Azure SQL database. You can use the provide `.env.template` as a guide. The connection string look like:
128
+
There are two may strategies that can be used: imperative and declarative. The imperative approach is the one that has been used until now, where the script to create the database objects are applied in the defined order. The declarative approach is instead where you have the database state (a snapshot of the schema) you want to have and a tool will take care of making all the correct changes to bring the target database to that state.
129
+
130
+
In both cases the first step is to have at hand the connection string needed to connect to create Azure SQL database. Switch to the `./database/` folder:
131
+
132
+
```sh
133
+
cd ./database
134
+
```
135
+
136
+
and create new `.env` file, that will contain the aforementioned connection string. Use the provided `.env.template` as a guide. The connection string look like:
replace the placeholder with the correct value for your database, username and password and you're good to go. Make sure the database user specified in the connection string has enough permission to create objects (for example, make sure is a server administrator or in the db_owner database role).
142
+
replace the placeholders with the correct value for your database, username and password and you're good to go. Make sure the database user specified in the connection string has enough permission to create objects (for example, make sure is a server administrator or in the db_owner database role).
131
143
132
-
Please note that using the server administrator login is not recommended as is way to powerful. If you are testing this on a sample server that you'll not use for production purposes, that shouldn't be an issue. But if want to be on the safe side and implement a correct security process you can create a user that will be used only for running the deployment script:
144
+
Please note that using the server administrator login is not recommended as is way too powerful. If you are testing this on a sample server that you'll not use for production purposes, that shouldn't be an issue. But if want to be on the safe side and implement a correct security process you can create a user that will be used only for running the deployment script:
133
145
134
146
```sql
135
147
create user [deployment_user] with password ='<a_strong_password>';
@@ -139,17 +151,19 @@ alter role [db_owner] add member [deployment_user]
139
151
go
140
152
```
141
153
142
-
Once you have configured the connection string, you can deploy the database objects:
154
+
### Imperative Deployment
155
+
156
+
Database is deployed using [DbUp](http://dbup.github.io/). The scripts that will be deployed are in the `./imperative-deploy/sql` folder. Once you have configured the connection string as explained before, you can deploy the database objects:
143
157
144
158
```sh
145
-
cd ./database/deploy
159
+
cd ./imperative-deploy/dbup
146
160
dotnet run
147
161
```
148
162
149
-
you will see something like:
163
+
you will see something like:
150
164
151
165
```sh
152
-
Deploying database: todo_v4
166
+
Deploying database: todo_v5
153
167
Testing connection...
154
168
Starting deployment...
155
169
Beginning database upgrade
@@ -169,9 +183,72 @@ Success!
169
183
170
184
Database has been deployed successfully!
171
185
186
+
### Declarative Deployment
187
+
188
+
Database scripts are stored in the `./database/declarative-deploy/todo_v5` folder, which contains also the related [MSBuild SQL Project](https://techcommunity.microsoft.com/t5/azure-sql-blog/microsoft-build-sql-the-next-frontier-of-sql-projects/ba-p/3290628).
189
+
190
+
A SQL Project can be build using the standard `dotnet build` action and it will generate a `.dacpac` file that contains the description of all the database objects that are needed to make your solution work: the database state you need to have deployed. If you want to work on the database project is recommended that you use Visual Studio Code or Azure Data Studio with the "SQL Database Projects" extension.
191
+
192
+
Deployment will be done via the `sqlpackage` tool. Make sure it is installed and added to your PATH as per the instructions here: [Download and install SqlPackage](https://docs.microsoft.com/en-us/sql/tools/sqlpackage/sqlpackage-download).
193
+
194
+
Once `sqlpackage` is installed you can deploy the database using the `./database/declarative-deployment/azure-deploy-sql-db.sh` script to automate the whole (build + deploy) process:
195
+
196
+
```sh
197
+
cd ./declarative-deployment
198
+
./azure-deploy-sql-db.sh
199
+
```
200
+
201
+
you'll see something like:
202
+
203
+
```sh
204
+
Loading from ../.env
205
+
Building .dacpac...
206
+
MSBuild version 17.3.0+92e077650 for .NET
207
+
Determining projects to restore...
208
+
All projects are up-to-date for restore.
209
+
Creating a model to represent the project...
210
+
Loading project references...
211
+
Loading project files...
212
+
Building the project model and resolving object interdependencies...
213
+
Validating the project model...
214
+
Writing model to /mnt/w/_git/_owned/azure-sql-db-fullstack-serverless-kickstart/database/declarative-deploy/todo_v5/obj/Debug/Model.xml...
Publishing to database 'todo_v5' on server 'zv6qimpc6cbrg.database.windows.net'.
226
+
Initializing deployment (Start)
227
+
Initializing deployment (Complete)
228
+
Analyzing deployment plan (Start)
229
+
Analyzing deployment plan (Complete)
230
+
Updating database (Start)
231
+
Creating SqlSchema [web]...
232
+
Creating SqlTable [dbo].[todos]...
233
+
Creating SqlDefaultConstraint unnamed constraint on [dbo].[todos]...
234
+
Creating SqlSequence [dbo].[global_sequence]...
235
+
Creating SqlDefaultConstraint unnamed constraint on [dbo].[todos]...
236
+
Creating SqlProcedure [web].[delete_todo]...
237
+
Creating SqlProcedure [web].[get_todo]...
238
+
Creating SqlProcedure [web].[patch_todo]...
239
+
Creating SqlProcedure [web].[post_todo]...
240
+
Update complete.
241
+
Updating database (Complete)
242
+
Successfully published database.
243
+
Time elapsed 0:00:44.97
244
+
Done.
245
+
```
246
+
247
+
Database has been deployed successfully!
248
+
172
249
## Test solution locally
173
250
174
-
Before starting the solution locally, you have to configure the Azure Function that is used to provide the backed API. In the `./api` folder create a `local.settings.json` file starting from the provided template. All you have to do is update the connection string with the value correct for you solution. If have created the Azure SQL database as described above you'll have a database named `todo_v4`. Just make sure you add the correct server name in the `local.settings.json`. The database name, user login and password are already set in the template file to match those used in this repository and in the `./database/sql/01-create-objects.sql` file.
251
+
Before starting the solution locally, you have to configure the Azure Function that is used to provide the backed API. In the `./api` folder create a `local.settings.json` file starting from the provided template. All you have to do is update the connection string with the value correct for you solution. If have created the Azure SQL database as described above you'll have a database named `todo_v5`. Just make sure you add the correct server name in the `local.settings.json`. The database name, user login and password are already set in the template file to match those used in this repository and in the `./database/sql/01-create-objects.sql` file.
175
252
176
253
To run Azure Functions locally, for example to debug them, you also need a local Azure Storage emulator. You can use [Azurite](https://docs.microsoft.com/azure/storage/common/storage-use-azurite?tabs=visual-studio) that also has a VS Code extension.
177
254
@@ -215,7 +292,9 @@ The generated GitHub Action doesn't know that we are using a database to store t
215
292
216
293
- AZURE_SQL_CONNECTION_STRING
217
294
218
-
The `AZURE_SQL_CONNECTION_STRING` is the connection string that can be used to deploy the database. You can use the same connection string used for deploying the database objects. You can find it in the `./database/deploy/.env` file.
295
+
The `AZURE_SQL_CONNECTION_STRING` is the connection string that can be used to deploy the database. You can use the same connection string used for deploying the database objects. You can find it in the `./database/.env` file.
296
+
297
+
### Imperative Deployment on Azure
219
298
220
299
Then you have to add the following code, just before the `Build And Deploy` step, to the file you'll find in `./.github/workflow`:
221
300
@@ -225,13 +304,31 @@ Then you have to add the following code, just before the `Build And Deploy` step
The file `./.github/workflow/azure-static-web-apps.yml.sample` shows an example of how the yaml should look like. Commit and push the changes and the deployment will start again, this time deploying also the database objects.
313
+
The file `./.github/workflow/azure-static-web-apps-imperative-db-deploy.yml.sample` shows an example of how the yaml should look like. Commit and push the changes and the deployment will start again, this time deploying also the database objects.
314
+
315
+
If you also want to deploy the Azure SQL server and database within the same pipeline, you can do so by using the provided ARM template `./database/azure-sql-db.arm.json` and the [Deploy ARM GitHub Action](https://github.com/Azure/arm-deploy).
316
+
317
+
318
+
### Declarative Deployment on Azure
319
+
320
+
Then you have to add the following code, just before the `Build And Deploy` step, to the file you'll find in `./.github/workflow`:
The file `./.github/workflow/azure-static-web-apps-declarative-db-deploy.yml.sample` shows an example of how the yaml should look like. Commit and push the changes and the deployment will start again, this time deploying also the database objects.
235
332
236
333
If you also want to deploy the Azure SQL server and database within the same pipeline, you can do so by using the provided ARM template `./database/azure-sql-db.arm.json` and the [Deploy ARM GitHub Action](https://github.com/Azure/arm-deploy).
0 commit comments