@@ -16,29 +16,46 @@ DB_VERSION=
1616## Start Database using Docker
1717
1818``` bash
19- $ docker run -d -p 5432:5432 -v todos:/var/lib/postgresql/data --name postgres postgres:10.4
19+ docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres - v todos:/var/lib/postgresql/data --name postgres postgres:16
2020```
2121
22- If the database was not initialized, we can solve this by:
22+ If the database was not initialized, we can solve this by running :
2323
2424``` bash
25- $ docker exec -i postgres psql -U postgres < create_todos_db .sql
25+ docker exec -i postgres psql -h localhost - U postgres < todos_db .sql
2626```
2727
2828Notice, that since we have a volume we will not need to run again the above code.
2929
30+ (Optional) We can check that the new data base and data ahs been created by running the following commands:
31+
32+ ``` bash
33+ docker exec -it postgres psql -h localhost -U postgres
34+ ```
35+
36+ ``` psql
37+ \l
38+ \c todos_db
39+ select * from todos;
40+ \q
41+ ```
42+
43+
3044## Migrations
3145
32- To create a new ` knexfile.js ` run:
46+ To create a new ` knexfile.js ` run:
3347
3448``` bash
35- $ $( npm bin ) / knex init
49+ npx knex init
3650```
3751
3852### Creating a Table
3953
4054``` bash
41- $ $( npm bin) /knex migrate:make create_todos_table
55+ npx knex migrate:make create_todos_table
56+ ```
57+
58+ ```
4259Using environment: development
4360Using environment: development
4461Using environment: development
@@ -48,48 +65,57 @@ Created Migration: /Users/jaimesalaszancada/Documents/paths/ci_cd-path/04_gitlab
4865We can create the migration code as follows:
4966
5067``` js
51- exports .up = function (knex ) {
52- return knex .schema .createTable (' todos' , function (table ) {
53- table .increments (' id' );
54- table .string (' title' , 255 ).notNullable ();
55- table .boolean (' completed' ).notNullable ();
56- });
68+ exports .up = function (knex ) {
69+ return knex .schema .createTable (' todos' , function (table ) {
70+ table .increments (' id' );
71+ table .string (' title' , 255 ).notNullable ();
72+ table .boolean (' completed' ).notNullable ();
73+ });
5774};
5875
59- exports .down = function (knex ) {
60- return knex .schema .dropTable (' users' );
76+ exports .down = function (knex ) {
77+ return knex .schema .dropTable (' users' );
6178};
6279```
6380
6481To see pending migrations
6582
6683``` bash
67- $ $( npm bin) /knex migrate:list
84+ npx knex migrate:list
85+ ```
86+
87+ ```
6888Using environment: development
69- No Completed Migration files Found.
89+ No Completed Migration files Found.
7090Found 1 Pending Migration file/files.
719120201122205735_create_todos_table.js
7292```
7393
7494To run the migration
7595
7696``` bash
77- $ $( npm bin) /knex migrate:up 20201122205735_create_todos_table.js
97+ npx knex migrate:up 20201122205735_create_todos_table.js
98+ ```
99+
100+ ```
78101Using environment: development
79102Batch 1 ran the following migrations:
8010320201122205735_create_todos_table.js
81104```
82105
83106### Updating table
84107
85- * The previous table must be updated with two new columns:
86- - due_date -> ` datetime `
87- - order -> ` int `
108+ - The previous table must be updated with two new columns:
109+ - due_date -> ` datetime `
110+ - order -> ` int `
88111
89112As before we create the migration
90113
91114``` bash
92- $ $( npm bin) /knex migrate:make update_todos_table
115+ npx knex migrate:make update_todos_table
116+ ```
117+
118+ ```
93119Using environment: development
94120Using environment: development
95121Using environment: development
@@ -99,32 +125,40 @@ Created Migration: /Users/jaimesalaszancada/Documents/paths/ci_cd-path/04_gitlab
99125And the migration will look as follows:
100126
101127``` js
102- exports .up = function (knex ) {
128+ exports .up = function (knex ) {
103129 return knex .schema .table (' todos' , (t ) => {
104- t .datetime (' due_date' );
105- t .integer (' order' );
130+ t .datetime (' due_date' );
131+ t .integer (' order' );
106132 });
107133};
108134
109- exports .down = function (knex ) {
110- return knex .schema .table (' todos' , (t ) => {
111- t .dropColumn (' due_date' );
112- t .dropColumn (' order' );
113- });
135+ exports .down = function (knex ) {
136+ return knex .schema .table (' todos' , (t ) => {
137+ t .dropColumn (' due_date' );
138+ t .dropColumn (' order' );
139+ });
114140};
115141```
116142
117143To apply all pending migrations, we can run:
118144
119145``` bash
120- $ $( npm bin) /knex migrate:list
146+ npx knex migrate:list
147+ ```
148+
149+ ```
121150Using environment: development
122- No Completed Migration files Found.
151+ No Completed Migration files Found.
123152Found 2 Pending Migration file/files.
124- 20201122205735_create_todos_table.js
125- 20201123104711_update_todos_table.js
153+ 20201122205735_create_todos_table.js
154+ 20201123104711_update_todos_table.js
155+ ```
126156
127- $ $( npm bin) /knex migrate:latest
157+ ``` bash
158+ npx knex migrate:latest
159+ ```
160+
161+ ```
128162Using environment: development
129163Batch 1 run: 2 migrations
130164```
@@ -159,20 +193,24 @@ development: {
159193# .....
160194```
161195
162- Now to generate the seed file we must run ` $(npm bin)/ knex seed:make todos`
196+ Now to generate the seed file we must run ` npx knex seed:make todos`
163197
164198``` bash
165- $ $( npm bin) /knex seed:make todos
199+ npx knex seed:make todos
200+ ```
201+
202+ ```
166203Using environment: development
167204Using environment: development
168205Using environment: development
169206Created seed file: /Users/jaimesalaszancada/Documents/paths/ci_cd-path/04_gitlab_or/todo-app-react/data/seeds/todos.js
170207```
171208
172209``` js
173- exports .seed = function (knex ) {
210+ exports .seed = function (knex ) {
174211 // Deletes ALL existing entries
175- return knex (' todos' ).truncate ()
212+ return knex (' todos' )
213+ .truncate ()
176214 .then (function () {
177215 // Inserts seed entries
178216 return knex (' todos' ).insert ([
@@ -182,69 +220,78 @@ exports.seed = function(knex) {
182220 ]);
183221 });
184222};
185-
186223```
187224
188225To run the seed now we can do
189226
190227``` bash
191- $ $( npm bin ) / knex seed:run
228+ npx knex seed:run
192229```
193230
194231#### Migration References
195232
196233> Quick Start Tutorial: http://perkframework.com/v1/guides/database-migrations-knex.html
197234> Seeding a database with Knex: https://dev.to/cesareferrari/database-seeding-with-knex-51gf
198235
199- ## Running the Application with Docker on Local
236+ ## Running the Application with Docker on Locally
200237
201238``` bash
202- $ docker build -t jaimesalas/lc-todo-monolith .
239+ docker build -t jaimesalas/lc-todo-monolith .
203240```
204241
205242Create network
206243
207244``` bash
208- $ docker create network lemoncode
245+ docker network create lemoncode
209246```
210247
211248Start database
212249
213250``` bash
214- docker run -d --network lemoncode -v todos:/var/lib/postgresql/data --name postgres postgres:10.4
251+ docker run -d -e POSTGRES_PASSWORD=postgres \
252+ --network lemoncode \
253+ -v todos:/var/lib/postgresql/data \
254+ --name postgres \
255+ postgres:16
215256```
216257
217258Start app without database
218259
219260``` bash
220- $ docker run -d -p 3000:3000 \
261+ docker run -d -p 3000:3000 \
221262 -e NODE_ENV=production \
222263 -e PORT=3000 \
223264 jaimesalas/lc-todo-monolith
224265```
225266
226- Start up with database
267+ Start up with database
227268
228269``` bash
229- $ docker run -d --network lemoncode -p 3000:3000 \
270+ docker run -d --network lemoncode -p 3000:3000 \
230271 -e NODE_ENV=production \
231272 -e PORT=3000 \
232273 -e DB_HOST=postgres \
233274 -e DB_USER=postgres \
234275 -e DB_PASSWORD=postgres \
235276 -e DB_PORT=5432 \
236277 -e DB_NAME=todos_db \
237- -e DB_VERSION=10.4 \
278+ -e DB_VERSION=16 \
238279 --name monolith \
239280 jaimesalas/lc-todo-monolith
240281```
241282
283+ We can visit now 'localhost:3000' to see our application running
284+
242285## Create stand alone database
243286
244287``` bash
245- $ docker build -t jaimesalas/lc-todo-db -f Dockerfile.todos_db .
288+ docker build -t jaimesalas/lc-todo-db -f Dockerfile.todos_db .
246289```
247290
248291``` bash
249- $ docker run -d -p 5432:5432 -v other_todos:/var/lib/postgresql/data --name lc-todo-db jaimesalas/lc-todo-db
250- ```
292+ docker run -d -e POSTGRES_PASSWORD=postgres \
293+ -p 5432:5432 \
294+ -v other_todos:/var/lib/postgresql/data \
295+ --name lc-todo-db \
296+ jaimesalas/lc-todo-db
297+ ```
0 commit comments