Skip to content

Commit d72ffeb

Browse files
committed
Add PostgreSQL instruction
1 parent 5fbe22f commit d72ffeb

File tree

6 files changed

+196
-4
lines changed

6 files changed

+196
-4
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.jpg filter=lfs diff=lfs merge=lfs -text

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ Welcome to a custom Copilot workshop prepared by GitHub’s Expert Service team!
66

77
1. [Story about Mona's dream to make her dream a reality](docs/1_Story/README.md)
88
2. [Before getting started](docs/2_BeforeGettingStarted/README.md)
9-
3. Build a PostgreSQL database layer using Docker container
9+
3. [Build a PostgreSQL database layer using Docker container](docs/3_BuildPostgreSQL/README.md)
10+
4. Deploy the PostgreSQL package to GitHub Packages

docs/2_BeforeGettingStarted/README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Before getting started
22

3-
[Prev - Story about Mona's dream to make her dream a reality](../1_Story/README.md.md) | [Next - Build a PostgreSQL database layer using Docker container](./3_Build_a_PostgreSQL_database_layer_using_Docker_container.md)
3+
[Prev - Story about Mona's dream to make her dream a reality](../1_Story/README.md.md) | [Next - Build a PostgreSQL database layer using Docker container](../3_BuildPostgreSQL/README.md)
44

55
## High Level View about what we will build
66

@@ -23,9 +23,23 @@ As a GitHub engineer who delivers this workshop, you should make sure that you h
2323
- Access to Copilot license
2424
- Access to Copilot Chat with Copilot for Business License
2525
- Visual Studio Code with Copilot and Copilot Chat
26-
- Docker
26+
- Docker with an ability to get base images from DockerHub
2727
- You have Python and PIP installed on your computer and can install dependencies
2828
- You have NodeJS installed
29+
- Have Git CLI installed for Microsoft Windows or Termina with git for Mac or Linux
2930

30-
[Prev - Story about Mona's dream to make her dream a reality](../1_Story/README.md) | [Next - Build a PostgreSQL database layer using Docker container](./3_Build_a_PostgreSQL_database_layer_using_Docker_container.md)
31+
## Expected Outcomes
3132

33+
At the end of this workshop, you should be able to build a full stack web application that has a Python Django backend, ReactJS front end, and a PostgreSQL database layer with GitHub Copilot.
34+
35+
## Recommended setup
36+
37+
It is recommended that you created a separate parent folder with individual folders for each of the following components:
38+
- `backend` - Python Django backend
39+
- `frontend` - ReactJS frontend
40+
- `database` - PostgreSQL database layer
41+
- `automation` - Automation scripts to build the application infrastructure layer
42+
43+
![Recommended setup](images/0_FolderStructure.jpg)
44+
45+
[Prev - Story about Mona's dream to make her dream a reality](../1_Story/README.md) | [Next - Build a PostgreSQL database layer using Docker container](../3_BuildPostgreSQL/README.md)

docs/3_BuildPostgreSQL/README.md

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Let’s build a PostgreSQL database layer
2+
3+
[Prev - Before getting started](../2_BeforeGettingStarted/README.md) | [Next - Deploy the PostgreSQL package to GitHub Packages](../4_DeployPostgreSQL/README.md)
4+
5+
Try to open **Visual Studio Chat** and enter the following.
6+
7+
```bash
8+
Can you help me to create a Dockerfile that helps to run on localhost with following conditions:
9+
10+
- Create a new PostgreSQL database table called “cats”. Name my SQL file as 'create-data.sql'
11+
- Insert more than 10 random data representing different cats into the table named “cats”
12+
```
13+
14+
This should generate some information including Dockerfile, a SQL script, and instruction on how to build and to run Dockerfile. First, try to create Dockerfile and `create-data.sql`` files using **Insert into New File** option
15+
16+
![Insert into New File](images/0_InsertIntoNewFile.jpg)
17+
18+
> :memo **TIP:** Once you created these files, you may want to make some random changes by deleting and adding some comments to see how Copilot can quickly generate some corrected data. For example, you may want to temporarily change cats to dogs in create-database.sql and remove the lines to insert the data into the database table. Then, write some comment like `Insert some 10 random data into the table`` and see how Copilot can generate data.
19+
20+
Next, you want to build the database using Dockerfile. Before building a Docker container, you want to inspect Dockerfile and make some changes. For example, you may want to modify database connection string (password, username, etc) as well as checking whether SQL file name is correct.
21+
22+
GitHub Copilot that may or may not show a Docker build command, but you may want to make sure to change the container name so it is meaningful. For example, I renamed it to `custom-database-layer`. Make sure to cd into the directory and execute inside the directory that has Dockerfile.
23+
24+
```bash
25+
docker build -t custom-database-layer:1.0 .
26+
```
27+
28+
![Docker build](images/1_DockerBuildPostgreSQL.jpg)
29+
30+
You can then verify using the following command:
31+
32+
```bash
33+
docker images
34+
```
35+
36+
Copilot Chat should also show how to run the image. However, we want to add some additional options so it is easier to stop and to remove.
37+
38+
```bash
39+
docker run -p 5432:5432 --rm --detach --interactive --name custom-database-layer -d custom-database-layer:1.0
40+
```
41+
42+
Please refer to the [troubleshooting guide](../docs/TroubleshootingGuide) if you see any errors. If you see a process ID, you may want to check it is running with the following:
43+
44+
```bash
45+
docker ps
46+
```
47+
48+
Since the database layer is now working, we want to quickly check that the database is actually working. If you use Visual Studio Code, you can install an extension like PostgreSQL to retrieve the data like below:
49+
50+
![PostgreSQL extension](images/2_PostgreSQLClient.jpg)
51+
52+
But you can also quickly create a script to retrieve data or insert data. Type the following command in Copilot Chat.
53+
54+
```bash
55+
Perfect. Can you help me to create a Python script that checks and prints data from local PostgreSQL?
56+
```
57+
58+
This will generate a Python script file that can helps to connect to local PostgreSQL database. But before that, you need to install a dependency called `psycopg2-binary` that is used in the script.
59+
60+
> :memo **TIP:** One of main issues that often happen with Copilot Chat is that lack of dependencies that are clearly defined and version incompatibility. This is one of the main areas that Copilot needs to get better and require developer’s knowledge.
61+
62+
```bash
63+
pip install psycopg2-binary
64+
```
65+
66+
You may need to run as pip3 if your environment is set differently.
67+
68+
![pip install psycopg2-binary](images/3_InstallPsycopg2PG2.jpg)
69+
70+
Now, create a test script with a file named `retrieve-data-db.py`
71+
72+
![Create retrieve data](images/4_CreateRetrieveDataDB.jpg)
73+
74+
Once you created the file, take a look at it. You may have noticed that the database connection string did not get updated as specified from Dockerfile. You may want to update those with the right information.
75+
76+
![Update database connection string](images/5_UpdateDBConnection.jpg)
77+
78+
And run the command. You may need to run python3 like I am.
79+
80+
```bash
81+
python retrieve-data-db.py
82+
```
83+
84+
![Run retrieve data](images/6_DatabaseResult.jpg)
85+
86+
But what if we want to test something else like inserting new data? Type the following command into the Copilot chat:
87+
88+
```bash
89+
Can you help me to create a Python script to insert new data into the database table?
90+
```
91+
92+
And it should generate a new Python script. Name it something like `insert-data-db.py` and run it to check. You may want to run `python test-db--connection.py` to make sure that data exists.
93+
94+
![Inserted data](images/7_DatabaseAfterInserted.jpg)
95+
96+
Before moving to the next step, here is one important thing to think about. Right now, we are running this as a local container, so it is alright to store those database credentials within the code itself.
97+
98+
> :memo **TIP:** This is a type of judgment that you have to make as a programmer to know what is right or wrong from a correctness perspective. Even if your code will work from a logical point of view, you have to know whether this is really effective, secure, ethical, and efficient. After all, you are the main pilot, and Github Copilot is just your assistant pilot.
99+
100+
However, if we later move those into a shared space, we want those credentials to be read from somewhere safe and be flexible. Thus, we need to change the credentials to be more secure. Type this into Copilot Chat:
101+
102+
```bash
103+
What can I do if I want to replace database connection strings in Dockerfile with environment variables that can be read from somewhere else?
104+
```
105+
106+
Then, a new Dockerfile with credentials changed to ARG will be printed out.
107+
108+
![Dockerfile with ARG](images/8_DockerfileWithARG.jpg)
109+
110+
You can select all lines in Dockerfile and click **Insert at Cursor** in Copilot Chat to insert that Dockerfile.
111+
112+
![Correct Dockerfile](images/9_CorrectDockerfile.jpg)
113+
114+
However, this Dockerfile has an error. After every FROM statement, all the ARGs get collected and are no longer available. Be careful with multi-stage builds. Thus, you need to move ARG from before FROM to right below FROM.
115+
116+
Since we created a new Dockerfile, we need to rebuild our container. Let’s stop our container first.
117+
118+
```bash
119+
docker ps
120+
docker stop <PID>
121+
```
122+
123+
Then, let’s rebuild our docker images with the following command:
124+
125+
```bash
126+
docker build –build-arg POSTGRES_USER=admin –build-arg POSTGRES_PASSWORD=P@ssw0rd –build-arg POSTGRES_DB=cat_db -t custom-database-layer:2.0 .
127+
```
128+
129+
Then, let’s run the image after checking with docker images
130+
131+
```bash
132+
docker images
133+
134+
docker run -p 5432:5432 --rm --detach --interactive --name custom-database-layer -d custom-database-layer:2.0
135+
```
136+
137+
Make sure to test connection again. We are successful! Now, let’s move onto the next step.
138+
139+
[Prev - Before getting started](../2_BeforeGettingStarted/README.md) | [Next - Deploy the PostgreSQL package to GitHub Packages](../4_DeployPostgreSQL/README.md)

docs/TroubleshootingGuide/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Troubleshooting guide
2+
3+
## Problem: You are getting an error port is getting used: docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:5432: bind: address already in use.
4+
5+
Terminate the Terminal. Run the following command.
6+
7+
```bash
8+
sudo lsof -i :5432
9+
10+
sudo kill -9 <PID>
11+
```
12+
13+
## Problem: docker: Error response from daemon: Conflict. The container name "/custom-database-layer" is already in use by container. You have to remove (or rename) that container to be able to reuse that name.
14+
15+
```bash
16+
docker ps -a
17+
18+
docker rm <PS ID>
19+
```
20+
21+
## Problem: My Docker container fails to start. What can I do?
22+
23+
Try to launch image with sh like this:
24+
25+
```bash
26+
docker run -it — rm — entrypoint sh Container Image Name:Container Image Version
27+
```
28+
29+
For example...
30+
31+
32+
```bash
33+
docker run -it — rm — entrypoint sh custom-database-layer:2.0
34+
```
35+
36+
And try to troubleshoot with commands like printenv.

0 commit comments

Comments
 (0)